From bf81f21001cead41585ecd549a98afe4e2797840 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 17 Sep 2021 22:14:32 -0400 Subject: [PATCH] Rename Model\Post\UserNotifications type constants --- include/api.php | 13 ++++-- include/enotify.php | 6 +-- src/Model/Notification.php | 54 +++++++++++---------- src/Model/Post/UserNotification.php | 57 +++++++++++------------ src/Module/Api/Mastodon/Notifications.php | 27 ++++++----- 5 files changed, 84 insertions(+), 73 deletions(-) diff --git a/include/api.php b/include/api.php index 52f04d954d..cd096c187a 100644 --- a/include/api.php +++ b/include/api.php @@ -2193,11 +2193,14 @@ function api_statuses_mentions($type) (SELECT `uri-id` FROM `post-user-notification` WHERE `uid` = ? AND `notification-type` & ? != 0 ORDER BY `uri-id`) AND (`uid` = 0 OR (`uid` = ? AND NOT `global`)) AND `id` > ?"; - $condition = [GRAVITY_PARENT, GRAVITY_COMMENT, api_user(), - Post\UserNotification::NOTIF_EXPLICIT_TAGGED | Post\UserNotification::NOTIF_IMPLICIT_TAGGED | - Post\UserNotification::NOTIF_THREAD_COMMENT | Post\UserNotification::NOTIF_DIRECT_COMMENT | - Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, - api_user(), $since_id]; + $condition = [ + GRAVITY_PARENT, GRAVITY_COMMENT, + api_user(), + Post\UserNotification::TYPE_EXPLICIT_TAGGED | Post\UserNotification::TYPE_IMPLICIT_TAGGED | + Post\UserNotification::TYPE_THREAD_COMMENT | Post\UserNotification::TYPE_DIRECT_COMMENT | + Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT, + api_user(), $since_id, + ]; if ($max_id > 0) { $query .= " AND `id` <= ?"; diff --git a/include/enotify.php b/include/enotify.php index c3d5e4a407..623347b8b0 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -495,7 +495,7 @@ function notification_from_array(array $notification) { Logger::info('Start', ['uid' => $notification['uid'], 'id' => $notification['id'], 'type' => $notification['type']]); - if ($notification['type'] == Post\UserNotification::NOTIF_NONE) { + if ($notification['type'] == Post\UserNotification::TYPE_NONE) { Logger::info('Not an item based notification, quitting', ['uid' => $notification['uid'], 'id' => $notification['id'], 'type' => $notification['type']]); return false; } @@ -562,10 +562,10 @@ function notification_from_array(array $notification) // So, we cannot have different subjects for notifications of the same thread. // Before this we have the name of the replier on the subject rendering // different subjects for messages on the same thread. - if ($notification['type'] == Post\UserNotification::NOTIF_EXPLICIT_TAGGED) { + if ($notification['type'] == Post\UserNotification::TYPE_EXPLICIT_TAGGED) { $params['type'] = Notification\Type::TAG_SELF; $subject = $l10n->t('%s %s tagged you', $subjectPrefix, $contact['name']); - } elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) { + } elseif ($notification['type'] == Post\UserNotification::TYPE_SHARED) { $params['type'] = Notification\Type::SHARE; $subject = $l10n->t('%s %s shared a new post', $subjectPrefix, $contact['name']); } else { diff --git a/src/Model/Notification.php b/src/Model/Notification.php index e9f9ff41c3..8411ed77db 100644 --- a/src/Model/Notification.php +++ b/src/Model/Notification.php @@ -44,20 +44,24 @@ class Notification extends BaseModel */ public static function getType(array $notification): string { - if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::NOTIF_NONE)) { + if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::TYPE_NONE)) { $contact = Contact::getById($notification['actor-id'], ['pending']); $type = $contact['pending'] ? 'follow_request' : 'follow'; } elseif (($notification['vid'] == Verb::getID(Activity::ANNOUNCE)) && - in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) { + in_array($notification['type'], [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) { $type = 'reblog'; } 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])) { + in_array($notification['type'], [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) { $type = 'favourite'; - } elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) { + } elseif ($notification['type'] == Post\UserNotification::TYPE_SHARED) { $type = 'status'; - } elseif (in_array($notification['type'], [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])) { + } elseif (in_array($notification['type'], [ + Post\UserNotification::TYPE_EXPLICIT_TAGGED, + Post\UserNotification::TYPE_IMPLICIT_TAGGED, + Post\UserNotification::TYPE_DIRECT_COMMENT, + Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT, + Post\UserNotification::TYPE_THREAD_COMMENT + ])) { $type = 'mention'; } else { return ''; @@ -90,7 +94,7 @@ class Notification extends BaseModel return $message; } - if ($notification['type'] == Post\UserNotification::NOTIF_NONE) { + if ($notification['type'] == Post\UserNotification::TYPE_NONE) { if ($contact['pending']) { $msg = $l10n->t('%1$s wants to follow you'); } else { @@ -108,7 +112,7 @@ class Notification extends BaseModel $announce = Verb::getID(Activity::ANNOUNCE); $post = Verb::getID(Activity::POST); - if (in_array($notification['type'], [Post\UserNotification::NOTIF_THREAD_COMMENT, Post\UserNotification::NOTIF_COMMENT_PARTICIPATION, Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION, Post\UserNotification::NOTIF_EXPLICIT_TAGGED])) { + if (in_array($notification['type'], [Post\UserNotification::TYPE_THREAD_COMMENT, Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION, Post\UserNotification::TYPE_EXPLICIT_TAGGED])) { $item = Post::selectFirst([], ['uri-id' => $notification['parent-uri-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]); if (empty($item)) { Logger::info('Parent post not found', ['uri-id' => $notification['parent-uri-id']]); @@ -137,13 +141,13 @@ class Notification extends BaseModel $cid = $item['causer-id']; } - if (($notification['type'] == Post\UserNotification::NOTIF_SHARED) && !empty($cid)) { + if (($notification['type'] == Post\UserNotification::TYPE_SHARED) && !empty($cid)) { $causer = Contact::getById($cid, ['id', 'name', 'url']); if (empty($contact)) { Logger::info('Causer not found', ['causer' => $cid]); return $message; } - } elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_COMMENT_PARTICIPATION, Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION])) { + } elseif (in_array($notification['type'], [Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION])) { $contact = Contact::getById($item['author-id'], ['id', 'name', 'url']); if (empty($contact)) { Logger::info('Author not found', ['author' => $item['author-id']]); @@ -163,54 +167,54 @@ class Notification extends BaseModel switch ($notification['vid']) { case $like: switch ($notification['type']) { - case Post\UserNotification::NOTIF_DIRECT_COMMENT: + case Post\UserNotification::TYPE_DIRECT_COMMENT: $msg = $l10n->t('%1$s liked your comment %2$s'); break; - case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT: + case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: $msg = $l10n->t('%1$s liked your post %2$s'); break; } break; case $dislike: switch ($notification['type']) { - case Post\UserNotification::NOTIF_DIRECT_COMMENT: + case Post\UserNotification::TYPE_DIRECT_COMMENT: $msg = $l10n->t('%1$s disliked your comment %2$s'); break; - case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT: + case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: $msg = $l10n->t('%1$s disliked your post %2$s'); break; } break; case $announce: switch ($notification['type']) { - case Post\UserNotification::NOTIF_DIRECT_COMMENT: + case Post\UserNotification::TYPE_DIRECT_COMMENT: $msg = $l10n->t('%1$s shared your comment %2$s'); break; - case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT: + case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: $msg = $l10n->t('%1$s shared your post %2$s'); break; } break; case $post: switch ($notification['type']) { - case Post\UserNotification::NOTIF_EXPLICIT_TAGGED: + case Post\UserNotification::TYPE_EXPLICIT_TAGGED: $msg = $l10n->t('%1$s tagged you on %2$s'); break; - case Post\UserNotification::NOTIF_IMPLICIT_TAGGED: + case Post\UserNotification::TYPE_IMPLICIT_TAGGED: $msg = $l10n->t('%1$s replied to you on %2$s'); break; - case Post\UserNotification::NOTIF_THREAD_COMMENT: + case Post\UserNotification::TYPE_THREAD_COMMENT: $msg = $l10n->t('%1$s commented in your thread %2$s'); break; - case Post\UserNotification::NOTIF_DIRECT_COMMENT: + case Post\UserNotification::TYPE_DIRECT_COMMENT: $msg = $l10n->t('%1$s commented on your comment %2$s'); break; - case Post\UserNotification::NOTIF_COMMENT_PARTICIPATION: - case Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION: + case Post\UserNotification::TYPE_COMMENT_PARTICIPATION: + case Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION: if (($causer['id'] == $contact['id']) && ($title != '')) { $msg = $l10n->t('%1$s commented in their thread %2$s'); } elseif ($causer['id'] == $contact['id']) { @@ -222,11 +226,11 @@ class Notification extends BaseModel } break; - case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT: + case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: $msg = $l10n->t('%1$s commented on your thread %2$s'); break; - case Post\UserNotification::NOTIF_SHARED: + case Post\UserNotification::TYPE_SHARED: if (($causer['id'] != $contact['id']) && ($title != '')) { $msg = $l10n->t('%1$s shared the post %2$s from %3$s'); } elseif ($causer['id'] != $contact['id']) { diff --git a/src/Model/Post/UserNotification.php b/src/Model/Post/UserNotification.php index f4bcb3e402..3f053e0f5c 100644 --- a/src/Model/Post/UserNotification.php +++ b/src/Model/Post/UserNotification.php @@ -39,16 +39,15 @@ use Friendica\Util\DateTimeFormat; class UserNotification { // Notification types - const NOTIF_NONE = 0; - const NOTIF_EXPLICIT_TAGGED = 1; - const NOTIF_IMPLICIT_TAGGED = 2; - const NOTIF_THREAD_COMMENT = 4; - const NOTIF_DIRECT_COMMENT = 8; - const NOTIF_COMMENT_PARTICIPATION = 16; - const NOTIF_ACTIVITY_PARTICIPATION = 32; - const NOTIF_DIRECT_THREAD_COMMENT = 64; - const NOTIF_SHARED = 128; - + const TYPE_NONE = 0; + const TYPE_EXPLICIT_TAGGED = 1; + const TYPE_IMPLICIT_TAGGED = 2; + const TYPE_THREAD_COMMENT = 4; + const TYPE_DIRECT_COMMENT = 8; + const TYPE_COMMENT_PARTICIPATION = 16; + const TYPE_ACTIVITY_PARTICIPATION = 32; + const TYPE_DIRECT_THREAD_COMMENT = 64; + const TYPE_SHARED = 128; /** * Insert a new user notification entry @@ -174,11 +173,11 @@ class UserNotification return; } - $notification_type = self::NOTIF_NONE; + $notification_type = self::TYPE_NONE; if (self::checkShared($item, $uid)) { - $notification_type = $notification_type | self::NOTIF_SHARED; - self::insertNoticationByItem(self::NOTIF_SHARED, $uid, $item); + $notification_type = $notification_type | self::TYPE_SHARED; + self::insertNoticationByItem(self::TYPE_SHARED, $uid, $item); $notified = true; } else { $notified = false; @@ -200,57 +199,57 @@ class UserNotification } if (self::checkExplicitMention($item, $profiles)) { - $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED; + $notification_type = $notification_type | self::TYPE_EXPLICIT_TAGGED; if (!$notified) { - self::insertNoticationByItem( self::NOTIF_EXPLICIT_TAGGED, $uid, $item); + self::insertNoticationByItem( self::TYPE_EXPLICIT_TAGGED, $uid, $item); $notified = true; } } if (self::checkImplicitMention($item, $profiles)) { - $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED; + $notification_type = $notification_type | self::TYPE_IMPLICIT_TAGGED; if (!$notified) { - self::insertNoticationByItem(self::NOTIF_IMPLICIT_TAGGED, $uid, $item); + self::insertNoticationByItem(self::TYPE_IMPLICIT_TAGGED, $uid, $item); $notified = true; } } if (self::checkDirectComment($item, $contacts)) { - $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT; + $notification_type = $notification_type | self::TYPE_DIRECT_COMMENT; if (!$notified) { - self::insertNoticationByItem(self::NOTIF_DIRECT_COMMENT, $uid, $item); + self::insertNoticationByItem(self::TYPE_DIRECT_COMMENT, $uid, $item); $notified = true; } } if (self::checkDirectCommentedThread($item, $contacts)) { - $notification_type = $notification_type | self::NOTIF_DIRECT_THREAD_COMMENT; + $notification_type = $notification_type | self::TYPE_DIRECT_THREAD_COMMENT; if (!$notified) { - self::insertNoticationByItem(self::NOTIF_DIRECT_THREAD_COMMENT, $uid, $item); + self::insertNoticationByItem(self::TYPE_DIRECT_THREAD_COMMENT, $uid, $item); $notified = true; } } if (self::checkCommentedThread($item, $contacts)) { - $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT; + $notification_type = $notification_type | self::TYPE_THREAD_COMMENT; if (!$notified) { - self::insertNoticationByItem(self::NOTIF_THREAD_COMMENT, $uid, $item); + self::insertNoticationByItem(self::TYPE_THREAD_COMMENT, $uid, $item); $notified = true; } } if (self::checkCommentedParticipation($item, $contacts)) { - $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION; + $notification_type = $notification_type | self::TYPE_COMMENT_PARTICIPATION; if (!$notified) { - self::insertNoticationByItem(self::NOTIF_COMMENT_PARTICIPATION, $uid, $item); + self::insertNoticationByItem(self::TYPE_COMMENT_PARTICIPATION, $uid, $item); $notified = true; } } if (self::checkActivityParticipation($item, $contacts)) { - $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION; + $notification_type = $notification_type | self::TYPE_ACTIVITY_PARTICIPATION; if (!$notified) { - self::insertNoticationByItem(self::NOTIF_ACTIVITY_PARTICIPATION, $uid, $item); + self::insertNoticationByItem(self::TYPE_ACTIVITY_PARTICIPATION, $uid, $item); $notified = true; } } @@ -278,7 +277,7 @@ class UserNotification private static function insertNoticationByItem(int $type, int $uid, array $item) { if (($item['gravity'] == GRAVITY_ACTIVITY) && - !in_array($type, [self::NOTIF_DIRECT_COMMENT, self::NOTIF_DIRECT_THREAD_COMMENT])) { + !in_array($type, [self::TYPE_DIRECT_COMMENT, self::TYPE_DIRECT_THREAD_COMMENT])) { // Activities are only stored when performed on the user's post or comment return; } @@ -321,7 +320,7 @@ class UserNotification $fields = [ 'uid' => $uid, 'vid' => $vid, - 'type' => self::NOTIF_NONE, + 'type' => self::TYPE_NONE, 'actor-id' => $actor, 'created' => DateTimeFormat::utcNow(), ]; diff --git a/src/Module/Api/Mastodon/Notifications.php b/src/Module/Api/Mastodon/Notifications.php index e02823dea5..763a768f58 100644 --- a/src/Module/Api/Mastodon/Notifications.php +++ b/src/Module/Api/Mastodon/Notifications.php @@ -77,37 +77,42 @@ class Notifications extends BaseApi if (in_array('follow_request', $request['exclude_types'])) { $condition = DBA::mergeConditions($condition, ["(`vid` != ? OR `type` != ? OR NOT EXISTS (SELECT `id` FROM `contact` WHERE `id` = `actor-id` AND `pending`))", - Verb::getID(Activity::FOLLOW), Post\UserNotification::NOTIF_NONE]); + Verb::getID(Activity::FOLLOW), Post\UserNotification::TYPE_NONE]); } if (in_array('follow', $request['exclude_types'])) { $condition = DBA::mergeConditions($condition, ["(`vid` != ? OR `type` != ? OR NOT EXISTS (SELECT `id` FROM `contact` WHERE `id` = `actor-id` AND NOT `pending`))", - Verb::getID(Activity::FOLLOW), Post\UserNotification::NOTIF_NONE]); + Verb::getID(Activity::FOLLOW), Post\UserNotification::TYPE_NONE]); } if (in_array('favourite', $request['exclude_types'])) { - $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?, ?) OR NOT `type` IN (?, ?))", + $condition = DBA::mergeConditions($condition, [ + "(NOT `vid` IN (?, ?) OR NOT `type` IN (?, ?))", Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE), - Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT]); + Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_THREAD_COMMENT + ]); } if (in_array('reblog', $request['exclude_types'])) { - $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?, ?))", + $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]); + Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_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]); + $condition = DBA::mergeConditions($condition, [ + "(NOT `vid` IN (?) OR NOT `type` IN (?, ?, ?, ?, ?))", + Verb::getID(Activity::POST), Post\UserNotification::TYPE_EXPLICIT_TAGGED, + Post\UserNotification::TYPE_IMPLICIT_TAGGED, Post\UserNotification::TYPE_DIRECT_COMMENT, + Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT, Post\UserNotification::TYPE_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]); + Verb::getID(Activity::POST), Post\UserNotification::TYPE_SHARED]); } if (!empty($request['max_id'])) {