1
0
Fork 0

Some code cleanup

This commit is contained in:
Michael 2018-09-16 05:49:13 +00:00
parent a7f5c1f4c6
commit 1f98414bdd

View file

@ -131,7 +131,7 @@ class ActivityPub
'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']]; 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
$data['summary'] = $contact['about']; $data['summary'] = $contact['about'];
$data['url'] = $contact['url']; $data['url'] = $contact['url'];
$data['manuallyApprovesFollowers'] = false; $data['manuallyApprovesFollowers'] = false; /// @todo
$data['publicKey'] = ['id' => $contact['url'] . '#main-key', $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
'owner' => $contact['url'], 'owner' => $contact['url'],
'publicKeyPem' => $user['pubkey']]; 'publicKeyPem' => $user['pubkey']];
@ -533,28 +533,18 @@ class ActivityPub
} }
} }
/*
// To-Do // To-Do
unset($data['type']); // type, manuallyApprovesFollowers
unset($data['manuallyApprovesFollowers']);
// Unhandled // Unhandled
unset($data['@context']); // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
unset($data['tag']);
unset($data['attachment']); // Unhandled from Misskey
unset($data['image']); // sharedInbox, isCat
unset($data['nomadicLocations']);
unset($data['signature']); // Unhandled from Kroeg
unset($data['following']); // kroeg:blocks, updated
unset($data['followers']);
unset($data['featured']);
unset($data['movedTo']);
unset($data['liked']);
unset($data['sharedInbox']); // Misskey
unset($data['isCat']); // Misskey
unset($data['kroeg:blocks']); // Kroeg
unset($data['updated']); // Kroeg
*/
return $profile; return $profile;
} }
@ -600,7 +590,72 @@ class ActivityPub
} }
} }
function processActivity($activity, $body = '', $uid = null) private static function prepareObjectData($activity, $uid)
{
$actor = self::processElement($activity, 'actor', 'id');
if (empty($actor)) {
logger('Empty actor', LOGGER_DEBUG);
return [];
}
// Fetch all receivers from to, cc, bto and bcc
$receivers = self::getReceivers($activity);
// When it is a delivery to a personal inbox we add that user to the receivers
if (!empty($uid)) {
$owner = User::getOwnerDataById($uid);
$additional = [$owner['url'] => $uid];
$receivers = array_merge($receivers, $additional);
}
logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
if (is_string($activity['object'])) {
$object_url = $activity['object'];
} elseif (!empty($activity['object']['id'])) {
$object_url = $activity['object']['id'];
} else {
logger('No object found', LOGGER_DEBUG);
return [];
}
// Fetch the content only on activities where this matters
if (in_array($activity['type'], ['Create', 'Update', 'Announce'])) {
$object_data = self::fetchObject($object_url, $activity['object']);
if (empty($object_data)) {
logger("Object data couldn't be processed", LOGGER_DEBUG);
return [];
}
} elseif ($activity['type'] == 'Accept') {
$object_data = [];
$object_data['object_type'] = self::processElement($activity, 'object', 'type');
$object_data['object'] = self::processElement($activity, 'object', 'actor');
} elseif ($activity['type'] == 'Undo') {
$object_data = [];
$object_data['object_type'] = self::processElement($activity, 'object', 'type');
$object_data['object'] = self::processElement($activity, 'object', 'object');
} elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
// Create a mostly empty array out of the activity data (instead of the object).
// This way we later don't have to check for the existence of ech individual array element.
$object_data = self::processCommonData($activity);
$object_data['name'] = $activity['type'];
$object_data['author'] = $activity['actor'];
$object_data['object'] = $object_url;
} elseif ($activity['type'] == 'Follow') {
$object_data['id'] = $activity['id'];
$object_data['object'] = $object_url;
}
$object_data = self::addActivityFields($object_data, $activity);
$object_data['type'] = $activity['type'];
$object_data['owner'] = $actor;
$object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
return $object_data;
}
private static function processActivity($activity, $body = '', $uid = null)
{ {
if (empty($activity['type'])) { if (empty($activity['type'])) {
logger('Empty type', LOGGER_DEBUG); logger('Empty type', LOGGER_DEBUG);
@ -618,118 +673,53 @@ class ActivityPub
} }
$actor = self::processElement($activity, 'actor', 'id');
if (empty($actor)) {
logger('Empty actor - 2', LOGGER_DEBUG);
return;
}
if (is_string($activity['object'])) {
$object_url = $activity['object'];
} elseif (!empty($activity['object']['id'])) {
$object_url = $activity['object']['id'];
} else {
logger('No object found', LOGGER_DEBUG);
return;
}
// ----------------------------------
/*
// unhandled
unset($activity['@context']);
unset($activity['id']);
// Non standard // Non standard
unset($activity['title']); // title, atomUri, context_id, statusnetConversationId
unset($activity['atomUri']);
unset($activity['context_id']);
unset($activity['statusnetConversationId']);
// To-Do? // To-Do?
unset($activity['context']); // context, location, signature;
unset($activity['location']);
unset($activity['signature']);
*/
// Fetch all receivers from to, cc, bto and bcc
$receivers = self::getReceivers($activity);
// When it is a delivery to a personal inbox we add that user to the receivers
if (!empty($uid)) {
$owner = User::getOwnerDataById($uid);
$additional = [$owner['url'] => $uid];
$receivers = array_merge($receivers, $additional);
}
logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
logger('Processing activity: ' . $activity['type'], LOGGER_DEBUG); logger('Processing activity: ' . $activity['type'], LOGGER_DEBUG);
// Fetch the content only on activities where this matters $object_data = self::prepareObjectData($activity, $uid);
if (in_array($activity['type'], ['Create', 'Update', 'Announce'])) { if (empty($object_data)) {
$item = self::fetchObject($object_url, $activity['object']); logger('No object data found', LOGGER_DEBUG);
if (empty($item)) { return;
logger("Object data couldn't be processed", LOGGER_DEBUG);
return;
}
} else {
if (in_array($activity['type'], ['Accept'])) {
$item['object'] = self::processElement($activity, 'object', 'actor', 'type', 'Follow');
} elseif (in_array($activity['type'], ['Undo'])) {
$item['object'] = self::processElement($activity, 'object', 'object', 'type', 'Follow');
} else {
$item['uri'] = $activity['id'];
$item['author'] = $activity['actor'];
$item['updated'] = $item['published'] = $activity['published'];
$item['uuid'] = '';
$item['name'] = $activity['type'];
$item['summary'] = '';
$item['content'] = '';
$item['location'] = '';
$item['tags'] = [];
$item['sensitive'] = false;
$item['service'] = '';
$item['attachments'] = [];
$item['conversation'] = '';
$item['object'] = $object_url;
}
$item['id'] = $activity['id'];
$item['receiver'] = [];
$item['type'] = $activity['type'];
} }
$item = self::addActivityFields($item, $activity);
$item['owner'] = $actor;
$item['receiver'] = array_merge($item['receiver'], $receivers);
switch ($activity['type']) { switch ($activity['type']) {
case 'Create': case 'Create':
case 'Update':
case 'Announce': case 'Announce':
self::createItem($item, $body); self::createItem($object_data, $body);
break; break;
case 'Like': case 'Like':
self::likeItem($item, $body); self::likeItem($object_data, $body);
break; break;
case 'Dislike': case 'Dislike':
break; break;
case 'Update':
break;
case 'Delete': case 'Delete':
break; break;
case 'Follow': case 'Follow':
self::followUser($item); self::followUser($object_data);
break; break;
case 'Accept': case 'Accept':
self::acceptFollowUser($item); if ($object_data['object_type'] == 'Follow') {
self::acceptFollowUser($object_data);
}
break; break;
case 'Undo': case 'Undo':
self::undoFollowUser($item); if ($object_data['object_type'] == 'Follow') {
self::undoFollowUser($object_data);
}
break; break;
default: default:
@ -769,24 +759,24 @@ class ActivityPub
return $receivers; return $receivers;
} }
private static function addActivityFields($item, $activity) private static function addActivityFields($object_data, $activity)
{ {
if (!empty($activity['published']) && empty($item['published'])) { if (!empty($activity['published']) && empty($object_data['published'])) {
$item['published'] = $activity['published']; $object_data['published'] = $activity['published'];
} }
if (!empty($activity['updated']) && empty($item['updated'])) { if (!empty($activity['updated']) && empty($object_data['updated'])) {
$item['updated'] = $activity['updated']; $object_data['updated'] = $activity['updated'];
} }
if (!empty($activity['inReplyTo']) && empty($item['parent-uri'])) { if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
$item['parent-uri'] = self::processElement($activity, 'inReplyTo', 'id'); $object_data['parent-uri'] = self::processElement($activity, 'inReplyTo', 'id');
} }
if (!empty($activity['instrument'])) { if (!empty($activity['instrument'])) {
$item['service'] = self::processElement($activity, 'instrument', 'name', 'type', 'Service'); $object_data['service'] = self::processElement($activity, 'instrument', 'name', 'type', 'Service');
} }
return $item; return $object_data;
} }
private static function fetchObject($object_url, $object = []) private static function fetchObject($object_url, $object = [])
@ -849,118 +839,86 @@ class ActivityPub
private static function processCommonData(&$object) private static function processCommonData(&$object)
{ {
if (empty($object['id']) || empty($object['attributedTo'])) { if (empty($object['id'])) {
return false; return false;
} }
$item = []; $object_data = [];
$item['type'] = $object['type']; $object_data['type'] = $object['type'];
$item['uri'] = $object['id']; $object_data['uri'] = $object['id'];
if (!empty($object['inReplyTo'])) { if (!empty($object['inReplyTo'])) {
$item['reply-to-uri'] = self::processElement($object, 'inReplyTo', 'id'); $object_data['reply-to-uri'] = self::processElement($object, 'inReplyTo', 'id');
} else { } else {
$item['reply-to-uri'] = $item['uri']; $object_data['reply-to-uri'] = $object_data['uri'];
} }
$item['published'] = defaults($object, 'published', null); $object_data['published'] = defaults($object, 'published', null);
$item['updated'] = defaults($object, 'updated', $item['published']); $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
if (empty($item['published']) && !empty($item['updated'])) { if (empty($object_data['published']) && !empty($object_data['updated'])) {
$item['published'] = $item['updated']; $object_data['published'] = $object_data['updated'];
} }
$item['uuid'] = defaults($object, 'uuid', null); $object_data['uuid'] = defaults($object, 'uuid', null);
$item['owner'] = $item['author'] = self::processElement($object, 'attributedTo', 'id'); $object_data['owner'] = $object_data['author'] = self::processElement($object, 'attributedTo', 'id');
$item['context'] = defaults($object, 'context', null); $object_data['context'] = defaults($object, 'context', null);
$item['conversation'] = defaults($object, 'conversation', null); $object_data['conversation'] = defaults($object, 'conversation', null);
$item['sensitive'] = defaults($object, 'sensitive', null); $object_data['sensitive'] = defaults($object, 'sensitive', null);
$item['name'] = defaults($object, 'title', null); $object_data['name'] = defaults($object, 'title', null);
$item['name'] = defaults($object, 'name', $item['name']); $object_data['name'] = defaults($object, 'name', $object_data['name']);
$item['summary'] = defaults($object, 'summary', null); $object_data['summary'] = defaults($object, 'summary', null);
$item['content'] = defaults($object, 'content', null); $object_data['content'] = defaults($object, 'content', null);
$item['source'] = defaults($object, 'source', null); $object_data['source'] = defaults($object, 'source', null);
$item['location'] = self::processElement($object, 'location', 'name', 'type', 'Place'); $object_data['location'] = self::processElement($object, 'location', 'name', 'type', 'Place');
$item['attachments'] = defaults($object, 'attachment', null); $object_data['attachments'] = defaults($object, 'attachment', null);
$item['tags'] = defaults($object, 'tag', null); $object_data['tags'] = defaults($object, 'tag', null);
$item['service'] = self::processElement($object, 'instrument', 'name', 'type', 'Service'); $object_data['service'] = self::processElement($object, 'instrument', 'name', 'type', 'Service');
$item['alternate-url'] = self::processElement($object, 'url', 'href'); $object_data['alternate-url'] = self::processElement($object, 'url', 'href');
$item['receiver'] = self::getReceivers($object); $object_data['receiver'] = self::getReceivers($object);
/*
// To-Do
unset($object['source']);
// Unhandled // Unhandled
unset($object['@context']); // @context, type, actor, signature, mediaType, duration, replies, icon
unset($object['type']);
unset($object['actor']);
unset($object['signature']);
unset($object['mediaType']);
unset($object['duration']);
unset($object['replies']);
unset($object['icon']);
// Also missing: // Also missing: (Defined in the standard, but currently unused)
audience, preview, endTime, startTime, generator, image // audience, preview, endTime, startTime, generator, image
*/
return $item; return $object_data;
} }
private static function processNote($object) private static function processNote($object)
{ {
$item = []; $object_data = [];
/*
// To-Do? // To-Do?
unset($object['emoji']); // emoji, atomUri, inReplyToAtomUri
unset($object['atomUri']);
unset($object['inReplyToAtomUri']);
// Unhandled // Unhandled
unset($object['contentMap']); // contentMap, announcement_count, announcements, context_id, likes, like_count
unset($object['announcement_count']); // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
unset($object['announcements']);
unset($object['context_id']); return $object_data;
unset($object['likes']);
unset($object['like_count']);
unset($object['inReplyToStatusId']);
unset($object['shares']);
unset($object['quoteUrl']);
unset($object['statusnetConversationId']);
*/
return $item;
} }
private static function processArticle($object) private static function processArticle($object)
{ {
$item = []; $object_data = [];
return $item; return $object_data;
} }
private static function processVideo($object) private static function processVideo($object)
{ {
$item = []; $object_data = [];
/*
// To-Do? // To-Do?
unset($object['category']); // category, licence, language, commentsEnabled
unset($object['licence']);
unset($object['language']);
unset($object['commentsEnabled']);
// Unhandled // Unhandled
unset($object['views']); // views, waitTranscoding, state, support, subtitleLanguage
unset($object['waitTranscoding']); // likes, dislikes, shares, comments
unset($object['state']);
unset($object['support']); return $object_data;
unset($object['subtitleLanguage']);
unset($object['likes']);
unset($object['dislikes']);
unset($object['shares']);
unset($object['comments']);
*/
return $item;
} }
private static function processElement($array, $element, $key, $type = null, $type_value = null) private static function processElement($array, $element, $key, $type = null, $type_value = null)