1
1
Fork 0

Preparation for creating push notification

This commit is contained in:
Michael 2021-08-15 16:18:25 +00:00
parent 5056376902
commit 19f3cad56c
4 changed files with 74 additions and 22 deletions

View file

@ -23,10 +23,7 @@ namespace Friendica\Factory\Api\Mastodon;
use Friendica\BaseFactory; use Friendica\BaseFactory;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Model\Contact; use Friendica\Model\Notification as ModelNotification;
use Friendica\Model\Post;
use Friendica\Model\Verb;
use Friendica\Protocol\Activity;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
class Notification extends BaseFactory class Notification extends BaseFactory
@ -62,22 +59,8 @@ class Notification extends BaseFactory
status = Someone you enabled notifications for has posted a status status = Someone you enabled notifications for has posted a status
*/ */
if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::NOTIF_NONE)) { $type = ModelNotification::getType($notification);
$contact = Contact::getById($notification['actor-id'], ['pending']); if (empty($type)) {
$type = $contact['pending'] ? $type = '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])) {
$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])) {
$type = 'favourite';
} elseif ($notification['type'] == Post\UserNotification::NOTIF_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])) {
$type = 'mention';
} else {
return null; return null;
} }

View file

@ -25,6 +25,7 @@ use Friendica\BaseModel;
use Friendica\Content\Text\BBCode; use Friendica\Content\Text\BBCode;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Protocol\Activity;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
/** /**
@ -123,4 +124,34 @@ class Notification extends BaseModel
return $message; return $message;
} }
/**
* Fetch the notification type for the given notification
*
* @param array $notification
* @return string
*/
public static function getType(array $notification): string
{
if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::NOTIF_NONE)) {
$contact = Contact::getById($notification['actor-id'], ['pending']);
$contact['pending'] ? $type = '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])) {
$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])) {
$type = 'favourite';
} elseif ($notification['type'] == Post\UserNotification::NOTIF_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])) {
$type = 'mention';
} else {
return '';
}
return $type;
}
} }

View file

@ -30,6 +30,7 @@ use Friendica\Database\DBStructure;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Model\Subscription;
use Friendica\Util\Strings; use Friendica\Util\Strings;
use Friendica\Model\Tag; use Friendica\Model\Tag;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
@ -297,7 +298,14 @@ class UserNotification
$fields['target-uri-id'] = $item['uri-id']; $fields['target-uri-id'] = $item['uri-id'];
} }
return DBA::insert('notification', $fields, Database::INSERT_IGNORE); $ret = DBA::insert('notification', $fields, Database::INSERT_IGNORE);
if ($ret) {
$id = DBA::lastInsertId();
if (!empty($id)) {
Subscription::pushByNotificationId($id);
}
}
return $ret;
} }
/** /**
@ -318,7 +326,14 @@ class UserNotification
'created' => DateTimeFormat::utcNow(), 'created' => DateTimeFormat::utcNow(),
]; ];
return DBA::insert('notification', $fields, Database::INSERT_IGNORE); $ret = DBA::insert('notification', $fields, Database::INSERT_IGNORE);
if ($ret) {
$id = DBA::lastInsertId();
if (!empty($id)) {
Subscription::pushByNotificationId($id);
}
}
return $ret;
} }
/** /**

View file

@ -23,8 +23,10 @@
* @see https://github.com/web-push-libs/web-push-php * @see https://github.com/web-push-libs/web-push-php
* Possibly we should simply use this. * Possibly we should simply use this.
*/ */
namespace Friendica\Model; namespace Friendica\Model;
use Friendica\Core\Logger;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Util\Crypto; use Friendica\Util\Crypto;
@ -110,4 +112,25 @@ class Subscription
} }
return $keypair['vapid-public']; return $keypair['vapid-public'];
} }
/**
* Prepare push notification
*
* @param int $nid
* @return void
*/
public static function pushByNotificationId(int $nid)
{
$notification = DBA::selectFirst('notification', [], ['id' => $nid]);
$type = Notification::getType($notification);
if (empty($type)) {
return;
}
$subscriptions = DBA::select('subscription', [], ['uid' => $notification['uid'], $type => true]);
while ($subscription = DBA::fetch($subscriptions)) {
Logger::info('Push notification', ['id' => $subscription['id'], 'uid' => $subscription['uid'], 'type' => $type]);
}
DBA::close($subscriptions);
}
} }