API: Show activity notifications

This commit is contained in:
Michael 2021-06-01 05:51:03 +00:00
parent dad6561715
commit 3f0937dd42
10 changed files with 204 additions and 80 deletions

View file

@ -1,6 +1,6 @@
-- ------------------------------------------ -- ------------------------------------------
-- Friendica 2021.06-rc (Siberian Iris) -- Friendica 2021.06-rc (Siberian Iris)
-- DB_UPDATE_VERSION 1420 -- DB_UPDATE_VERSION 1421
-- ------------------------------------------ -- ------------------------------------------
@ -819,6 +819,33 @@ CREATE TABLE IF NOT EXISTS `manage` (
FOREIGN KEY (`mid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE FOREIGN KEY (`mid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE
) DEFAULT COLLATE utf8mb4_general_ci COMMENT='table of accounts that can manage each other'; ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='table of accounts that can manage each other';
--
-- TABLE notification
--
CREATE TABLE IF NOT EXISTS `notification` (
`id` int unsigned NOT NULL auto_increment COMMENT 'sequential ID',
`uid` mediumint unsigned COMMENT 'Owner User id',
`vid` smallint unsigned COMMENT 'Id of the verb table entry that contains the activity verbs',
`type` tinyint unsigned COMMENT '',
`actor-id` int unsigned COMMENT 'Link to the contact table with uid=0 of the actor that caused the notification',
`target-uri-id` int unsigned COMMENT 'Item-uri id of the related post',
`parent-uri-id` int unsigned COMMENT 'Item-uri id of the parent of the related post',
`created` datetime COMMENT '',
`seen` boolean DEFAULT '0' COMMENT '',
PRIMARY KEY(`id`),
UNIQUE INDEX `uid_vid_type_actor-id_target-uri-id` (`uid`,`vid`,`type`,`actor-id`,`target-uri-id`),
INDEX `vid` (`vid`),
INDEX `actor-id` (`actor-id`),
INDEX `target-uri-id` (`target-uri-id`),
INDEX `parent-uri-id` (`parent-uri-id`),
INDEX `seen_uid` (`seen`,`uid`),
FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`vid`) REFERENCES `verb` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT,
FOREIGN KEY (`actor-id`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`target-uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`parent-uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE
) DEFAULT COLLATE utf8mb4_general_ci COMMENT='notifications';
-- --
-- TABLE notify -- TABLE notify
-- --

View file

@ -236,6 +236,7 @@ function display_content(App $a, $update = false, $update_uid = 0)
} }
if (!DI::pConfig()->get(local_user(), 'system', 'detailed_notif')) { if (!DI::pConfig()->get(local_user(), 'system', 'detailed_notif')) {
DBA::update('notification', ['seen' => true], ['parent-uri-id' => $item['parent-uri-id'], 'uid' => local_user()]);
DBA::update('notify', ['seen' => true], ['parent-uri-id' => $item['parent-uri-id'], 'uid' => local_user()]); DBA::update('notify', ['seen' => true], ['parent-uri-id' => $item['parent-uri-id'], 'uid' => local_user()]);
} }

View file

@ -24,23 +24,18 @@ namespace Friendica\Factory\Api\Mastodon;
use Friendica\BaseFactory; use Friendica\BaseFactory;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Post;
use Friendica\Model\Notification as ModelNotification; use Friendica\Model\Verb;
use Friendica\Protocol\Activity;
class Notification extends BaseFactory class Notification extends BaseFactory
{ {
public function createFromNotifyId(int $id) public function createFromNotifyId(int $id)
{ {
$notification = DBA::selectFirst('notify', [], ['id' => $id]); $notification = DBA::selectFirst('notification', [], ['id' => $id]);
if (!DBA::isResult($notification)) { if (!DBA::isResult($notification)) {
return null; return null;
} }
$cid = Contact::getIdForURL($notification['url'], 0, false);
if (empty($cid)) {
return null;
}
/* /*
follow = Someone followed you follow = Someone followed you
follow_request = Someone requested to follow you follow_request = Someone requested to follow you
@ -51,32 +46,27 @@ class Notification extends BaseFactory
status = Someone you enabled notifications for has posted a status status = Someone you enabled notifications for has posted a status
*/ */
switch ($notification['type']) { if (($notification['vid'] == Verb::getID(Activity::ANNOUNCE)) &&
case ModelNotification\Type::INTRO: in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
$type = 'follow_request'; $type = 'reblog';
break; } elseif (in_array($notification['vid'], [Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE)]) &&
in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
case ModelNotification\Type::WALL: $type = 'favourite';
case ModelNotification\Type::COMMENT: } elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) {
case ModelNotification\Type::MAIL:
case ModelNotification\Type::TAG_SELF:
case ModelNotification\Type::POKE:
$type = 'mention';
break;
case ModelNotification\Type::SHARE:
$type = 'status'; $type = 'status';
break; } elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
default: Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT])) {
$type = 'mention';
} else {
return null; return null;
} }
$account = DI::mstdnAccount()->createFromContactId($cid); $account = DI::mstdnAccount()->createFromContactId($notification['actor-id']);
if (!empty($notification['uri-id'])) { if (!empty($notification['target-uri-id'])) {
try { try {
$status = DI::mstdnStatus()->createFromUriId($notification['uri-id'], $notification['uid']); $status = DI::mstdnStatus()->createFromUriId($notification['target-uri-id'], $notification['uid']);
} catch (\Throwable $th) { } catch (\Throwable $th) {
$status = null; $status = null;
} }
@ -84,6 +74,6 @@ class Notification extends BaseFactory
$status = null; $status = null;
} }
return new \Friendica\Object\Api\Mastodon\Notification($id, $type, $notification['date'], $account, $status); return new \Friendica\Object\Api\Mastodon\Notification($id, $type, $notification['created'], $account, $status);
} }
} }

