Skip to content

Commit

Permalink
feat(contactsinteraction): allow users to disable contacts interactio…
Browse files Browse the repository at this point in the history
…n addressbook

This allows simple users to opt-out of the contacts interaction
addressbook even if admins have the app installed.

Similar to how the birthday calendar works, the functionnality can be
toggled in the user's settings or by doing a DELETE on the addressbook.

A new contacts personal section has been added to contain this new
setting.

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
  • Loading branch information
tcitworld committed Jul 13, 2023
1 parent c3c58b6 commit c43c995
Showing 22 changed files with 600 additions and 29 deletions.
6 changes: 6 additions & 0 deletions apps/contactsinteraction/appinfo/info.xml
Original file line number Diff line number Diff line change
@@ -26,5 +26,11 @@
<address-book-plugins>
<plugin>OCA\ContactsInteraction\AddressBookProvider</plugin>
</address-book-plugins>
<plugins>
<plugin>OCA\ContactsInteraction\DAV\Plugin</plugin>
</plugins>
</sabre>
<settings>
<personal>OCA\ContactsInteraction\Settings\Personal</personal>
</settings>
</info>
30 changes: 30 additions & 0 deletions apps/contactsinteraction/appinfo/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Thomas Citharel <nextcloud@tcit.fr>
*
* @author Thomas Citharel <nextcloud@tcit.fr>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
return [
'ocs' => [
['name' => 'config#disable', 'url' => '/config/user/disable', 'verb' => 'POST'],
],
];
Original file line number Diff line number Diff line change
@@ -12,9 +12,12 @@
'OCA\\ContactsInteraction\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
'OCA\\ContactsInteraction\\BackgroundJob\\CleanupJob' => $baseDir . '/../lib/BackgroundJob/CleanupJob.php',
'OCA\\ContactsInteraction\\Card' => $baseDir . '/../lib/Card.php',
'OCA\\ContactsInteraction\\Controller\\ConfigController' => $baseDir . '/../lib/Controller/ConfigController.php',
'OCA\\ContactsInteraction\\DAV\\Plugin' => $baseDir . '/../lib/DAV/Plugin.php',
'OCA\\ContactsInteraction\\Db\\CardSearchDao' => $baseDir . '/../lib/Db/CardSearchDao.php',
'OCA\\ContactsInteraction\\Db\\RecentContact' => $baseDir . '/../lib/Db/RecentContact.php',
'OCA\\ContactsInteraction\\Db\\RecentContactMapper' => $baseDir . '/../lib/Db/RecentContactMapper.php',
'OCA\\ContactsInteraction\\Listeners\\ContactInteractionListener' => $baseDir . '/../lib/Listeners/ContactInteractionListener.php',
'OCA\\ContactsInteraction\\Migration\\Version010000Date20200304152605' => $baseDir . '/../lib/Migration/Version010000Date20200304152605.php',
'OCA\\ContactsInteraction\\Settings\\Personal' => $baseDir . '/../lib/Settings/Personal.php',
);
Original file line number Diff line number Diff line change
@@ -27,11 +27,14 @@ class ComposerStaticInitContactsInteraction
'OCA\\ContactsInteraction\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
'OCA\\ContactsInteraction\\BackgroundJob\\CleanupJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/CleanupJob.php',
'OCA\\ContactsInteraction\\Card' => __DIR__ . '/..' . '/../lib/Card.php',
'OCA\\ContactsInteraction\\Controller\\ConfigController' => __DIR__ . '/..' . '/../lib/Controller/ConfigController.php',
'OCA\\ContactsInteraction\\DAV\\Plugin' => __DIR__ . '/..' . '/../lib/DAV/Plugin.php',
'OCA\\ContactsInteraction\\Db\\CardSearchDao' => __DIR__ . '/..' . '/../lib/Db/CardSearchDao.php',
'OCA\\ContactsInteraction\\Db\\RecentContact' => __DIR__ . '/..' . '/../lib/Db/RecentContact.php',
'OCA\\ContactsInteraction\\Db\\RecentContactMapper' => __DIR__ . '/..' . '/../lib/Db/RecentContactMapper.php',
'OCA\\ContactsInteraction\\Listeners\\ContactInteractionListener' => __DIR__ . '/..' . '/../lib/Listeners/ContactInteractionListener.php',
'OCA\\ContactsInteraction\\Migration\\Version010000Date20200304152605' => __DIR__ . '/..' . '/../lib/Migration/Version010000Date20200304152605.php',
'OCA\\ContactsInteraction\\Settings\\Personal' => __DIR__ . '/..' . '/../lib/Settings/Personal.php',
);

