Skip to content

Commit

Permalink
fixup! feat: allow inviting contact groups
Browse files Browse the repository at this point in the history
  • Loading branch information
miaulalala committed Oct 14, 2024
1 parent f464833 commit 6202619
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 29 deletions.
83 changes: 54 additions & 29 deletions lib/Service/ContactsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,83 +10,108 @@
class ContactsService {


public function isSystemBook(array $r): bool {
/**
* @param array $contact
* @return bool
*/
public function isSystemBook(array $contact): bool {
// Information about system users is fetched via DAV nowadays
return (isset($contact['isLocalSystemBook']) && $contact['isLocalSystemBook'] === true);
}

public function hasEmail(array $r): bool {
return !isset($r['EMAIL']);
/**
* @param array $contact
* @return bool
*/
public function hasEmail(array $contact): bool {
return isset($contact['EMAIL']);
}

/**
* Extract name from an array containing a contact's information
*
* @param array $r
* @param array $contact
* @return string
*/
public function getNameFromContact(array $r): string {
return $r['FN'] ?? '';
public function getNameFromContact(array $contact): string {
return $contact['FN'] ?? '';
}

/**
* Get photo uri from contact
*
* @param string $raw
* @param array $contact
* @return string|null
*/
public function getPhotoUri(array $r): ?string {
if (!isset($r['PHOTO'])) {
public function getPhotoUri(array $contact): ?string {
if (!isset($contact['PHOTO'])) {
return null;
}

$raw = $r['PHOTO'];
$raw = $contact['PHOTO'];
$uriPrefix = 'VALUE=uri:';
if (str_starts_with($raw, $uriPrefix)) {
return substr($raw, strpos($raw, 'http'));
$strpos = strpos($raw, 'http');
return $strpos !== false ? substr($raw, $strpos) : null;
}

return null;
}

public function getEmail(array $r): array {
if (\is_string($r['EMAIL'])) {
return [$r['EMAIL']];
/**
* @param array $contact
* @return string[]
*/
public function getEmail(array $contact): array {
if (\is_string($contact['EMAIL'])) {
return [$contact['EMAIL']];
}
return $r['EMAIL'];
return $contact['EMAIL'];
}

public function getTimezoneId(array $r): ?string {
if (!isset($r['TZ'])) {
/**
* @param array $contact
* @return string|null
*/
public function getTimezoneId(array $contact): ?string {
if (!isset($contact['TZ'])) {
return null;
}

if (\is_array($r['TZ'])) {
return $r['TZ'][0];
if (\is_array($contact['TZ'])) {
return $contact['TZ'][0];
}
return $r['TZ'];
return $contact['TZ'];
}

public function getLanguageId(array $r): ?string {
if (!isset($r['LANG'])) {
/**
* @param array $contact
* @return string|null
*/
public function getLanguageId(array $contact): ?string {
if (!isset($contact['LANG'])) {
return null;
}

if (\is_array($r['LANG'])) {
return $r['LANG'][0];
if (\is_array($contact['LANG'])) {
return $contact['LANG'][0];
}
return $r['LANG'];
return $contact['LANG'];
}

/**
* @param array $groups
* @param string $search
* @return array
*/
public function filterGroupsWithCount(array $groups, string $search): array {
//filter to be unique
$categoryMem = [];
$categories = [];
foreach ($groups as $group) {
$categoryNames = explode(',', $group['CATEGORIES']);
$categoryMem[] = array_filter($categoryNames, static function ($cat) use ($search) {
$categories[] = array_filter(explode(',', $group['CATEGORIES']), static function ($cat) use ($search) {
return str_contains(strtolower($cat), $search);
});
}
return array_count_values(array_merge(...$categoryMem));
return array_count_values(array_merge(...$categories));
}
}
96 changes: 96 additions & 0 deletions tests/php/unit/Service/ContactsServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2014 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Calendar\Service;

use ChristophWurst\Nextcloud\Testing\TestCase;

class ContactsServiceTest extends TestCase {
private ContactsService $service;

public function setUp(): void {
$this->service = new ContactsService();
}

public function testGetEmail(): void {
$contact = ['EMAIL' => 'test@test.com'];
$this->assertEquals(['test@test.com'], $this->service->getEmail($contact));
}

public function testIsSystemBook(): void {
$contact = ['isLocalSystemBook' => true];
$this->assertTrue($this->service->isSystemBook($contact));
}

public function testIsNotSystemBook(): void {
$contact = ['isLocalSystemBook' => false];
$this->assertFalse($this->service->isSystemBook($contact));
}

public function testNotSetSystemBook(): void {
$this->assertFalse($this->service->isSystemBook([]));
}

public function testHasEmail(): void {
$contact = ['EMAIL' => 'test@test.com'];
$this->assertTrue($this->service->hasEmail($contact));
}

public function testHasNoEmail(): void {
$this->assertFalse($this->service->hasEmail([]));
}

public function testGetPhotoUri(): void {
$contact = ['PHOTO' => 'VALUE=uri:http://test'];
$this->assertEquals('http://test', $this->service->getPhotoUri($contact));
}

public function testGetPhotoInvalidUri(): void {
$contact = ['PHOTO' => 'VALUE=uri:thisisnotit'];
$this->assertNull($this->service->getPhotoUri($contact));
}

public function testGetPhotoUriNoPhoto(): void {
$this->assertNull($this->service->getPhotoUri([]));
}

public function testFilterGroupsWithCount(): void {
$contact = [
['CATEGORIES' => 'The Proclaimers,I\'m gonna be,When I go out,I would walk 500 Miles,I would walk 500 more'],
['CATEGORIES' => 'The Proclaimers,When I\'m lonely,I would walk 500 Miles,I would walk 500 more'],
];

$searchterm = 'walk';

$expected = [
'I would walk 500 Miles' => 2,
'I would walk 500 more' => 2,
];

$this->assertEqualsCanonicalizing($expected, $this->service->filterGroupsWithCount($contact, $searchterm));
}

public function testGetTimezoneId(): void {
$contact = ['TZ' => ['UTC']];
$this->assertEquals('UTC', $this->service->getTimezoneId($contact));
}

public function testGetLanguageId(): void {
$contact = ['LANG' => ['de_de']];
$this->assertEquals('de_de', $this->service->getLanguageId($contact));
}

public function testGetNameFromContact(): void {
$contact = ['FN' => 'test'];
$this->assertEquals('test', $this->service->getNameFromContact($contact));
}

public function testGetNameFromContactNoName(): void {
$this->assertEquals('', $this->service->getNameFromContact([]));
}
}

0 comments on commit 6202619

Please sign in to comment.