<?php
/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
use Friendica\Content\Feature;
use Friendica\Content\Text\BBCode;
-use Friendica\Content\Text\Plaintext;
-use Friendica\Core\Cache\Duration;
+use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
use Friendica\Core\System;
use Friendica\Model\Conversation;
use Friendica\Model\GServer;
use Friendica\Model\Item;
-use Friendica\Model\ItemURI;
-use Friendica\Model\Profile;
use Friendica\Model\Photo;
use Friendica\Model\Post;
+use Friendica\Model\Profile;
use Friendica\Model\Tag;
use Friendica\Model\User;
+use Friendica\Network\HTTPException;
use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Relay;
$activity_id = ActivityPub\Transmitter::activityIDFromContact($contact['id']);
$success = ActivityPub\Transmitter::sendActivity('Follow', $url, 0, $activity_id);
if ($success) {
- DBA::update('contact', ['rel' => Contact::FRIEND], ['id' => $contact['id']]);
+ Contact::update(['rel' => Contact::FRIEND], ['id' => $contact['id']]);
}
return $success;
$success = self::sendContactUndo($url, $contact['id'], 0);
if ($success || $force) {
- DBA::update('contact', ['rel' => Contact::NOTHING], ['id' => $contact['id']]);
+ Contact::update(['rel' => Contact::NOTHING], ['id' => $contact['id']]);
}
return $success;
'pending' => false,
'blocked' => false,
];
- $condition = DBA::buildCondition($parameters);
- $sql = "SELECT COUNT(*) as `count`
- FROM `contact`
- JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url`
- " . $condition;
+ $condition = DBA::mergeConditions($parameters, ["`url` IN (SELECT `url` FROM `apcontact`)"]);
- $contacts = DBA::fetchFirst($sql, ...$parameters);
+ $total = DBA::count('contact', $condition);
$modulePath = '/' . $module . '/';
$data = ['@context' => ActivityPub::CONTEXT];
$data['id'] = DI::baseUrl() . $modulePath . $owner['nickname'];
$data['type'] = 'OrderedCollection';
- $data['totalItems'] = $contacts['count'];
+ $data['totalItems'] = $total;
// When we hide our friends we will only show the pure number but don't allow more.
$profile = Profile::getByUID($owner['uid']);
$data['type'] = 'OrderedCollectionPage';
$list = [];
- $sql = "SELECT `contact`.`url`
- FROM `contact`
- JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url`
- " . $condition . "
- LIMIT ?, ?";
-
- $parameters[] = ($page - 1) * 100;
- $parameters[] = 100;
-
- $contacts = DBA::p($sql, ...$parameters);
+ $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
while ($contact = DBA::fetch($contacts)) {
$list[] = $contact['url'];
}
*/
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]];
+ $condition = ['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 = ['psid' => array_merge($permissionSets->column('id'),
+ [DI::permissionSet()->selectPublicForUser($owner['uid'])])];
}
}
}
$condition = array_merge($condition,
- ['author-id' => $public_contact,
- 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
- 'deleted' => false, 'visible' => true]);
+ ['uid' => $owner['uid'],
+ 'author-id' => Contact::getIdForURL($owner['url'], 0, false),
+ 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
+ 'network' => Protocol::FEDERATED,
+ 'parent-network' => Protocol::FEDERATED,
+ 'origin' => true,
+ 'deleted' => false,
+ 'visible' => true]);
$count = Post::count($condition);
$data['type'] = 'OrderedCollectionPage';
$list = [];
- $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
-
$items = Post::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
while ($item = Post::fetch($items)) {
$activity = self::createActivityFromItem($item['id'], true);
/**
* Return the ActivityPub profile of the given user
*
- * @param integer $uid User ID
+ * @param int $uid User ID
* @return array with profile data
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @throws HTTPException\NotFoundException
+ * @throws HTTPException\InternalServerErrorException
*/
- public static function getProfile($uid)
+ public static function getProfile(int $uid): array
{
$owner = User::getOwnerDataById($uid);
+ 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.');
+ }
$data = ['@context' => ActivityPub::CONTEXT];
$data['id'] = $owner['url'];
}
if (!empty($owner['about'])) {
- $data['summary'] = BBCode::convert($owner['about'], false);
+ $data['summary'] = BBCode::convertForUriId($owner['uri-id'] ?? 0, $owner['about'], BBCode::EXTERNAL);
+ }
+
+ 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'];
+ }
}
$data['url'] = $owner['url'];
$data['manuallyApprovesFollowers'] = in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
- $data['discoverable'] = $owner['net-publish'];
+ $data['discoverable'] = (bool)$owner['net-publish'];
$data['publicKey'] = ['id' => $owner['url'] . '#main-key',
'owner' => $owner['url'],
'publicKeyPem' => $owner['pubkey']];
$data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
- $data['icon'] = ['type' => 'Image', 'url' => Contact::getAvatarUrlForId($owner['id'], '', $owner['updated'])];
+ $data['icon'] = ['type' => 'Image', 'url' => User::getAvatarUrl($owner)];
$resourceid = Photo::ridFromURI($owner['photo']);
if (!empty($resourceid)) {
}
}
+ $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)
+ ];
+ };
+
+ if (!empty($custom_fields)) {
+ $data['attachment'] = $custom_fields;
+ }
+
$data['generator'] = self::getService();
// tags: https://kitty.town/@inmysocks/100656097926961126.json
return [];
}
+ $permissions = [
+ 'to' => [],
+ 'cc' => [],
+ 'bto' => [],
+ 'bcc' => [],
+ ];
+
$activity = json_decode($conversation['source'], true);
$actor = JsonLD::fetchElement($activity, 'actor', 'id');
}
$reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'from-url'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
+ if (!DBA::isResult($reply)) {
+ $reply = $mail;
+ }
// Making the post more compatible for Mastodon by:
// - Making it a note and not an article (no title)
$urls = [];
foreach ($uriids as $uriid) {
- foreach (Post\Media::getByURIId($uriid, [Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) {
- if (in_array($attachment['url'], $urls)) {
- continue;
- }
- $urls[] = $attachment['url'];
-
- $attach = ['type' => 'Document',
- 'mediaType' => $attachment['mimetype'],
- 'url' => $attachment['url'],
- 'name' => $attachment['description']];
-
- if (!empty($attachment['height'])) {
- $attach['height'] = $attachment['height'];
- }
-
- if (!empty($attachment['width'])) {
- $attach['width'] = $attachment['width'];
- }
-
- if (!empty($attachment['preview'])) {
- $attach['image'] = $attachment['preview'];
- }
-
- $attachments[] = $attach;
- }
- }
-
- if ($type != 'Note') {
- return $attachments;
- }
-
- foreach ($uriids as $uriid) {
- foreach (Post\Media::getByURIId($uriid, [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]) as $attachment) {
+ foreach (Post\Media::getByURIId($uriid, [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO, Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) {
if (in_array($attachment['url'], $urls)) {
continue;
}
$attachments[] = $attach;
}
- // Currently deactivated, since it creates side effects on Mastodon and Pleroma.
- // It will be activated, once this cleared.
- /*
- foreach (Post\Media::getByURIId($uriid, [Post\Media::HTML]) as $attachment) {
- if (in_array($attachment['url'], $urls)) {
- continue;
- }
- $urls[] = $attachment['url'];
-
- $attachments[] = ['type' => 'Page',
- 'mediaType' => $attachment['mimetype'],
- 'url' => $attachment['url'],
- 'name' => $attachment['description']];
- }*/
}
return $attachments;
}
- /**
- * Callback function to replace a Friendica style mention in a mention that is used on AP
- *
- * @param array $match Matching values for the callback
- * @return string Replaced mention
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- */
- private static function mentionCallback($match)
- {
- if (empty($match[1])) {
- return '';
- }
-
- $data = Contact::getByURL($match[1], false, ['url', 'alias', 'nick']);
- if (empty($data['nick'])) {
- return $match[0];
- }
-
- return '[url=' . $data['url'] . ']@' . $data['nick'] . '[/url]';
- }
-
/**
* Callback function to replace a Friendica style mention in a mention for a summary
*
{
$event = [];
$event['name'] = $item['event-summary'];
- $event['content'] = BBCode::convert($item['event-desc'], false, BBCode::ACTIVITYPUB);
- $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
+ $event['content'] = BBCode::convertForUriId($item['uri-id'], $item['event-desc'], BBCode::ACTIVITYPUB);
+ $event['startTime'] = DateTimeFormat::utc($item['event-start'], 'c');
if (!$item['event-nofinish']) {
- $event['endTime'] = DateTimeFormat::utc($item['event-finish'] . '+00:00', DateTimeFormat::ATOM);
+ $event['endTime'] = DateTimeFormat::utc($item['event-finish'], 'c');
}
if (!empty($item['event-location'])) {
$event['location'] = self::createLocation($item);
}
- $event['dfrn:adjust'] = (bool)$item['event-adjust'];
+ // 2021.12: Backward compatibility value, all the events now "adjust" to the viewer timezone
+ $event['dfrn:adjust'] = true;
return $event;
}
if ($type == 'Event') {
$data = array_merge($data, self::createEvent($item));
} else {
- $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
- $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
+ $body = BBCode::setMentionsToNicknames($body);
- $data['content'] = BBCode::convert($body, false, BBCode::ACTIVITYPUB);
+ $data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);
}
// The regular "content" field does contain a minimized HTML. This is done since systems like
// The contentMap does contain the unmodified HTML.
$language = self::getLanguage($item);
if (!empty($language)) {
- $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
- $richbody = preg_replace_callback($regexp, ['self', 'mentionCallback'], $item['body']);
+ $richbody = BBCode::setMentionsToNicknames($item['body'] ?? '');
$richbody = BBCode::removeAttachment($richbody);
- $data['contentMap'][$language] = BBCode::convert($richbody, false, BBCode::EXTERNAL);
+ $data['contentMap'][$language] = BBCode::convertForUriId($item['uri-id'], $richbody, BBCode::EXTERNAL);
}
$data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
{
$owner = User::getOwnerDataById($uid);
- $suggestion = DI::fsuggest()->getById($suggestion_id);
+ $suggestion = DI::fsuggest()->selectOneById($suggestion_id);
$data = ['@context' => ActivityPub::CONTEXT,
'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
$signed = LDSignature::sign($data, $owner);
- Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
+ Logger::info('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub');
return HTTPSignature::transmit($signed, $inbox, $uid);
}
$signed = LDSignature::sign($data, $owner);
- Logger::log('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
+ Logger::info('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub');
return HTTPSignature::transmit($signed, $inbox, $uid);
}
$signed = LDSignature::sign($data, $owner);
- Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
+ Logger::info('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub');
return HTTPSignature::transmit($signed, $inbox, $uid);
}
* @param string $inbox Target inbox
*
* @return boolean was the transmission successful?
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @throws HTTPException\InternalServerErrorException
+ * @throws HTTPException\NotFoundException
* @throws \ImagickException
*/
- public static function sendProfileUpdate($uid, $inbox)
+ public static function sendProfileUpdate(int $uid, string $inbox): bool
{
$owner = User::getOwnerDataById($uid);
$profile = APContact::getByURL($owner['url']);
$signed = LDSignature::sign($data, $owner);
- Logger::log('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
+ Logger::info('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub');
return HTTPSignature::transmit($signed, $inbox, $uid);
}
'instrument' => self::getService(),
'to' => [$profile['url']]];
- Logger::log('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
+ Logger::info('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid);
$signed = LDSignature::sign($data, $owner);
return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
$condition = ['verb' => Activity::FOLLOW, 'uid' => 0, 'parent-uri' => $object,
'author-id' => Contact::getPublicIdByUserId($uid)];
if (Post::exists($condition)) {
- Logger::log('Follow for ' . $object . ' for user ' . $uid . ' does already exist.', Logger::DEBUG);
+ Logger::info('Follow for ' . $object . ' for user ' . $uid . ' does already exist.');
return false;
}
'instrument' => self::getService(),
'to' => [$profile['url']]];
- Logger::log('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
+ Logger::info('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid);
$signed = LDSignature::sign($data, $owner);
return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
* @param string $target Target profile
* @param $id
* @param integer $uid User ID
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @return bool Operation success
+ * @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
- public static function sendContactReject($target, $id, $uid)
+ public static function sendContactReject($target, $id, $uid): bool
{
$profile = APContact::getByURL($target);
if (empty($profile['inbox'])) {
Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
- return;
+ return false;
}
$owner = User::getOwnerDataById($uid);
Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id);
$signed = LDSignature::sign($data, $owner);
- HTTPSignature::transmit($signed, $profile['inbox'], $uid);
+ return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
}
/**
'instrument' => self::getService(),
'to' => [$profile['url']]];
- Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
+ Logger::info('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id);
$signed = LDSignature::sign($data, $owner);
return HTTPSignature::transmit($signed, $profile['inbox'], $uid);