friendica/src/Protocol/ActivityPub/Queue.php

344 lines
9.2 KiB
PHP
Raw Normal View History

2022-07-21 01:16:14 -04:00
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Protocol\ActivityPub;
use Friendica\Core\Logger;
use Friendica\Database\Database;
use Friendica\Database\DBA;
2022-07-24 09:49:57 -04:00
use Friendica\DI;
2022-07-21 01:16:14 -04:00
use Friendica\Util\DateTimeFormat;
use Friendica\Util\JsonLD;
2022-07-21 01:16:14 -04:00
/**
* This class handles the processing of incoming posts
*/
class Queue
{
2022-07-21 01:33:01 -04:00
/**
* Add activity to the queue
*
* @param array $activity
* @param string $type
* @param integer $uid
* @param string $http_signer
* @param boolean $push
* @return array
*/
2022-07-24 10:26:06 -04:00
public static function add(array $activity, string $type, int $uid, string $http_signer, bool $push, bool $trust_source): array
2022-07-21 01:16:14 -04:00
{
$fields = [
2022-07-21 01:42:53 -04:00
'activity-id' => $activity['id'],
2022-07-21 01:16:14 -04:00
'object-id' => $activity['object_id'],
'type' => $type,
'object-type' => $activity['object_type'],
'activity' => json_encode($activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
'received' => DateTimeFormat::utcNow(),
'push' => $push,
2022-07-24 10:26:06 -04:00
'trust' => $trust_source,
2022-07-21 01:16:14 -04:00
];
if (!empty($activity['reply-to-id'])) {
$fields['in-reply-to-id'] = $activity['reply-to-id'];
}
2022-07-23 09:58:14 -04:00
if (!empty($activity['context'])) {
$fields['conversation'] = $activity['context'];
2022-07-23 10:10:07 -04:00
} elseif (!empty($activity['conversation'])) {
2022-07-23 09:58:14 -04:00
$fields['conversation'] = $activity['conversation'];
}
2022-07-21 01:16:14 -04:00
if (!empty($activity['object_object_type'])) {
$fields['object-object-type'] = $activity['object_object_type'];
}
if (!empty($http_signer)) {
$fields['signer'] = $http_signer;
}
DBA::insert('inbox-entry', $fields, Database::INSERT_IGNORE);
$queue = DBA::selectFirst('inbox-entry', ['id'], ['activity-id' => $activity['id']]);
if (!empty($queue['id'])) {
$activity['entry-id'] = $queue['id'];
DBA::insert('inbox-entry-receiver', ['queue-id' => $queue['id'], 'uid' => $uid], Database::INSERT_IGNORE);
}
return $activity;
}
/**
2022-08-03 00:31:56 -04:00
* Checks if an entry for a given url and type already exists
*
* @param string $url
* @param string $type
* @return boolean
*/
public static function exists(string $url, string $type): bool
{
return DBA::exists('inbox-entry', ['type' => $type, 'object-id' => $url]);
}
2022-07-21 01:33:01 -04:00
/**
* Remove activity from the queue
*
* @param array $activity
* @return void
*/
2022-07-21 01:16:14 -04:00
public static function remove(array $activity = [])
{
if (empty($activity['entry-id'])) {
return;
}
DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
}
2022-07-24 05:26:52 -04:00
/**
* Delete all entries that depend on the given worker id
*
* @param integer $wid
* @return void
*/
public static function deleteByWorkerId(int $wid)
{
$entries = DBA::select('inbox-entry', ['id'], ['wid' => $wid]);
while ($entry = DBA::fetch($entries)) {
self::deleteById($entry['id']);
}
DBA::close($entries);
}
/**
* Delete recursively an entry and all their children
*
* @param integer $id
* @return void
*/
2022-07-24 09:09:35 -04:00
public static function deleteById(int $id)
2022-07-24 05:26:52 -04:00
{
$entry = DBA::selectFirst('inbox-entry', ['id', 'object-id'], ['id' => $id]);
if (empty($entry)) {
return;
}
DBA::delete('inbox-entry', ['id' => $entry['id']]);
}
/**
* Set the worker id for the queue entry
*
2022-08-03 00:31:56 -04:00
* @param int $entry_id
* @param int $wid
* @return void
*/
2022-08-03 00:31:56 -04:00
public static function setWorkerId(int $entry_id, int $wid)
{
2022-08-03 00:31:56 -04:00
if (empty($entry_id) || empty($wid)) {
return;
}
2022-08-03 00:31:56 -04:00
DBA::update('inbox-entry', ['wid' => $wid], ['id' => $entry_id]);
}
2022-07-21 04:55:45 -04:00
/**
* Check if there is an assigned worker task
*
* @param int $wid
*
2022-07-21 04:55:45 -04:00
* @return bool
*/
public static function hasWorker(int $wid): bool
2022-07-21 04:55:45 -04:00
{
if (empty($wid)) {
2022-07-21 04:55:45 -04:00
return false;
}
return DBA::exists('workerqueue', ['id' => $wid, 'done' => false]);
2022-07-21 04:55:45 -04:00
}
2022-07-21 01:33:01 -04:00
/**
* Process the activity with the given id
*
* @param integer $id
*
* @return bool
2022-07-21 01:33:01 -04:00
*/
public static function process(int $id): bool
2022-07-21 01:16:14 -04:00
{
$entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
if (empty($entry)) {
return false;
2022-07-21 01:16:14 -04:00
}
2022-07-21 01:33:01 -04:00
if (!empty($entry['wid'])) {
$worker = DI::app()->getQueue();
$wid = $worker['id'] ?? 0;
if ($entry['wid'] != $wid) {
$workerqueue = DBA::selectFirst('workerqueue', ['pid'], ['id' => $entry['wid'], 'done' => false]);
if (!empty($workerqueue['pid']) && posix_kill($workerqueue['pid'], 0)) {
Logger::notice('Entry is already processed via another process.', ['current' => $wid, 'processor' => $entry['wid']]);
return false;
}
}
}
2022-07-21 01:16:14 -04:00
Logger::debug('Processing queue entry', ['id' => $entry['id'], 'type' => $entry['type'], 'object-type' => $entry['object-type'], 'uri' => $entry['object-id'], 'in-reply-to' => $entry['in-reply-to-id']]);
2022-07-21 01:42:53 -04:00
$activity = json_decode($entry['activity'], true);
$type = $entry['type'];
$push = $entry['push'];
2022-07-21 01:16:14 -04:00
2022-07-24 05:26:52 -04:00
$activity['entry-id'] = $entry['id'];
$activity['worker-id'] = $entry['wid'];
$activity['recursion-depth'] = 0;
2022-07-21 01:16:14 -04:00
2022-07-29 18:56:53 -04:00
$receivers = DBA::select('inbox-entry-receiver', ['uid'], ["`queue-id` = ? AND `uid` != ?", $entry['id'], 0]);
2022-07-23 02:52:43 -04:00
while ($receiver = DBA::fetch($receivers)) {
if (!in_array($receiver['uid'], $activity['receiver'])) {
$activity['receiver'][] = $receiver['uid'];
}
}
DBA::close($receivers);
2022-07-21 01:16:14 -04:00
if (!Receiver::routeActivities($activity, $type, $push)) {
self::remove($activity);
}
return true;
2022-07-21 01:16:14 -04:00
}
2022-07-21 01:33:01 -04:00
/**
* Process all activities
*
* @return void
*/
2022-07-21 01:16:14 -04:00
public static function processAll()
{
2022-07-24 10:26:06 -04:00
$entries = DBA::select('inbox-entry', ['id', 'type', 'object-type', 'object-id', 'in-reply-to-id'], ["`trust` AND `wid` IS NULL"], ['order' => ['id' => true]]);
2022-07-21 01:16:14 -04:00
while ($entry = DBA::fetch($entries)) {
2022-07-24 05:26:52 -04:00
// We don't need to process entries that depend on already existing entries.
2022-07-24 09:09:35 -04:00
if (!empty($entry['in-reply-to-id']) && DBA::exists('inbox-entry', ["`id` != ? AND `object-id` = ?", $entry['id'], $entry['in-reply-to-id']])) {
2022-07-24 05:26:52 -04:00
continue;
}
Logger::debug('Process leftover entry', $entry);
2022-07-21 01:16:14 -04:00
self::process($entry['id']);
}
DBA::close($entries);
2022-07-21 02:23:55 -04:00
}
2022-07-21 01:33:01 -04:00
2022-07-21 02:23:55 -04:00
/**
* Clear old activities
2022-07-21 02:23:55 -04:00
*
* @return void
*/
public static function clear()
{
2022-07-23 19:10:47 -04:00
// We delete all entries that aren't associated with a worker entry after seven days.
// The other entries are deleted when the worker deferred for too long.
DBA::delete('inbox-entry', ["`wid` IS NULL AND `received` < ?", DateTimeFormat::utc('now - 7 days')]);
2022-07-24 09:49:57 -04:00
// Optimizing this table only last seconds
if (DI::config()->get('system', 'optimize_tables')) {
Logger::info('Optimize start');
DBA::e("OPTIMIZE TABLE `inbox-entry`");
Logger::info('Optimize end');
}
2022-07-21 01:16:14 -04:00
}
2022-07-21 01:33:01 -04:00
/**
* Process all activities that are children of a given post url
*
* @param string $uri
* @return int
2022-07-21 01:33:01 -04:00
*/
public static function processReplyByUri(string $uri): int
2022-07-21 01:16:14 -04:00
{
$count = 0;
2022-07-23 19:10:47 -04:00
$entries = DBA::select('inbox-entry', ['id'], ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
2022-07-21 01:16:14 -04:00
while ($entry = DBA::fetch($entries)) {
$count += 1;
2022-07-21 01:16:14 -04:00
self::process($entry['id']);
}
DBA::close($entries);
return $count;
}
/**
* Checks if there are children of the given uri
*
* @param string $uri
*
* @return bool
*/
public static function hasChildren(string $uri): bool
{
return DBA::exists('inbox-entry', ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
}
/**
* Prepare the queue entry.
* This is a test function that is used solely for development.
*
* @param integer $id
* @return array
*/
public static function reprepareActivityById(int $id): array
{
$entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
if (empty($entry)) {
return [];
}
$receiver = DBA::selectFirst('inbox-entry-receiver', ['uid'], ['queue-id' => $id]);
if (!empty($receiver)) {
$uid = $receiver['uid'];
} else {
$uid = 0;
}
$trust_source = $entry['trust'];
$data = json_decode($entry['activity'], true);
$activity = json_decode($data['raw'], true);
$ldactivity = JsonLD::compact($activity);
return [
'data' => Receiver::prepareObjectData($ldactivity, $uid, $entry['push'], $trust_source),
'trust' => $trust_source
];
}
/**
* Set the trust for all untrusted entries.
* This is a test function that is used solely for development.
*
* @return void
*/
public static function reprepareAll()
{
$entries = DBA::select('inbox-entry', ['id'], ["NOT `trust` AND `wid` IS NULL"], ['order' => ['id' => true]]);
while ($entry = DBA::fetch($entries)) {
$data = self::reprepareActivityById($entry['id'], false);
if ($data['trust']) {
DBA::update('inbox-entry', ['trust' => true], ['id' => $entry['id']]);
}
}
DBA::close($entries);
2022-07-21 01:16:14 -04:00
}
}