Merge pull request #12782 from annando/c2s-2

C2S: Improve C2S-API, fix inbox endpoint
This commit is contained in:
Hypolite Petovan 2023-02-08 15:30:00 -05:00 committed by GitHub
commit ffa44b8dc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 101 additions and 54 deletions

View file

@ -148,13 +148,14 @@ class Photo
* on success, "no sign" image info, if user has no permission, * on success, "no sign" image info, if user has no permission,
* false if photo does not exists * false if photo does not exists
* *
* @param string $resourceid Rescource ID of the photo * @param string $resourceid Rescource ID of the photo
* @param integer $scale Scale of the photo. Defaults to 0 * @param integer $scale Scale of the photo. Defaults to 0
* @param integer $visitor_uid UID of the visitor
* *
* @return boolean|array * @return boolean|array
* @throws \Exception * @throws \Exception
*/ */
public static function getPhoto(string $resourceid, int $scale = 0) public static function getPhoto(string $resourceid, int $scale = 0, int $visitor_uid = 0)
{ {
$r = self::selectFirst(['uid'], ['resource-id' => $resourceid]); $r = self::selectFirst(['uid'], ['resource-id' => $resourceid]);
if (!DBA::isResult($r)) { if (!DBA::isResult($r)) {
@ -165,7 +166,11 @@ class Photo
$accessible = $uid ? (bool)DI::pConfig()->get($uid, 'system', 'accessible-photos', false) : false; $accessible = $uid ? (bool)DI::pConfig()->get($uid, 'system', 'accessible-photos', false) : false;
$sql_acl = Security::getPermissionsSQLByUserId($uid, $accessible); if (!empty($visitor_uid) && ($uid == $visitor_uid)) {
$sql_acl = '';
} else {
$sql_acl = Security::getPermissionsSQLByUserId($uid, $accessible);
}
$conditions = ["`resource-id` = ? AND `scale` <= ? " . $sql_acl, $resourceid, $scale]; $conditions = ["`resource-id` = ? AND `scale` <= ? " . $sql_acl, $resourceid, $scale];
$params = ['order' => ['scale' => true]]; $params = ['order' => ['scale' => true]];

View file

@ -29,7 +29,6 @@ use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Model\User;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Protocol\ActivityPub; use Friendica\Protocol\ActivityPub;
use Friendica\Util\HTTPSignature; use Friendica\Util\HTTPSignature;

View file

@ -51,7 +51,7 @@ class Outbox extends BaseApi
} }
$requester = HTTPSignature::getSigner('', $_SERVER); $requester = HTTPSignature::getSigner('', $_SERVER);
$outbox = ActivityPub\Transmitter::getOutbox($owner, $page, $request['max_id'] ?? null, $requester); $outbox = ActivityPub\Transmitter::getOutbox($owner, $uid, $page, $request['max_id'] ?? null, $requester);
System::jsonExit($outbox, 'application/activity+json'); System::jsonExit($outbox, 'application/activity+json');
} }

View file

