Merge pull request #5904 from annando/ap-announce

AP: Announce will now work
This commit is contained in:
Hypolite Petovan 2018-10-13 20:17:14 -04:00 committed by GitHub
commit db0b848ae3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 97 additions and 31 deletions

View file

@ -46,7 +46,7 @@ if ($a->isMaxProcessesReached() || $a->isMaxLoadReached()) {
System::httpExit(503, ['title' => 'Error 503 - Service Temporarily Unavailable', 'description' => 'System is currently overloaded. Please try again later.']);
}
if (strstr($a->query_string, '.well-known/host-meta') and ($a->query_string != '.well-known/host-meta')) {
if (strstr($a->query_string, '.well-known/host-meta') && ($a->query_string != '.well-known/host-meta')) {
System::httpExit(404);
}

View file

@ -133,7 +133,7 @@ function crepair_content(App $a)
$remote_self_options = ['0' => L10n::t('No mirroring'), '2' => L10n::t('Mirror as my own posting')];
}
$update_profile = in_array($contact['network'], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]);
$update_profile = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]);
$tab_str = contacts_tab($a, $contact, 5);

View file

@ -212,8 +212,13 @@ class Processor
$item['content-warning'] = HTML::toBBCode($activity['summary']);
$item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
$item['location'] = $activity['location'];
if (!empty($item['latitude']) && !empty($item['longitude'])) {
$item['coord'] = $item['latitude'] . ' ' . $item['longitude'];
}
$item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
$item['app'] = $activity['service'];
$item['app'] = $activity['generator'];
$item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
$item['diaspora_signed_text'] = defaults($activity, 'diaspora:comment', '');

View file

@ -513,24 +513,15 @@ class Receiver
private static function addActivityFields($object_data, $activity)
{
if (!empty($activity['published']) && empty($object_data['published'])) {
$object_data['published'] = JsonLD::fetchElement($activity, 'published', '@value');
}
if (!empty($activity['updated']) && empty($object_data['updated'])) {
$object_data['updated'] = JsonLD::fetchElement($activity, 'updated', '@value');
$object_data['published'] = JsonLD::fetchElement($activity, 'as:published', '@value');
}
if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) {
$object_data['diaspora:guid'] = JsonLD::fetchElement($activity, 'diaspora:guid');
}
if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
$object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo');
}
$object_data['service'] = JsonLD::fetchElement($activity, 'as:instrument', 'as:name', '@type', 'as:Service');
if (!empty($activity['instrument'])) {
$object_data['service'] = JsonLD::fetchElement($activity, 'instrument', 'name', 'type', 'Service');
}
return $object_data;
}
@ -696,10 +687,13 @@ class Receiver
$object_data['content'] = JsonLD::fetchElement($object, 'as:content');
$object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/bbcode');
$object_data['location'] = JsonLD::fetchElement($object, 'as:location', 'as:name', '@type', 'as:Place');
$object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place');
$object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value');
$object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place');
$object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
$object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment'));
$object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag'));
// $object_data['service'] = JsonLD::fetchElement($object, 'instrument', 'name', 'type', 'Service'); // todo
$object_data['service'] = null;
$object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
$object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url');
// Special treatment for Hubzilla links
@ -719,7 +713,7 @@ class Receiver
// @context, type, actor, signature, mediaType, duration, replies, icon
// Also missing: (Defined in the standard, but currently unused)
// audience, preview, endTime, startTime, generator, image
// audience, preview, endTime, startTime, image
// Data in Notes:

View file

