- added some type-hints
- added documentation
This commit is contained in:
Roland Häder 2022-06-23 13:56:18 +02:00
parent 84d3eecc33
commit a49fb9cbf9
No known key found for this signature in database
GPG Key ID: C82EDE5DDFA0BA77
8 changed files with 64 additions and 19 deletions

View File

@ -31,7 +31,14 @@ use Friendica\Protocol\ActivityPub;
* Send updated profile data to Diaspora and ActivityPub * Send updated profile data to Diaspora and ActivityPub
*/ */
class ProfileUpdate { class ProfileUpdate {
public static function execute($uid = 0) { /**
* Sends updated profile data to Diaspora and ActivityPub
*
* @param int $uid User id (optional, default: 0)
* @return void
*/
public static function execute(int $uid = 0)
{
if (empty($uid)) { if (empty($uid)) {
return; return;
} }
@ -43,7 +50,13 @@ class ProfileUpdate {
foreach ($inboxes as $inbox => $receivers) { foreach ($inboxes as $inbox => $receivers) {
Logger::info('Profile update for user ' . $uid . ' to ' . $inbox .' via ActivityPub'); Logger::info('Profile update for user ' . $uid . ' to ' . $inbox .' via ActivityPub');
Worker::add(['priority' => $a->getQueueValue('priority'), 'created' => $a->getQueueValue('created'), 'dont_fork' => true], Worker::add(['priority' => $a->getQueueValue('priority'), 'created' => $a->getQueueValue('created'), 'dont_fork' => true],
'APDelivery', Delivery::PROFILEUPDATE, 0, $inbox, $uid, $receivers); 'APDelivery',
Delivery::PROFILEUPDATE,
0,
$inbox,
$uid,
$receivers
);
} }
Diaspora::sendProfile($uid); Diaspora::sendProfile($uid);

View File

@ -29,7 +29,13 @@ use Friendica\Protocol\OStatus;
class PubSubPublish class PubSubPublish
{ {
public static function execute($pubsubpublish_id = 0) /**
* Publishes subscriber id
*
* @param int $pubsubpublish_id Push subscriber id
* @return void
*/
public static function execute(int $pubsubpublish_id = 0)
{ {
if ($pubsubpublish_id == 0) { if ($pubsubpublish_id == 0) {
return; return;
@ -38,7 +44,13 @@ class PubSubPublish
self::publish($pubsubpublish_id); self::publish($pubsubpublish_id);
} }
private static function publish($id) /**
* Publishes push subscriber
*
* @param int $id Push subscriber id
* @return void
*/
private static function publish(int $id)
{ {
$subscriber = DBA::selectFirst('push_subscriber', [], ['id' => $id]); $subscriber = DBA::selectFirst('push_subscriber', [], ['id' => $id]);
if (!DBA::isResult($subscriber)) { if (!DBA::isResult($subscriber)) {
@ -48,7 +60,7 @@ class PubSubPublish
/// @todo Check server status with GServer::check() /// @todo Check server status with GServer::check()
// Before this can be done we need a way to safely detect the server url. // Before this can be done we need a way to safely detect the server url.
Logger::info("Generate feed of user " . $subscriber['nickname']. " to " . $subscriber['callback_url']. " - last updated " . $subscriber['last_update']); Logger::info('Generate feed of user ' . $subscriber['nickname'] . ' to ' . $subscriber['callback_url'] . ' - last updated ' . $subscriber['last_update']);
$last_update = $subscriber['last_update']; $last_update = $subscriber['last_update'];
$params = OStatus::feed($subscriber['nickname'], $last_update); $params = OStatus::feed($subscriber['nickname'], $last_update);
@ -57,11 +69,11 @@ class PubSubPublish
return; return;
} }
$hmac_sig = hash_hmac("sha1", $params, $subscriber['secret']); $hmac_sig = hash_hmac('sha1', $params, $subscriber['secret']);
$headers = [ $headers = [
'Content-type' => 'application/atom+xml', 'Content-type' => 'application/atom+xml',
'Link' => sprintf("<%s>;rel=hub,<%s>;rel=self", 'Link' => sprintf('<%s>;rel=hub,<%s>;rel=self',
DI::baseUrl() . '/pubsubhubbub/' . $subscriber['nickname'], DI::baseUrl() . '/pubsubhubbub/' . $subscriber['nickname'],
$subscriber['topic']), $subscriber['topic']),
'X-Hub-Signature' => 'sha1=' . $hmac_sig]; 'X-Hub-Signature' => 'sha1=' . $hmac_sig];

View File

@ -37,6 +37,13 @@ use Minishlink\WebPush\Subscription;
class PushSubscription class PushSubscription
{ {
/**
* Creates push subscription by subscription and notification ids
*
* @param int $sid Subscription id
* @param int $nid Notification id
* @return void
*/
public static function execute(int $sid, int $nid) public static function execute(int $sid, int $nid)
{ {
Logger::info('Start', ['subscription' => $sid, 'notification' => $nid]); Logger::info('Start', ['subscription' => $sid, 'notification' => $nid]);
@ -48,7 +55,7 @@ class PushSubscription
} }
try { try {
$Notification = DI::notification()->selectOneById($nid); $notification = DI::notification()->selectOneById($nid);
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
Logger::info('Notification not found', ['notification' => $nid]); Logger::info('Notification not found', ['notification' => $nid]);
return; return;
@ -60,7 +67,7 @@ class PushSubscription
return; return;
} }
$user = User::getById($Notification->uid); $user = User::getById($notification->uid);
if (empty($user)) { if (empty($user)) {
Logger::info('User not found', ['application' => $subscription['uid']]); Logger::info('User not found', ['application' => $subscription['uid']]);
return; return;
@ -68,21 +75,21 @@ class PushSubscription
$l10n = DI::l10n()->withLang($user['language']); $l10n = DI::l10n()->withLang($user['language']);
if ($Notification->actorId) { if ($notification->actorId) {
$actor = Contact::getById($Notification->actorId); $actor = Contact::getById($notification->actorId);
} }
$body = ''; $body = '';
if ($Notification->targetUriId) { if ($notification->targetUriId) {
$post = Post::selectFirst([], ['uri-id' => $Notification->targetUriId, 'uid' => [0, $Notification->uid]]); $post = Post::selectFirst([], ['uri-id' => $notification->targetUriId, 'uid' => [0, $notification->uid]]);
if (!empty($post['body'])) { if (!empty($post['body'])) {
$body = BBCode::toPlaintext($post['body'], false); $body = BBCode::toPlaintext($post['body'], false);
$body = Plaintext::shorten($body, 160, $Notification->uid); $body = Plaintext::shorten($body, 160, $notification->uid);
} }
} }
$message = DI::notificationFactory()->getMessageFromNotification($Notification); $message = DI::notificationFactory()->getMessageFromNotification($notification);
$title = $message['plain'] ?: ''; $title = $message['plain'] ?: '';
$push = Subscription::create([ $push = Subscription::create([
@ -94,11 +101,12 @@ class PushSubscription
], ],
]); ]);
// @todo Only used for logging?
$payload = [ $payload = [
'access_token' => $application_token['access_token'], 'access_token' => $application_token['access_token'],
'preferred_locale' => $user['language'], 'preferred_locale' => $user['language'],
'notification_id' => $nid, 'notification_id' => $nid,
'notification_type' => \Friendica\Factory\Api\Mastodon\Notification::getType($Notification), 'notification_type' => \Friendica\Factory\Api\Mastodon\Notification::getType($notification),
'icon' => $actor['thumb'] ?? '', 'icon' => $actor['thumb'] ?? '',
'title' => $title ?: $l10n->t('Notification from Friendica'), 'title' => $title ?: $l10n->t('Notification from Friendica'),
'body' => $body ?: $l10n->t('Empty Post'), 'body' => $body ?: $l10n->t('Empty Post'),

View File

@ -29,7 +29,13 @@ use Friendica\Model\Post;
* Removes orphaned data from deleted users * Removes orphaned data from deleted users
*/ */
class RemoveUser { class RemoveUser {
public static function execute($uid) /**
* Removes user by id
*
* @param int $uid User id
* @return void
*/
public static function execute(int $uid)
{ {
// Only delete if the user is archived // Only delete if the user is archived
$condition = ['account_removed' => true, 'uid' => $uid]; $condition = ['account_removed' => true, 'uid' => $uid];

View File

@ -28,9 +28,11 @@ class UpdateContact
{ {
/** /**
* Update contact data via probe * Update contact data via probe
*
* @param int $contact_id Contact ID * @param int $contact_id Contact ID
* @return void
*/ */
public static function execute($contact_id) public static function execute(int $contact_id)
{ {
$success = Contact::updateFromProbe($contact_id); $success = Contact::updateFromProbe($contact_id);

View File

@ -102,7 +102,7 @@ class UpdateContacts
* @param array $ids * @param array $ids
* @return array contact ids * @return array contact ids
*/ */
private static function getContactsToUpdate(array $condition, int $limit, array $ids = []) private static function getContactsToUpdate(array $condition, int $limit, array $ids = []): array
{ {
$contacts = DBA::select('contact', ['id'], $condition, ['limit' => $limit]); $contacts = DBA::select('contact', ['id'], $condition, ['limit' => $limit]);
while ($contact = DBA::fetch($contacts)) { while ($contact = DBA::fetch($contacts)) {

View File

@ -30,8 +30,10 @@ class UpdateGServer
{ {
/** /**
* Update the given server * Update the given server
*
* @param string $server_url Server URL * @param string $server_url Server URL
* @param boolean $only_nodeinfo Only use nodeinfo for server detection * @param boolean $only_nodeinfo Only use nodeinfo for server detection
* @return void
*/ */
public static function execute(string $server_url, bool $only_nodeinfo = false) public static function execute(string $server_url, bool $only_nodeinfo = false)
{ {

View File

@ -32,7 +32,9 @@ class UpdateServerPeers
{ {
/** /**
* Query the given server for their known peers * Query the given server for their known peers
*
* @param string $gserver Server URL * @param string $gserver Server URL
* @return void
*/ */
public static function execute(string $url) public static function execute(string $url)
{ {