use Friendica\BaseFactory;
use Friendica\Database\Database;
-use Friendica\Model\Contact;
-use Friendica\Model\Post;
-use Friendica\Model\Verb;
-use Friendica\Protocol\Activity;
+use Friendica\Model\Notification as ModelNotification;
use Psr\Log\LoggerInterface;
class Notification extends BaseFactory
status = Someone you enabled notifications for has posted a status
*/
- if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::NOTIF_NONE)) {
- $contact = Contact::getById($notification['actor-id'], ['pending']);
- $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 {
+ $type = ModelNotification::getType($notification);
+ if (empty($type)) {
return null;
}
use Friendica\Content\Text\BBCode;
use Friendica\Database\Database;
use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Protocol\Activity;
use Psr\Log\LoggerInterface;
/**
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;
+ }
}
use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Model\Post;
+use Friendica\Model\Subscription;
use Friendica\Util\Strings;
use Friendica\Model\Tag;
use Friendica\Protocol\Activity;
$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;
}
/**
'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;
}
/**
* @see https://github.com/web-push-libs/web-push-php
* Possibly we should simply use this.
*/
+
namespace Friendica\Model;
+use Friendica\Core\Logger;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Util\Crypto;
}
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);
+ }
}