@ -49,7 +49,7 @@ use Friendica\Worker\UpdateContact;
/** /**
* Photo Module * Photo Module
*/ */
class Photo extends BaseModule class Photo extends BaseApi
{ {
/** /**
* Module initializer * Module initializer
@ -149,7 +149,7 @@ class Photo extends BaseModule
} }
} }
$photo = MPhoto::getPhoto($photoid, $scale); $photo = MPhoto::getPhoto($photoid, $scale, self::getCurrentUserID());
if ($photo === false) { if ($photo === false) {
throw new HTTPException\NotFoundException(DI::l10n()->t('The Photo with id %s is not available.', $photoid)); throw new HTTPException\NotFoundException(DI::l10n()->t('The Photo with id %s is not available.', $photoid));
} }
@ -282,7 +282,7 @@ class Photo extends BaseModule
} }
if (Network::isLocalLink($url) && preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $url, $matches)) { if (Network::isLocalLink($url) && preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $url, $matches)) {
return MPhoto::getPhoto($matches[1], $matches[2]); return MPhoto::getPhoto($matches[1], $matches[2], self::getCurrentUserID());
} }
return MPhoto::createPhotoForExternalResource($url, (int)DI::userSession()->getLocalUserId(), $media['mimetype'] ?? '', $media['blurhash'], $width, $height); return MPhoto::createPhotoForExternalResource($url, (int)DI::userSession()->getLocalUserId(), $media['mimetype'] ?? '', $media['blurhash'], $width, $height);
@ -293,7 +293,7 @@ class Photo extends BaseModule
} }
if (Network::isLocalLink($media['url']) && preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $media['url'], $matches)) { if (Network::isLocalLink($media['url']) && preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $media['url'], $matches)) {
return MPhoto::getPhoto($matches[1], $matches[2]); return MPhoto::getPhoto($matches[1], $matches[2], self::getCurrentUserID());
} }
return MPhoto::createPhotoForExternalResource($media['url'], (int)DI::userSession()->getLocalUserId(), $media['mimetype'], $media['blurhash'], $media['width'], $media['height']); return MPhoto::createPhotoForExternalResource($media['url'], (int)DI::userSession()->getLocalUserId(), $media['mimetype'], $media['blurhash'], $media['width'], $media['height']);

View file

@ -41,7 +41,6 @@ use Friendica\Model\User;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub; use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Diaspora;
use Friendica\Protocol\Relay; use Friendica\Protocol\Relay;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\HTTPSignature; use Friendica\Util\HTTPSignature;
@ -115,8 +114,8 @@ class Transmitter
return false; return false;
} }
$activity_id = ActivityPub\Transmitter::activityIDFromContact($contact['id']); $activity_id = self::activityIDFromContact($contact['id']);
$success = ActivityPub\Transmitter::sendActivity('Follow', $url, 0, $activity_id); $success = self::sendActivity('Follow', $url, 0, $activity_id);
if ($success) { if ($success) {
Contact::update(['rel' => Contact::FRIEND], ['id' => $contact['id']]); Contact::update(['rel' => Contact::FRIEND], ['id' => $contact['id']]);
} }
@ -243,6 +242,7 @@ class Transmitter
* Public posts for the given owner * Public posts for the given owner
* *
* @param array $owner Owner array * @param array $owner Owner array
* @param integer $uid User id
* @param integer $page Page number * @param integer $page Page number
* @param integer $max_id Maximum ID * @param integer $max_id Maximum ID
* @param string $requester URL of requesting account * @param string $requester URL of requesting account
@ -251,7 +251,7 @@ class Transmitter
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
public static function getOutbox(array $owner, int $page = null, int $max_id = null, string $requester = ''): array public static function getOutbox(array $owner, int $uid, int $page = null, int $max_id = null, string $requester = ''): array
{ {
$condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'private' => [Item::PUBLIC, Item::UNLISTED]]; $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'private' => [Item::PUBLIC, Item::UNLISTED]];
@ -267,7 +267,7 @@ class Transmitter
} }
$condition = array_merge($condition, [ $condition = array_merge($condition, [
'uid' => $owner['uid'], 'uid' => $owner['uid'],
'author-id' => Contact::getIdForURL($owner['url'], 0, false), 'author-id' => Contact::getIdForURL($owner['url'], 0, false),
'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT],
'network' => Protocol::FEDERATED, 'network' => Protocol::FEDERATED,
@ -279,14 +279,14 @@ class Transmitter
$apcontact = APContact::getByURL($owner['url']); $apcontact = APContact::getByURL($owner['url']);
return self::getCollection($condition, DI::baseUrl() . '/outbox/' . $owner['nickname'], $page, $max_id, null, $apcontact['statuses_count']); return self::getCollection($condition, DI::baseUrl() . '/outbox/' . $owner['nickname'], $page, $max_id, $uid, $apcontact['statuses_count']);
} }
public static function getInbox(int $uid, int $page = null, int $max_id = null) public static function getInbox(int $uid, int $page = null, int $max_id = null)
{ {
$owner = User::getOwnerDataById($uid); $owner = User::getOwnerDataById($uid);
$condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], [Protocol::ACTIVITYPUB, Protocol::DFRN], 'uid' => $uid]; $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN], 'uid' => $uid];
return self::getCollection($condition, DI::baseUrl() . '/inbox/' . $owner['nickname'], $page, $max_id, $uid, null); return self::getCollection($condition, DI::baseUrl() . '/inbox/' . $owner['nickname'], $page, $max_id, $uid, null);
} }
@ -527,40 +527,42 @@ class Transmitter
'owner' => $owner['url'], 'owner' => $owner['url'],
'publicKeyPem' => $owner['pubkey']]; 'publicKeyPem' => $owner['pubkey']];
$data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox']; $data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
$data['icon'] = ['type' => 'Image', 'url' => User::getAvatarUrl($owner)]; if ($uid != 0) {
$data['icon'] = ['type' => 'Image', 'url' => User::getAvatarUrl($owner)];
$resourceid = Photo::ridFromURI($owner['photo']); $resourceid = Photo::ridFromURI($owner['photo']);
if (!empty($resourceid)) {
$photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
if (!empty($photo['type'])) {
$data['icon']['mediaType'] = $photo['type'];
}
}
if (!empty($owner['header'])) {
$data['image'] = ['type' => 'Image', 'url' => Contact::getHeaderUrlForId($owner['id'], '', $owner['updated'])];
$resourceid = Photo::ridFromURI($owner['header']);
if (!empty($resourceid)) { if (!empty($resourceid)) {
$photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]); $photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
if (!empty($photo['type'])) { if (!empty($photo['type'])) {
$data['image']['mediaType'] = $photo['type']; $data['icon']['mediaType'] = $photo['type'];
} }
} }
}
$custom_fields = []; if (!empty($owner['header'])) {
$data['image'] = ['type' => 'Image', 'url' => Contact::getHeaderUrlForId($owner['id'], '', $owner['updated'])];
foreach (DI::profileField()->selectByContactId(0, $uid) as $profile_field) { $resourceid = Photo::ridFromURI($owner['header']);
$custom_fields[] = [ if (!empty($resourceid)) {
'type' => 'PropertyValue', $photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
'name' => $profile_field->label, if (!empty($photo['type'])) {
'value' => BBCode::convertForUriId($owner['uri-id'], $profile_field->value) $data['image']['mediaType'] = $photo['type'];
]; }
}; }
}
if (!empty($custom_fields)) { $custom_fields = [];
$data['attachment'] = $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(); $data['generator'] = self::getService();
@ -569,6 +571,34 @@ class Transmitter
return $data; return $data;
} }
/**
* Get a minimal actror array for the C2S API
*
* @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;
}
/** /**
* @param string $username * @param string $username
* @return array * @return array
@ -1325,7 +1355,7 @@ class Transmitter
*/ */
private static function createActivityFromArray(array $item, bool $object_mode = false, $api_mode = false) private static function createActivityFromArray(array $item, bool $object_mode = false, $api_mode = false)
{ {
if (!$item['deleted'] && $item['network'] == Protocol::ACTIVITYPUB) { if (!$api_mode && !$item['deleted'] && $item['network'] == Protocol::ACTIVITYPUB) {
$data = Post\Activity::getByURIId($item['uri-id']); $data = Post\Activity::getByURIId($item['uri-id']);
if (!$item['origin'] && !empty($data)) { if (!$item['origin'] && !empty($data)) {
if (!$object_mode) { if (!$object_mode) {
@ -1368,9 +1398,17 @@ class Transmitter
$data['type'] = $type; $data['type'] = $type;
if (($type != 'Announce') || ($item['gravity'] != Item::GRAVITY_PARENT)) { if (($type != 'Announce') || ($item['gravity'] != Item::GRAVITY_PARENT)) {
$data['actor'] = $item['author-link']; $link = $item['author-link'];
$id = $item['author-id'];
} else { } else {
$data['actor'] = $item['owner-link']; $link = $item['owner-link'];
$id = $item['owner-id'];
}
if ($api_mode) {
$data['actor'] = self::getActorArrayByCid($id);
} else {
$data['actor'] = $link;
} }
$data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM); $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
@ -1380,15 +1418,14 @@ class Transmitter
$data = array_merge($data, self::createPermissionBlockForItem($item, false)); $data = array_merge($data, self::createPermissionBlockForItem($item, false));
if (in_array($data['type'], ['Create', 'Update', 'Delete'])) { if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
$data['object'] = self::createNote($item); $data['object'] = self::createNote($item, $api_mode);
$data['published'] = DateTimeFormat::utcNow(DateTimeFormat::ATOM);
} elseif ($data['type'] == 'Add') { } elseif ($data['type'] == 'Add') {
$data = self::createAddTag($item, $data); $data = self::createAddTag($item, $data);
} elseif ($data['type'] == 'Announce') { } elseif ($data['type'] == 'Announce') {
if ($item['verb'] == ACTIVITY::ANNOUNCE) { if ($item['verb'] == ACTIVITY::ANNOUNCE) {
$data['object'] = $item['thr-parent']; $data['object'] = $item['thr-parent'];
} else { } else {
$data = self::createAnnounce($item, $data); $data = self::createAnnounce($item, $data, $api_mode);
} }
} elseif ($data['type'] == 'Follow') { } elseif ($data['type'] == 'Follow') {
$data['object'] = $item['parent-uri']; $data['object'] = $item['parent-uri'];
@ -1646,11 +1683,12 @@ class Transmitter
* Creates a note/article object array * Creates a note/article object array
* *
* @param array $item * @param array $item
* @param bool $api_mode
* @return array with the object data * @return array with the object data
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
public static function createNote(array $item): array public static function createNote(array $item, bool $api_mode = false): array
{ {
if (empty($item)) { if (empty($item)) {
return []; return [];
@ -1710,7 +1748,11 @@ class Transmitter
} }
$data['url'] = $link ?? $item['plink']; $data['url'] = $link ?? $item['plink'];
$data['attributedTo'] = $item['author-link']; if ($api_mode) {
$data['attributedTo'] = self::getActorArrayByCid($item['author-id']);
} else {
$data['attributedTo'] = $item['author-link'];
}
$data['sensitive'] = self::isSensitive($item['uri-id']); $data['sensitive'] = self::isSensitive($item['uri-id']);
if (!empty($item['conversation']) && ($item['conversation'] != './')) { if (!empty($item['conversation']) && ($item['conversation'] != './')) {
@ -1882,17 +1924,18 @@ class Transmitter
* *
* @param array $item Item array * @param array $item Item array
* @param array $activity activity data * @param array $activity activity data
* @param bool $api_mode
* @return array with activity data * @return array with activity data
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
private static function createAnnounce(array $item, array $activity): array private static function createAnnounce(array $item, array $activity, bool $api_mode = false): array
{ {
$orig_body = $item['body']; $orig_body = $item['body'];
$announce = self::getAnnounceArray($item); $announce = self::getAnnounceArray($item);
if (empty($announce)) { if (empty($announce)) {
$activity['type'] = 'Create'; $activity['type'] = 'Create';
$activity['object'] = self::createNote($item); $activity['object'] = self::createNote($item, $api_mode);
return $activity; return $activity;
} }
@ -1906,7 +1949,7 @@ class Transmitter
// Quote // Quote
$activity['type'] = 'Create'; $activity['type'] = 'Create';
$item['body'] = $announce['comment'] . "\n" . $announce['object']['plink']; $item['body'] = $announce['comment'] . "\n" . $announce['object']['plink'];
$activity['object'] = self::createNote($item); $activity['object'] = self::createNote($item, $api_mode);
/// @todo Finally descide how to implement this in AP. This is a possible way: /// @todo Finally descide how to implement this in AP. This is a possible way:
$activity['object']['attachment'][] = self::createNote($announce['object']); $activity['object']['attachment'][] = self::createNote($announce['object']);