public static function getInitializer(ClassLoader $loader)
52 changes: 52 additions & 0 deletions apps/contactsinteraction/lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* @copyright Copyright (c) 2023 Thomas Citharel <nextcloud@tcit.fr>
*
* @author Thomas Citharel <nextcloud@tcit.fr>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\ContactsInteraction\Controller;

use OCA\ContactsInteraction\Db\RecentContactMapper;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
use OCP\IConfig;
use OCP\IRequest;

class ConfigController extends Controller {

public function __construct(string $appName, IRequest $request, private IConfig $config, private RecentContactMapper $recentContactMapper, private ?string $userId) {
parent::__construct($appName, $request);
}

/**
* @NoAdminRequired
*/
public function disable(): Response {
if (!$this->userId) {
return new DataResponse([], Http::STATUS_UNAUTHORIZED);
}
$this->config->setUserValue($this->userId, $this->appName, 'generateContactsInteraction', 'no');

$this->recentContactMapper->cleanForUser($this->userId);

return new DataResponse([]);
}
}
110 changes: 110 additions & 0 deletions apps/contactsinteraction/lib/DAV/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* @copyright 2023, Thomas Citharel <nextcloud@tcit.fr>
*
* @author Thomas Citharel <nextcloud@tcit.fr>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\ContactsInteraction\DAV;

use OCA\ContactsInteraction\AddressBook;
use OCA\ContactsInteraction\AppInfo\Application;
use OCP\IConfig;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;

/**
* Allows users to disable the feature by deleting the addressbook
*
* @package OCA\DAV\CalDAV\BirthdayCalendar
*/
class Plugin extends ServerPlugin {

protected IConfig $config;
protected Server $server;

public function __construct(IConfig $config) {
$this->config = $config;
}

/**
* This method should return a list of server-features.
*
* This is for example 'versioning' and is added to the DAV: header
* in an OPTIONS response.
*
* @return string[]
*/
public function getFeatures() {
return ['nc-disable-recently-contacted'];
}

/**
* Returns a plugin name.
*
* Using this name other plugins will be able to access other plugins
* using Sabre\DAV\Server::getPlugin
*
* @return string
*/
public function getPluginName() {
return 'nc-disable-recently-contacted';
}

/**
* This initializes the plugin.
*
* This function is called by Sabre\DAV\Server, after
* addPlugin is called.
*
* This method should set up the required event subscriptions.
*
* @param Server $server
*/
public function initialize(Server $server) {
$this->server = $server;

$this->server->on('method:DELETE', [$this, 'httpDelete']);
}

/**
* We intercept this to handle POST requests on calendar homes.
*
* @param RequestInterface $request
* @param ResponseInterface $response
*
* @return bool|void
*/
public function httpDelete(RequestInterface $request, ResponseInterface $response) {
$node = $this->server->tree->getNodeForPath($this->server->getRequestUri());
if (!($node instanceof AddressBook)) {
return;
}

$principalUri = $node->getOwner();
$userId = substr($principalUri, 17);

$this->config->setUserValue($userId, Application::APP_ID, 'generateContactsInteraction', 'no');

$this->server->httpResponse->setStatus(204);

return false;
}
}
10 changes: 10 additions & 0 deletions apps/contactsinteraction/lib/Db/RecentContactMapper.php
Original file line number Diff line number Diff line change
@@ -128,4 +128,14 @@ public function cleanUp(int $olderThan): void {

$delete->executeStatement();
}

public function cleanForUser(string $userId): void {
$qb = $this->db->getQueryBuilder();

$delete = $qb
->delete($this->getTableName())
->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($userId)));

$delete->executeStatement();
}
}
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
*/
namespace OCA\ContactsInteraction\Listeners;