View file

@ -715,6 +715,8 @@ class Contact
DBA::update('contact', $fields, ['id' => $self['id']]); DBA::update('contact', $fields, ['id' => $self['id']]);
// Update the public contact as well // Update the public contact as well
$fields['prvkey'] = null;
$fields['self'] = false;
DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $self['nurl']]); DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $self['nurl']]);
// Update the profile // Update the profile

View file

@ -33,7 +33,7 @@ use Friendica\Model\Post;
use Friendica\Util\Strings; use Friendica\Util\Strings;
use Friendica\Model\Tag; use Friendica\Model\Tag;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
use Friendica\Util\DateTimeFormat;
class UserNotification class UserNotification
{ {
@ -128,8 +128,8 @@ class UserNotification
*/ */
public static function setNotification(int $uri_id, int $uid) public static function setNotification(int $uri_id, int $uid)
{ {
$fields = ['id', 'uri-id', 'parent-uri-id', 'uid', 'body', 'parent', 'gravity', $fields = ['id', 'uri-id', 'parent-uri-id', 'uid', 'body', 'parent', 'gravity', 'vid', 'gravity',
'private', 'contact-id', 'thr-parent', 'parent-uri-id', 'parent-uri', 'author-id', 'verb']; 'private', 'contact-id', 'thr-parent', 'thr-parent-id', 'parent-uri-id', 'parent-uri', 'author-id', 'verb'];
$item = Post::selectFirst($fields, ['uri-id' => $uri_id, 'uid' => $uid, 'origin' => false]); $item = Post::selectFirst($fields, ['uri-id' => $uri_id, 'uid' => $uid, 'origin' => false]);
if (!DBA::isResult($item)) { if (!DBA::isResult($item)) {
return; return;
@ -177,6 +177,10 @@ class UserNotification
if (self::checkShared($item, $uid)) { if (self::checkShared($item, $uid)) {
$notification_type = $notification_type | self::NOTIF_SHARED; $notification_type = $notification_type | self::NOTIF_SHARED;
self::insertNotication(self::NOTIF_SHARED, $uid, $item);
$notified = true;
} else {
$notified = false;
} }
$profiles = self::getProfileForUser($uid); $profiles = self::getProfileForUser($uid);
@ -194,38 +198,64 @@ class UserNotification
return; return;
} }
// Only create notifications for posts and comments, not for activities
if (in_array($item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT])) {
if (self::checkImplicitMention($item, $profiles)) {
$notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
}
if (self::checkExplicitMention($item, $profiles)) { if (self::checkExplicitMention($item, $profiles)) {
$notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED; $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
if (!$notified) {
self::insertNotication( self::NOTIF_EXPLICIT_TAGGED, $uid, $item);
$notified = true;
}
} }
if (self::checkCommentedThread($item, $contacts)) { if (self::checkImplicitMention($item, $profiles)) {
$notification_type = $notification_type | self::NOTIF_THREAD_COMMENT; $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
if (!$notified) {
self::insertNotication(self::NOTIF_IMPLICIT_TAGGED, $uid, $item);
$notified = true;
}
} }
if (self::checkDirectComment($item, $contacts)) { if (self::checkDirectComment($item, $contacts)) {
$notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT; $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
if (!$notified) {
self::insertNotication(self::NOTIF_DIRECT_COMMENT, $uid, $item);
$notified = true;
}
} }
if (self::checkDirectCommentedThread($item, $contacts)) { if (self::checkDirectCommentedThread($item, $contacts)) {
$notification_type = $notification_type | self::NOTIF_DIRECT_THREAD_COMMENT; $notification_type = $notification_type | self::NOTIF_DIRECT_THREAD_COMMENT;
if (!$notified) {
self::insertNotication(self::NOTIF_DIRECT_THREAD_COMMENT, $uid, $item);
$notified = true;
}
}
if (self::checkCommentedThread($item, $contacts)) {
$notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
if (!$notified) {
self::insertNotication(self::NOTIF_THREAD_COMMENT, $uid, $item);
$notified = true;
}
} }
if (self::checkCommentedParticipation($item, $contacts)) { if (self::checkCommentedParticipation($item, $contacts)) {
$notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION; $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
if (!$notified) {
self::insertNotication(self::NOTIF_COMMENT_PARTICIPATION, $uid, $item);
$notified = true;
}
} }
if (self::checkActivityParticipation($item, $contacts)) { if (self::checkActivityParticipation($item, $contacts)) {
$notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION; $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
if (!$notified) {
self::insertNotication(self::NOTIF_ACTIVITY_PARTICIPATION, $uid, $item);
$notified = true;
} }
} }
if (empty($notification_type)) { // Only create notifications for posts and comments, not for activities
if (empty($notification_type) || !in_array($item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT])) {
return; return;
} }
@ -236,6 +266,32 @@ class UserNotification
self::update($item['uri-id'], $uid, $fields, true); self::update($item['uri-id'], $uid, $fields, true);
} }
private static function insertNotication(int $type, int $uid, array $item)
{
if (($item['gravity'] == GRAVITY_ACTIVITY) &&
!in_array($type, [self::NOTIF_DIRECT_COMMENT, self::NOTIF_DIRECT_THREAD_COMMENT])) {
// Activities are only stored when performed on the user's post or comment
return;
}
$fields = [
'uid' => $uid,
'vid' => $item['vid'],
'type' => $type,
'actor-id' => $item['author-id'],
'parent-uri-id' => $item['parent-uri-id'],
'created' => DateTimeFormat::utcNow(),
];
if ($item['gravity'] == GRAVITY_ACTIVITY) {
$fields['target-uri-id'] = $item['thr-parent-id'];
} else {
$fields['target-uri-id'] = $item['uri-id'];
}
dba::insert('notification', $fields);
}
/** /**
* Fetch all profiles (contact URL) of a given user * Fetch all profiles (contact URL) of a given user
* @param int $uid User ID * @param int $uid User ID

View file

@ -25,8 +25,10 @@ use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Notification; use Friendica\Model\Post;
use Friendica\Model\Verb;
use Friendica\Module\BaseApi; use Friendica\Module\BaseApi;
use Friendica\Protocol\Activity;
/** /**
* @see https://docs.joinmastodon.org/methods/notifications/ * @see https://docs.joinmastodon.org/methods/notifications/
@ -63,7 +65,7 @@ class Notifications extends BaseApi
$params = ['order' => ['id' => true], 'limit' => $request['limit']]; $params = ['order' => ['id' => true], 'limit' => $request['limit']];
$condition = ['uid' => $uid, 'seen' => false, 'type' => []]; $condition = ['uid' => $uid, 'seen' => false];
if (!empty($request['account_id'])) { if (!empty($request['account_id'])) {
$contact = Contact::getById($request['account_id'], ['url']); $contact = Contact::getById($request['account_id'], ['url']);
@ -72,17 +74,32 @@ class Notifications extends BaseApi
} }
} }
if (!in_array('follow_request', $request['exclude_types'])) { if (in_array('follow_request', $request['exclude_types'])) {
$condition['type'] = array_merge($condition['type'], [Notification\Type::INTRO]); $condition = DBA::mergeConditions($condition, ["NOT `vid` IN (?)", Verb::getID(Activity::FOLLOW)]);
} }
if (!in_array('mention', $request['exclude_types'])) { if (in_array('favourite', $request['exclude_types'])) {
$condition['type'] = array_merge($condition['type'], $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?, ?) OR NOT `type` IN (?, ?))",
[Notification\Type::WALL, Notification\Type::COMMENT, Notification\Type::MAIL, Notification\Type::TAG_SELF, Notification\Type::POKE]); Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE),
Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT]);
} }
if (!in_array('status', $request['exclude_types'])) { if (in_array('reblog', $request['exclude_types'])) {
$condition['type'] = array_merge($condition['type'], [Notification\Type::SHARE]); $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?, ?))",
Verb::getID(Activity::ANNOUNCE),
Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT]);
}
if (in_array('mention', $request['exclude_types'])) {
$condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?, ?, ?, ?, ?))",
Verb::getID(Activity::POST), Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT]);
}
if (in_array('status', $request['exclude_types'])) {
$condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?))",
Verb::getID(Activity::POST), Post\UserNotification::NOTIF_SHARED]);
} }
if (!empty($request['max_id'])) { if (!empty($request['max_id'])) {
@ -101,9 +118,12 @@ class Notifications extends BaseApi
$notifications = []; $notifications = [];
$notify = DBA::select('notify', ['id'], $condition, $params); $notify = DBA::select('notification', ['id'], $condition, $params);
while ($notification = DBA::fetch($notify)) { while ($notification = DBA::fetch($notify)) {
$notifications[] = DI::mstdnNotification()->createFromNotifyId($notification['id']); $entry = DI::mstdnNotification()->createFromNotifyId($notification['id']);
if (!empty($entry)) {
$notifications[] = $entry;
}
} }
if (!empty($request['min_id'])) { if (!empty($request['min_id'])) {

View file

@ -35,7 +35,7 @@ class Clear extends BaseApi
self::login(self::SCOPE_WRITE); self::login(self::SCOPE_WRITE);
$uid = self::getCurrentUserID(); $uid = self::getCurrentUserID();
DBA::update('notify', ['seen' => true], ['uid' => $uid]); DBA::update('notification', ['seen' => true], ['uid' => $uid]);
System::jsonExit([]); System::jsonExit([]);
} }

View file

@ -40,7 +40,7 @@ class Dismiss extends BaseApi
DI::mstdnError()->UnprocessableEntity(); DI::mstdnError()->UnprocessableEntity();
} }
DBA::update('notify', ['seen' => true], ['uid' => $uid, 'id' => $parameters['id']]); DBA::update('notification', ['seen' => true], ['uid' => $uid, 'id' => $parameters['id']]);
System::jsonExit([]); System::jsonExit([]);
} }

View file

@ -87,8 +87,13 @@ class Notification extends BaseRepository
public function setSeen(bool $seen = true, Model\Notification $notify = null) public function setSeen(bool $seen = true, Model\Notification $notify = null)
{ {
if (empty($notify)) { if (empty($notify)) {
$this->dba->update('notification', ['seen' => $seen], ['uid' => local_user()]);
$conditions = ['uid' => local_user()]; $conditions = ['uid' => local_user()];
} else { } else {
if (!empty($notify->{'uri-id'})) {
$this->dba->update('notification', ['seen' => $seen], ['uid' => local_user(), 'target-uri-id' => $notify->{'uri-id'}]);
}
$conditions = ['(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?', $conditions = ['(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
$notify->link, $notify->link,
$notify->parent, $notify->parent,

View file

@ -55,7 +55,7 @@
use Friendica\Database\DBA; use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) { if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1420); define('DB_UPDATE_VERSION', 1421);
} }
return [ return [
@ -882,6 +882,29 @@ return [
"mid" => ["mid"], "mid" => ["mid"],
] ]
], ],
"notification" => [
"comment" => "notifications",
"fields" => [
"id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"],
"uid" => ["type" => "mediumint unsigned", "foreign" => ["user" => "uid"], "comment" => "Owner User id"],
"vid" => ["type" => "smallint unsigned", "foreign" => ["verb" => "id", "on delete" => "restrict"], "comment" => "Id of the verb table entry that contains the activity verbs"],
"type" => ["type" => "tinyint unsigned", "comment" => ""],
"actor-id" => ["type" => "int unsigned", "foreign" => ["contact" => "id"], "comment" => "Link to the contact table with uid=0 of the actor that caused the notification"],
"target-uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Item-uri id of the related post"],
"parent-uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Item-uri id of the parent of the related post"],
"created" => ["type" => "datetime", "comment" => ""],
"seen" => ["type" => "boolean", "default" => "0", "comment" => ""],
],
"indexes" => [
"PRIMARY" => ["id"],
"uid_vid_type_actor-id_target-uri-id" => ["UNIQUE", "uid", "vid", "type", "actor-id", "target-uri-id"],
"vid" => ["vid"],
"actor-id" => ["actor-id"],
"target-uri-id" => ["target-uri-id"],
"parent-uri-id" => ["parent-uri-id"],
"seen_uid" => ["seen", "uid"],
]
],
"notify" => [ "notify" => [
"comment" => "notifications", "comment" => "notifications",
"fields" => [ "fields" => [