Implement notification backend for mentions

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2022-08-25 08:03:43 +02:00
Родитель df441b7f29
Коммит 2e3c92ac00
9 изменённых файлов: 299 добавлений и 26 удалений

Просмотреть файл

@ -36,6 +36,7 @@ return [
['name' => 'Session#sync', 'url' => '/session/sync', 'verb' => 'POST'],
['name' => 'Session#push', 'url' => '/session/push', 'verb' => 'POST'],
['name' => 'Session#close', 'url' => '/session/close', 'verb' => 'POST'],
['name' => 'Session#mention', 'url' => '/session/mention', 'verb' => 'PUT'],
['name' => 'PublicSession#create', 'url' => '/public/session/create', 'verb' => 'PUT'],
['name' => 'PublicSession#updateSession', 'url' => '/public/session', 'verb' => 'POST'],

Просмотреть файл

@ -34,6 +34,7 @@ use OCA\Text\Listeners\FilesLoadAdditionalScriptsListener;
use OCA\Text\Listeners\FilesSharingLoadAdditionalScriptsListener;
use OCA\Text\Listeners\LoadViewerListener;
use OCA\Text\Listeners\RegisterDirectEditorEventListener;
use OCA\Text\Notification\Notifier;
use OCA\Text\Service\ConfigService;
use OCA\Viewer\Event\LoadViewer;
use OCP\AppFramework\App;
@ -64,6 +65,7 @@ class Application extends App implements IBootstrap {
$context->registerEventListener(NodeCopiedEvent::class, NodeCopiedListener::class);
$context->registerEventListener(BeforeNodeRenamedEvent::class, BeforeNodeRenamedListener::class);
$context->registerEventListener(BeforeNodeDeletedEvent::class, BeforeNodeDeletedListener::class);
$context->registerNotifierService(Notifier::class);
}
public function boot(IBootContext $context): void {

Просмотреть файл

@ -26,6 +26,8 @@ declare(strict_types=1);
namespace OCA\Text\Controller;
use OCA\Text\Service\ApiService;
use OCA\Text\Service\NotificationService;
use OCA\Text\Service\SessionService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
@ -33,10 +35,14 @@ use OCP\IRequest;
class SessionController extends Controller {
private ApiService $apiService;
private SessionService $sessionService;
private NotificationService $notificationService;
public function __construct(string $appName, IRequest $request, ApiService $apiService) {
public function __construct(string $appName, IRequest $request, ApiService $apiService, SessionService $sessionService, NotificationService $notificationService) {
parent::__construct($appName, $request);
$this->apiService = $apiService;
$this->sessionService = $sessionService;
$this->notificationService = $notificationService;
}
/**
@ -77,4 +83,23 @@ class SessionController extends Controller {
public function sync(int $documentId, int $sessionId, string $sessionToken, int $version = 0, string $autosaveContent = null, bool $force = false, bool $manualSave = false): DataResponse {
return $this->apiService->sync($documentId, $sessionId, $sessionToken, $version, $autosaveContent, $force, $manualSave);
}
/**
* @NoAdminRequired
* @PublicPage
* @UserRateThrottle(limit=5, period=120)
*/
public function mention(int $documentId, int $sessionId, string $sessionToken, string $mention): DataResponse {
if (!$this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) {
return new DataResponse([], 403);
}
$currentSession = $this->sessionService->getSession($documentId, $sessionId, $sessionToken);
if ($currentSession->getUserId() === null && !$this->sessionService->isUserInDocument($documentId, $mention)) {
return new DataResponse([], 403);
}
return new DataResponse($this->notificationService->mention($documentId, $mention));
}
}

Просмотреть файл

@ -2,37 +2,68 @@
namespace OCA\Text\Controller;
use OCA\Text\Service\SessionService;
use \OCP\AppFramework\ApiController;
use OCP\AppFramework\Http\DataResponse;
use OCP\Collaboration\Collaborators\ISearch;
use \OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Share\IShare;
class UserApiController extends ApiController {
private ISearch $collaboratorSearch;
private IUserSession $userSession;
private IUserManager $userManager;
private SessionService $sessionService;
protected ISearch $collaboratorSearch;
public function __construct($appName, IRequest $request, SessionService $sessionService, ISearch $ISearch, IUserManager $userManager, IUserSession $userSession) {
parent::__construct($appName, $request);
public function __construct($appName, IRequest $request, ISearch $ISearch) {
parent::__construct($appName, $request);
$this->collaboratorSearch = $ISearch;
}
/**
* @NoAdminRequired
* @param string $filter
*/
public function index(string $filter, int $limit = 5) {
$users = [];
[$result] = $this->collaboratorSearch->search($filter, [IShare::TYPE_USER], false, $limit, 0);
foreach ($result['users'] as ['label' => $label, 'value' => $value]) {
if (isset($value['shareWith'])) {
$id = $value['shareWith'];
$users[$id] = $label;
}
}
return $users;
$this->sessionService = $sessionService;
$this->collaboratorSearch = $ISearch;
$this->userSession = $userSession;
$this->userManager = $userManager;
$this->sessionService = $sessionService;
}
}
/**
* @NoAdminRequired
* @PublicPage
*/
public function index(int $documentId, int $sessionId, string $sessionToken, string $filter, int $limit = 5): DataResponse {
if (!$this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) {
return new DataResponse([], 403);
}
$sessions = $this->sessionService->getAllSessions($documentId);
$users = [];
// Add joined users to the autocomplete list
foreach ($sessions as $session) {
$sessionUserId = $session['userId'];
if ($sessionUserId !== null && !isset($users[$sessionUserId])) {
$users[$sessionUserId] = $this->userManager->getDisplayName($sessionUserId);
}
}
$currentSession = $this->sessionService->getSession($documentId, $sessionId, $sessionToken);
if ($currentSession->getUserId() !== null) {
// Add other users to the autocomplete list
[$result] = $this->collaboratorSearch->search($filter, [IShare::TYPE_USER], false, $limit, 0);
foreach ($result['users'] as ['label' => $label, 'value' => $value]) {
if (isset($value['shareWith'])) {
$id = $value['shareWith'];
$users[$id] = $label;
}
}
$user = $this->userSession->getUser();
$users[$user->getUID()] = $user->getDisplayName();
}
return new DataResponse($users);
}
}

Просмотреть файл

@ -117,4 +117,22 @@ class SessionMapper extends QBMapper {
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)));
return $qb->execute();
}
public function isUserInDocument($documentId, $userId): bool {
$qb = $this->db->getQueryBuilder();
$result = $qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)))
->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
->setMaxResults(1)
->executeQuery();
$data = $result->fetch();
$result->closeCursor();
if ($data === false) {
return false;
}
return true;
}
}