use OCA\ContactsInteraction\AppInfo\Application;
use OCA\ContactsInteraction\Db\CardSearchDao;
use OCA\ContactsInteraction\Db\RecentContact;
use OCA\ContactsInteraction\Db\RecentContactMapper;
@@ -33,6 +34,7 @@
use OCP\Contacts\Events\ContactInteractedWithEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IUserManager;
@@ -46,28 +48,14 @@ class ContactInteractionListener implements IEventListener {

use TTransactional;

private RecentContactMapper $mapper;
private CardSearchDao $cardSearchDao;
private IUserManager $userManager;
private IDBConnection $dbConnection;
private ITimeFactory $timeFactory;
private IL10N $l10n;
private LoggerInterface $logger;

public function __construct(RecentContactMapper $mapper,
CardSearchDao $cardSearchDao,
IUserManager $userManager,
IDBConnection $connection,
ITimeFactory $timeFactory,
IL10N $l10nFactory,
LoggerInterface $logger) {
$this->mapper = $mapper;
$this->cardSearchDao = $cardSearchDao;
$this->userManager = $userManager;
$this->dbConnection = $connection;
$this->timeFactory = $timeFactory;
$this->l10n = $l10nFactory;
$this->logger = $logger;
public function __construct(private RecentContactMapper $mapper,
private CardSearchDao $cardSearchDao,
private IUserManager $userManager,
private IDBConnection $connection,
private ITimeFactory $timeFactory,
private IL10N $l10n,
private IConfig $config,
private LoggerInterface $logger) {
}

public function handle(Event $event): void {
@@ -85,6 +73,11 @@ public function handle(Event $event): void {
return;
}

if ($this->config->getUserValue($event->getActor()->getUID(), Application::APP_ID, 'generateContactsInteraction', 'yes') === 'no') {
$this->logger->debug("Ignoring contact interaction as it's disabled for this user");
return;
}

$this->atomic(function () use ($event) {
$uid = $event->getUid();
$email = $event->getEmail();
@@ -96,9 +89,9 @@ public function handle(Event $event): void {
$federatedCloudId
);
if (!empty($existing)) {
$now = $this->timeFactory->getTime();
$now = $this->timeFactory->now();
foreach ($existing as $c) {
$c->setLastContact($now);
$c->setLastContact($now->getTimestamp());
$this->mapper->update($c);
}

@@ -116,7 +109,7 @@ public function handle(Event $event): void {
if ($federatedCloudId !== null) {
$contact->setFederatedCloudId($federatedCloudId);
}
$contact->setLastContact($this->timeFactory->getTime());
$contact->setLastContact($this->timeFactory->now()->getTimestamp());

$copy = $this->cardSearchDao->findExisting(
$event->getActor(),
@@ -141,7 +134,7 @@ public function handle(Event $event): void {
$contact->setCard($this->generateCard($contact));
}
$this->mapper->insert($contact);
}, $this->dbConnection);
}, $this->connection);
}

private function getDisplayName(?string $uid): ?string {
52 changes: 52 additions & 0 deletions apps/contactsinteraction/lib/Settings/Personal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* @copyright Copyright (c) 2023 Thomas Citharel <nextcloud@tcit.fr>
*
* @author Thomas Citharel <nextcloud@tcit.fr>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\ContactsInteraction\Settings;

use OCA\ContactsInteraction\AppInfo\Application;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IConfig;
use OCP\Settings\ISettings;
use OCP\Util;

class Personal implements ISettings {
public function __construct(private IInitialState $initialState, private IConfig $config, private ?string $userId) { }

public function getForm(): TemplateResponse {
$this->initialState->provideInitialState('generateContactsInteraction', $this->config->getUserValue($this->userId, Application::APP_ID, 'generateContactsInteraction', 'yes') === 'yes');
Util::addScript(Application::APP_ID, 'settings-personal');
return new TemplateResponse(Application::APP_ID, 'personal');
}

public function getSection(): string {
return 'contacts';
}

/**
* @psalm-return 40
*/
public function getPriority(): int {
return 40;
}
}
Loading

0 comments on commit c43c995

Please sign in to comment.