[AP] Support Private Messaging

ActivityPubPlugin:
- Subscribe DirectMessage events

Activitypub_inbox_handler:
- Update handle_create_note to create private messages

Activitypub_postman:
- Add create_direct_note for sending private messages

Activitypub_create:
- Update create_to_array to support the 'directMessage' attribute
- Add isPrivateNote to verify private activities

Activitypub_notice:
- Update create_note to support the 'directMessage' attribute
- Remove isPrivateNote

lib/models:
- Add Activitypub_message, the model in charge of private notes
This commit is contained in:
tenma
2019-08-19 23:33:18 +01:00
committed by Diogo Cordeiro
parent 9733f3c02c
commit ebeae261de
6 changed files with 225 additions and 34 deletions

View File

@@ -42,15 +42,16 @@ class Activitypub_create
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @param string $actor
* @param array $object
* @param bool $directMesssage whether it is a private Create activity or not
* @return array pretty array to be used in a response
*/
public static function create_to_array(string $actor, array $object): array
public static function create_to_array(string $actor, array $object, bool $directMessage = false): array
{
$res = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $object['id'].'/create',
'type' => 'Create',
'directMessage' => false,
'directMessage' => $directMessage,
'to' => $object['to'],
'cc' => $object['cc'],
'actor' => $actor,
@@ -89,4 +90,21 @@ class Activitypub_create
throw new Exception('This is not a supported Object Type for Create Activity.');
}
}
/**
* Verify if received note is private (direct).
* Note that we're conformant with the (yet) non-standard directMessage attribute:
* https://github.com/w3c/activitypub/issues/196#issuecomment-304958984
*
* @param array $activity received Create-Note activity
* @return bool true if note is private, false otherwise
* @author Bruno casteleiro <brunoccast@fc.up.pt>
*/
public static function isPrivateNote(array $activity): bool {
if (isset($activity['directMessage'])) {
return $activity['directMessage'];
}
return empty($activity['cc']) && !in_array('https://www.w3.org/ns/activitystreams#Public', $activity['to']);
}
}

View File

@@ -0,0 +1,91 @@
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/**
* ActivityPub implementation for GNU social
*
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
defined('GNUSOCIAL') || die();
/**
* ActivityPub direct note representation
*
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class Activitypub_message
{
/**
* Generates a pretty message from a Notice object
*
* @param Notice $message
* @return array array to be used in a response
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
*/
public static function message_to_array(Notice $message): array
{
$from = $message->getProfile();
$tags = [];
foreach ($message->getTags() as $tag) {
if ($tag != "") { // Hacky workaround to avoid stupid outputs
$tags[] = Activitypub_tag::tag_to_array($tag);
}
}
$to = [];
foreach ($message->getAttentionProfiles() as $to_profile) {
$to[] = $href = $to_profile->getUri();
$tags[] = Activitypub_mention_tag::mention_tag_to_array_from_values($href, $to_profile->getNickname().'@'.parse_url($href, PHP_URL_HOST));
}
$item = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => common_local_url('showmessage', ['message' => $message->getID()]),
'type' => 'Note',
'published' => str_replace(' ', 'T', $message->created).'Z',
'attributedTo' => ActivityPubPlugin::actor_uri($from),
'to' => $to,
'cc' => [],
'content' => $message->getRendered(),
'attachment' => [],
'tag' => $tags
];
return $item;
}
/**
* Create a private Notice via ActivityPub Note Object.
* Returns created Notice.
*
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
* @param array $object
* @param Profile $actor_profile
* @return Notice
* @throws Exception
*/
public static function create_message(array $object, Profile $actor_profile = null): Notice
{
return Activitypub_notice::create_notice($object, $actor_profile, true);
}
}

View File

@@ -118,10 +118,11 @@ class Activitypub_notice
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @param array $object
* @param Profile $actor_profile
* @param bool $directMessage
* @return Notice
* @throws Exception
*/
public static function create_notice(array $object, Profile $actor_profile = null)
public static function create_notice(array $object, Profile $actor_profile = null, bool $directMessage = false): Notice
{
$id = $object['id']; // int
$url = isset($object['url']) ? $object['url'] : $id; // string
@@ -154,6 +155,10 @@ class Activitypub_notice
'url' => $url,
'is_local' => self::getNotePolicyType($object, $actor_profile)];
if ($directMessage) {
$options['scope'] = Notice::MESSAGE_SCOPE;
}
// Is this a reply?
if (isset($settings['inReplyTo'])) {
try {
@@ -192,7 +197,9 @@ class Activitypub_notice
unset($discovery);
foreach ($mentions_profiles as $mp) {
$act->context->attention[ActivityPubPlugin::actor_uri($mp)] = 'http://activitystrea.ms/schema/1.0/person';
if (!$mp->hasBlocked($actor_profile)) {
$act->context->attention[ActivityPubPlugin::actor_uri($mp)] = 'http://activitystrea.ms/schema/1.0/person';
}
}
// Add location if that is set
@@ -291,20 +298,4 @@ class Activitypub_notice
return Notice::GATEWAY;
}
}
/**
* Verify if received note is private (direct).
* Note that we're conformant with the (yet) non-standard directMessage attribute:
* https://github.com/w3c/activitypub/issues/196#issuecomment-304958984
*
* @param array $activity received Create-Note activity
* @return bool true if note is private, false otherwise
*/
public static function isPrivateNote(array $activity): bool {
if (isset($activity['directMessage'])) {
return $activity['directMessage'];
}
return empty($activity['cc']) && !in_array('https://www.w3.org/ns/activitystreams#Public', $activity['to']);
}
}