Move notification to the new paradigm

This commit is contained in:
Hypolite Petovan 2021-09-18 00:03:32 -04:00
parent bc0734e0f1
commit 7a2d5f6a8e
16 changed files with 175 additions and 420 deletions

View file

@ -28,6 +28,8 @@ use Friendica\Model\Contact;
use Friendica\Model\Post;
use Friendica\Model\Verb;
use Friendica\Module\BaseApi;
use Friendica\Navigation\Notifications\Entity;
use Friendica\Object\Api\Mastodon\Notification;
use Friendica\Protocol\Activity;
/**
@ -46,10 +48,12 @@ class Notifications extends BaseApi
if (!empty($parameters['id'])) {
$id = $parameters['id'];
if (!DBA::exists('notification', ['id' => $id, 'uid' => $uid])) {
try {
$notification = DI::notification()->selectOneForUser($uid, ['id' => $id]);
System::jsonExit(DI::mstdnNotification()->createFromNotification($notification));
} catch (\Exception $e) {
DI::mstdnError()->RecordNotFound();
}
System::jsonExit(DI::mstdnNotification()->createFromNotificationId($id));
}
$request = self::getRequest([
@ -63,7 +67,7 @@ class Notifications extends BaseApi
'count' => 0, // Unknown parameter
]);
$params = ['order' => ['id' => true], 'limit' => $request['limit']];
$params = ['order' => ['id' => true]];
$condition = ['uid' => $uid, 'seen' => false];
@ -115,36 +119,26 @@ class Notifications extends BaseApi
Verb::getID(Activity::POST), Post\UserNotification::TYPE_SHARED]);
}
if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`id` < ?", $request['max_id']]);
}
$mstdnNotifications = [];
if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`id` > ?", $request['since_id']]);
}
$Notifications = DI::notification()->selectByBoundaries(
$condition,
$params,
$request['min_id'] ?? null,
$request['min_id'] ?? $request['since_id'] ?? null,
$request['limit']
);
if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`id` > ?", $request['min_id']]);
$params['order'] = ['id'];
}
$notifications = [];
$notify = DBA::select('notification', ['id'], $condition, $params);
while ($notification = DBA::fetch($notify)) {
self::setBoundaries($notification['id']);
$entry = DI::mstdnNotification()->createFromNotificationId($notification['id']);
if (!empty($entry)) {
$notifications[] = $entry;
foreach($Notifications as $Notification) {
try {
$mstdnNotifications[] = DI::mstdnNotification()->createFromNotification($Notification);
self::setBoundaries($Notification->id);
} catch (\Exception $e) {
// Skip this notification
}
}
if (!empty($request['min_id'])) {
array_reverse($notifications);
}
self::setLinkHeader();
System::jsonExit($notifications);
System::jsonExit($mstdnNotifications);
}
}

View file

@ -22,7 +22,7 @@
namespace Friendica\Module\Api\Mastodon\Notifications;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\BaseApi;
/**
@ -35,7 +35,7 @@ class Clear extends BaseApi
self::checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID();
DBA::update('notification', ['seen' => true], ['uid' => $uid]);
DI::notification()->setAllSeenForUser($uid);
System::jsonExit([]);
}

View file

@ -25,6 +25,7 @@ use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException\ForbiddenException;
/**
* @see https://docs.joinmastodon.org/methods/notifications/
@ -40,7 +41,9 @@ class Dismiss extends BaseApi
DI::mstdnError()->UnprocessableEntity();
}
DBA::update('notification', ['seen' => true], ['uid' => $uid, 'id' => $parameters['id']]);
$Notification = DI::notification()->selectOneForUser($uid, $parameters['id']);
$Notification->setSeen();
DI::notification()->save($Notification);
System::jsonExit([]);
}

View file

@ -78,7 +78,7 @@ class Notification extends BaseModule
if (DI::args()->get(1) === 'mark' && DI::args()->get(2) === 'all') {
try {
DI::dba()->update('notification', ['seen' => true], ['uid' => local_user()]);
DI::notification()->setAllSeenForUser(local_user());
$success = DI::notify()->setAllSeenForUser(local_user());
} catch (\Exception $e) {
DI::logger()->warning('set all seen failed.', ['exception' => $e]);
@ -118,7 +118,7 @@ class Notification extends BaseModule
DI::notify()->save($Notify);
} else {
if ($Notify->uriId) {
DI::dba()->update('notification', ['seen' => true], ['uid' => $Notify->uid, 'target-uri-id' => $Notify->uriId]);
DI::notification()->setAllSeenForUser($Notify->uid, ['target-uri-id' => $Notify->uriId]);
}
DI::notify()->setAllSeenForRelatedNotify($Notify);

View file

@ -95,25 +95,24 @@ class Notifications extends BaseNotifications
$notificationHeader = $notificationResult['header'] ?? '';
if (!empty($notifications['notifications'])) {
$notificationTemplates = [
'like' => 'notifications/likes_item.tpl',
'dislike' => 'notifications/dislikes_item.tpl',
'attend' => 'notifications/attend_item.tpl',
'attendno' => 'notifications/attend_item.tpl',
'attendmaybe' => 'notifications/attend_item.tpl',
'friend' => 'notifications/friends_item.tpl',
'comment' => 'notifications/comments_item.tpl',
'post' => 'notifications/posts_item.tpl',
'notification' => 'notifications/notification.tpl',
];
// Loop trough ever notification This creates an array with the output html for each
// notification and apply the correct template according to the notificationtype (label).
/** @var FormattedNotification $notification */
foreach ($notifications['notifications'] as $notification) {
$notification_templates = [
'like' => 'notifications/likes_item.tpl',
'dislike' => 'notifications/dislikes_item.tpl',
'attend' => 'notifications/attend_item.tpl',
'attendno' => 'notifications/attend_item.tpl',
'attendmaybe' => 'notifications/attend_item.tpl',
'friend' => 'notifications/friends_item.tpl',
'comment' => 'notifications/comments_item.tpl',
'post' => 'notifications/posts_item.tpl',
'notification' => 'notifications/notification.tpl',
];
/** @var FormattedNotification $Notification */
foreach ($notifications['notifications'] as $Notification) {
$notificationArray = $Notification->toArray();
$notificationArray = $notification->toArray();
$notificationTemplate = Renderer::getMarkupTemplate($notification_templates[$notificationArray['label']]);
$notificationTemplate = Renderer::getMarkupTemplate($notificationTemplates[$notificationArray['label']]);
$notificationContent[] = Renderer::replaceMacros($notificationTemplate, [
'$notification' => $notificationArray