Просмотреть файл

@ -0,0 +1,133 @@
<?php
/*
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @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/>.
*
*/
declare(strict_types=1);
namespace OCA\Text\Notification;
use InvalidArgumentException;
use OC\User\NoUserException;
use OCP\Files\IRootFolder;
use OCP\Files\NotPermittedException;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
class Notifier implements INotifier {
public const TYPE_MENTIONED = 'mentioned';
public const SUBJECT_MENTIONED_SOURCE_USER = 'sourceUser';
public const SUBJECT_MENTIONED_TARGET_USER = 'targetUser';
private IFactory $factory;
private IURLGenerator $url;
private IUserManager $userManager;
private IRootFolder $rootFolder;
public function __construct(IFactory $factory, IUserManager $userManager, IURLGenerator $urlGenerator, IRootFolder $rootFolder) {
$this->factory = $factory;
$this->userManager = $userManager;
$this->url = $urlGenerator;
$this->rootFolder = $rootFolder;
}
public function getID(): string {
return 'text';
}
public function getName(): string {
return 'Text';
}
public function prepare(INotification $notification, string $languageCode): INotification {
if ($notification->getApp() !== 'text') {
throw new InvalidArgumentException('Application should be text instead of ' . $notification->getApp());
}
$l = $this->factory->get('text', $languageCode);
switch ($notification->getSubject()) {
case self::TYPE_MENTIONED:
$parameters = $notification->getSubjectParameters();
$sourceUser = $parameters[self::SUBJECT_MENTIONED_SOURCE_USER];
$sourceUserDisplayName = $this->userManager->getDisplayName($sourceUser);
$targetUser = $notification->getUser();
$fileId = (int)$notification->getObjectId();
if ($sourceUserDisplayName === null) {
throw new InvalidArgumentException();
}
try {
$userFolder = $this->rootFolder->getUserFolder($targetUser);
} catch (NotPermittedException|NoUserException $e) {
throw new InvalidArgumentException();
}
$nodes = $userFolder->getById($fileId);
$node = array_shift($nodes);
if ($node === null) {
throw new InvalidArgumentException();
}
$fileLink = $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $node->getId()]);
$notification->setRichSubject($l->t('{user} has mentioned you in the text document {node}'), [
'user' => [
'type' => 'user',
'id' => $sourceUser,
'name' => $sourceUserDisplayName,
],
'node' => [
'type' => 'file',
'id' => $node->getId(),
'name' => $node->getName(),
'path' => $userFolder->getRelativePath($node->getPath()),
'link' => $fileLink,
],
]);
break;
default:
throw new InvalidArgumentException();
}
$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('text', 'app.svg')));
$notification->setLink($fileLink);
$this->setParsedSubjectFromRichSubject($notification);
return $notification;
}
protected function setParsedSubjectFromRichSubject(INotification $notification): void {
$placeholders = $replacements = [];
foreach ($notification->getRichSubjectParameters() as $placeholder => $parameter) {
$placeholders[] = '{' . $placeholder . '}';
if ($parameter['type'] === 'file') {
$replacements[] = $parameter['path'];
} else {
$replacements[] = $parameter['name'];
}
}
$notification->setParsedSubject(str_replace($placeholders, $replacements, $notification->getRichSubject()));
}
}

