use Friendica\Model\ItemURI;
use Friendica\Model\Profile;
use Friendica\Model\Photo;
+use Friendica\Model\Post;
use Friendica\Model\Tag;
use Friendica\Model\User;
use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub;
+use Friendica\Protocol\Relay;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\HTTPSignature;
-use Friendica\Util\Images;
use Friendica\Util\JsonLD;
use Friendica\Util\LDSignature;
use Friendica\Util\Map;
*/
class Transmitter
{
+ /**
+ * Add relay servers to the list of inboxes
+ *
+ * @param array $inboxes
+ * @return array inboxes with added relay servers
+ */
+ public static function addRelayServerInboxes(array $inboxes = [])
+ {
+ $contacts = DBA::select('apcontact', ['inbox'],
+ ["`type` = ? AND `url` IN (SELECT `url` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))",
+ 'Application', 0, Contact::FOLLOWER, Contact::FRIEND]);
+ while ($contact = DBA::fetch($contacts)) {
+ $inboxes[] = $contact['inbox'];
+ }
+ DBA::close($contacts);
+
+ return $inboxes;
+ }
+
+ /**
+ * Add relay servers to the list of inboxes
+ *
+ * @param array $inboxes
+ * @return array inboxes with added relay servers
+ */
+ public static function addRelayServerInboxesForItem(int $item_id, array $inboxes = [])
+ {
+ $relays = Relay::getList($item_id, [], [Protocol::ACTIVITYPUB]);
+ if (empty($relays)) {
+ return $inboxes;
+ }
+
+ foreach ($relays as $relay) {
+ if (!in_array($relay['batch'], $inboxes)) {
+ $inboxes[] = $relay['batch'];
+ }
+ }
+ return $inboxes;
+ }
+
+ /**
+ * Subscribe to a relay
+ *
+ * @param string $url Subscribe actor url
+ * @return bool success
+ */
+ public static function sendRelayFollow(string $url)
+ {
+ $contact = Contact::getByURL($url);
+ if (empty($contact)) {
+ return false;
+ }
+
+ $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact['id']);
+ $success = ActivityPub\Transmitter::sendActivity('Follow', $url, 0, $activity_id);
+ if ($success) {
+ $rel = $contact['rel'] == Contact::SHARING ? Contact::FRIEND : Contact::FOLLOWER;
+ DBA::update('contact', ['rel' => $rel], ['id' => $contact['id']]);
+ }
+
+ return $success;
+ }
+
+ /**
+ * Unsubscribe from a relay
+ *
+ * @param string $url Subscribe actor url
+ * @param bool $force Set the relay status as non follower even if unsubscribe hadn't worked
+ * @return bool success
+ */
+ public static function sendRelayUndoFollow(string $url, bool $force = false)
+ {
+ $contact = Contact::getByURL($url);
+ if (empty($contact)) {
+ return false;
+ }
+
+ $success = self::sendContactUndo($url, $contact['id'], 0);
+ if ($success || $force) {
+ $rel = $contact['rel'] == Contact::FRIEND ? Contact::SHARING : Contact::NOTHING;
+ DBA::update('contact', ['rel' => $rel], ['id' => $contact['id']]);
+ }
+
+ return $success;
+ }
+
/**
* Collects a list of contacts of the given owner
*
'deleted' => false,
'hidden' => false,
'archive' => false,
- 'pending' => false
+ 'pending' => false,
+ 'blocked' => false,
];
$condition = DBA::buildCondition($parameters);
/**
* Public posts for the given owner
*
- * @param array $owner Owner array
- * @param integer $page Page numbe
+ * @param array $owner Owner array
+ * @param integer $page Page number
+ * @param string $requester URL of requesting account
*
* @return array of posts
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
- public static function getOutbox($owner, $page = null)
+ public static function getOutbox($owner, $page = null, $requester = '')
{
$public_contact = Contact::getIdForURL($owner['url']);
+ $condition = ['uid' => 0, 'contact-id' => $public_contact,
+ 'private' => [Item::PUBLIC, Item::UNLISTED]];
+
+ if (!empty($requester)) {
+ $requester_id = Contact::getIdForURL($requester, $owner['uid']);
+ if (!empty($requester_id)) {
+ $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $owner['uid']);
+ if (!empty($permissionSets)) {
+ $condition = ['uid' => $owner['uid'], 'origin' => true,
+ 'psid' => array_merge($permissionSets->column('id'),
+ [DI::permissionSet()->getIdFromACL($owner['uid'], '', '', '', '')])];
+ }
+ }
+ }
+
+ $condition = array_merge($condition,
+ ['author-id' => $public_contact,
+ 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
+ 'deleted' => false, 'visible' => true, 'moderated' => false]);
- $condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact,
- 'private' => [Item::PUBLIC, Item::UNLISTED], 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
- 'deleted' => false, 'visible' => true, 'moderated' => false];
$count = DBA::count('item', $condition);
$data = ['@context' => ActivityPub::CONTEXT];
*/
public static function getProfile($uid)
{
- $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
- 'account_removed' => false, 'verified' => true];
- $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
- $user = DBA::selectFirst('user', $fields, $condition);
- if (!DBA::isResult($user)) {
- return [];
- }
+ if ($uid != 0) {
+ $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
+ 'account_removed' => false, 'verified' => true];
+ $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
+ $user = DBA::selectFirst('user', $fields, $condition);
+ if (!DBA::isResult($user)) {
+ return [];
+ }
- $fields = ['locality', 'region', 'country-name'];
- $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
- if (!DBA::isResult($profile)) {
- return [];
- }
+ $fields = ['locality', 'region', 'country-name'];
+ $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
+ if (!DBA::isResult($profile)) {
+ return [];
+ }
- $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo'];
- $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
- if (!DBA::isResult($contact)) {
- return [];
+ $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo'];
+ $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
+ if (!DBA::isResult($contact)) {
+ return [];
+ }
+ } else {
+ $contact = User::getSystemAccount();
+ $user = ['guid' => '', 'nickname' => $contact['nick'], 'pubkey' => $contact['pubkey'],
+ 'account-type' => $contact['contact-type'], 'page-flags' => User::PAGE_FLAGS_NORMAL];
+ $profile = ['locality' => '', 'region' => '', 'country-name' => ''];
}
$data = ['@context' => ActivityPub::CONTEXT];
$data['id'] = $contact['url'];
- $data['diaspora:guid'] = $user['guid'];
+
+ if (!empty($user['guid'])) {
+ $data['diaspora:guid'] = $user['guid'];
+ }
+
$data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
- $data['following'] = DI::baseUrl() . '/following/' . $user['nickname'];
- $data['followers'] = DI::baseUrl() . '/followers/' . $user['nickname'];
- $data['inbox'] = DI::baseUrl() . '/inbox/' . $user['nickname'];
- $data['outbox'] = DI::baseUrl() . '/outbox/' . $user['nickname'];
+
+ if ($uid != 0) {
+ $data['following'] = DI::baseUrl() . '/following/' . $user['nickname'];
+ $data['followers'] = DI::baseUrl() . '/followers/' . $user['nickname'];
+ $data['inbox'] = DI::baseUrl() . '/inbox/' . $user['nickname'];
+ $data['outbox'] = DI::baseUrl() . '/outbox/' . $user['nickname'];
+ } else {
+ $data['inbox'] = DI::baseUrl() . '/friendica/inbox';
+ }
+
$data['preferredUsername'] = $user['nickname'];
$data['name'] = $contact['name'];
- $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
- 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
- $data['summary'] = BBCode::convert($contact['about'], false);
+
+ if (!empty($profile['country-name'] . $profile['region'] . $profile['locality'])) {
+ $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
+ 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
+ }
+
+ if (!empty($contact['about'])) {
+ $data['summary'] = BBCode::convert($contact['about'], false);
+ }
+
$data['url'] = $contact['url'];
$data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
$data['publicKey'] = ['id' => $contact['url'] . '#main-key',
$activity = json_decode($conversation['source'], true);
$actor = JsonLD::fetchElement($activity, 'actor', 'id');
- $profile = APContact::getByURL($actor);
+ if (!empty($actor)) {
+ $permissions['to'][] = $actor;
+ $profile = APContact::getByURL($actor);
+ } else {
+ $profile = [];
+ }
$item_profile = APContact::getByURL($item['author-link']);
$exclude[] = $item['author-link'];
$exclude[] = $item['owner-link'];
}
- $permissions['to'][] = $actor;
-
foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
if (empty($activity[$element])) {
continue;
}
$always_bcc = false;
- $isforum = false;
// Check if we should always deliver our stuff via BCC
if (!empty($item['uid'])) {
$profile = User::getOwnerDataById($item['uid']);
if (!empty($profile)) {
$always_bcc = $profile['hide-friends'];
- $isforum = $profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY;
}
}
$always_bcc = true;
}
- if ((self::isAnnounce($item) && !$isforum) || DI::config()->get('debug', 'total_ap_delivery')) {
+ if (self::isAnnounce($item) || DI::config()->get('debug', 'total_ap_delivery')) {
// Will be activated in a later step
$networks = Protocol::FEDERATED;
} else {
continue;
}
- if ($isforum && DBA::isResult($contact) && ($contact['dfrn'] == Protocol::DFRN)) {
- continue;
- }
-
if (!empty($profile = APContact::getByURL($contact['url'], false))) {
$data['to'][] = $profile['url'];
}
continue;
}
- if ($isforum && DBA::isResult($contact) && ($contact['dfrn'] == Protocol::DFRN)) {
- continue;
- }
-
if (!empty($profile = APContact::getByURL($contact['url'], false))) {
if ($contact['hidden'] || $always_bcc) {
$data['bcc'][] = $profile['url'];
*
* @return boolean "true" if inbox is archived
*/
- private static function archivedInbox($url)
+ public static function archivedInbox($url)
{
return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
}
$networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
}
- $condition = ['uid' => $uid, 'archive' => false, 'pending' => false];
+ $condition = ['uid' => $uid, 'archive' => false, 'pending' => false, 'blocked' => false];
if (!empty($uid)) {
$condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
continue;
}
- if ($isforum && ($contact['dfrn'] == Protocol::DFRN)) {
+ if ($isforum && ($contact['network'] == Protocol::DFRN)) {
continue;
}
$item_profile = APContact::getByURL($item['owner-link'], false);
}
+ if (empty($item_profile)) {
+ return [];
+ }
+
$profile_uid = User::getIdForURL($item_profile['url']);
foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
$mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
$mail['event-type'] = '';
- $mail['attach'] = '';
$mail['parent'] = 0;
public static function createActivityFromMail($mail_id, $object_mode = false)
{
$mail = self::ItemArrayFromMail($mail_id);
+ if (empty($mail)) {
+ return [];
+ }
$object = self::createNote($mail);
if (!$object_mode) {
return false;
}
- /// @todo This code should be removed by the end of the year 2020
- if ($item['wall'] && ($item['uri'] == $item['parent-uri'])) {
- $owner = User::getOwnerDataById($item['uid']);
- if (($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && ($item['author-link'] != $owner['url'])) {
- $type = 'Announce';
-
- // Disguise forum posts as reshares. Will later be converted to a real announce
- $item['body'] = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'],
- $item['plink'], $item['created'], $item['guid']) . $item['body'] . '[/share]';
- }
- }
-
- /*
- /// @todo This code should be activated by the end of the year 2020
- // See also "tagDeliver";
// In case of a forum post ensure to return the original post if author and forum are on the same machine
if (!empty($item['forum_mode'])) {
$author = Contact::getById($item['author-id'], ['nurl']);
if (!empty($author['nurl'])) {
$self = Contact::selectFirst(['uid'], ['nurl' => $author['nurl'], 'self' => true]);
if (!empty($self['uid'])) {
- $item = Item::selectFirst([], ['uri-id' => $item['uri-id'], 'uid' => $self['uid']]);
+ $forum_item = Item::selectFirst([], ['uri-id' => $item['uri-id'], 'uid' => $self['uid']]);
+ if (DBA::isResult($item)) {
+ $item = $forum_item;
+ }
}
}
}
- */
+
+ if (empty($item['uri-id'])) {
+ Logger::warning('Item without uri-id', ['item' => $item]);
+ return false;
+ }
if (empty($type)) {
$condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
$data = [];
}
- $data['id'] = $item['uri'] . '/' . $type;
+ if (($item['gravity'] == GRAVITY_ACTIVITY) && ($type != 'Undo')) {
+ $data['id'] = $item['uri'];
+ } else {
+ $data['id'] = $item['uri'] . '/' . $type;
+ }
+
$data['type'] = $type;
if (($type != 'Announce') || ($item['gravity'] != GRAVITY_PARENT)) {
$attachments[] = $attachment;
}
*/
- $arr = explode('[/attach],', $item['attach']);
- if (count($arr)) {
- foreach ($arr as $r) {
- $matches = false;
- $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
- if ($cnt) {
- $attributes = ['type' => 'Document',
- 'mediaType' => $matches[3],
- 'url' => $matches[1],
- 'name' => null];
-
- if (trim($matches[4]) != '') {
- $attributes['name'] = trim($matches[4]);
- }
-
- $attachments[] = $attributes;
- }
- }
+ foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
+ $attachments[] = ['type' => 'Document',
+ 'mediaType' => $attachment['mimetype'],
+ 'url' => $attachment['url'],
+ 'name' => $attachment['description']];
}
if ($type != 'Note') {
return $attachments;
}
- // Simplify image codes
- $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
-
- // Grab all pictures without alternative descriptions and create attachments out of them
- if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
- foreach ($pictures[1] as $picture) {
- $imgdata = Images::getInfoFromURLCached($picture);
- if ($imgdata) {
- $attachments[] = ['type' => 'Document',
- 'mediaType' => $imgdata['mime'],
- 'url' => $picture,
- 'name' => null];
- }
- }
- }
-
- // Grab all pictures with alternative description and create attachments out of them
- if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
- foreach ($pictures as $picture) {
- $imgdata = Images::getInfoFromURLCached($picture[1]);
- if ($imgdata) {
- $attachments[] = ['type' => 'Document',
- 'mediaType' => $imgdata['mime'],
- 'url' => $picture[1],
- 'name' => $picture[2]];
- }
- }
+ foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]) as $attachment) {
+ $attachments[] = ['type' => 'Document',
+ 'mediaType' => $attachment['mimetype'],
+ 'url' => $attachment['url'],
+ 'name' => $attachment['description']];
}
return $attachments;
return '';
}
- $data = Contact::getByURL($match[1], false, ['url', 'nick']);
+ $data = Contact::getByURL($match[1], false, ['url', 'alias', 'nick']);
if (empty($data['nick'])) {
return $match[0];
}
- return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
+ return '[url=' . ($data['alias'] ?: $data['url']) . ']@' . $data['nick'] . '[/url]';
}
/**
$body = $item['body'];
- if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
- $body = self::prependMentions($body, $item['uri-id']);
- }
-
if ($type == 'Note') {
- $body = self::removePictures($body);
+ $body = $item['raw-body'] ?? self::removePictures($body);
} elseif (($type == 'Article') && empty($data['summary'])) {
$data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000));
}
+ if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
+ $body = self::prependMentions($body, $item['uri-id'], $item['author-link']);
+ }
+
if ($type == 'Event') {
$data = array_merge($data, self::createEvent($item));
} else {
*/
public static function isAnnounce($item)
{
- if ($item['verb'] == Activity::ANNOUNCE) {
+ if (!empty($item['verb']) && ($item['verb'] == Activity::ANNOUNCE)) {
return true;
}
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
* @throws \Exception
+ * @return bool success
*/
public static function sendContactUndo($target, $cid, $uid)
{
$profile = APContact::getByURL($target);
if (empty($profile['inbox'])) {
Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
- return;
+ return false;
}
$object_id = self::activityIDFromContact($cid);
if (empty($object_id)) {
- return;
+ return false;
}
$id = DI::baseUrl() . '/activity/' . System::createGUID();
Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
$signed = LDSignature::sign($data, $owner);
- HTTPSignature::transmit($signed, $profile['inbox'], $uid);
+ return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
}
- private static function prependMentions($body, int $uriid)
+ private static function prependMentions($body, int $uriid, string $authorLink)
{
$mentions = [];
&& $profile['contact-type'] != Contact::TYPE_COMMUNITY
&& !strstr($body, $profile['addr'])
&& !strstr($body, $tag['url'])
+ && $tag['url'] !== $authorLink
) {
$mentions[] = '@[url=' . $tag['url'] . ']' . $profile['nick'] . '[/url]';
}