2018-10-03 06:15:07 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2023-01-01 14:36:24 +00:00
|
|
|
* @copyright Copyright (C) 2010-2023, the Friendica project
|
2020-02-09 15:18:46 +00:00
|
|
|
*
|
|
|
|
* @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/>.
|
|
|
|
*
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2020-02-09 15:18:46 +00:00
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
namespace Friendica\Protocol\ActivityPub;
|
|
|
|
|
2022-10-17 10:37:48 +00:00
|
|
|
use Friendica\App;
|
2019-02-09 04:09:02 +00:00
|
|
|
use Friendica\Content\Feature;
|
2019-10-18 01:26:15 +00:00
|
|
|
use Friendica\Content\Text\BBCode;
|
2021-10-23 08:49:27 +00:00
|
|
|
use Friendica\Core\Cache\Enum\Duration;
|
2018-10-29 21:20:46 +00:00
|
|
|
use Friendica\Core\Logger;
|
2018-10-03 06:15:07 +00:00
|
|
|
use Friendica\Core\Protocol;
|
2019-11-02 21:29:16 +00:00
|
|
|
use Friendica\Core\System;
|
2019-10-18 01:26:15 +00:00
|
|
|
use Friendica\Database\DBA;
|
2019-12-15 21:34:11 +00:00
|
|
|
use Friendica\DI;
|
2018-10-03 06:15:07 +00:00
|
|
|
use Friendica\Model\APContact;
|
2019-10-18 01:26:15 +00:00
|
|
|
use Friendica\Model\Contact;
|
2021-01-10 15:08:40 +00:00
|
|
|
use Friendica\Model\GServer;
|
2018-10-03 06:15:07 +00:00
|
|
|
use Friendica\Model\Item;
|
2019-11-11 22:37:50 +00:00
|
|
|
use Friendica\Model\Photo;
|
2020-11-06 04:14:29 +00:00
|
|
|
use Friendica\Model\Post;
|
2020-04-26 15:24:58 +00:00
|
|
|
use Friendica\Model\Tag;
|
2018-10-03 06:15:07 +00:00
|
|
|
use Friendica\Model\User;
|
2021-07-20 17:04:25 +00:00
|
|
|
use Friendica\Network\HTTPException;
|
2019-11-02 21:29:16 +00:00
|
|
|
use Friendica\Protocol\Activity;
|
2019-10-18 01:26:15 +00:00
|
|
|
use Friendica\Protocol\ActivityPub;
|
2020-11-15 23:28:05 +00:00
|
|
|
use Friendica\Protocol\Relay;
|
2018-10-03 06:15:07 +00:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2019-10-18 01:26:15 +00:00
|
|
|
use Friendica\Util\HTTPSignature;
|
2018-10-03 06:15:07 +00:00
|
|
|
use Friendica\Util\LDSignature;
|
2018-10-13 19:49:20 +00:00
|
|
|
use Friendica\Util\Map;
|
2018-11-22 22:23:31 +00:00
|
|
|
use Friendica\Util\Network;
|
2022-02-08 21:05:15 +00:00
|
|
|
use Friendica\Util\Strings;
|
2019-10-18 01:26:15 +00:00
|
|
|
use Friendica\Util\XML;
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
/**
|
2020-01-19 06:05:23 +00:00
|
|
|
* ActivityPub Transmitter Protocol class
|
2018-10-03 09:53:12 +00:00
|
|
|
*
|
|
|
|
* To-Do:
|
2020-02-09 15:18:46 +00:00
|
|
|
* @todo Undo Announce
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
|
|
|
class Transmitter
|
|
|
|
{
|
2022-05-17 08:17:41 +00:00
|
|
|
const CACHEKEY_FEATURED = 'transmitter:getFeatured:';
|
|
|
|
const CACHEKEY_CONTACTS = 'transmitter:getContacts:';
|
|
|
|
|
2020-09-15 17:45:19 +00:00
|
|
|
/**
|
|
|
|
* Add relay servers to the list of inboxes
|
|
|
|
*
|
|
|
|
* @param array $inboxes
|
|
|
|
* @return array inboxes with added relay servers
|
|
|
|
*/
|
2022-06-16 12:59:29 +00:00
|
|
|
public static function addRelayServerInboxes(array $inboxes = []): array
|
2020-09-15 17:45:19 +00:00
|
|
|
{
|
2021-06-13 11:15:04 +00:00
|
|
|
foreach (Relay::getList(['inbox']) as $contact) {
|
2020-12-12 16:45:23 +00:00
|
|
|
$inboxes[$contact['inbox']] = $contact['inbox'];
|
2020-09-15 17:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $inboxes;
|
|
|
|
}
|
|
|
|
|
2020-11-15 23:28:05 +00:00
|
|
|
/**
|
|
|
|
* Add relay servers to the list of inboxes
|
|
|
|
*
|
|
|
|
* @param array $inboxes
|
|
|
|
* @return array inboxes with added relay servers
|
|
|
|
*/
|
2022-06-16 12:59:29 +00:00
|
|
|
public static function addRelayServerInboxesForItem(int $item_id, array $inboxes = []): array
|
2020-11-15 23:28:05 +00:00
|
|
|
{
|
2021-01-16 04:14:58 +00:00
|
|
|
$item = Post::selectFirst(['uid'], ['id' => $item_id]);
|
2020-12-12 16:45:23 +00:00
|
|
|
if (empty($item)) {
|
|
|
|
return $inboxes;
|
|
|
|
}
|
|
|
|
|
2021-06-13 11:15:04 +00:00
|
|
|
$relays = Relay::getDirectRelayList($item_id);
|
2020-11-15 23:28:05 +00:00
|
|
|
if (empty($relays)) {
|
|
|
|
return $inboxes;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($relays as $relay) {
|
2020-12-12 16:45:23 +00:00
|
|
|
$contact = Contact::getByURLForUser($relay['url'], $item['uid'], false, ['id']);
|
|
|
|
$inboxes[$relay['batch']][] = $contact['id'] ?? 0;
|
2020-11-15 23:28:05 +00:00
|
|
|
}
|
|
|
|
return $inboxes;
|
|
|
|
}
|
|
|
|
|
2020-09-15 17:45:19 +00:00
|
|
|
/**
|
2022-06-20 19:21:32 +00:00
|
|
|
* Subscribe to a relay and updates contact on success
|
2020-09-15 17:45:19 +00:00
|
|
|
*
|
|
|
|
* @param string $url Subscribe actor url
|
|
|
|
* @return bool success
|
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
public static function sendRelayFollow(string $url): bool
|
2020-09-15 17:45:19 +00:00
|
|
|
{
|
2020-09-29 05:06:37 +00:00
|
|
|
$contact = Contact::getByURL($url);
|
|
|
|
if (empty($contact)) {
|
2020-09-15 17:45:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-08 20:16:19 +00:00
|
|
|
$activity_id = self::activityIDFromContact($contact['id']);
|
|
|
|
$success = self::sendActivity('Follow', $url, 0, $activity_id);
|
2020-09-15 17:45:19 +00:00
|
|
|
if ($success) {
|
2021-09-10 18:21:19 +00:00
|
|
|
Contact::update(['rel' => Contact::FRIEND], ['id' => $contact['id']]);
|
2020-09-15 17:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-20 19:21:32 +00:00
|
|
|
* Unsubscribe from a relay and updates contact on success or forced
|
2020-09-15 17:45:19 +00:00
|
|
|
*
|
2020-09-29 05:06:37 +00:00
|
|
|
* @param string $url Subscribe actor url
|
|
|
|
* @param bool $force Set the relay status as non follower even if unsubscribe hadn't worked
|
2020-09-15 17:45:19 +00:00
|
|
|
* @return bool success
|
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
public static function sendRelayUndoFollow(string $url, bool $force = false): bool
|
2020-09-15 17:45:19 +00:00
|
|
|
{
|
2020-09-29 05:06:37 +00:00
|
|
|
$contact = Contact::getByURL($url);
|
|
|
|
if (empty($contact)) {
|
2020-09-15 17:45:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-30 06:45:04 +00:00
|
|
|
$success = self::sendContactUndo($url, $contact['id'], User::getSystemAccount());
|
2022-06-20 19:21:32 +00:00
|
|
|
|
2020-09-29 05:06:37 +00:00
|
|
|
if ($success || $force) {
|
2021-09-10 18:21:19 +00:00
|
|
|
Contact::update(['rel' => Contact::NOTHING], ['id' => $contact['id']]);
|
2020-09-15 17:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
2021-07-04 06:30:54 +00:00
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
/**
|
2020-05-06 02:32:45 +00:00
|
|
|
* Collects a list of contacts of the given owner
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
2022-04-21 04:29:08 +00:00
|
|
|
* @param array $owner Owner array
|
|
|
|
* @param array $rel The relevant value(s) contact.rel should match
|
|
|
|
* @param string $module The name of the relevant AP endpoint module (followers|following)
|
|
|
|
* @param integer $page Page number
|
|
|
|
* @param string $requester URL of the requester
|
2022-05-17 04:58:54 +00:00
|
|
|
* @param boolean $nocache Wether to bypass caching
|
2018-10-03 06:15:07 +00:00
|
|
|
* @return array of owners
|
2020-05-06 02:32:45 +00:00
|
|
|
* @throws \Exception
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
public static function getContacts(array $owner, array $rel, string $module, int $page = null, string $requester = null, bool $nocache = false): array
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
2022-05-17 04:58:54 +00:00
|
|
|
if (empty($page)) {
|
2022-05-17 08:17:41 +00:00
|
|
|
$cachekey = self::CACHEKEY_CONTACTS . $module . ':'. $owner['uid'];
|
2022-05-17 04:58:54 +00:00
|
|
|
$result = DI::cache()->get($cachekey);
|
|
|
|
if (!$nocache && !is_null($result)) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 02:33:26 +00:00
|
|
|
$parameters = [
|
|
|
|
'rel' => $rel,
|
|
|
|
'uid' => $owner['uid'],
|
|
|
|
'self' => false,
|
|
|
|
'deleted' => false,
|
|
|
|
'hidden' => false,
|
|
|
|
'archive' => false,
|
2020-11-11 14:18:51 +00:00
|
|
|
'pending' => false,
|
|
|
|
'blocked' => false,
|
2020-05-06 02:33:26 +00:00
|
|
|
];
|
|
|
|
|
2021-10-12 05:53:29 +00:00
|
|
|
$condition = DBA::mergeConditions($parameters, ["`url` IN (SELECT `url` FROM `apcontact`)"]);
|
2020-05-06 02:33:26 +00:00
|
|
|
|
2021-10-12 05:53:29 +00:00
|
|
|
$total = DBA::count('contact', $condition);
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2020-05-06 02:32:45 +00:00
|
|
|
$modulePath = '/' . $module . '/';
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
$data = ['@context' => ActivityPub::CONTEXT];
|
2020-05-06 02:32:45 +00:00
|
|
|
$data['id'] = DI::baseUrl() . $modulePath . $owner['nickname'];
|
2018-10-03 06:15:07 +00:00
|
|
|
$data['type'] = 'OrderedCollection';
|
2021-10-12 05:53:29 +00:00
|
|
|
$data['totalItems'] = $total;
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2022-04-21 04:29:08 +00:00
|
|
|
if (!empty($page)) {
|
|
|
|
$data['id'] .= '?' . http_build_query(['page' => $page]);
|
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
// When we hide our friends we will only show the pure number but don't allow more.
|
2022-02-08 21:05:15 +00:00
|
|
|
$show_contacts = empty($owner['hide-friends']);
|
|
|
|
|
|
|
|
// Allow fetching the contact list when the requester is part of the list.
|
|
|
|
if (($owner['page-flags'] == User::PAGE_FLAGS_PRVGROUP) && !empty($requester)) {
|
2022-02-12 18:38:36 +00:00
|
|
|
$show_contacts = DBA::exists('contact', ['nurl' => Strings::normaliseLink($requester), 'uid' => $owner['uid'], 'blocked' => false]);
|
2022-02-08 21:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$show_contacts) {
|
2022-05-17 04:58:54 +00:00
|
|
|
if (!empty($cachekey)) {
|
2022-05-17 12:32:25 +00:00
|
|
|
DI::cache()->set($cachekey, $data, Duration::DAY);
|
2022-05-17 04:58:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($page)) {
|
2020-05-06 02:32:45 +00:00
|
|
|
$data['first'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=1';
|
2018-10-03 06:15:07 +00:00
|
|
|
} else {
|
2019-08-29 02:12:01 +00:00
|
|
|
$data['type'] = 'OrderedCollectionPage';
|
2018-10-03 06:15:07 +00:00
|
|
|
$list = [];
|
|
|
|
|
2021-10-12 05:53:29 +00:00
|
|
|
$contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
|
2018-10-03 06:15:07 +00:00
|
|
|
while ($contact = DBA::fetch($contacts)) {
|
|
|
|
$list[] = $contact['url'];
|
|
|
|
}
|
2020-04-28 13:33:03 +00:00
|
|
|
DBA::close($contacts);
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2022-04-21 04:29:08 +00:00
|
|
|
if (count($list) == 100) {
|
2020-05-06 02:32:45 +00:00
|
|
|
$data['next'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=' . ($page + 1);
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 02:32:45 +00:00
|
|
|
$data['partOf'] = DI::baseUrl() . $modulePath . $owner['nickname'];
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
$data['orderedItems'] = $list;
|
|
|
|
}
|
|
|
|
|
2022-05-17 04:58:54 +00:00
|
|
|
if (!empty($cachekey)) {
|
2022-05-17 12:32:25 +00:00
|
|
|
DI::cache()->set($cachekey, $data, Duration::DAY);
|
2022-05-17 04:58:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2022-04-08 21:25:31 +00:00
|
|
|
/**
|
|
|
|
* Public posts for the given owner
|
|
|
|
*
|
2022-05-17 04:58:54 +00:00
|
|
|
* @param array $owner Owner array
|
|
|
|
* @param integer $page Page number
|
|
|
|
* @param boolean $nocache Wether to bypass caching
|
2022-04-08 21:25:31 +00:00
|
|
|
*
|
|
|
|
* @return array of posts
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
public static function getFeatured(array $owner, int $page = null, bool $nocache = false): array
|
2022-04-08 21:25:31 +00:00
|
|
|
{
|
2022-05-17 04:58:54 +00:00
|
|
|
if (empty($page)) {
|
2022-05-17 08:17:41 +00:00
|
|
|
$cachekey = self::CACHEKEY_FEATURED . $owner['uid'];
|
2022-05-17 04:58:54 +00:00
|
|
|
$result = DI::cache()->get($cachekey);
|
|
|
|
if (!$nocache && !is_null($result)) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:17:41 +00:00
|
|
|
$owner_cid = Contact::getIdForURL($owner['url'], 0, false);
|
|
|
|
|
2022-04-08 21:25:31 +00:00
|
|
|
$condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)",
|
2022-05-17 04:58:54 +00:00
|
|
|
$owner_cid, Post\Collection::FEATURED];
|
2022-04-08 21:25:31 +00:00
|
|
|
|
2022-06-20 19:21:32 +00:00
|
|
|
$condition = DBA::mergeConditions($condition, [
|
|
|
|
'uid' => $owner['uid'],
|
2022-05-17 04:58:54 +00:00
|
|
|
'author-id' => $owner_cid,
|
2022-04-08 21:25:31 +00:00
|
|
|
'private' => [Item::PUBLIC, Item::UNLISTED],
|
2022-09-12 21:12:11 +00:00
|
|
|
'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT],
|
2022-04-08 21:25:31 +00:00
|
|
|
'network' => Protocol::FEDERATED,
|
|
|
|
'parent-network' => Protocol::FEDERATED,
|
|
|
|
'origin' => true,
|
|
|
|
'deleted' => false,
|
2022-06-20 19:21:32 +00:00
|
|
|
'visible' => true
|
|
|
|
]);
|
2022-04-08 21:25:31 +00:00
|
|
|
|
|
|
|
$count = Post::count($condition);
|
|
|
|
|
|
|
|
$data = ['@context' => ActivityPub::CONTEXT];
|
|
|
|
$data['id'] = DI::baseUrl() . '/featured/' . $owner['nickname'];
|
|
|
|
$data['type'] = 'OrderedCollection';
|
|
|
|
$data['totalItems'] = $count;
|
|
|
|
|
2022-04-21 04:29:08 +00:00
|
|
|
if (!empty($page)) {
|
|
|
|
$data['id'] .= '?' . http_build_query(['page' => $page]);
|
|
|
|
}
|
|
|
|
|
2022-04-08 21:25:31 +00:00
|
|
|
if (empty($page)) {
|
2022-05-13 03:42:04 +00:00
|
|
|
$items = Post::select(['id'], $condition, ['limit' => 20, 'order' => ['created' => true]]);
|
2022-04-08 21:25:31 +00:00
|
|
|
} else {
|
|
|
|
$data['type'] = 'OrderedCollectionPage';
|
|
|
|
$items = Post::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
|
2022-05-13 03:42:04 +00:00
|
|
|
}
|
|
|
|
$list = [];
|
2022-04-08 21:25:31 +00:00
|
|
|
|
2022-05-13 03:42:04 +00:00
|
|
|
while ($item = Post::fetch($items)) {
|
|
|
|
$activity = self::createActivityFromItem($item['id'], true);
|
2023-01-29 14:41:14 +00:00
|
|
|
if (!empty($activity)) {
|
|
|
|
$list[] = $activity;
|
2022-04-08 21:25:31 +00:00
|
|
|
}
|
2022-05-13 03:42:04 +00:00
|
|
|
}
|
|
|
|
DBA::close($items);
|
2022-04-08 21:25:31 +00:00
|
|
|
|
2022-05-13 03:42:04 +00:00
|
|
|
if (count($list) == 20) {
|
|
|
|
$data['next'] = DI::baseUrl() . '/featured/' . $owner['nickname'] . '?page=' . ($page + 1);
|
|
|
|
}
|
2022-04-08 21:25:31 +00:00
|
|
|
|
2022-05-13 03:42:04 +00:00
|
|
|
if (!empty($page)) {
|
|
|
|
$data['partOf'] = DI::baseUrl() . '/featured/' . $owner['nickname'];
|
2022-04-08 21:25:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 03:42:04 +00:00
|
|
|
$data['orderedItems'] = $list;
|
|
|
|
|
2022-05-17 04:58:54 +00:00
|
|
|
if (!empty($cachekey)) {
|
2022-05-17 12:32:25 +00:00
|
|
|
DI::cache()->set($cachekey, $data, Duration::DAY);
|
2022-05-17 04:58:54 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 21:25:31 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2019-04-30 18:07:20 +00:00
|
|
|
/**
|
|
|
|
* Return the service array containing information the used software and it's url
|
|
|
|
*
|
|
|
|
* @return array with service data
|
|
|
|
*/
|
2023-01-29 14:41:14 +00:00
|
|
|
public static function getService(): array
|
2019-04-30 18:07:20 +00:00
|
|
|
{
|
2022-06-20 19:21:32 +00:00
|
|
|
return [
|
|
|
|
'type' => 'Service',
|
2022-10-17 10:37:48 +00:00
|
|
|
'name' => App::PLATFORM . " '" . App::CODENAME . "' " . App::VERSION . '-' . DB_UPDATE_VERSION,
|
2023-05-11 08:13:19 +00:00
|
|
|
'url' => (string)DI::baseUrl()
|
2022-06-20 19:21:32 +00:00
|
|
|
];
|
2019-04-30 18:07:20 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
/**
|
|
|
|
* Return the ActivityPub profile of the given user
|
|
|
|
*
|
2021-07-20 17:04:25 +00:00
|
|
|
* @param int $uid User ID
|
2018-10-04 12:53:41 +00:00
|
|
|
* @return array with profile data
|
2021-07-20 17:04:25 +00:00
|
|
|
* @throws HTTPException\NotFoundException
|
|
|
|
* @throws HTTPException\InternalServerErrorException
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2022-05-17 12:53:31 +00:00
|
|
|
public static function getProfile(int $uid): array
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
2021-07-04 17:25:08 +00:00
|
|
|
$owner = User::getOwnerDataById($uid);
|
2021-07-20 17:04:25 +00:00
|
|
|
if (!isset($owner['id'])) {
|
|
|
|
DI::logger()->error('Unable to find owner data for uid', ['uid' => $uid, 'callstack' => System::callstack(20)]);
|
|
|
|
throw new HTTPException\NotFoundException('User not found.');
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
$data = ['@context' => ActivityPub::CONTEXT];
|
2021-07-04 17:25:08 +00:00
|
|
|
$data['id'] = $owner['url'];
|
2020-08-22 14:48:09 +00:00
|
|
|
|
2021-07-04 17:25:08 +00:00
|
|
|
if (!empty($owner['guid'])) {
|
|
|
|
$data['diaspora:guid'] = $owner['guid'];
|
2020-08-22 14:48:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 17:25:08 +00:00
|
|
|
$data['type'] = ActivityPub::ACCOUNT_TYPES[$owner['account-type']];
|
2021-07-04 06:30:54 +00:00
|
|
|
|
2020-08-22 14:48:09 +00:00
|
|
|
if ($uid != 0) {
|
2021-07-04 17:25:08 +00:00
|
|
|
$data['following'] = DI::baseUrl() . '/following/' . $owner['nick'];
|
|
|
|
$data['followers'] = DI::baseUrl() . '/followers/' . $owner['nick'];
|
2022-04-08 21:25:31 +00:00
|
|
|
$data['inbox'] = DI::baseUrl() . '/inbox/' . $owner['nick'];
|
|
|
|
$data['outbox'] = DI::baseUrl() . '/outbox/' . $owner['nick'];
|
|
|
|
$data['featured'] = DI::baseUrl() . '/featured/' . $owner['nick'];
|
2020-08-22 14:48:09 +00:00
|
|
|
} else {
|
|
|
|
$data['inbox'] = DI::baseUrl() . '/friendica/inbox';
|
|
|
|
}
|
|
|
|
|
2021-07-04 17:25:08 +00:00
|
|
|
$data['preferredUsername'] = $owner['nick'];
|
|
|
|
$data['name'] = $owner['name'];
|
2020-08-22 14:48:09 +00:00
|
|
|
|
2021-07-04 17:25:08 +00:00
|
|
|
if (!empty($owner['country-name'] . $owner['region'] . $owner['locality'])) {
|
|
|
|
$data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $owner['country-name'],
|
|
|
|
'vcard:region' => $owner['region'], 'vcard:locality' => $owner['locality']];
|
2020-08-22 14:48:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 17:25:08 +00:00
|
|
|
if (!empty($owner['about'])) {
|
2021-07-11 09:39:34 +00:00
|
|
|
$data['summary'] = BBCode::convertForUriId($owner['uri-id'] ?? 0, $owner['about'], BBCode::EXTERNAL);
|
2020-08-22 14:48:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 23:49:09 +00:00
|
|
|
if (!empty($owner['xmpp']) || !empty($owner['matrix'])) {
|
|
|
|
$data['vcard:hasInstantMessage'] = [];
|
|
|
|
|
|
|
|
if (!empty($owner['xmpp'])) {
|
|
|
|
$data['vcard:hasInstantMessage'][] = 'xmpp:' . $owner['xmpp'];
|
|
|
|
}
|
|
|
|
if (!empty($owner['matrix'])) {
|
|
|
|
$data['vcard:hasInstantMessage'][] = 'matrix:' . $owner['matrix'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-04 17:25:08 +00:00
|
|
|
$data['url'] = $owner['url'];
|
|
|
|
$data['manuallyApprovesFollowers'] = in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
|
2021-08-09 06:56:41 +00:00
|
|
|
$data['discoverable'] = (bool)$owner['net-publish'];
|
2021-07-04 17:25:08 +00:00
|
|
|
$data['publicKey'] = ['id' => $owner['url'] . '#main-key',
|
|
|
|
'owner' => $owner['url'],
|
|
|
|
'publicKeyPem' => $owner['pubkey']];
|
2019-12-30 22:00:08 +00:00
|
|
|
$data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
|
2023-02-08 20:16:19 +00:00
|
|
|
if ($uid != 0) {
|
|
|
|
$data['icon'] = ['type' => 'Image', 'url' => User::getAvatarUrl($owner)];
|
2021-06-22 13:58:29 +00:00
|
|
|
|
2023-02-08 20:16:19 +00:00
|
|
|
$resourceid = Photo::ridFromURI($owner['photo']);
|
2021-06-22 13:58:29 +00:00
|
|
|
if (!empty($resourceid)) {
|
|
|
|
$photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
|
|
|
|
if (!empty($photo['type'])) {
|
2023-02-08 20:16:19 +00:00
|
|
|
$data['icon']['mediaType'] = $photo['type'];
|
2021-06-22 13:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2023-02-08 20:16:19 +00:00
|
|
|
if (!empty($owner['header'])) {
|
|
|
|
$data['image'] = ['type' => 'Image', 'url' => Contact::getHeaderUrlForId($owner['id'], '', $owner['updated'])];
|
2021-09-14 01:28:58 +00:00
|
|
|
|
2023-02-08 20:16:19 +00:00
|
|
|
$resourceid = Photo::ridFromURI($owner['header']);
|
|
|
|
if (!empty($resourceid)) {
|
|
|
|
$photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
|
|
|
|
if (!empty($photo['type'])) {
|
|
|
|
$data['image']['mediaType'] = $photo['type'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$custom_fields = [];
|
|
|
|
|
|
|
|
foreach (DI::profileField()->selectByContactId(0, $uid) as $profile_field) {
|
|
|
|
$custom_fields[] = [
|
|
|
|
'type' => 'PropertyValue',
|
|
|
|
'name' => $profile_field->label,
|
|
|
|
'value' => BBCode::convertForUriId($owner['uri-id'], $profile_field->value)
|
|
|
|
];
|
|
|
|
};
|
2021-09-14 01:28:58 +00:00
|
|
|
|
2023-02-08 20:16:19 +00:00
|
|
|
if (!empty($custom_fields)) {
|
|
|
|
$data['attachment'] = $custom_fields;
|
|
|
|
}
|
2021-09-14 01:28:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 18:07:20 +00:00
|
|
|
$data['generator'] = self::getService();
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
// tags: https://kitty.town/@inmysocks/100656097926961126.json
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2023-02-08 20:16:19 +00:00
|
|
|
/**
|
2023-03-22 03:16:41 +00:00
|
|
|
* Get a minimal actor array for the C2S API
|
2023-02-08 20:16:19 +00:00
|
|
|
*
|
|
|
|
* @param integer $cid
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function getActorArrayByCid(int $cid): array
|
|
|
|
{
|
|
|
|
$contact = Contact::getById($cid);
|
|
|
|
$data = [
|
|
|
|
'id' => $contact['url'],
|
|
|
|
'type' => $data['type'] = ActivityPub::ACCOUNT_TYPES[$contact['contact-type']],
|
|
|
|
'url' => $contact['alias'],
|
|
|
|
'preferredUsername' => $contact['nick'],
|
|
|
|
'name' => $contact['name'],
|
|
|
|
'icon' => ['type' => 'Image', 'url' => Contact::getAvatarUrlForId($cid, '', $contact['updated'])],
|
|
|
|
'image' => ['type' => 'Image', 'url' => Contact::getHeaderUrlForId($cid, '', $contact['updated'])],
|
|
|
|
'manuallyApprovesFollowers' => (bool)$contact['manually-approve'],
|
|
|
|
'discoverable' => !$contact['unsearchable'],
|
|
|
|
];
|
|
|
|
|
|
|
|
if (empty($data['url'])) {
|
|
|
|
$data['url'] = $data['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2019-04-04 11:38:40 +00:00
|
|
|
/**
|
|
|
|
* @param string $username
|
|
|
|
* @return array
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
public static function getDeletedUser(string $username): array
|
2019-04-04 11:38:40 +00:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'@context' => ActivityPub::CONTEXT,
|
2019-12-30 22:00:08 +00:00
|
|
|
'id' => DI::baseUrl() . '/profile/' . $username,
|
2019-04-04 11:38:40 +00:00
|
|
|
'type' => 'Tombstone',
|
|
|
|
'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
|
|
|
|
'updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
|
|
|
|
'deleted' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
/**
|
2022-02-19 13:31:49 +00:00
|
|
|
* Returns an array with permissions of the thread parent of the given item array
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
|
|
|
* @param array $item
|
|
|
|
*
|
|
|
|
* @return array with permissions
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2023-06-18 16:50:45 +00:00
|
|
|
private static function fetchPermissionBlockFromThreadParent(array $item, bool $is_group_thread): array
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
2022-02-19 13:31:49 +00:00
|
|
|
if (empty($item['thr-parent-id'])) {
|
2018-10-03 06:15:07 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-02-19 13:31:49 +00:00
|
|
|
$parent = Post::selectFirstPost(['author-link'], ['uri-id' => $item['thr-parent-id']]);
|
|
|
|
if (empty($parent)) {
|
2018-10-03 06:15:07 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-12-07 01:16:46 +00:00
|
|
|
$permissions = [
|
2022-02-19 13:31:49 +00:00
|
|
|
'to' => [$parent['author-link']],
|
2021-12-07 01:16:46 +00:00
|
|
|
'cc' => [],
|
|
|
|
'bto' => [],
|
|
|
|
'bcc' => [],
|
2023-06-15 22:04:28 +00:00
|
|
|
'audience' => [],
|
2021-12-07 01:16:46 +00:00
|
|
|
];
|
|
|
|
|
2022-02-19 13:31:49 +00:00
|
|
|
$parent_profile = APContact::getByURL($parent['author-link']);
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
$item_profile = APContact::getByURL($item['author-link']);
|
|
|
|
$exclude[] = $item['author-link'];
|
|
|
|
|
2022-09-12 21:12:11 +00:00
|
|
|
if ($item['gravity'] == Item::GRAVITY_PARENT) {
|
2018-10-03 06:15:07 +00:00
|
|
|
$exclude[] = $item['owner-link'];
|
|
|
|
}
|
|
|
|
|
2023-06-15 22:04:28 +00:00
|
|
|
$type = [Tag::TO => 'to', Tag::CC => 'cc', Tag::BTO => 'bto', Tag::BCC => 'bcc', Tag::AUDIENCE => 'audience'];
|
|
|
|
foreach (Tag::getByURIId($item['thr-parent-id'], [Tag::TO, Tag::CC, Tag::BTO, Tag::BCC, Tag::AUDIENCE]) as $receiver) {
|
2022-02-19 13:31:49 +00:00
|
|
|
if (!empty($parent_profile['followers']) && $receiver['url'] == $parent_profile['followers'] && !empty($item_profile['followers'])) {
|
2023-06-18 16:50:45 +00:00
|
|
|
if (!$is_group_thread) {
|
|
|
|
$permissions[$type[$receiver['type']]][] = $item_profile['followers'];
|
|
|
|
}
|
2022-02-19 13:31:49 +00:00
|
|
|
} elseif (!in_array($receiver['url'], $exclude)) {
|
|
|
|
$permissions[$type[$receiver['type']]][] = $receiver['url'];
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-19 13:31:49 +00:00
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
return $permissions;
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:35:38 +00:00
|
|
|
/**
|
|
|
|
* Check if the given item id is from ActivityPub
|
|
|
|
*
|
|
|
|
* @param integer $item_id
|
|
|
|
* @return boolean "true" if the post is from ActivityPub
|
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
private static function isAPPost(int $item_id): bool
|
2020-12-11 20:20:27 +00:00
|
|
|
{
|
2020-12-11 06:35:38 +00:00
|
|
|
if (empty($item_id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-16 04:14:58 +00:00
|
|
|
return Post::exists(['id' => $item_id, 'network' => Protocol::ACTIVITYPUB]);
|
2020-12-11 06:35:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Creates an array of permissions from an item thread
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
2023-04-28 05:13:23 +00:00
|
|
|
* @param array $item Item array
|
|
|
|
* @param boolean $blindcopy addressing via "bcc" or "cc"?
|
|
|
|
* @param boolean $expand_followers Expand the list of followers
|
|
|
|
* @param integer $last_id Last item id for adding receivers
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
2018-10-04 12:53:41 +00:00
|
|
|
* @return array with permission data
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2023-04-28 05:13:23 +00:00
|
|
|
private static function createPermissionBlockForItem(array $item, bool $blindcopy, bool $expand_followers, int $last_id = 0): array
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
2019-01-17 23:06:27 +00:00
|
|
|
if ($last_id == 0) {
|
|
|
|
$last_id = $item['id'];
|
|
|
|
}
|
|
|
|
|
2019-01-19 16:44:15 +00:00
|
|
|
$always_bcc = false;
|
2023-05-30 13:15:17 +00:00
|
|
|
$is_group = false;
|
2022-02-16 22:56:55 +00:00
|
|
|
$follower = '';
|
2019-01-19 16:44:15 +00:00
|
|
|
|
|
|
|
// Check if we should always deliver our stuff via BCC
|
|
|
|
if (!empty($item['uid'])) {
|
2022-02-16 22:56:55 +00:00
|
|
|
$owner = User::getOwnerDataById($item['uid']);
|
|
|
|
if (!empty($owner)) {
|
|
|
|
$always_bcc = $owner['hide-friends'];
|
2023-05-30 13:15:17 +00:00
|
|
|
$is_group = ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && $owner['manually-approve'];
|
2022-02-16 22:56:55 +00:00
|
|
|
|
|
|
|
$profile = APContact::getByURL($owner['url'], false);
|
|
|
|
$follower = $profile['followers'] ?? '';
|
2019-01-19 16:44:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 20:21:13 +00:00
|
|
|
if (DI::config()->get('system', 'ap_always_bcc')) {
|
2019-12-09 21:37:31 +00:00
|
|
|
$always_bcc = true;
|
|
|
|
}
|
|
|
|
|
2022-03-14 06:07:51 +00:00
|
|
|
$parent = Post::selectFirst(['causer-link', 'post-reason'], ['id' => $item['parent']]);
|
2022-09-03 13:32:41 +00:00
|
|
|
if (!empty($parent) && ($parent['post-reason'] == Item::PR_ANNOUNCEMENT) && !empty($parent['causer-link'])) {
|
2022-03-14 06:07:51 +00:00
|
|
|
$profile = APContact::getByURL($parent['causer-link'], false);
|
2023-05-30 13:15:17 +00:00
|
|
|
$is_group_thread = isset($profile['type']) && $profile['type'] == 'Group';
|
2022-03-14 06:07:51 +00:00
|
|
|
} else {
|
2023-05-30 13:15:17 +00:00
|
|
|
$is_group_thread = false;
|
2022-03-14 06:07:51 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 22:04:28 +00:00
|
|
|
$exclusive = false;
|
|
|
|
$mention = false;
|
2023-06-18 16:50:45 +00:00
|
|
|
$audience = [];
|
2023-06-15 22:04:28 +00:00
|
|
|
|
|
|
|
$parent_tags = Tag::getByURIId($item['parent-uri-id'], [Tag::AUDIENCE, Tag::MENTION]);
|
|
|
|
if (!empty($parent_tags)) {
|
|
|
|
$is_group_thread = false;
|
|
|
|
foreach ($parent_tags as $tag) {
|
|
|
|
if ($tag['type'] != Tag::AUDIENCE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$profile = APContact::getByURL($tag['url'], false);
|
|
|
|
if (!empty($profile) && ($profile['type'] == 'Group')) {
|
2023-06-18 16:50:45 +00:00
|
|
|
$audience[] = $tag['url'];
|
2023-06-15 22:04:28 +00:00
|
|
|
$is_group_thread = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($is_group_thread) {
|
|
|
|
foreach ($parent_tags as $tag) {
|
2023-06-18 16:50:45 +00:00
|
|
|
if (($tag['type'] == Tag::MENTION) && in_array($tag['url'], $audience)) {
|
|
|
|
$mention = true;
|
2023-06-15 22:04:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$exclusive = !$mention;
|
|
|
|
}
|
|
|
|
} elseif ($is_group_thread) {
|
|
|
|
foreach (Tag::getByURIId($item['parent-uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]) as $term) {
|
|
|
|
$profile = APContact::getByURL($term['url'], false);
|
|
|
|
if (!empty($profile) && ($profile['type'] == 'Group')) {
|
|
|
|
if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
|
2023-06-18 16:50:45 +00:00
|
|
|
$audience[] = $term['url'];
|
|
|
|
$exclusive = true;
|
2023-06-15 22:04:28 +00:00
|
|
|
} elseif ($term['type'] == Tag::MENTION) {
|
|
|
|
$mention = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 20:01:32 +00:00
|
|
|
if (self::isAnnounce($item) || self::isAPPost($last_id)) {
|
2019-01-30 19:33:08 +00:00
|
|
|
// Will be activated in a later step
|
2019-07-01 18:00:55 +00:00
|
|
|
$networks = Protocol::FEDERATED;
|
2019-01-30 19:33:08 +00:00
|
|
|
} else {
|
|
|
|
// For now only send to these contacts:
|
|
|
|
$networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
|
|
|
|
}
|
2018-10-20 07:53:45 +00:00
|
|
|
|
2023-06-18 16:50:45 +00:00
|
|
|
$data = ['to' => [], 'cc' => [], 'bcc' => [] , 'audience' => $audience];
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2022-09-12 21:12:11 +00:00
|
|
|
if ($item['gravity'] == Item::GRAVITY_PARENT) {
|
2019-01-30 13:07:23 +00:00
|
|
|
$actor_profile = APContact::getByURL($item['owner-link']);
|
|
|
|
} else {
|
|
|
|
$actor_profile = APContact::getByURL($item['author-link']);
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2023-06-18 16:50:45 +00:00
|
|
|
$terms = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION, Tag::AUDIENCE]);
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2020-03-02 07:57:23 +00:00
|
|
|
if ($item['private'] != Item::PRIVATE) {
|
2019-12-07 21:42:55 +00:00
|
|
|
// Directly mention the original author upon a quoted reshare.
|
|
|
|
// Else just ensure that the original author receives the reshare.
|
|
|
|
$announce = self::getAnnounceArray($item);
|
|
|
|
if (!empty($announce['comment'])) {
|
|
|
|
$data['to'][] = $announce['actor']['url'];
|
|
|
|
} elseif (!empty($announce)) {
|
|
|
|
$data['cc'][] = $announce['actor']['url'];
|
|
|
|
}
|
|
|
|
|
2023-06-18 16:50:45 +00:00
|
|
|
if (!$exclusive) {
|
|
|
|
$data = array_merge($data, self::fetchPermissionBlockFromThreadParent($item, $is_group_thread));
|
2023-06-15 22:04:28 +00:00
|
|
|
}
|
2018-10-27 06:17:17 +00:00
|
|
|
|
2020-03-02 07:57:23 +00:00
|
|
|
// Check if the item is completely public or unlisted
|
|
|
|
if ($item['private'] == Item::PUBLIC) {
|
|
|
|
$data['to'][] = ActivityPub::PUBLIC_COLLECTION;
|
|
|
|
} else {
|
|
|
|
$data['cc'][] = ActivityPub::PUBLIC_COLLECTION;
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
foreach ($terms as $term) {
|
|
|
|
$profile = APContact::getByURL($term['url'], false);
|
2018-10-20 07:53:45 +00:00
|
|
|
if (!empty($profile)) {
|
2023-06-18 16:50:45 +00:00
|
|
|
if (($term['type'] == Tag::AUDIENCE) && ($profile['type'] == 'Group')) {
|
|
|
|
$data['audience'][] = $profile['url'];
|
|
|
|
}
|
2021-06-05 18:38:21 +00:00
|
|
|
if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
|
|
|
|
$exclusive = true;
|
2021-06-06 13:25:42 +00:00
|
|
|
if (!empty($profile['followers']) && ($profile['type'] == 'Group')) {
|
2023-04-28 05:13:23 +00:00
|
|
|
$data['cc'][] = $profile['followers'];
|
|
|
|
$data['audience'][] = $profile['url'];
|
2021-06-06 13:25:42 +00:00
|
|
|
}
|
2022-12-27 16:14:04 +00:00
|
|
|
} elseif (($term['type'] == Tag::MENTION) && ($profile['type'] == 'Group')) {
|
2022-12-27 17:01:40 +00:00
|
|
|
$mention = true;
|
2021-06-05 18:38:21 +00:00
|
|
|
}
|
2018-10-13 18:59:39 +00:00
|
|
|
$data['to'][] = $profile['url'];
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-28 05:13:23 +00:00
|
|
|
if (!$exclusive && ($item['private'] == Item::UNLISTED)) {
|
|
|
|
$data['to'][] = $actor_profile['followers'];
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
} else {
|
2023-04-28 05:13:23 +00:00
|
|
|
$receiver_list = Item::enumeratePermissions($item, true, $expand_followers);
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
foreach ($terms as $term) {
|
|
|
|
$cid = Contact::getIdForURL($term['url'], $item['uid']);
|
|
|
|
if (!empty($cid) && in_array($cid, $receiver_list)) {
|
2021-01-10 15:08:40 +00:00
|
|
|
$contact = DBA::selectFirst('contact', ['url', 'network', 'protocol', 'gsid'], ['id' => $cid, 'network' => Protocol::FEDERATED]);
|
|
|
|
if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) {
|
2019-05-11 05:58:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:04:33 +00:00
|
|
|
$profile = APContact::getByURL($term['url'], false);
|
|
|
|
if (!empty($profile)) {
|
2023-06-18 16:50:45 +00:00
|
|
|
if (($term['type'] == Tag::AUDIENCE) && ($profile['type'] == 'Group')) {
|
|
|
|
$data['audience'][] = $profile['url'];
|
|
|
|
}
|
2022-02-14 22:04:33 +00:00
|
|
|
if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
|
|
|
|
$exclusive = true;
|
|
|
|
if (!empty($profile['followers']) && ($profile['type'] == 'Group')) {
|
2023-04-28 05:13:23 +00:00
|
|
|
$data['cc'][] = $profile['followers'];
|
|
|
|
$data['audience'][] = $profile['url'];
|
2022-02-14 22:04:33 +00:00
|
|
|
}
|
2022-12-27 16:14:04 +00:00
|
|
|
} elseif (($term['type'] == Tag::MENTION) && ($profile['type'] == 'Group')) {
|
2022-12-27 17:01:40 +00:00
|
|
|
$mention = true;
|
2022-02-14 22:04:33 +00:00
|
|
|
}
|
2018-10-20 07:53:45 +00:00
|
|
|
$data['to'][] = $profile['url'];
|
2018-10-14 07:47:04 +00:00
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 17:01:40 +00:00
|
|
|
if ($mention) {
|
|
|
|
$exclusive = false;
|
|
|
|
}
|
|
|
|
|
2023-05-30 13:15:17 +00:00
|
|
|
if ($is_group && !$exclusive && !empty($follower)) {
|
2022-02-16 22:56:55 +00:00
|
|
|
$data['cc'][] = $follower;
|
|
|
|
} elseif (!$exclusive) {
|
2022-02-14 22:04:33 +00:00
|
|
|
foreach ($receiver_list as $receiver) {
|
2023-04-28 05:13:23 +00:00
|
|
|
if ($receiver == -1) {
|
|
|
|
$data['to'][] = $actor_profile['followers'];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:04:33 +00:00
|
|
|
$contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol', 'gsid'], ['id' => $receiver, 'network' => Protocol::FEDERATED]);
|
|
|
|
if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-11 05:58:22 +00:00
|
|
|
|
2022-02-14 22:04:33 +00:00
|
|
|
if (!empty($profile = APContact::getByURL($contact['url'], false))) {
|
|
|
|
if ($contact['hidden'] || $always_bcc) {
|
|
|
|
$data['bcc'][] = $profile['url'];
|
|
|
|
} else {
|
|
|
|
$data['cc'][] = $profile['url'];
|
|
|
|
}
|
2019-01-19 16:44:15 +00:00
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-18 16:50:45 +00:00
|
|
|
if (!empty($item['parent']) && (!$exclusive || ($item['private'] == Item::PRIVATE))) {
|
2023-03-23 22:25:48 +00:00
|
|
|
if ($item['private'] == Item::PRIVATE) {
|
|
|
|
$condition = ['parent' => $item['parent'], 'uri-id' => $item['thr-parent-id']];
|
|
|
|
} else {
|
|
|
|
$condition = ['parent' => $item['parent']];
|
|
|
|
}
|
|
|
|
$parents = Post::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], $condition, ['order' => ['id']]);
|
2021-01-16 04:14:58 +00:00
|
|
|
while ($parent = Post::fetch($parents)) {
|
2022-09-12 21:12:11 +00:00
|
|
|
if ($parent['gravity'] == Item::GRAVITY_PARENT) {
|
2019-05-18 09:09:13 +00:00
|
|
|
$profile = APContact::getByURL($parent['owner-link'], false);
|
|
|
|
if (!empty($profile)) {
|
2022-09-12 21:12:11 +00:00
|
|
|
if ($item['gravity'] != Item::GRAVITY_PARENT) {
|
2023-05-30 13:15:17 +00:00
|
|
|
// Comments to groups are directed to the group
|
|
|
|
// But comments to groups aren't directed to the followers collection
|
|
|
|
// This rule is only valid when the actor isn't the group.
|
|
|
|
// The group needs to transmit their content to their followers.
|
2021-06-17 18:48:23 +00:00
|
|
|
if (($profile['type'] == 'Group') && ($profile['url'] != ($actor_profile['url'] ?? ''))) {
|
2019-05-18 09:09:13 +00:00
|
|
|
$data['to'][] = $profile['url'];
|
|
|
|
} else {
|
|
|
|
$data['cc'][] = $profile['url'];
|
2023-05-30 13:15:17 +00:00
|
|
|
if (($item['private'] != Item::PRIVATE) && !empty($actor_profile['followers']) && (!$exclusive || !$is_group_thread)) {
|
2019-05-18 09:09:13 +00:00
|
|
|
$data['cc'][] = $actor_profile['followers'];
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 13:15:17 +00:00
|
|
|
} elseif (!$exclusive && !$is_group_thread) {
|
2021-06-06 10:07:21 +00:00
|
|
|
// Public thread parent post always are directed to the followers.
|
2022-02-16 22:56:55 +00:00
|
|
|
if ($item['private'] != Item::PRIVATE) {
|
2019-01-30 13:07:23 +00:00
|
|
|
$data['cc'][] = $actor_profile['followers'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 09:09:13 +00:00
|
|
|
// Don't include data from future posts
|
|
|
|
if ($parent['id'] >= $last_id) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2019-05-18 09:09:13 +00:00
|
|
|
$profile = APContact::getByURL($parent['author-link'], false);
|
|
|
|
if (!empty($profile)) {
|
|
|
|
if (($profile['type'] == 'Group') || ($parent['uri'] == $item['thr-parent'])) {
|
|
|
|
$data['to'][] = $profile['url'];
|
|
|
|
} else {
|
|
|
|
$data['cc'][] = $profile['url'];
|
|
|
|
}
|
2018-10-14 07:47:04 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-18 09:09:13 +00:00
|
|
|
DBA::close($parents);
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 05:13:23 +00:00
|
|
|
$data['to'] = array_unique($data['to']);
|
|
|
|
$data['cc'] = array_unique($data['cc']);
|
|
|
|
$data['bcc'] = array_unique($data['bcc']);
|
|
|
|
$data['audience'] = array_unique($data['audience']);
|
2018-10-20 07:53:45 +00:00
|
|
|
|
2018-10-14 07:47:04 +00:00
|
|
|
if (($key = array_search($item['author-link'], $data['to'])) !== false) {
|
|
|
|
unset($data['to'][$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
|
|
|
|
unset($data['cc'][$key]);
|
|
|
|
}
|
|
|
|
|
2018-10-24 20:06:57 +00:00
|
|
|
if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
|
|
|
|
unset($data['bcc'][$key]);
|
|
|
|
}
|
|
|
|
|
2023-04-28 05:13:23 +00:00
|
|
|
if (($key = array_search($item['author-link'], $data['audience'])) !== false) {
|
|
|
|
unset($data['audience'][$key]);
|
|
|
|
}
|
|
|
|
|
2018-10-20 07:53:45 +00:00
|
|
|
foreach ($data['to'] as $to) {
|
|
|
|
if (($key = array_search($to, $data['cc'])) !== false) {
|
|
|
|
unset($data['cc'][$key]);
|
|
|
|
}
|
2018-10-24 20:06:57 +00:00
|
|
|
|
|
|
|
if (($key = array_search($to, $data['bcc'])) !== false) {
|
|
|
|
unset($data['bcc'][$key]);
|
|
|
|
}
|
2018-10-20 07:53:45 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 20:06:57 +00:00
|
|
|
foreach ($data['cc'] as $cc) {
|
|
|
|
if (($key = array_search($cc, $data['bcc'])) !== false) {
|
|
|
|
unset($data['bcc'][$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 22:04:28 +00:00
|
|
|
$receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc']), 'audience' => array_values($data['audience'])];
|
2023-04-28 05:13:23 +00:00
|
|
|
|
2019-11-22 18:39:51 +00:00
|
|
|
if (!$blindcopy) {
|
|
|
|
unset($receivers['bcc']);
|
|
|
|
}
|
|
|
|
|
2023-06-15 22:04:28 +00:00
|
|
|
foreach (['to' => Tag::TO, 'cc' => Tag::CC, 'bcc' => Tag::BCC, 'audience' => Tag::AUDIENCE] as $element => $type) {
|
2022-02-19 20:32:19 +00:00
|
|
|
if (!empty($receivers[$element])) {
|
|
|
|
foreach ($receivers[$element] as $receiver) {
|
|
|
|
if ($receiver == ActivityPub::PUBLIC_COLLECTION) {
|
|
|
|
$name = Receiver::PUBLIC_COLLECTION;
|
|
|
|
} else {
|
|
|
|
$name = trim(parse_url($receiver, PHP_URL_PATH), '/');
|
|
|
|
}
|
|
|
|
Tag::store($item['uri-id'], $type, $name, $receiver);
|
2022-02-19 13:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 22:04:28 +00:00
|
|
|
if (!$blindcopy && count($receivers['audience']) == 1) {
|
|
|
|
$receivers['audience'] = $receivers['audience'][0];
|
|
|
|
} elseif (!$receivers['audience']) {
|
|
|
|
unset($receivers['audience']);
|
|
|
|
}
|
|
|
|
|
2019-11-22 18:39:51 +00:00
|
|
|
return $receivers;
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 21:51:32 +00:00
|
|
|
/**
|
|
|
|
* Check if an inbox is archived
|
|
|
|
*
|
|
|
|
* @param string $url Inbox url
|
|
|
|
* @return boolean "true" if inbox is archived
|
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
public static function archivedInbox(string $url): bool
|
2019-03-25 21:51:32 +00:00
|
|
|
{
|
|
|
|
return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
|
|
|
|
}
|
|
|
|
|
2021-01-10 15:08:40 +00:00
|
|
|
/**
|
|
|
|
* Check if a given contact should be delivered via AP
|
|
|
|
*
|
2022-06-20 19:21:32 +00:00
|
|
|
* @param array $contact Contact array
|
|
|
|
* @param array $networks Array with networks
|
|
|
|
* @return bool Whether the used protocol matches ACTIVITYPUB
|
2021-07-04 06:30:54 +00:00
|
|
|
* @throws Exception
|
2021-01-10 15:08:40 +00:00
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
private static function isAPContact(array $contact, array $networks): bool
|
2021-01-10 15:08:40 +00:00
|
|
|
{
|
|
|
|
if (in_array($contact['network'], $networks) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GServer::getProtocol($contact['gsid'] ?? 0) == Post\DeliveryData::ACTIVITYPUB;
|
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Fetches a list of inboxes of followers of a given user
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param integer $uid User ID
|
2018-10-20 07:53:45 +00:00
|
|
|
* @param boolean $personal fetch personal inboxes
|
2020-12-11 06:35:38 +00:00
|
|
|
* @param boolean $all_ap Retrieve all AP enabled inboxes
|
2018-10-03 06:15:07 +00:00
|
|
|
* @return array of follower inboxes
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
public static function fetchTargetInboxesforUser(int $uid, bool $personal = false, bool $all_ap = false): array
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
|
|
|
$inboxes = [];
|
|
|
|
|
2023-05-30 13:15:17 +00:00
|
|
|
$isGroup = false;
|
2020-08-10 19:44:37 +00:00
|
|
|
if (!empty($item['uid'])) {
|
|
|
|
$profile = User::getOwnerDataById($item['uid']);
|
|
|
|
if (!empty($profile)) {
|
2023-05-30 13:15:17 +00:00
|
|
|
$isGroup = $profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY;
|
2020-08-10 19:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 20:01:32 +00:00
|
|
|
if ($all_ap) {
|
2019-01-30 19:33:08 +00:00
|
|
|
// Will be activated in a later step
|
2019-07-01 18:00:55 +00:00
|
|
|
$networks = Protocol::FEDERATED;
|
2019-01-30 19:33:08 +00:00
|
|
|
} else {
|
|
|
|
// For now only send to these contacts:
|
|
|
|
$networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
|
|
|
|
}
|
2018-10-20 07:53:45 +00:00
|
|
|
|
2022-06-24 01:24:51 +00:00
|
|
|
$condition = [
|
|
|
|
'uid' => $uid,
|
|
|
|
'archive' => false,
|
|
|
|
'pending' => false,
|
|
|
|
'blocked' => false,
|
|
|
|
'network' => Protocol::FEDERATED,
|
|
|
|
];
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
if (!empty($uid)) {
|
|
|
|
$condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
|
|
|
|
}
|
|
|
|
|
2021-01-10 15:08:40 +00:00
|
|
|
$contacts = DBA::select('contact', ['id', 'url', 'network', 'protocol', 'gsid'], $condition);
|
2018-10-03 06:15:07 +00:00
|
|
|
while ($contact = DBA::fetch($contacts)) {
|
2021-01-10 15:08:40 +00:00
|
|
|
if (!self::isAPContact($contact, $networks)) {
|
2019-05-11 05:58:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-05-30 13:15:17 +00:00
|
|
|
if ($isGroup && ($contact['network'] == Protocol::DFRN)) {
|
2020-08-10 19:44:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-22 22:23:31 +00:00
|
|
|
if (Network::isUrlBlocked($contact['url'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-20 07:53:45 +00:00
|
|
|
$profile = APContact::getByURL($contact['url'], false);
|
|
|
|
if (!empty($profile)) {
|
2021-05-26 09:24:37 +00:00
|
|
|
if (empty($profile['sharedinbox']) || $personal || Contact::isLocal($contact['url'])) {
|
2018-10-20 07:53:45 +00:00
|
|
|
$target = $profile['inbox'];
|
|
|
|
} else {
|
|
|
|
$target = $profile['sharedinbox'];
|
|
|
|
}
|
2019-03-25 21:51:32 +00:00
|
|
|
if (!self::archivedInbox($target)) {
|
2020-12-12 16:45:23 +00:00
|
|
|
$inboxes[$target][] = $contact['id'];
|
2019-03-25 21:51:32 +00:00
|
|
|
}
|
2018-10-20 07:53:45 +00:00
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
DBA::close($contacts);
|
|
|
|
|
|
|
|
return $inboxes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Fetches an array of inboxes for the given item and user
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
2022-02-16 22:56:55 +00:00
|
|
|
* @param array $item Item array
|
|
|
|
* @param integer $uid User ID
|
|
|
|
* @param boolean $personal fetch personal inboxes
|
|
|
|
* @param integer $last_id Last item id for adding receivers
|
2018-10-03 06:15:07 +00:00
|
|
|
* @return array with inboxes
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
public static function fetchTargetInboxes(array $item, int $uid, bool $personal = false, int $last_id = 0): array
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
2023-04-28 05:13:23 +00:00
|
|
|
$permissions = self::createPermissionBlockForItem($item, true, true, $last_id);
|
2018-10-03 06:15:07 +00:00
|
|
|
if (empty($permissions)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$inboxes = [];
|
|
|
|
|
2022-09-12 21:12:11 +00:00
|
|
|
if ($item['gravity'] == Item::GRAVITY_ACTIVITY) {
|
2018-10-20 07:53:45 +00:00
|
|
|
$item_profile = APContact::getByURL($item['author-link'], false);
|
2018-10-03 06:15:07 +00:00
|
|
|
} else {
|
2018-10-20 07:53:45 +00:00
|
|
|
$item_profile = APContact::getByURL($item['owner-link'], false);
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 14:48:09 +00:00
|
|
|
if (empty($item_profile)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-08-09 18:42:25 +00:00
|
|
|
$profile_uid = User::getIdForURL($item_profile['url']);
|
|
|
|
|
2023-06-15 22:04:28 +00:00
|
|
|
foreach (['to', 'cc', 'bto', 'bcc', 'audience'] as $element) {
|
2018-10-03 06:15:07 +00:00
|
|
|
if (empty($permissions[$element])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-24 19:19:51 +00:00
|
|
|
$blindcopy = in_array($element, ['bto', 'bcc']);
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
foreach ($permissions[$element] as $receiver) {
|
2020-03-05 08:13:18 +00:00
|
|
|
if (empty($receiver) || Network::isUrlBlocked($receiver)) {
|
2018-11-22 22:31:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-09 18:42:25 +00:00
|
|
|
if ($item_profile && ($receiver == $item_profile['followers']) && ($uid == $profile_uid)) {
|
2020-12-11 06:35:38 +00:00
|
|
|
$inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal, self::isAPPost($last_id)));
|
2018-10-03 06:15:07 +00:00
|
|
|
} else {
|
2018-10-20 07:53:45 +00:00
|
|
|
$profile = APContact::getByURL($receiver, false);
|
2018-10-03 06:15:07 +00:00
|
|
|
if (!empty($profile)) {
|
2020-12-12 16:45:23 +00:00
|
|
|
$contact = Contact::getByURLForUser($receiver, $uid, false, ['id']);
|
|
|
|
|
2021-05-26 09:24:37 +00:00
|
|
|
if (empty($profile['sharedinbox']) || $personal || $blindcopy || Contact::isLocal($receiver)) {
|
2018-10-20 07:53:45 +00:00
|
|
|
$target = $profile['inbox'];
|
|
|
|
} else {
|
|
|
|
$target = $profile['sharedinbox'];
|
|
|
|
}
|
2023-06-15 22:04:28 +00:00
|
|
|
if (!self::archivedInbox($target) && !in_array($contact['id'], $inboxes[$target] ?? [])) {
|
2020-12-12 16:45:23 +00:00
|
|
|
$inboxes[$target][] = $contact['id'] ?? 0;
|
2019-03-25 21:51:32 +00:00
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $inboxes;
|
|
|
|
}
|
|
|
|
|
2019-05-14 17:50:45 +00:00
|
|
|
/**
|
|
|
|
* Creates an array in the structure of the item table for a given mail id
|
|
|
|
*
|
2022-06-20 19:21:32 +00:00
|
|
|
* @param integer $mail_id Mail id
|
2019-05-14 17:50:45 +00:00
|
|
|
* @return array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
public static function getItemArrayFromMail(int $mail_id, bool $use_title = false): array
|
2019-05-14 17:50:45 +00:00
|
|
|
{
|
|
|
|
$mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
|
2019-11-19 10:02:35 +00:00
|
|
|
if (!DBA::isResult($mail)) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-05-14 17:50:45 +00:00
|
|
|
|
2021-05-22 13:37:04 +00:00
|
|
|
$reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'from-url'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
|
2021-08-30 12:29:09 +00:00
|
|
|
if (!DBA::isResult($reply)) {
|
|
|
|
$reply = $mail;
|
|
|
|
}
|
2019-05-14 17:50:45 +00:00
|
|
|
|
2019-05-17 17:58:42 +00:00
|
|
|
// Making the post more compatible for Mastodon by:
|
|
|
|
// - Making it a note and not an article (no title)
|
|
|
|
// - Moving the title into the "summary" field that is used as a "content warning"
|
2021-05-20 04:39:45 +00:00
|
|
|
|
2021-05-22 13:37:04 +00:00
|
|
|
if (!$use_title) {
|
2021-05-20 04:39:45 +00:00
|
|
|
$mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
|
|
|
|
$mail['title'] = '';
|
|
|
|
}
|
|
|
|
|
2022-03-08 18:32:09 +00:00
|
|
|
$mail['content-warning'] = '';
|
2021-05-20 04:39:45 +00:00
|
|
|
$mail['author-link'] = $mail['owner-link'] = $mail['from-url'];
|
2021-05-22 13:37:04 +00:00
|
|
|
$mail['owner-id'] = $mail['author-id'];
|
2021-05-20 04:39:45 +00:00
|
|
|
$mail['allow_cid'] = '<'.$mail['contact-id'].'>';
|
|
|
|
$mail['allow_gid'] = '';
|
|
|
|
$mail['deny_cid'] = '';
|
|
|
|
$mail['deny_gid'] = '';
|
|
|
|
$mail['private'] = Item::PRIVATE;
|
|
|
|
$mail['deleted'] = false;
|
|
|
|
$mail['edited'] = $mail['created'];
|
2021-05-22 13:37:04 +00:00
|
|
|
$mail['plink'] = DI::baseUrl() . '/message/' . $mail['id'];
|
|
|
|
$mail['parent-uri'] = $reply['uri'];
|
|
|
|
$mail['parent-uri-id'] = $reply['uri-id'];
|
2021-05-20 04:39:45 +00:00
|
|
|
$mail['parent-author-id'] = Contact::getIdForURL($reply['from-url'], 0, false);
|
2022-09-12 21:12:11 +00:00
|
|
|
$mail['gravity'] = ($mail['reply'] ? Item::GRAVITY_COMMENT: Item::GRAVITY_PARENT);
|
2021-05-20 04:39:45 +00:00
|
|
|
$mail['event-type'] = '';
|
|
|
|
$mail['language'] = '';
|
|
|
|
$mail['parent'] = 0;
|
2019-05-14 17:50:45 +00:00
|
|
|
|
|
|
|
return $mail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an activity array for a given mail id
|
|
|
|
*
|
|
|
|
* @param integer $mail_id
|
|
|
|
* @param boolean $object_mode Is the activity item is used inside another object?
|
|
|
|
*
|
|
|
|
* @return array of activity
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
public static function createActivityFromMail(int $mail_id, bool $object_mode = false): array
|
2019-05-14 17:50:45 +00:00
|
|
|
{
|
2022-06-20 19:21:32 +00:00
|
|
|
$mail = self::getItemArrayFromMail($mail_id);
|
2020-09-18 12:08:40 +00:00
|
|
|
if (empty($mail)) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-05-14 17:50:45 +00:00
|
|
|
$object = self::createNote($mail);
|
|
|
|
|
|
|
|
if (!$object_mode) {
|
|
|
|
$data = ['@context' => ActivityPub::CONTEXT];
|
|
|
|
} else {
|
|
|
|
$data = [];
|
|
|
|
}
|
|
|
|
|
2020-05-27 19:05:33 +00:00
|
|
|
$data['id'] = $mail['uri'] . '/Create';
|
2019-05-14 17:50:45 +00:00
|
|
|
$data['type'] = 'Create';
|
|
|
|
$data['actor'] = $mail['author-link'];
|
|
|
|
$data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
|
|
|
|
$data['instrument'] = self::getService();
|
2023-04-28 05:13:23 +00:00
|
|
|
$data = array_merge($data, self::createPermissionBlockForItem($mail, true, false));
|
2019-05-14 17:50:45 +00:00
|
|
|
|
|
|
|
if (empty($data['to']) && !empty($data['cc'])) {
|
|
|
|
$data['to'] = $data['cc'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($data['to']) && !empty($data['bcc'])) {
|
|
|
|
$data['to'] = $data['bcc'];
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($data['cc']);
|
|
|
|
unset($data['bcc']);
|
2023-06-15 22:04:28 +00:00
|
|
|
unset($data['audience']);
|
2019-05-14 17:50:45 +00:00
|
|
|
|
|
|
|
$object['to'] = $data['to'];
|
2019-11-22 19:47:35 +00:00
|
|
|
$object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => '']];
|
2019-11-22 18:39:51 +00:00
|
|
|
|
2019-05-14 17:50:45 +00:00
|
|
|
unset($object['cc']);
|
|
|
|
unset($object['bcc']);
|
2023-06-15 22:04:28 +00:00
|
|
|
unset($object['audience']);
|
2019-05-14 17:50:45 +00:00
|
|
|
|
|
|
|
$data['directMessage'] = true;
|
|
|
|
|
|
|
|
$data['object'] = $object;
|
|
|
|
|
|
|
|
$owner = User::getOwnerDataById($mail['uid']);
|
|
|
|
|
|
|
|
if (!$object_mode && !empty($owner)) {
|
|
|
|
return LDSignature::sign($data, $owner);
|
|
|
|
} else {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Returns the activity type of a given item
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
2022-06-20 19:21:32 +00:00
|
|
|
* @param array $item Item array
|
2018-10-04 12:53:41 +00:00
|
|
|
* @return string with activity type
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
private static function getTypeOfItem(array $item): string
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
2019-01-20 21:52:43 +00:00
|
|
|
$reshared = false;
|
|
|
|
|
|
|
|
// Only check for a reshare, if it is a real reshare and no quoted reshare
|
2022-06-20 19:21:32 +00:00
|
|
|
if (strpos($item['body'], '[share') === 0) {
|
2019-12-04 09:15:03 +00:00
|
|
|
$announce = self::getAnnounceArray($item);
|
2019-12-04 07:02:39 +00:00
|
|
|
$reshared = !empty($announce);
|
2019-01-20 21:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($reshared) {
|
2018-10-13 16:41:29 +00:00
|
|
|
$type = 'Announce';
|
2019-10-23 22:25:43 +00:00
|
|
|
} elseif ($item['verb'] == Activity::POST) {
|
2018-10-03 06:15:07 +00:00
|
|
|
if ($item['created'] == $item['edited']) {
|
|
|
|
$type = 'Create';
|
|
|
|
} else {
|
|
|
|
$type = 'Update';
|
|
|
|
}
|
2019-10-23 22:25:43 +00:00
|
|
|
} elseif ($item['verb'] == Activity::LIKE) {
|
2018-10-03 06:15:07 +00:00
|
|
|
$type = 'Like';
|
2019-10-23 22:25:43 +00:00
|
|
|
} elseif ($item['verb'] == Activity::DISLIKE) {
|
2018-10-03 06:15:07 +00:00
|
|
|
$type = 'Dislike';
|
2019-10-23 22:25:43 +00:00
|
|
|
} elseif ($item['verb'] == Activity::ATTEND) {
|
2018-10-03 06:15:07 +00:00
|
|
|
$type = 'Accept';
|
2019-10-23 22:25:43 +00:00
|
|
|
} elseif ($item['verb'] == Activity::ATTENDNO) {
|
2018-10-03 06:15:07 +00:00
|
|
|
$type = 'Reject';
|
2019-10-23 22:25:43 +00:00
|
|
|
} elseif ($item['verb'] == Activity::ATTENDMAYBE) {
|
2018-10-03 06:15:07 +00:00
|
|
|
$type = 'TentativeAccept';
|
2019-10-23 22:25:43 +00:00
|
|
|
} elseif ($item['verb'] == Activity::FOLLOW) {
|
2019-01-30 16:30:01 +00:00
|
|
|
$type = 'Follow';
|
2019-10-23 22:25:43 +00:00
|
|
|
} elseif ($item['verb'] == Activity::TAG) {
|
2019-05-25 19:33:58 +00:00
|
|
|
$type = 'Add';
|
2020-08-09 18:42:25 +00:00
|
|
|
} elseif ($item['verb'] == Activity::ANNOUNCE) {
|
|
|
|
$type = 'Announce';
|
2018-10-03 06:15:07 +00:00
|
|
|
} else {
|
|
|
|
$type = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:00:40 +00:00
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Creates the activity or fetches it from the cache
|
2018-10-05 21:00:40 +00:00
|
|
|
*
|
2022-06-20 19:21:32 +00:00
|
|
|
* @param integer $item_id Item id
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param boolean $force Force new cache entry
|
2022-07-03 02:16:46 +00:00
|
|
|
* @return array|false activity or false on failure
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-10-05 21:00:40 +00:00
|
|
|
*/
|
2022-09-07 19:46:24 +00:00
|
|
|
public static function createCachedActivityFromItem(int $item_id, bool $force = false, bool $object_mode = false)
|
2018-10-05 21:00:40 +00:00
|
|
|
{
|
2022-09-07 19:46:24 +00:00
|
|
|
$cachekey = 'APDelivery:createActivity:' . $item_id . ':' . (int)$object_mode;
|
2018-10-27 11:09:23 +00:00
|
|
|
|
|
|
|
if (!$force) {
|
2020-01-06 23:45:49 +00:00
|
|
|
$data = DI::cache()->get($cachekey);
|
2018-10-27 11:09:23 +00:00
|
|
|
if (!is_null($data)) {
|
|
|
|
return $data;
|
|
|
|
}
|
2018-10-05 21:00:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 19:46:24 +00:00
|
|
|
$data = self::createActivityFromItem($item_id, $object_mode);
|
2018-10-05 21:00:40 +00:00
|
|
|
|
2020-01-18 14:41:19 +00:00
|
|
|
DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
|
2018-10-05 21:00:40 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Creates an activity array for a given item id
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
|
|
|
* @param integer $item_id
|
|
|
|
* @param boolean $object_mode Is the activity item is used inside another object?
|
2023-01-29 14:41:14 +00:00
|
|
|
* @param boolean $api_mode "true" if used for the API
|
2020-12-08 15:08:49 +00:00
|
|
|
* @return false|array
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2023-01-29 14:41:14 +00:00
|
|
|
public static function createActivityFromItem(int $item_id, bool $object_mode = false, $api_mode = false)
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
2023-01-29 14:41:14 +00:00
|
|
|
$condition = ['id' => $item_id];
|
|
|
|
if (!$api_mode) {
|
|
|
|
$condition['parent-network'] = Protocol::NATIVE_SUPPORT;
|
|
|
|
}
|
|
|
|
Logger::info('Fetching activity', $condition);
|
|
|
|
$item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition);
|
2018-10-03 06:15:07 +00:00
|
|
|
if (!DBA::isResult($item)) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-01-29 14:41:14 +00:00
|
|
|
return self::createActivityFromArray($item, $object_mode, $api_mode);
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2023-01-29 14:41:14 +00:00
|
|
|
/**
|
|
|
|
* Creates an activity array for a given URI-Id and uid
|
|
|
|
*
|
|
|
|
* @param integer $uri_id
|
|
|
|
* @param integer $uid
|
|
|
|
* @param boolean $object_mode Is the activity item is used inside another object?
|
|
|
|
* @param boolean $api_mode "true" if used for the API
|
|
|
|
* @return false|array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function createActivityFromUriId(int $uri_id, int $uid, bool $object_mode = false, $api_mode = false)
|
|
|
|
{
|
|
|
|
$condition = ['uri-id' => $uri_id, 'uid' => [0, $uid]];
|
|
|
|
if (!$api_mode) {
|
|
|
|
$condition['parent-network'] = Protocol::NATIVE_SUPPORT;
|
|
|
|
}
|
|
|
|
Logger::info('Fetching activity', $condition);
|
|
|
|
$item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition, ['order' => ['uid' => true]]);
|
|
|
|
if (!DBA::isResult($item)) {
|
2020-11-12 05:17:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-29 14:41:14 +00:00
|
|
|
return self::createActivityFromArray($item, $object_mode, $api_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an activity array for a given item id
|
|
|
|
*
|
|
|
|
* @param integer $item_id
|
|
|
|
* @param boolean $object_mode Is the activity item is used inside another object?
|
|
|
|
* @param boolean $api_mode "true" if used for the API
|
|
|
|
* @return false|array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
private static function createActivityFromArray(array $item, bool $object_mode = false, $api_mode = false)
|
|
|
|
{
|
2023-02-08 20:16:19 +00:00
|
|
|
if (!$api_mode && !$item['deleted'] && $item['network'] == Protocol::ACTIVITYPUB) {
|
2022-07-27 17:39:00 +00:00
|
|
|
$data = Post\Activity::getByURIId($item['uri-id']);
|
|
|
|
if (!$item['origin'] && !empty($data)) {
|
2023-01-29 14:41:14 +00:00
|
|
|
if (!$object_mode) {
|
|
|
|
Logger::info('Return stored conversation', ['item' => $item['id']]);
|
|
|
|
return $data;
|
|
|
|
} elseif (!empty($data['object'])) {
|
|
|
|
Logger::info('Return stored conversation object', ['item' => $item['id']]);
|
|
|
|
return $data['object'];
|
2019-01-28 06:23:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2023-01-29 14:41:14 +00:00
|
|
|
if (!$api_mode && !$item['origin']) {
|
|
|
|
Logger::debug('Post is not ours and is not stored', ['id' => $item['id'], 'uri-id' => $item['uri-id']]);
|
2022-07-29 14:17:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-11 20:20:27 +00:00
|
|
|
$type = self::getTypeOfItem($item);
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
if (!$object_mode) {
|
2020-07-11 07:15:54 +00:00
|
|
|
$data = ['@context' => $context ?? ActivityPub::CONTEXT];
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2022-09-12 21:12:11 +00:00
|
|
|
if ($item['deleted'] && ($item['gravity'] == Item::GRAVITY_ACTIVITY)) {
|
2018-10-03 06:15:07 +00:00
|
|
|
$type = 'Undo';
|
|
|
|
} elseif ($item['deleted']) {
|
|
|
|
$type = 'Delete';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data = [];
|
|
|
|
}
|
|
|
|
|
2021-03-24 22:22:14 +00:00
|
|
|
if ($type == 'Delete') {
|
2022-07-09 11:32:32 +00:00
|
|
|
$data['id'] = Item::newURI($item['guid']) . '/' . $type;;
|
2022-09-12 21:12:11 +00:00
|
|
|
} elseif (($item['gravity'] == Item::GRAVITY_ACTIVITY) && ($type != 'Undo')) {
|
2020-09-01 02:38:44 +00:00
|
|
|
$data['id'] = $item['uri'];
|
|
|
|
} else {
|
|
|
|
$data['id'] = $item['uri'] . '/' . $type;
|
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
$data['type'] = $type;
|
2019-03-14 18:44:41 +00:00
|
|
|
|
2022-09-12 21:12:11 +00:00
|
|
|
if (($type != 'Announce') || ($item['gravity'] != Item::GRAVITY_PARENT)) {
|
2023-02-08 20:16:19 +00:00
|
|
|
$link = $item['author-link'];
|
|
|
|
$id = $item['author-id'];
|
2019-03-14 18:44:41 +00:00
|
|
|
} else {
|
2023-02-08 20:16:19 +00:00
|
|
|
$link = $item['owner-link'];
|
|
|
|
$id = $item['owner-id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($api_mode) {
|
|
|
|
$data['actor'] = self::getActorArrayByCid($id);
|
|
|
|
} else {
|
|
|
|
$data['actor'] = $link;
|
2019-03-14 18:44:41 +00:00
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2018-10-05 04:04:37 +00:00
|
|
|
$data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2019-04-30 18:07:20 +00:00
|
|
|
$data['instrument'] = self::getService();
|
2018-10-13 20:36:44 +00:00
|
|
|
|
2023-04-28 05:13:23 +00:00
|
|
|
$data = array_merge($data, self::createPermissionBlockForItem($item, false, false));
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2018-10-13 16:41:29 +00:00
|
|
|
if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
|
2023-02-08 20:16:19 +00:00
|
|
|
$data['object'] = self::createNote($item, $api_mode);
|
2019-05-25 19:33:58 +00:00
|
|
|
} elseif ($data['type'] == 'Add') {
|
|
|
|
$data = self::createAddTag($item, $data);
|
2018-10-13 16:41:29 +00:00
|
|
|
} elseif ($data['type'] == 'Announce') {
|
2020-08-09 18:42:25 +00:00
|
|
|
if ($item['verb'] == ACTIVITY::ANNOUNCE) {
|
|
|
|
$data['object'] = $item['thr-parent'];
|
|
|
|
} else {
|
2023-02-08 20:16:19 +00:00
|
|
|
$data = self::createAnnounce($item, $data, $api_mode);
|
2020-08-09 18:42:25 +00:00
|
|
|
}
|
2019-01-30 16:30:01 +00:00
|
|
|
} elseif ($data['type'] == 'Follow') {
|
|
|
|
$data['object'] = $item['parent-uri'];
|
2018-10-03 06:15:07 +00:00
|
|
|
} elseif ($data['type'] == 'Undo') {
|
2023-01-29 14:41:14 +00:00
|
|
|
$data['object'] = self::createActivityFromItem($item['id'], true);
|
2018-10-03 06:15:07 +00:00
|
|
|
} else {
|
2018-10-06 14:02:23 +00:00
|
|
|
$data['diaspora:guid'] = $item['guid'];
|
2018-10-27 14:35:22 +00:00
|
|
|
if (!empty($item['signed_text'])) {
|
|
|
|
$data['diaspora:like'] = $item['signed_text'];
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
$data['object'] = $item['thr-parent'];
|
|
|
|
}
|
|
|
|
|
2019-01-17 23:06:27 +00:00
|
|
|
if (!empty($item['contact-uid'])) {
|
|
|
|
$uid = $item['contact-uid'];
|
|
|
|
} else {
|
|
|
|
$uid = $item['uid'];
|
|
|
|
}
|
|
|
|
|
2023-01-29 14:41:14 +00:00
|
|
|
Logger::info('Fetched activity', ['item' => $item['id'], 'uid' => $uid]);
|
2020-08-09 18:42:25 +00:00
|
|
|
|
2023-01-29 14:41:14 +00:00
|
|
|
// We only sign our own activities
|
|
|
|
if (!$api_mode && !$object_mode && $item['origin']) {
|
|
|
|
$owner = User::getOwnerDataById($uid);
|
2018-10-03 06:15:07 +00:00
|
|
|
return LDSignature::sign($data, $owner);
|
|
|
|
} else {
|
|
|
|
return $data;
|
|
|
|
}
|
2018-10-04 09:46:29 +00:00
|
|
|
|
|
|
|
/// @todo Create "conversation" entry
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 19:49:20 +00:00
|
|
|
/**
|
|
|
|
* Creates a location entry for a given item array
|
|
|
|
*
|
2022-06-20 19:21:32 +00:00
|
|
|
* @param array $item Item array
|
2018-10-13 19:49:20 +00:00
|
|
|
* @return array with location array
|
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
private static function createLocation(array $item): array
|
2018-10-13 19:49:20 +00:00
|
|
|
{
|
|
|
|
$location = ['type' => 'Place'];
|
|
|
|
|
|
|
|
if (!empty($item['location'])) {
|
|
|
|
$location['name'] = $item['location'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$coord = [];
|
|
|
|
|
|
|
|
if (empty($item['coord'])) {
|
|
|
|
$coord = Map::getCoordinates($item['location']);
|
|
|
|
} else {
|
|
|
|
$coords = explode(' ', $item['coord']);
|
|
|
|
if (count($coords) == 2) {
|
|
|
|
$coord = ['lat' => $coords[0], 'lon' => $coords[1]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($coord['lat']) && !empty($coord['lon'])) {
|
|
|
|
$location['latitude'] = $coord['lat'];
|
|
|
|
$location['longitude'] = $coord['lon'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $location;
|
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Returns a tag array for a given item array
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
2022-09-29 22:29:15 +00:00
|
|
|
* @param array $item Item array
|
|
|
|
* @param string $quote_url Url of the attached quote link
|
2018-10-03 06:15:07 +00:00
|
|
|
* @return array of tags
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2022-09-29 22:29:15 +00:00
|
|
|
private static function createTagList(array $item, string $quote_url): array
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
|
|
|
$tags = [];
|
|
|
|
|
2020-05-01 06:01:22 +00:00
|
|
|
$terms = Tag::getByURIId($item['uri-id'], [Tag::HASHTAG, Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
|
2018-10-03 06:15:07 +00:00
|
|
|
foreach ($terms as $term) {
|
2020-04-26 15:24:58 +00:00
|
|
|
if ($term['type'] == Tag::HASHTAG) {
|
2020-05-01 15:27:56 +00:00
|
|
|
$url = DI::baseUrl() . '/search?tag=' . urlencode($term['name']);
|
|
|
|
$tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['name']];
|
2020-04-26 15:24:58 +00:00
|
|
|
} else {
|
2020-07-15 17:06:48 +00:00
|
|
|
$contact = Contact::getByURL($term['url'], false, ['addr']);
|
2020-08-09 18:42:25 +00:00
|
|
|
if (empty($contact)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-03 18:27:01 +00:00
|
|
|
if (!empty($contact['addr'])) {
|
|
|
|
$mention = '@' . $contact['addr'];
|
|
|
|
} else {
|
|
|
|
$mention = '@' . $term['url'];
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2018-10-03 18:27:01 +00:00
|
|
|
$tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
2019-12-04 07:02:39 +00:00
|
|
|
|
2019-12-04 09:15:03 +00:00
|
|
|
$announce = self::getAnnounceArray($item);
|
2019-12-04 07:02:39 +00:00
|
|
|
// Mention the original author upon commented reshares
|
|
|
|
if (!empty($announce['comment'])) {
|
|
|
|
$tags[] = ['type' => 'Mention', 'href' => $announce['actor']['url'], 'name' => '@' . $announce['actor']['addr']];
|
|
|
|
}
|
|
|
|
|
2022-09-29 22:29:15 +00:00
|
|
|
// @see https://codeberg.org/fediverse/fep/src/branch/main/feps/fep-e232.md
|
|
|
|
if (!empty($quote_url)) {
|
2022-09-30 07:17:07 +00:00
|
|
|
// Currently deactivated because of compatibility issues with Pleroma
|
|
|
|
//$tags[] = [
|
|
|
|
// 'type' => 'Link',
|
|
|
|
// 'mediaType' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
|
|
|
|
// 'href' => $quote_url,
|
2022-10-01 22:36:02 +00:00
|
|
|
// 'name' => '♲ ' . BBCode::convertForUriId($item['uri-id'], $quote_url, BBCode::ACTIVITYPUB)
|
2022-09-30 07:17:07 +00:00
|
|
|
//];
|
2022-09-29 22:29:15 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
2018-10-03 17:36:55 +00:00
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Adds attachment data to the JSON document
|
2018-10-03 17:36:55 +00:00
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param array $item Data of the item that is to be posted
|
2018-10-05 04:04:37 +00:00
|
|
|
*
|
2018-10-04 12:53:41 +00:00
|
|
|
* @return array with attachment data
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-10-03 17:36:55 +00:00
|
|
|
*/
|
2022-09-30 07:10:43 +00:00
|
|
|
private static function createAttachmentList(array $item): array
|
2018-10-03 17:36:55 +00:00
|
|
|
{
|
|
|
|
$attachments = [];
|
|
|
|
|
2021-05-05 16:46:55 +00:00
|
|
|
$urls = [];
|
2022-09-30 07:10:43 +00:00
|
|
|
foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO, Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) {
|
|
|
|
if (in_array($attachment['url'], $urls)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$urls[] = $attachment['url'];
|
2021-07-04 17:25:08 +00:00
|
|
|
|
2022-09-30 07:10:43 +00:00
|
|
|
$attach = ['type' => 'Document',
|
|
|
|
'mediaType' => $attachment['mimetype'],
|
|
|
|
'url' => $attachment['url'],
|
|
|
|
'name' => $attachment['description']];
|
2021-07-04 06:30:54 +00:00
|
|
|
|
2022-09-30 07:10:43 +00:00
|
|
|
if (!empty($attachment['height'])) {
|
|
|
|
$attach['height'] = $attachment['height'];
|
|
|
|
}
|
2021-07-04 17:25:08 +00:00
|
|
|
|
2022-09-30 07:10:43 +00:00
|
|
|
if (!empty($attachment['width'])) {
|
|
|
|
$attach['width'] = $attachment['width'];
|
|
|
|
}
|
2021-07-04 06:30:54 +00:00
|
|
|
|
2022-09-30 07:10:43 +00:00
|
|
|
if (!empty($attachment['preview'])) {
|
|
|
|
$attach['image'] = $attachment['preview'];
|
2021-04-29 20:22:38 +00:00
|
|
|
}
|
2022-09-30 07:10:43 +00:00
|
|
|
|
|
|
|
$attachments[] = $attach;
|
2018-10-03 17:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $attachments;
|
|
|
|
}
|
|
|
|
|
2021-04-10 21:13:37 +00:00
|
|
|
/**
|
|
|
|
* Callback function to replace a Friendica style mention in a mention for a summary
|
|
|
|
*
|
|
|
|
* @param array $match Matching values for the callback
|
|
|
|
* @return string Replaced mention
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
private static function mentionAddrCallback(array $match): string
|
2021-04-10 21:13:37 +00:00
|
|
|
{
|
|
|
|
if (empty($match[1])) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2021-04-10 21:33:18 +00:00
|
|
|
$data = Contact::getByURL($match[1], false, ['addr']);
|
2021-04-10 21:13:37 +00:00
|
|
|
if (empty($data['addr'])) {
|
|
|
|
return $match[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '@' . $data['addr'];
|
|
|
|
}
|
|
|
|
|
2018-10-05 04:04:37 +00:00
|
|
|
/**
|
2019-11-11 22:37:50 +00:00
|
|
|
* Remove image elements since they are added as attachment
|
2018-10-05 04:04:37 +00:00
|
|
|
*
|
2022-06-20 19:21:32 +00:00
|
|
|
* @param string $body HTML code
|
2019-11-11 22:37:50 +00:00
|
|
|
* @return string with removed images
|
2018-10-05 04:04:37 +00:00
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
private static function removePictures(string $body): string
|
2018-10-05 04:04:37 +00:00
|
|
|
{
|
2022-12-19 04:32:52 +00:00
|
|
|
return BBCode::performWithEscapedTags($body, ['code', 'noparse', 'nobb', 'pre'], function ($text) {
|
|
|
|
// Simplify image codes
|
|
|
|
$text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $text);
|
|
|
|
$text = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $text);
|
|
|
|
|
|
|
|
// Now remove local links
|
|
|
|
$text = preg_replace_callback(
|
|
|
|
'/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi',
|
|
|
|
function ($match) {
|
|
|
|
// We remove the link when it is a link to a local photo page
|
|
|
|
if (Photo::isLocalPage($match[1])) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
// otherwise we just return the link
|
|
|
|
return '[url]' . $match[1] . '[/url]';
|
|
|
|
},
|
|
|
|
$text
|
|
|
|
);
|
|
|
|
|
|
|
|
// Remove all pictures
|
|
|
|
return preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '', $text);
|
|
|
|
});
|
2018-10-05 04:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Returns if the post contains sensitive content ("nsfw")
|
2018-10-05 04:04:37 +00:00
|
|
|
*
|
2022-06-20 19:21:32 +00:00
|
|
|
* @param integer $uri_id URI id
|
|
|
|
* @return boolean Whether URI id was found
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-10-05 04:04:37 +00:00
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
private static function isSensitive(int $uri_id): bool
|
2018-10-03 18:27:01 +00:00
|
|
|
{
|
2022-03-11 20:39:03 +00:00
|
|
|
return DBA::exists('tag-view', ['uri-id' => $uri_id, 'name' => 'nsfw', 'type' => Tag::HASHTAG]);
|
2018-10-03 18:27:01 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 04:13:26 +00:00
|
|
|
/**
|
|
|
|
* Creates event data
|
|
|
|
*
|
2022-06-20 19:21:32 +00:00
|
|
|
* @param array $item Item array
|
2018-10-26 04:13:26 +00:00
|
|
|
* @return array with the event data
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-10-26 04:13:26 +00:00
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
private static function createEvent(array $item): array
|
2018-10-26 04:13:26 +00:00
|
|
|
{
|
|
|
|
$event = [];
|
|
|
|
$event['name'] = $item['event-summary'];
|
2021-07-09 06:29:24 +00:00
|
|
|
$event['content'] = BBCode::convertForUriId($item['uri-id'], $item['event-desc'], BBCode::ACTIVITYPUB);
|
2021-10-03 17:21:17 +00:00
|
|
|
$event['startTime'] = DateTimeFormat::utc($item['event-start'], 'c');
|
2018-10-26 04:13:26 +00:00
|
|
|
|
|
|
|
if (!$item['event-nofinish']) {
|
2021-10-03 17:21:17 +00:00
|
|
|
$event['endTime'] = DateTimeFormat::utc($item['event-finish'], 'c');
|
2018-10-26 04:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($item['event-location'])) {
|
|
|
|
$item['location'] = $item['event-location'];
|
|
|
|
$event['location'] = self::createLocation($item);
|
|
|
|
}
|
|
|
|
|
2021-10-03 17:21:17 +00:00
|
|
|
// 2021.12: Backward compatibility value, all the events now "adjust" to the viewer timezone
|
|
|
|
$event['dfrn:adjust'] = true;
|
2021-04-08 19:38:16 +00:00
|
|
|
|
2018-10-26 04:13:26 +00:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Creates a note/article object array
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
|
|
|
* @param array $item
|
2023-02-08 20:16:19 +00:00
|
|
|
* @param bool $api_mode
|
2018-10-04 12:53:41 +00:00
|
|
|
* @return array with the object data
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2023-02-08 20:16:19 +00:00
|
|
|
public static function createNote(array $item, bool $api_mode = false): array
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
2019-11-19 10:02:35 +00:00
|
|
|
if (empty($item)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-03-24 10:57:55 +00:00
|
|
|
// We are treating posts differently when they are directed to a community.
|
|
|
|
// This is done to better support Lemmy. Most of the changes should work with other systems as well.
|
|
|
|
// But to not risk compatibility issues we currently perform the changes only for communities.
|
2022-09-12 21:12:11 +00:00
|
|
|
if ($item['gravity'] == Item::GRAVITY_PARENT) {
|
2022-03-24 10:57:55 +00:00
|
|
|
$isCommunityPost = !empty(Tag::getByURIId($item['uri-id'], [Tag::EXCLUSIVE_MENTION]));
|
|
|
|
$links = Post\Media::getByURIId($item['uri-id'], [Post\Media::HTML]);
|
|
|
|
if ($isCommunityPost && (count($links) == 1)) {
|
|
|
|
$link = $links[0]['url'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$isCommunityPost = false;
|
|
|
|
}
|
|
|
|
|
2018-10-26 04:13:26 +00:00
|
|
|
if ($item['event-type'] == 'event') {
|
|
|
|
$type = 'Event';
|
|
|
|
} elseif (!empty($item['title'])) {
|
2022-03-24 10:57:55 +00:00
|
|
|
if (!$isCommunityPost || empty($link)) {
|
|
|
|
$type = 'Article';
|
|
|
|
} else {
|
|
|
|
// "Page" is used by Lemmy for posts that contain an external link
|
|
|
|
$type = 'Page';
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
} else {
|
|
|
|
$type = 'Note';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($item['deleted']) {
|
|
|
|
$type = 'Tombstone';
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [];
|
|
|
|
$data['id'] = $item['uri'];
|
|
|
|
$data['type'] = $type;
|
|
|
|
|
|
|
|
if ($item['deleted']) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2019-03-18 22:38:10 +00:00
|
|
|
$data['summary'] = BBCode::toPlaintext(BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB));
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
if ($item['uri'] != $item['thr-parent']) {
|
|
|
|
$data['inReplyTo'] = $item['thr-parent'];
|
|
|
|
} else {
|
|
|
|
$data['inReplyTo'] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['diaspora:guid'] = $item['guid'];
|
2018-10-05 04:04:37 +00:00
|
|
|
$data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2018-10-05 04:04:37 +00:00
|
|
|
if ($item['created'] != $item['edited']) {
|
|
|
|
$data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 10:57:55 +00:00
|
|
|
$data['url'] = $link ?? $item['plink'];
|
2023-02-08 20:16:19 +00:00
|
|
|
if ($api_mode) {
|
|
|
|
$data['attributedTo'] = self::getActorArrayByCid($item['author-id']);
|
|
|
|
} else {
|
|
|
|
$data['attributedTo'] = $item['author-link'];
|
|
|
|
}
|
2020-04-26 15:24:58 +00:00
|
|
|
$data['sensitive'] = self::isSensitive($item['uri-id']);
|
2022-09-06 15:18:41 +00:00
|
|
|
|
|
|
|
if (!empty($item['conversation']) && ($item['conversation'] != './')) {
|
|
|
|
$data['conversation'] = $data['context'] = $item['conversation'];
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
if (!empty($item['title'])) {
|
2018-10-06 13:28:17 +00:00
|
|
|
$data['name'] = BBCode::toPlaintext($item['title'], false);
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 05:13:23 +00:00
|
|
|
$permission_block = self::createPermissionBlockForItem($item, false, false);
|
2019-02-09 04:09:02 +00:00
|
|
|
|
2022-10-15 13:00:52 +00:00
|
|
|
$real_quote = false;
|
|
|
|
|
2023-03-04 19:04:37 +00:00
|
|
|
$item = Post\Media::addHTMLAttachmentToItem($item);
|
|
|
|
|
2018-10-05 04:04:37 +00:00
|
|
|
$body = $item['body'];
|
|
|
|
|
|
|
|
if ($type == 'Note') {
|
2020-11-06 04:14:29 +00:00
|
|
|
$body = $item['raw-body'] ?? self::removePictures($body);
|
2018-10-05 04:04:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 16:11:10 +00:00
|
|
|
/**
|
|
|
|
* @todo Improve the automated summary
|
|
|
|
* This part is currently deactivated. The automated summary seems to be more
|
|
|
|
* confusing than helping. But possibly we will find a better way.
|
|
|
|
* So the code is left here for now as a reminder
|
2021-07-04 06:30:54 +00:00
|
|
|
*
|
2021-05-22 16:11:10 +00:00
|
|
|
* } elseif (($type == 'Article') && empty($data['summary'])) {
|
|
|
|
* $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
|
2023-01-19 14:02:05 +00:00
|
|
|
* $summary = preg_replace_callback($regexp, [self::class, 'mentionAddrCallback'], $body);
|
2021-05-22 16:11:10 +00:00
|
|
|
* $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($summary), 1000));
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
2020-11-06 04:14:29 +00:00
|
|
|
if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
|
2020-11-08 19:21:08 +00:00
|
|
|
$body = self::prependMentions($body, $item['uri-id'], $item['author-link']);
|
2020-11-06 04:14:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 04:13:26 +00:00
|
|
|
if ($type == 'Event') {
|
|
|
|
$data = array_merge($data, self::createEvent($item));
|
|
|
|
} else {
|
2022-03-24 10:57:55 +00:00
|
|
|
if ($isCommunityPost) {
|
|
|
|
// For community posts we remove the visible "!user@domain.tld".
|
|
|
|
// This improves the look at systems like Lemmy.
|
|
|
|
// Also in the future we should control the community delivery via other methods.
|
|
|
|
$body = preg_replace("/!\[url\=[^\[\]]*\][^\[\]]*\[\/url\]/ism", '', $body);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type == 'Page') {
|
|
|
|
// When we transmit "Page" posts we have to remove the attachment.
|
|
|
|
// The attachment contains the link that we already transmit in the "url" field.
|
2023-02-27 07:47:31 +00:00
|
|
|
$body = BBCode::removeAttachment($body);
|
2022-03-24 10:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 22:11:39 +00:00
|
|
|
$body = BBCode::setMentionsToNicknames($body);
|
|
|
|
|
|
|
|
if (!empty($item['quote-uri-id'])) {
|
|
|
|
if (Post::exists(['uri-id' => $item['quote-uri-id'], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN]])) {
|
|
|
|
$real_quote = true;
|
|
|
|
$data['quoteUrl'] = $item['quote-uri'];
|
|
|
|
$body = DI::contentItem()->addShareLink($body, $item['quote-uri-id']);
|
|
|
|
} else {
|
|
|
|
$body = DI::contentItem()->addSharedPost($item, $body);
|
2022-10-15 13:00:52 +00:00
|
|
|
}
|
2022-09-29 22:29:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 06:29:24 +00:00
|
|
|
$data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);
|
2018-10-26 04:13:26 +00:00
|
|
|
}
|
2018-10-19 05:27:54 +00:00
|
|
|
|
2020-01-04 14:35:14 +00:00
|
|
|
// The regular "content" field does contain a minimized HTML. This is done since systems like
|
|
|
|
// Mastodon has got problems with - for example - embedded pictures.
|
|
|
|
// The contentMap does contain the unmodified HTML.
|
|
|
|
$language = self::getLanguage($item);
|
|
|
|
if (!empty($language)) {
|
2021-07-27 12:25:38 +00:00
|
|
|
$richbody = BBCode::setMentionsToNicknames($item['body'] ?? '');
|
2023-01-07 15:11:34 +00:00
|
|
|
$richbody = Post\Media::removeFromEndOfBody($richbody);
|
2022-10-29 22:11:39 +00:00
|
|
|
if (!empty($item['quote-uri-id'])) {
|
|
|
|
if ($real_quote) {
|
|
|
|
$richbody = DI::contentItem()->addShareLink($richbody, $item['quote-uri-id']);
|
|
|
|
} else {
|
|
|
|
$richbody = DI::contentItem()->addSharedPost($item, $richbody);
|
2022-10-15 13:00:52 +00:00
|
|
|
}
|
2022-09-30 07:10:43 +00:00
|
|
|
}
|
2023-02-27 07:47:31 +00:00
|
|
|
$richbody = BBCode::replaceAttachment($richbody);
|
2019-11-13 06:43:36 +00:00
|
|
|
|
2021-07-09 06:29:24 +00:00
|
|
|
$data['contentMap'][$language] = BBCode::convertForUriId($item['uri-id'], $richbody, BBCode::EXTERNAL);
|
2020-01-04 14:35:14 +00:00
|
|
|
}
|
2019-11-12 20:22:35 +00:00
|
|
|
|
2022-11-05 16:25:04 +00:00
|
|
|
if (!empty($item['quote-uri-id'])) {
|
|
|
|
$source = DI::contentItem()->addSharedPost($item, $item['body']);
|
|
|
|
} else {
|
|
|
|
$source = $item['body'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['source'] = ['content' => $source, 'mediaType' => "text/bbcode"];
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
|
|
|
|
$data['diaspora:comment'] = $item['signed_text'];
|
|
|
|
}
|
|
|
|
|
2022-09-30 07:10:43 +00:00
|
|
|
$data['attachment'] = self::createAttachmentList($item);
|
2022-11-12 17:01:22 +00:00
|
|
|
$data['tag'] = self::createTagList($item, $data['quoteUrl'] ?? '');
|
2018-10-13 19:49:20 +00:00
|
|
|
|
2018-10-26 04:13:26 +00:00
|
|
|
if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
|
2018-10-13 19:49:20 +00:00
|
|
|
$data['location'] = self::createLocation($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($item['app'])) {
|
|
|
|
$data['generator'] = ['type' => 'Application', 'name' => $item['app']];
|
|
|
|
}
|
|
|
|
|
2019-02-09 04:09:02 +00:00
|
|
|
$data = array_merge($data, $permission_block);
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2020-01-04 14:35:14 +00:00
|
|
|
/**
|
|
|
|
* Fetches the language from the post, the user or the system.
|
|
|
|
*
|
|
|
|
* @param array $item
|
|
|
|
* @return string language string
|
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
private static function getLanguage(array $item): string
|
2020-01-04 14:35:14 +00:00
|
|
|
{
|
|
|
|
// Try to fetch the language from the post itself
|
|
|
|
if (!empty($item['language'])) {
|
|
|
|
$languages = array_keys(json_decode($item['language'], true));
|
|
|
|
if (!empty($languages[0])) {
|
|
|
|
return $languages[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise use the user's language
|
|
|
|
if (!empty($item['uid'])) {
|
|
|
|
$user = DBA::selectFirst('user', ['language'], ['uid' => $item['uid']]);
|
|
|
|
if (!empty($user['language'])) {
|
|
|
|
return $user['language'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// And finally just use the system language
|
2020-01-19 20:21:13 +00:00
|
|
|
return DI::config()->get('system', 'language');
|
2020-01-04 14:35:14 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 19:33:58 +00:00
|
|
|
/**
|
|
|
|
* Creates an an "add tag" entry
|
|
|
|
*
|
2022-06-20 19:21:32 +00:00
|
|
|
* @param array $item Item array
|
|
|
|
* @param array $activity activity data
|
2019-05-25 19:33:58 +00:00
|
|
|
* @return array with activity data for adding tags
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
private static function createAddTag(array $item, array $activity): array
|
2019-05-25 19:33:58 +00:00
|
|
|
{
|
2020-04-27 14:35:50 +00:00
|
|
|
$object = XML::parseString($item['object']);
|
2022-06-20 19:21:32 +00:00
|
|
|
$target = XML::parseString($item['target']);
|
2019-05-25 19:33:58 +00:00
|
|
|
|
2022-06-20 19:21:32 +00:00
|
|
|
$activity['diaspora:guid'] = $item['guid'];
|
|
|
|
$activity['actor'] = $item['author-link'];
|
|
|
|
$activity['target'] = (string)$target->id;
|
|
|
|
$activity['summary'] = BBCode::toPlaintext($item['body']);
|
|
|
|
$activity['object'] = ['id' => (string)$object->id, 'type' => 'tag', 'name' => (string)$object->title, 'content' => (string)$object->content];
|
2019-05-25 19:33:58 +00:00
|
|
|
|
2022-06-20 19:21:32 +00:00
|
|
|
return $activity;
|
2019-05-25 19:33:58 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 16:41:29 +00:00
|
|
|
/**
|
|
|
|
* Creates an announce object entry
|
|
|
|
*
|
2022-06-20 19:21:32 +00:00
|
|
|
* @param array $item Item array
|
|
|
|
* @param array $activity activity data
|
2023-02-08 20:16:19 +00:00
|
|
|
* @param bool $api_mode
|
2019-01-30 13:07:23 +00:00
|
|
|
* @return array with activity data
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
2018-10-13 16:41:29 +00:00
|
|
|
*/
|
2023-02-08 20:16:19 +00:00
|
|
|
private static function createAnnounce(array $item, array $activity, bool $api_mode = false): array
|
2018-10-13 16:41:29 +00:00
|
|
|
{
|
2019-07-20 21:32:19 +00:00
|
|
|
$orig_body = $item['body'];
|
2019-12-04 09:15:03 +00:00
|
|
|
$announce = self::getAnnounceArray($item);
|
2019-12-04 07:02:39 +00:00
|
|
|
if (empty($announce)) {
|
2022-06-20 19:21:32 +00:00
|
|
|
$activity['type'] = 'Create';
|
2023-02-08 20:16:19 +00:00
|
|
|
$activity['object'] = self::createNote($item, $api_mode);
|
2022-06-20 19:21:32 +00:00
|
|
|
return $activity;
|
2018-10-13 16:41:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 08:08:48 +00:00
|
|
|
if (empty($announce['comment'])) {
|
|
|
|
// Pure announce, without a quote
|
2022-06-20 19:21:32 +00:00
|
|
|
$activity['type'] = 'Announce';
|
|
|
|
$activity['object'] = $announce['object']['uri'];
|
|
|
|
return $activity;
|
2019-01-20 22:19:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 08:08:48 +00:00
|
|
|
// Quote
|
2022-06-20 19:21:32 +00:00
|
|
|
$activity['type'] = 'Create';
|
2019-12-04 09:36:46 +00:00
|
|
|
$item['body'] = $announce['comment'] . "\n" . $announce['object']['plink'];
|
2023-02-08 20:16:19 +00:00
|
|
|
$activity['object'] = self::createNote($item, $api_mode);
|
2019-12-04 08:08:48 +00:00
|
|
|
|
2023-03-22 03:17:25 +00:00
|
|
|
/// @todo Finally decide how to implement this in AP. This is a possible way:
|
2022-06-20 19:21:32 +00:00
|
|
|
$activity['object']['attachment'][] = self::createNote($announce['object']);
|
2019-12-04 08:08:48 +00:00
|
|
|
|
2022-06-20 19:21:32 +00:00
|
|
|
$activity['object']['source']['content'] = $orig_body;
|
|
|
|
return $activity;
|
2018-10-13 16:41:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 07:02:39 +00:00
|
|
|
/**
|
2023-03-26 21:54:16 +00:00
|
|
|
* Return announce related data if the item is an announce
|
2019-12-04 07:02:39 +00:00
|
|
|
*
|
|
|
|
* @param array $item
|
2022-06-20 19:21:32 +00:00
|
|
|
* @return array Announcement array
|
2019-12-04 07:02:39 +00:00
|
|
|
*/
|
2022-10-26 17:00:55 +00:00
|
|
|
private static function getAnnounceArray(array $item): array
|
2019-12-04 07:02:39 +00:00
|
|
|
{
|
2022-10-26 17:00:55 +00:00
|
|
|
$reshared = DI::contentItem()->getSharedPost($item, Item::DELIVER_FIELDLIST);
|
|
|
|
if (empty($reshared)) {
|
2019-12-04 07:02:39 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-10-26 17:00:55 +00:00
|
|
|
if (!in_array($reshared['post']['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
|
2019-12-04 08:08:48 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-10-26 17:00:55 +00:00
|
|
|
$profile = APContact::getByURL($reshared['post']['author-link'], false);
|
2019-12-04 07:02:39 +00:00
|
|
|
if (empty($profile)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-10-26 17:00:55 +00:00
|
|
|
return ['object' => $reshared['post'], 'actor' => $profile, 'comment' => $reshared['comment']];
|
2019-12-04 07:02:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 06:39:50 +00:00
|
|
|
/**
|
|
|
|
* Checks if the provided item array is an announce
|
|
|
|
*
|
2022-06-20 19:21:32 +00:00
|
|
|
* @param array $item Item array
|
|
|
|
* @return boolean Whether item is an announcement
|
2019-12-06 06:39:50 +00:00
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
public static function isAnnounce(array $item): bool
|
2019-12-06 06:39:50 +00:00
|
|
|
{
|
2020-08-15 20:05:08 +00:00
|
|
|
if (!empty($item['verb']) && ($item['verb'] == Activity::ANNOUNCE)) {
|
2020-08-09 18:42:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-06 06:39:50 +00:00
|
|
|
$announce = self::getAnnounceArray($item);
|
|
|
|
if (empty($announce)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return empty($announce['comment']);
|
|
|
|
}
|
|
|
|
|
2019-01-10 07:24:12 +00:00
|
|
|
/**
|
|
|
|
* Creates an activity id for a given contact id
|
|
|
|
*
|
|
|
|
* @param integer $cid Contact ID of target
|
|
|
|
*
|
|
|
|
* @return bool|string activity id
|
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
public static function activityIDFromContact(int $cid)
|
2019-01-10 07:24:12 +00:00
|
|
|
{
|
|
|
|
$contact = DBA::selectFirst('contact', ['uid', 'id', 'created'], ['id' => $cid]);
|
|
|
|
if (!DBA::isResult($contact)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$hash = hash('ripemd128', $contact['uid'].'-'.$contact['id'].'-'.$contact['created']);
|
|
|
|
$uuid = substr($hash, 0, 8). '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12);
|
2019-12-30 22:00:08 +00:00
|
|
|
return DI::baseUrl() . '/activity/' . $uuid;
|
2019-01-10 07:24:12 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 18:42:26 +00:00
|
|
|
/**
|
|
|
|
* Transmits a contact suggestion to a given inbox
|
|
|
|
*
|
2022-12-30 06:45:04 +00:00
|
|
|
* @param array $owner Sender owner-view record
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param string $inbox Target inbox
|
2018-10-06 18:42:26 +00:00
|
|
|
* @param integer $suggestion_id Suggestion ID
|
2018-10-23 03:54:18 +00:00
|
|
|
* @return boolean was the transmission successful?
|
2022-12-30 06:45:04 +00:00
|
|
|
* @throws \Exception
|
2018-10-06 18:42:26 +00:00
|
|
|
*/
|
2022-12-30 06:45:04 +00:00
|
|
|
public static function sendContactSuggestion(array $owner, string $inbox, int $suggestion_id): bool
|
2018-10-06 18:42:26 +00:00
|
|
|
{
|
2021-10-21 20:57:13 +00:00
|
|
|
$suggestion = DI::fsuggest()->selectOneById($suggestion_id);
|
2018-10-06 18:42:26 +00:00
|
|
|
|
2022-06-20 19:21:32 +00:00
|
|
|
$data = [
|
|
|
|
'@context' => ActivityPub::CONTEXT,
|
2019-12-30 22:00:08 +00:00
|
|
|
'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
|
2018-10-06 18:42:26 +00:00
|
|
|
'type' => 'Announce',
|
|
|
|
'actor' => $owner['url'],
|
2020-01-31 22:50:46 +00:00
|
|
|
'object' => $suggestion->url,
|
|
|
|
'content' => $suggestion->note,
|
2019-04-30 18:07:20 +00:00
|
|
|
'instrument' => self::getService(),
|
2018-10-06 18:42:26 +00:00
|
|
|
'to' => [ActivityPub::PUBLIC_COLLECTION],
|
2022-06-20 19:21:32 +00:00
|
|
|
'cc' => []
|
|
|
|
];
|
2018-10-06 18:42:26 +00:00
|
|
|
|
|
|
|
$signed = LDSignature::sign($data, $owner);
|
|
|
|
|
2022-12-30 06:45:04 +00:00
|
|
|
Logger::info('Deliver profile deletion for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
|
|
|
|
return HTTPSignature::transmit($signed, $inbox, $owner);
|
2018-10-06 18:42:26 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 21:57:06 +00:00
|
|
|
/**
|
|
|
|
* Transmits a profile relocation to a given inbox
|
|
|
|
*
|
2022-12-30 06:45:04 +00:00
|
|
|
* @param array $owner Sender owner-view record
|
|
|
|
* @param string $inbox Target inbox
|
2018-11-02 21:57:06 +00:00
|
|
|
* @return boolean was the transmission successful?
|
2022-12-30 06:45:04 +00:00
|
|
|
* @throws \Exception
|
2018-11-02 21:57:06 +00:00
|
|
|
*/
|
2022-12-30 06:45:04 +00:00
|
|
|
public static function sendProfileRelocation(array $owner, string $inbox): bool
|
2018-11-02 21:57:06 +00:00
|
|
|
{
|
2022-06-20 19:21:32 +00:00
|
|
|
$data = [
|
|
|
|
'@context' => ActivityPub::CONTEXT,
|
2019-12-30 22:00:08 +00:00
|
|
|
'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
|
2018-11-02 21:57:06 +00:00
|
|
|
'type' => 'dfrn:relocate',
|
|
|
|
'actor' => $owner['url'],
|
|
|
|
'object' => $owner['url'],
|
|
|
|
'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
|
2019-04-30 18:07:20 +00:00
|
|
|
'instrument' => self::getService(),
|
2018-11-02 21:57:06 +00:00
|
|
|
'to' => [ActivityPub::PUBLIC_COLLECTION],
|
2022-06-20 19:21:32 +00:00
|
|
|
'cc' => []
|
|
|
|
];
|
2018-11-02 21:57:06 +00:00
|
|
|
|
|
|
|
$signed = LDSignature::sign($data, $owner);
|
|
|
|
|
2022-12-30 06:45:04 +00:00
|
|
|
Logger::info('Deliver profile relocation for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
|
|
|
|
return HTTPSignature::transmit($signed, $inbox, $owner);
|
2018-11-02 21:57:06 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Transmits a profile deletion to a given inbox
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
2022-12-30 06:45:04 +00:00
|
|
|
* @param array $owner Sender owner-view record
|
|
|
|
* @param string $inbox Target inbox
|
2018-10-23 03:54:18 +00:00
|
|
|
* @return boolean was the transmission successful?
|
2022-12-30 06:45:04 +00:00
|
|
|
* @throws \Exception
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2022-12-30 06:45:04 +00:00
|
|
|
public static function sendProfileDeletion(array $owner, string $inbox): bool
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
2019-03-04 06:52:43 +00:00
|
|
|
if (empty($owner['uprvkey'])) {
|
2022-12-30 06:45:04 +00:00
|
|
|
Logger::error('No private key for owner found, the deletion message cannot be processed.', ['user' => $owner['uid']]);
|
2019-03-04 06:52:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-02 21:57:06 +00:00
|
|
|
$data = ['@context' => ActivityPub::CONTEXT,
|
2019-12-30 22:00:08 +00:00
|
|
|
'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
|
2018-10-03 06:15:07 +00:00
|
|
|
'type' => 'Delete',
|
|
|
|
'actor' => $owner['url'],
|
2018-10-03 09:15:38 +00:00
|
|
|
'object' => $owner['url'],
|
2018-10-03 06:15:07 +00:00
|
|
|
'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
|
2019-04-30 18:07:20 +00:00
|
|
|
'instrument' => self::getService(),
|
2018-10-03 06:15:07 +00:00
|
|
|
'to' => [ActivityPub::PUBLIC_COLLECTION],
|
|
|
|
'cc' => []];
|
|
|
|
|
|
|
|
$signed = LDSignature::sign($data, $owner);
|
|
|
|
|
2022-12-30 06:45:04 +00:00
|
|
|
Logger::info('Deliver profile deletion for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
|
|
|
|
return HTTPSignature::transmit($signed, $inbox, $owner);
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Transmits a profile change to a given inbox
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
2022-12-30 06:45:04 +00:00
|
|
|
* @param array $owner Sender owner-view record
|
|
|
|
* @param string $inbox Target inbox
|
2018-10-23 03:54:18 +00:00
|
|
|
* @return boolean was the transmission successful?
|
2021-07-20 17:04:25 +00:00
|
|
|
* @throws HTTPException\InternalServerErrorException
|
|
|
|
* @throws HTTPException\NotFoundException
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \ImagickException
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2022-12-30 06:45:04 +00:00
|
|
|
public static function sendProfileUpdate(array $owner, string $inbox): bool
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
|
|
|
$profile = APContact::getByURL($owner['url']);
|
|
|
|
|
2018-11-02 21:57:06 +00:00
|
|
|
$data = ['@context' => ActivityPub::CONTEXT,
|
2019-12-30 22:00:08 +00:00
|
|
|
'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
|
2018-10-03 06:15:07 +00:00
|
|
|
'type' => 'Update',
|
|
|
|
'actor' => $owner['url'],
|
2022-12-30 06:45:04 +00:00
|
|
|
'object' => self::getProfile($owner['uid']),
|
2018-10-03 06:15:07 +00:00
|
|
|
'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
|
2019-04-30 18:07:20 +00:00
|
|
|
'instrument' => self::getService(),
|
2018-10-03 06:15:07 +00:00
|
|
|
'to' => [$profile['followers']],
|
|
|
|
'cc' => []];
|
|
|
|
|
|
|
|
$signed = LDSignature::sign($data, $owner);
|
|
|
|
|
2022-12-30 06:45:04 +00:00
|
|
|
Logger::info('Deliver profile update for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
|
|
|
|
return HTTPSignature::transmit($signed, $inbox, $owner);
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Transmits a given activity to a target
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
2019-01-07 06:07:42 +00:00
|
|
|
* @param string $activity Type name
|
|
|
|
* @param string $target Target profile
|
|
|
|
* @param integer $uid User ID
|
2022-06-20 19:21:32 +00:00
|
|
|
* @param string $id Activity-identifier
|
2019-01-07 06:07:42 +00:00
|
|
|
* @return bool
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
|
|
|
* @throws \Exception
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
public static function sendActivity(string $activity, string $target, int $uid, string $id = ''): bool
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
|
|
|
$profile = APContact::getByURL($target);
|
2019-06-20 03:06:34 +00:00
|
|
|
if (empty($profile['inbox'])) {
|
|
|
|
Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
|
2022-06-20 19:21:32 +00:00
|
|
|
return false;
|
2019-06-20 03:06:34 +00:00
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
$owner = User::getOwnerDataById($uid);
|
2022-11-23 15:40:41 +00:00
|
|
|
if (empty($owner)) {
|
|
|
|
Logger::warning('No user found for actor, aborting', ['uid' => $uid]);
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2019-01-10 07:24:12 +00:00
|
|
|
if (empty($id)) {
|
2019-12-30 22:00:08 +00:00
|
|
|
$id = DI::baseUrl() . '/activity/' . System::createGUID();
|
2019-01-10 07:24:12 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 23:29:20 +00:00
|
|
|
$data = [
|
|
|
|
'@context' => ActivityPub::CONTEXT,
|
2019-01-10 07:24:12 +00:00
|
|
|
'id' => $id,
|
2018-10-03 06:15:07 +00:00
|
|
|
'type' => $activity,
|
|
|
|
'actor' => $owner['url'],
|
|
|
|
'object' => $profile['url'],
|
2019-04-30 18:07:20 +00:00
|
|
|
'instrument' => self::getService(),
|
2022-06-20 23:29:20 +00:00
|
|
|
'to' => [$profile['url']],
|
|
|
|
];
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2021-11-03 23:19:24 +00:00
|
|
|
Logger::info('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid);
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
$signed = LDSignature::sign($data, $owner);
|
2022-12-30 06:45:04 +00:00
|
|
|
return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 16:30:01 +00:00
|
|
|
/**
|
|
|
|
* Transmits a "follow object" activity to a target
|
|
|
|
* This is a preparation for sending automated "follow" requests when receiving "Announce" messages
|
|
|
|
*
|
|
|
|
* @param string $object Object URL
|
|
|
|
* @param string $target Target profile
|
|
|
|
* @param integer $uid User ID
|
|
|
|
* @return bool
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2022-06-20 19:21:32 +00:00
|
|
|
public static function sendFollowObject(string $object, string $target, int $uid = 0): bool
|
2019-01-30 16:30:01 +00:00
|
|
|
{
|
|
|
|
$profile = APContact::getByURL($target);
|
2019-06-20 03:06:34 +00:00
|
|
|
if (empty($profile['inbox'])) {
|
|
|
|
Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
|
2022-06-20 19:21:32 +00:00
|
|
|
return false;
|
2019-06-20 03:06:34 +00:00
|
|
|
}
|
2019-01-30 16:30:01 +00:00
|
|
|
|
2019-01-30 21:33:23 +00:00
|
|
|
if (empty($uid)) {
|
|
|
|
// We need to use some user as a sender. It doesn't care who it will send. We will use an administrator account.
|
2022-11-12 17:01:22 +00:00
|
|
|
$admin = User::getFirstAdmin(['uid']);
|
|
|
|
if (!$admin) {
|
|
|
|
Logger::warning('No available admin user for transmission', ['target' => $target]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$uid = $admin['uid'];
|
2019-01-30 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 22:25:43 +00:00
|
|
|
$condition = ['verb' => Activity::FOLLOW, 'uid' => 0, 'parent-uri' => $object,
|
2019-02-10 18:03:01 +00:00
|
|
|
'author-id' => Contact::getPublicIdByUserId($uid)];
|
2021-01-16 04:14:58 +00:00
|
|
|
if (Post::exists($condition)) {
|
2021-11-03 23:19:24 +00:00
|
|
|
Logger::info('Follow for ' . $object . ' for user ' . $uid . ' does already exist.');
|
2019-02-10 18:03:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-30 16:30:01 +00:00
|
|
|
$owner = User::getOwnerDataById($uid);
|
|
|
|
|
2022-06-20 23:29:20 +00:00
|
|
|
$data = [
|
|
|
|
'@context' => ActivityPub::CONTEXT,
|
2019-12-30 22:00:08 +00:00
|
|
|
'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
|
2019-01-30 16:30:01 +00:00
|
|
|
'type' => 'Follow',
|
|
|
|
'actor' => $owner['url'],
|
|
|
|
'object' => $object,
|
2019-04-30 18:07:20 +00:00
|
|
|
'instrument' => self::getService(),
|
2022-06-20 23:29:20 +00:00
|
|
|
'to' => [$profile['url']],
|
|
|
|
];
|
2019-01-30 16:30:01 +00:00
|
|
|
|
2021-11-03 23:19:24 +00:00
|
|
|
Logger::info('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid);
|
2019-01-30 16:30:01 +00:00
|
|
|
|
|
|
|
$signed = LDSignature::sign($data, $owner);
|
2022-12-30 06:45:04 +00:00
|
|
|
return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
|
2019-01-30 16:30:01 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 06:15:07 +00:00
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Transmit a message that the contact request had been accepted
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param string $target Target profile
|
2022-06-23 14:46:44 +00:00
|
|
|
* @param string $id Object id
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param integer $uid User ID
|
2022-06-20 19:21:32 +00:00
|
|
|
* @return void
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2022-06-23 14:46:44 +00:00
|
|
|
public static function sendContactAccept(string $target, string $id, int $uid)
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
|
|
|
$profile = APContact::getByURL($target);
|
2019-06-20 03:06:34 +00:00
|
|
|
if (empty($profile['inbox'])) {
|
|
|
|
Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
$owner = User::getOwnerDataById($uid);
|
2022-12-30 06:45:04 +00:00
|
|
|
if (!$owner) {
|
|
|
|
Logger::notice('No user found for actor', ['uid' => $uid]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-20 23:29:20 +00:00
|
|
|
$data = [
|
|
|
|
'@context' => ActivityPub::CONTEXT,
|
2019-12-30 22:00:08 +00:00
|
|
|
'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
|
2018-10-03 06:15:07 +00:00
|
|
|
'type' => 'Accept',
|
|
|
|
'actor' => $owner['url'],
|
2019-05-19 22:44:57 +00:00
|
|
|
'object' => [
|
2022-06-23 14:46:44 +00:00
|
|
|
'id' => $id,
|
2019-05-19 22:44:57 +00:00
|
|
|
'type' => 'Follow',
|
2018-10-03 06:15:07 +00:00
|
|
|
'actor' => $profile['url'],
|
2019-05-19 22:44:57 +00:00
|
|
|
'object' => $owner['url']
|
|
|
|
],
|
2019-04-30 18:07:20 +00:00
|
|
|
'instrument' => self::getService(),
|
2022-06-20 23:29:20 +00:00
|
|
|
'to' => [$profile['url']],
|
|
|
|
];
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2019-05-19 22:44:57 +00:00
|
|
|
Logger::debug('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id);
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
$signed = LDSignature::sign($data, $owner);
|
2022-12-30 06:45:04 +00:00
|
|
|
HTTPSignature::transmit($signed, $profile['inbox'], $owner);
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Reject a contact request or terminates the contact relation
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
2022-07-06 03:41:48 +00:00
|
|
|
* @param string $target Target profile
|
|
|
|
* @param string $objectId Object id
|
2022-12-30 06:45:04 +00:00
|
|
|
* @param array $owner Sender owner-view record
|
2021-10-02 15:44:47 +00:00
|
|
|
* @return bool Operation success
|
|
|
|
* @throws HTTPException\InternalServerErrorException
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \ImagickException
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2022-12-30 06:45:04 +00:00
|
|
|
public static function sendContactReject(string $target, string $objectId, array $owner): bool
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
|
|
|
$profile = APContact::getByURL($target);
|
2019-06-20 03:06:34 +00:00
|
|
|
if (empty($profile['inbox'])) {
|
|
|
|
Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
|
2021-10-02 15:44:47 +00:00
|
|
|
return false;
|
2019-06-20 03:06:34 +00:00
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2022-06-20 23:29:20 +00:00
|
|
|
$data = [
|
|
|
|
'@context' => ActivityPub::CONTEXT,
|
2019-12-30 22:00:08 +00:00
|
|
|
'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
|
2018-10-03 06:15:07 +00:00
|
|
|
'type' => 'Reject',
|
2022-07-06 03:41:48 +00:00
|
|
|
'actor' => $owner['url'],
|
2019-05-19 22:44:57 +00:00
|
|
|
'object' => [
|
2022-07-06 03:41:48 +00:00
|
|
|
'id' => $objectId,
|
2019-05-19 22:44:57 +00:00
|
|
|
'type' => 'Follow',
|
2018-10-03 06:15:07 +00:00
|
|
|
'actor' => $profile['url'],
|
2019-05-19 22:44:57 +00:00
|
|
|
'object' => $owner['url']
|
|
|
|
],
|
2019-04-30 18:07:20 +00:00
|
|
|
'instrument' => self::getService(),
|
2022-06-20 23:29:20 +00:00
|
|
|
'to' => [$profile['url']],
|
|
|
|
];
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2022-12-30 06:45:04 +00:00
|
|
|
Logger::debug('Sending reject to ' . $target . ' for user ' . $owner['uid'] . ' with id ' . $objectId);
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
$signed = LDSignature::sign($data, $owner);
|
2022-12-30 06:45:04 +00:00
|
|
|
return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-06 04:18:40 +00:00
|
|
|
* Transmits a message that we don't want to follow this contact anymore
|
2018-10-03 06:15:07 +00:00
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param string $target Target profile
|
2022-06-20 18:59:08 +00:00
|
|
|
* @param integer $cid Contact id
|
2022-12-30 06:45:04 +00:00
|
|
|
* @param array $owner Sender owner-view record
|
2022-06-20 18:59:08 +00:00
|
|
|
* @return bool success
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
|
|
|
* @throws \Exception
|
2018-10-03 06:15:07 +00:00
|
|
|
*/
|
2022-12-30 06:45:04 +00:00
|
|
|
public static function sendContactUndo(string $target, int $cid, array $owner): bool
|
2018-10-03 06:15:07 +00:00
|
|
|
{
|
|
|
|
$profile = APContact::getByURL($target);
|
2019-06-20 03:06:34 +00:00
|
|
|
if (empty($profile['inbox'])) {
|
|
|
|
Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
|
2020-09-15 17:45:19 +00:00
|
|
|
return false;
|
2019-06-20 03:06:34 +00:00
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2019-01-10 07:24:12 +00:00
|
|
|
$object_id = self::activityIDFromContact($cid);
|
|
|
|
if (empty($object_id)) {
|
2020-09-15 17:45:19 +00:00
|
|
|
return false;
|
2019-01-10 07:24:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 03:41:48 +00:00
|
|
|
$objectId = DI::baseUrl() . '/activity/' . System::createGUID();
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2022-06-20 18:59:08 +00:00
|
|
|
$data = [
|
|
|
|
'@context' => ActivityPub::CONTEXT,
|
2022-07-06 03:41:48 +00:00
|
|
|
'id' => $objectId,
|
2018-10-03 06:15:07 +00:00
|
|
|
'type' => 'Undo',
|
|
|
|
'actor' => $owner['url'],
|
2022-06-20 18:59:08 +00:00
|
|
|
'object' => [
|
|
|
|
'id' => $object_id,
|
|
|
|
'type' => 'Follow',
|
2018-10-03 06:15:07 +00:00
|
|
|
'actor' => $owner['url'],
|
2022-06-20 18:59:08 +00:00
|
|
|
'object' => $profile['url']
|
|
|
|
],
|
2019-04-30 18:07:20 +00:00
|
|
|
'instrument' => self::getService(),
|
2022-06-20 23:29:20 +00:00
|
|
|
'to' => [$profile['url']],
|
2022-06-20 18:59:08 +00:00
|
|
|
];
|
2018-10-03 06:15:07 +00:00
|
|
|
|
2022-12-30 06:45:04 +00:00
|
|
|
Logger::info('Sending undo to ' . $target . ' for user ' . $owner['uid'] . ' with id ' . $objectId);
|
2018-10-03 06:15:07 +00:00
|
|
|
|
|
|
|
$signed = LDSignature::sign($data, $owner);
|
2022-12-30 06:45:04 +00:00
|
|
|
return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|
2019-02-09 04:09:02 +00:00
|
|
|
|
2022-06-20 18:59:08 +00:00
|
|
|
/**
|
|
|
|
* Prepends mentions (@) to $body variable
|
|
|
|
*
|
|
|
|
* @param string $body HTML code
|
2022-06-23 14:03:55 +00:00
|
|
|
* @param int $uriId
|
2022-06-20 18:59:08 +00:00
|
|
|
* @param string $authorLink Author link
|
|
|
|
* @return string HTML code with prepended mentions
|
|
|
|
*/
|
|
|
|
private static function prependMentions(string $body, int $uriid, string $authorLink): string
|
2019-02-09 04:09:02 +00:00
|
|
|
{
|
|
|
|
$mentions = [];
|
|
|
|
|
2020-05-09 06:33:59 +00:00
|
|
|
foreach (Tag::getByURIId($uriid, [Tag::IMPLICIT_MENTION]) as $tag) {
|
2020-07-15 17:06:48 +00:00
|
|
|
$profile = Contact::getByURL($tag['url'], false, ['addr', 'contact-type', 'nick']);
|
2019-02-09 04:09:02 +00:00
|
|
|
if (!empty($profile['addr'])
|
|
|
|
&& $profile['contact-type'] != Contact::TYPE_COMMUNITY
|
|
|
|
&& !strstr($body, $profile['addr'])
|
2020-05-09 06:33:59 +00:00
|
|
|
&& !strstr($body, $tag['url'])
|
2020-11-08 19:21:08 +00:00
|
|
|
&& $tag['url'] !== $authorLink
|
2019-02-09 04:09:02 +00:00
|
|
|
) {
|
2020-05-09 06:33:59 +00:00
|
|
|
$mentions[] = '@[url=' . $tag['url'] . ']' . $profile['nick'] . '[/url]';
|
2019-02-09 04:09:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$mentions[] = $body;
|
|
|
|
|
|
|
|
return implode(' ', $mentions);
|
|
|
|
}
|
2018-10-03 06:15:07 +00:00
|
|
|
}
|