Add Like delivery and some minor bug fixes

This commit is contained in:
Diogo Cordeiro 2018-07-14 00:13:28 +01:00
parent 6a9b649cd7
commit 2d4e634fad
8 changed files with 129 additions and 21 deletions

View File

@ -122,7 +122,7 @@ class ActivityPubPlugin extends Plugin
}
try {
$other = Activitypub_profile::fromProfile ($other);
$other = Activitypub_profile::from_profile ($other);
} catch (Exception $e) {
return true;
}
@ -148,7 +148,7 @@ class ActivityPubPlugin extends Plugin
}
try {
$other = Activitypub_profile::fromProfile ($other);
$other = Activitypub_profile::from_profile ($other);
} catch (Exception $e) {
return true;
}
@ -159,6 +159,66 @@ class ActivityPubPlugin extends Plugin
return true;
}
/**
* Notify remote users when their notices get favorited.
*
* @param Profile $profile of local user doing the faving
* @param Notice $notice Notice being favored
* @return hook return value
*/
function onEndFavorNotice (Profile $profile, Notice $notice)
{
// Only distribute local users' favor actions, remote users
// will have already distributed theirs.
if (!$profile->isLocal ()) {
return true;
}
$other = array ();
foreach ($notice->getAttentionProfileIDs () as $to_id) {
try {
$other[] = Activitypub_profile::from_profile (Profile::getById ($to_id));
} catch (Exception $e) {
// Local user can be ignored
}
}
$postman = new Activitypub_postman ($profile, $other);
$postman->like ($notice);
return true;
}
/**
* Notify remote users when their notices get de-favorited.
*
* @param Profile $profile of local user doing the de-faving
* @param Notice $notice Notice being favored
* @return hook return value
*/
function onEndDisfavorNotice (Profile $profile, Notice $notice)
{
// Only distribute local users' favor actions, remote users
// will have already distributed theirs.
if (!$profile->isLocal ()) {
return true;
}
$other = array ();
foreach ($notice->getAttentionProfileIDs () as $to_id) {
try {
$other[] = Activitypub_profile::from_profile (Profile::getById ($to_id));
} catch (Exception $e) {
// Local user can be ignored
}
}
$postman = new Activitypub_postman ($profile, $other);
$postman->undo_like ($notice);
return true;
}
}
/**

View File

@ -66,13 +66,13 @@ class apActorInboxAction extends ManagedAction
$data = json_decode (file_get_contents ('php://input'));
// Validate data
if (!(isset($data->type))) {
if (!(isset ($data->type))) {
ActivityPubReturn::error ("Type was not specified.");
}
if (!isset($data->actor)) {
if (!isset ($data->actor)) {
ActivityPubReturn::error ("Actor was not specified.");
}
if (!isset($data->object)) {
if (!isset ($data->object)) {
ActivityPubReturn::error ("Object was not specified.");
}

View File

@ -72,9 +72,9 @@ class apActorLikedCollectionAction extends ManagedAction
$limit = 80;
}
$fave = $this->fetch_faves($user->getID(), $limit, $since_id, $max_id);
$fave = $this->fetch_faves ($user->getID(), $limit, $since_id, $max_id);
$faves = array();
$faves = array ();
while ($fave->fetch ()) {
$faves[] = $this->pretty_fave (clone ($fave));
}

View File

@ -30,7 +30,7 @@ if (!defined ('GNUSOCIAL')) {
}
try {
Fave::addNew ($actor_profile, Notice::getByUri ($data->object));
Fave::addNew ($actor_profile, Notice::getKV ("url", $data->object));
$res = array ("@context" => "https://www.w3.org/ns/activitystreams",
"type" => "Like",
"actor" => $data->actor,

View File

@ -42,7 +42,7 @@ case "Like":
ActivityPubReturn::error ("Object Notice URL was not specified.");
}
Fave::removeEntry ($actor_profile, Notice::getByUri ($data->object->object));
Fave::removeEntry ($actor_profile, Notice::getKV ("url", $data->object->object));
ActivityPubReturn::answer ("Notice disfavorited successfully.");
} catch (Exception $e) {
ActivityPubReturn::error ($e->getMessage (), 403);

View File

@ -120,7 +120,7 @@ class Activitypub_profile extends Profile
* @access public
* @throws ServerException
*/
public function doInsert ()
public function do_insert ()
{
$profile = new Profile ();
@ -156,7 +156,7 @@ class Activitypub_profile extends Profile
* @return Profile
* @throws NoProfileException if it was not found
*/
public function localProfile ()
public function local_profile ()
{
$profile = Profile::getKV ('id', $this->profile_id);
if (!$profile instanceof Profile) {
@ -172,13 +172,13 @@ class Activitypub_profile extends Profile
* @return Activitypub_profile
* @throws Exception if no Activitypub_profile exists for given Profile
*/
static function fromProfile (Profile $profile)
static function from_profile (Profile $profile)
{
$profile_id = $profile->getID ();
$aprofile = Activitypub_profile::getKV ('profile_id', $profile_id);
if (!$aprofile instanceof Activitypub_profile) {
throw new Exception('No Activitypub_profile for Profile ID: '.$profile_id);
throw new Exception ('No Activitypub_profile for Profile ID: '.$profile_id);
}
foreach ($profile as $key => $value) {
@ -193,7 +193,7 @@ class Activitypub_profile extends Profile
*
* @return string Inbox URL
*/
public function getInbox ()
public function get_inbox ()
{
if (is_null ($this->sharedInboxuri)) {
return $this->inboxuri;

View File

@ -165,9 +165,9 @@ class Activitypub_explorer
$aprofile->inboxuri = $res["inbox"];
$aprofile->sharedInboxuri = $res["sharedInbox"];
$aprofile->doInsert ();
$aprofile->do_insert ();
return $aprofile->localProfile ();
return $aprofile->local_profile ();
}
/**

View File

@ -51,10 +51,12 @@ class Activitypub_postman
/**
* Create a postman to deliver something to someone
*
* @param Profile of sender
* @param Activitypub_profile $to array of destinataries
*/
public function __construct ($from, $to)
public function __construct ($from, $to = array ())
{
$this->client = new HTTPClient ();
$this->actor = $from;
$this->to = $to;
$this->headers = array();
@ -67,13 +69,12 @@ class Activitypub_postman
*/
public function follow ()
{
$this->client = new HTTPClient ();
$data = array ("@context" => "https://www.w3.org/ns/activitystreams",
"type" => "Follow",
"actor" => $this->actor->getUrl (),
"object" => $this->to[0]->getUrl ());
$this->client->setBody (json_encode ($data));
$response = $this->client->post ($this->to[0]->getInbox (), $this->headers);
$this->client->post ($this->to[0]->get_inbox (), $this->headers);
}
/**
@ -81,7 +82,6 @@ class Activitypub_postman
*/
public function undo_follow ()
{
$this->client = new HTTPClient ();
$data = array ("@context" => "https://www.w3.org/ns/activitystreams",
"type" => "Undo",
"actor" => $this->actor->getUrl (),
@ -91,6 +91,54 @@ class Activitypub_postman
)
);
$this->client->setBody (json_encode ($data));
$response = $this->client->post ($this->to[0]->getInbox (), $this->headers);
$this->client->post ($this->to[0]->get_inbox (), $this->headers);
}
/**
* Send a Like notification to remote instances holding the notice
*
* @param Notice $notice
*/
public function like ($notice)
{
$data = array ("@context" => "https://www.w3.org/ns/activitystreams",
"type" => "Like",
"actor" => $this->actor->getUrl (),
"object" => $notice->getUrl ());
$this->client->setBody (json_encode ($data));
foreach ($this->to_inbox () as $inbox) {
$this->client->post ($inbox, $this->headers);
}
}
/**
* Send a Undo Like notification to remote instances holding the notice
*
* @param Notice $notice
*/
public function undo_like ($notice)
{
$data = array ("@context" => "https://www.w3.org/ns/activitystreams",
"type" => "Undo",
"actor" => $this->actor->getUrl (),
"object" => array (
"type" => "Like",
"object" => $notice->getUrl ()
)
);
$this->client->setBody (json_encode ($data));
foreach ($this->to_inbox () as $inbox) {
$this->client->post ($inbox, $this->headers);
}
}
private function to_inbox ()
{
$to_inboxes = array ();
foreach ($this->to as $to_profile) {
$to_inboxes[] = $to_profile->get_inbox ();
}
return array_unique ($to_inboxes);
}
}