Merge pull request #5887 from annando/ap-store-conversation

Store conversation data directly from the AP receiver
This commit is contained in:
Hypolite Petovan 2018-10-11 16:32:00 -04:00 committed by GitHub
commit fc19373ec4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 19 deletions

View file

@ -107,9 +107,8 @@ class Processor
* Prepares data for a message * Prepares data for a message
* *
* @param array $activity Activity array * @param array $activity Activity array
* @param string $body original source
*/ */
public static function createItem($activity, $body) public static function createItem($activity)
{ {
$item = []; $item = [];
$item['verb'] = ACTIVITY_POST; $item['verb'] = ACTIVITY_POST;
@ -128,16 +127,15 @@ class Processor
self::fetchMissingActivity($activity['reply-to-id'], $activity); self::fetchMissingActivity($activity['reply-to-id'], $activity);
} }
self::postItem($activity, $item, $body); self::postItem($activity, $item);
} }
/** /**
* Prepare the item array for a "like" * Prepare the item array for a "like"
* *
* @param array $activity Activity array * @param array $activity Activity array
* @param string $body original source
*/ */
public static function likeItem($activity, $body) public static function likeItem($activity)
{ {
$item = []; $item = [];
$item['verb'] = ACTIVITY_LIKE; $item['verb'] = ACTIVITY_LIKE;
@ -145,7 +143,7 @@ class Processor
$item['gravity'] = GRAVITY_ACTIVITY; $item['gravity'] = GRAVITY_ACTIVITY;
$item['object-type'] = ACTIVITY_OBJ_NOTE; $item['object-type'] = ACTIVITY_OBJ_NOTE;
self::postItem($activity, $item, $body); self::postItem($activity, $item);
} }
/** /**
@ -165,9 +163,8 @@ class Processor
* Prepare the item array for a "dislike" * Prepare the item array for a "dislike"
* *
* @param array $activity Activity array * @param array $activity Activity array
* @param string $body original source
*/ */
public static function dislikeItem($activity, $body) public static function dislikeItem($activity)
{ {
$item = []; $item = [];
$item['verb'] = ACTIVITY_DISLIKE; $item['verb'] = ACTIVITY_DISLIKE;
@ -175,7 +172,7 @@ class Processor
$item['gravity'] = GRAVITY_ACTIVITY; $item['gravity'] = GRAVITY_ACTIVITY;
$item['object-type'] = ACTIVITY_OBJ_NOTE; $item['object-type'] = ACTIVITY_OBJ_NOTE;
self::postItem($activity, $item, $body); self::postItem($activity, $item);
} }
/** /**
@ -183,9 +180,8 @@ class Processor
* *
* @param array $activity Activity data * @param array $activity Activity data
* @param array $item item array * @param array $item item array
* @param string $body original source
*/ */
private static function postItem($activity, $item, $body) private static function postItem($activity, $item)
{ {
/// @todo What to do with $activity['context']? /// @todo What to do with $activity['context']?
@ -226,11 +222,6 @@ class Processor
$item['body'] = $activity['source']; $item['body'] = $activity['source'];
} }
$item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
$item['source'] = $body;
$item['conversation-href'] = $activity['context'];
$item['conversation-uri'] = $activity['conversation'];
foreach ($activity['receiver'] as $receiver) { foreach ($activity['receiver'] as $receiver) {
$item['uid'] = $receiver; $item['uid'] = $receiver;
$item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true); $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);

View file

@ -14,6 +14,8 @@ use Friendica\Model\User;
use Friendica\Util\JsonLD; use Friendica\Util\JsonLD;
use Friendica\Util\LDSignature; use Friendica\Util\LDSignature;
use Friendica\Protocol\ActivityPub; use Friendica\Protocol\ActivityPub;
use Friendica\Model\Conversation;
use Friendica\Util\DateTimeFormat;
/** /**
* @brief ActivityPub Receiver Protocol class * @brief ActivityPub Receiver Protocol class
@ -229,6 +231,32 @@ class Receiver
return $object_data; return $object_data;
} }
/**
* Store the unprocessed data into the conversation table
* This has to be done outside the regular function,
* since we store everything - not only item posts.
*
* @param array $activity Array with activity data
* @param string $body The raw message
*/
private static function storeConversation($activity, $body)
{
if (empty($body) || empty($activity['id'])) {
return;
}
$conversation = [
'protocol' => Conversation::PARCEL_ACTIVITYPUB,
'item-uri' => $activity['id'],
'reply-to-uri' => defaults($activity, 'reply-to-id', ''),
'conversation-href' => defaults($activity, 'context', ''),
'conversation-uri' => defaults($activity, 'conversation', ''),
'source' => $body,
'received' => DateTimeFormat::utcNow()];
DBA::insert('conversation', $conversation, true);
}
/** /**
* Processes the activity object * Processes the activity object
* *
@ -268,6 +296,8 @@ class Receiver
return; return;
} }
self::storeConversation($object_data, $body);
// Internal flag for thread completion. See Processor.php // Internal flag for thread completion. See Processor.php
if (!empty($activity['thread-completion'])) { if (!empty($activity['thread-completion'])) {
$object_data['thread-completion'] = $activity['thread-completion']; $object_data['thread-completion'] = $activity['thread-completion'];
@ -276,15 +306,15 @@ class Receiver
switch ($type) { switch ($type) {
case 'as:Create': case 'as:Create':
case 'as:Announce': case 'as:Announce':
ActivityPub\Processor::createItem($object_data, $body); ActivityPub\Processor::createItem($object_data);
break; break;
case 'as:Like': case 'as:Like':
ActivityPub\Processor::likeItem($object_data, $body); ActivityPub\Processor::likeItem($object_data);
break; break;
case 'as:Dislike': case 'as:Dislike':
ActivityPub\Processor::dislikeItem($object_data, $body); ActivityPub\Processor::dislikeItem($object_data);
break; break;
case 'as:Update': case 'as:Update':