@ -4,6 +4,7 @@
*/
namespace Friendica\Protocol\ActivityPub;
use Friendica\BaseObject;
use Friendica\Database\DBA;
use Friendica\Core\System;
use Friendica\Util\HTTPSignature;
@ -22,22 +23,21 @@ use Friendica\Model\Profile;
use Friendica\Core\Config;
use Friendica\Object\Image;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Diaspora;
use Friendica\Core\Cache;
use Friendica\Util\Map;
require_once 'include/api.php';
/**
* @brief ActivityPub Transmitter Protocol class
*
* To-Do:
*
* Missing object fields:
* - service (App)
* - location
*
* Missing object types:
* - Event
*
* Complicated object types:
* - Announce
* - Undo Announce
*
* General:
@ -327,7 +327,7 @@ class Transmitter
foreach ($terms as $term) {
$profile = APContact::getByURL($term['url'], false);
if (!empty($profile) && empty($contacts[$profile['url']])) {
$data['cc'][] = $profile['url'];
$data['to'][] = $profile['url'];
$contacts[$profile['url']] = $profile['url'];
}
}
@ -379,11 +379,6 @@ class Transmitter
}
DBA::close($parents);
if (empty($data['to'])) {
$data['to'] = $data['cc'];
$data['cc'] = [];
}
return $data;
}
@ -467,7 +462,9 @@ class Transmitter
*/
private static function getTypeOfItem($item)
{
if ($item['verb'] == ACTIVITY_POST) {
if (!empty(Diaspora::isReshare($item['body'], false))) {
$type = 'Announce';
} elseif ($item['verb'] == ACTIVITY_POST) {
if ($item['created'] == $item['edited']) {
$type = 'Create';
} else {
@ -556,10 +553,14 @@ class Transmitter
$data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
$data['instrument'] = ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()];
$data = array_merge($data, self::createPermissionBlockForItem($item));
if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) {
if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
$data['object'] = self::createNote($item);
} elseif ($data['type'] == 'Announce') {
$data['object'] = self::createAnnounce($item);
} elseif ($data['type'] == 'Undo') {
$data['object'] = self::createActivityFromItem($item_id, true);
} else {
@ -599,6 +600,40 @@ class Transmitter
return $data;
}
/**
* Creates a location entry for a given item array
*
* @param array $item
*
* @return array with location array
*/
private static function createLocation($item)
{
$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;
}
/**
* Returns a tag array for a given item array
*
@ -800,11 +835,37 @@ class Transmitter
$data['attachment'] = self::createAttachmentList($item, $type);
$data['tag'] = self::createTagList($item);
if (!empty($item['coord']) || !empty($item['location'])) {
$data['location'] = self::createLocation($item);
}
if (!empty($item['app'])) {
$data['generator'] = ['type' => 'Application', 'name' => $item['app']];
}
$data = array_merge($data, self::createPermissionBlockForItem($item));
return $data;
}
/**
* Creates an announce object entry
*
* @param array $item
*
* @return string with announced object url
*/
public static function createAnnounce($item)
{
$announce = api_share_as_retweet($item);
if (empty($announce['plink'])) {
return self::createNote($item);
}
return $announce['plink'];
}
/**
* Transmits a contact suggestion to a given inbox
*
@ -825,7 +886,7 @@ class Transmitter
'actor' => $owner['url'],
'object' => $suggestion['url'],
'content' => $suggestion['note'],
'published' => DateTimeFormat::utc($suggestion['created'] . '+00:00', DateTimeFormat::ATOM),
'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
'to' => [ActivityPub::PUBLIC_COLLECTION],
'cc' => []];
@ -852,6 +913,7 @@ class Transmitter
'actor' => $owner['url'],
'object' => $owner['url'],
'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
'to' => [ActivityPub::PUBLIC_COLLECTION],
'cc' => []];
@ -878,6 +940,7 @@ class Transmitter
'actor' => $owner['url'],
'object' => self::getProfile($uid),
'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
'to' => [$profile['followers']],
'cc' => []];
@ -905,6 +968,7 @@ class Transmitter
'type' => $activity,
'actor' => $owner['url'],
'object' => $profile['url'],
'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
'to' => $profile['url']];
logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
@ -932,6 +996,7 @@ class Transmitter
'object' => ['id' => $id, 'type' => 'Follow',
'actor' => $profile['url'],
'object' => $owner['url']],
'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
'to' => $profile['url']];
logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
@ -959,6 +1024,7 @@ class Transmitter
'object' => ['id' => $id, 'type' => 'Follow',
'actor' => $profile['url'],
'object' => $owner['url']],
'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
'to' => $profile['url']];
logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
@ -987,6 +1053,7 @@ class Transmitter
'object' => ['id' => $id, 'type' => 'Follow',
'actor' => $owner['url'],
'object' => $profile['url']],
'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
'to' => $profile['url']];
logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);