Просмотреть файл

@ -234,7 +234,7 @@ class ApiService {
*/
public function updateSession(int $documentId, int $sessionId, string $sessionToken, string $guestName): DataResponse {
if (!$this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) {
return new DataResponse([], 500);
return new DataResponse([], 403);
}
return new DataResponse($this->sessionService->updateSession($documentId, $sessionId, $sessionToken, $guestName));

Просмотреть файл

@ -0,0 +1,59 @@
<?php
/**
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @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\Text\Service;
use OCA\Text\Notification\Notifier;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Notification\IManager;
class NotificationService {
private IManager $manager;
private ITimeFactory $timeFactory;
private ?string $userId;
public function __construct(IManager $manager, ITimeFactory $timeFactory, ?string $userId = null) {
$this->manager = $manager;
$this->timeFactory = $timeFactory;
$this->userId = $userId;
}
public function mention(int $fileId, string $userId): bool {
$notification = $this->manager->createNotification();
$notification->setUser($userId)
->setApp('text')
->setSubject(Notifier::TYPE_MENTIONED, [
Notifier::SUBJECT_MENTIONED_SOURCE_USER => $this->userId,
Notifier::SUBJECT_MENTIONED_TARGET_USER => $userId,
])
->setObject('file', (string)$fileId);
;
if ($this->manager->getCount($notification) === 0) {
$notification->setDateTime($this->timeFactory->getDateTime());
$this->manager->notify($notification);
return true;
}
return false;
}
}

Просмотреть файл

@ -219,4 +219,8 @@ class SessionService {
$color = $this->avatarManager->getGuestAvatar($uniqueGuestId)->avatarBackgroundColor($uniqueGuestId);
return $color->name();
}
public function isUserInDocument(int $documentId, string $mention): bool {
return $this->sessionMapper->isUserInDocument($documentId, $mention);
}
}