Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow inviting contact groups #6285

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
['name' => 'contact#searchPhoto', 'url' => '/v1/autocompletion/photo', 'verb' => 'POST'],
// Circles
['name' => 'contact#getCircleMembers', 'url' => '/v1/circles/getmembers', 'verb' => 'GET'],
// Contact Groups
['name' => 'contact#getContactGroupMembers', 'url' => '/v1/autocompletion/groupmembers', 'verb' => 'POST'],
// Settings
['name' => 'settings#setConfig', 'url' => '/v1/config/{key}', 'verb' => 'POST'],
// Tools
Expand Down
184 changes: 82 additions & 102 deletions lib/Controller/ContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
*/
namespace OCA\Calendar\Controller;

use Exception;
use OCA\Calendar\Service\ContactsService;
use OCA\Calendar\Service\ServiceException;
use OCA\Circles\Exceptions\CircleNotFoundException;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\QueryException;
use OCP\Contacts\IManager;
Expand All @@ -24,31 +27,21 @@
* @package OCA\Calendar\Controller
*/
class ContactController extends Controller {
/** @var IManager */
private $contactsManager;

/** @var IAppManager */
private $appManager;

/** @var IUserManager */
private $userManager;

/**
* ContactController constructor.
*
* @param string $appName
* @param IRequest $request
* @param IManager $contacts
*/
public function __construct(string $appName,
public function __construct(
string $appName,
IRequest $request,
IManager $contacts,
IAppManager $appManager,
IUserManager $userManager) {
private IManager $contactsManager,
private IAppManager $appManager,
private IUserManager $userManager,
private ContactsService $contactsService,
) {
parent::__construct($appName, $request);
$this->contactsManager = $contacts;
$this->appManager = $appManager;
$this->userManager = $userManager;
}

/**
Expand All @@ -59,7 +52,7 @@ public function __construct(string $appName,
*
* @NoAdminRequired
*/
public function searchLocation(string $search):JSONResponse {
public function searchLocation(string $search): JSONResponse {
if (!$this->contactsManager->isEnabled()) {
return new JSONResponse();
}
Expand All @@ -69,27 +62,17 @@ public function searchLocation(string $search):JSONResponse {
$contacts = [];
foreach ($result as $r) {
// Information about system users is fetched via DAV nowadays
if (isset($r['isLocalSystemBook']) && $r['isLocalSystemBook']) {
if ($this->contactsService->isSystemBook($r)) {
continue;
}

if (!isset($r['ADR'])) {
continue;
}

$name = $this->getNameFromContact($r);
if (\is_string($r['ADR'])) {
$r['ADR'] = [$r['ADR']];
}

$photo = isset($r['PHOTO'])
? $this->getPhotoUri($r['PHOTO'])
: null;

$addresses = [];
foreach ($r['ADR'] as $address) {
$addresses[] = trim(preg_replace("/\n+/", "\n", str_replace(';', "\n", $address)));
}
$name = $this->contactsService->getNameFromContact($r);
$photo = $this->contactsService->getPhotoUri($r);
$addresses = $this->contactsService->getAddress($r);

$contacts[] = [
'name' => $name,
Expand Down Expand Up @@ -119,51 +102,78 @@ public function searchAttendee(string $search):JSONResponse {

$contacts = [];
foreach ($result as $r) {
// Information about system users is fetched via DAV nowadays
if (isset($r['isLocalSystemBook']) && $r['isLocalSystemBook']) {
if ($this->contactsService->isSystemBook($r) || !$this->contactsService->hasEmail($r)) {
continue;
}

if (!isset($r['EMAIL'])) {
$name = $this->contactsService->getNameFromContact($r);
$email = $this->contactsService->getEmail($r);
$photo = $this->contactsService->getPhotoUri($r);
$timezoneId = $this->contactsService->getTimezoneId($r);
$lang = $this->contactsService->getLanguageId($r);
$contacts[] = [
'name' => $name,
'emails' => $email,
'lang' => $lang,
'tzid' => $timezoneId,
'photo' => $photo,
'type' => 'individual'
];
}

$groups = $this->contactsManager->search($search, ['CATEGORIES']);
$groups = array_filter($groups, function ($group) {
return $this->contactsService->hasEmail($group);
});
$filtered = $this->contactsService->filterGroupsWithCount($groups, $search);
foreach ($filtered as $groupName => $count) {
if ($count === 0) {
continue;
}
$contacts[] = [
'name' => $groupName,
'emails' => ['mailto:group+' . urlencode($groupName) . '@group'],
'lang' => '',
'tzid' => '',
'photo' => '',
'type' => 'contactsgroup',
'members' => $count,
];
}

$name = $this->getNameFromContact($r);
if (\is_string($r['EMAIL'])) {
$r['EMAIL'] = [$r['EMAIL']];
}
return new JSONResponse($contacts);
}

$photo = isset($r['PHOTO'])
? $this->getPhotoUri($r['PHOTO'])
: null;
#[NoAdminRequired]
public function getContactGroupMembers(string $groupName): JSONResponse {
if (!$this->contactsManager->isEnabled()) {
return new JSONResponse();
}

$lang = null;
if (isset($r['LANG'])) {
if (\is_array($r['LANG'])) {
$lang = $r['LANG'][0];
} else {
$lang = $r['LANG'];
}
$groupmembers = $this->contactsManager->search($groupName, ['CATEGORIES']);
$contacts = [];
foreach ($groupmembers as $r) {
if (!in_array($groupName, explode(',', $r['CATEGORIES']), true)) {
continue;
}

$timezoneId = null;
if (isset($r['TZ'])) {
if (\is_array($r['TZ'])) {
$timezoneId = $r['TZ'][0];
} else {
$timezoneId = $r['TZ'];
}
if (!$this->contactsService->hasEmail($r) || $this->contactsService->isSystemBook($r)) {
continue;
}

$name = $this->contactsService->getNameFromContact($r);
$email = $this->contactsService->getEmail($r);
$photo = $this->contactsService->getPhotoUri($r);
$timezoneId = $this->contactsService->getTimezoneId($r);
$lang = $this->contactsService->getLanguageId($r);
$contacts[] = [
'name' => $name,
'emails' => $r['EMAIL'],
'lang' => $lang,
'tzid' => $timezoneId,
'photo' => $photo,
'commonName' => $name,
'email' => $email[0],
'calendarUserType' => 'INDIVIDUAL',
'language' => $lang,
'timezoneId' => $timezoneId,
'avatar' => $photo,
'isUser' => false,
'member' => 'mailto:group+' . urlencode($groupName) . '@group',
];
}

return new JSONResponse($contacts);
}

Expand Down Expand Up @@ -243,17 +253,14 @@ public function searchPhoto(string $search):JSONResponse {
$result = $this->contactsManager->search($search, ['EMAIL']);

foreach ($result as $r) {
if (!isset($r['EMAIL'])) {
if (!$this->contactsService->hasEmail($r) || $this->contactsService->isSystemBook($r)) {
continue;
}

if (\is_string($r['EMAIL'])) {
$r['EMAIL'] = [$r['EMAIL']];
}
$email = $this->contactsService->getEmail($r);

$match = false;
foreach ($r['EMAIL'] as $email) {
if ($email === $search) {
foreach ($email as $e) {
if ($e === $search) {
$match = true;
}
}
Expand All @@ -262,15 +269,12 @@ public function searchPhoto(string $search):JSONResponse {
continue;
}

if (!isset($r['PHOTO'])) {
$photo = $this->contactsService->getPhotoUri($r);
if ($photo === null) {
continue;
}

$name = $this->getNameFromContact($r);
$photo = $this->getPhotoUri($r['PHOTO']);
if (!$photo) {
continue;
}
$name = $this->contactsService->getNameFromContact($r);

return new JSONResponse([
'name' => $name,
Expand All @@ -281,28 +285,4 @@ public function searchPhoto(string $search):JSONResponse {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}

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

/**
* Get photo uri from contact
*
* @param string $raw
* @return string|null
*/
private function getPhotoUri(string $raw):?string {
$uriPrefix = 'VALUE=uri:';
if (substr($raw, 0, strlen($uriPrefix)) === $uriPrefix) {
return substr($raw, strpos($raw, 'http'));
}

return null;
}
}
Loading
Loading