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 { try {
$other = Activitypub_profile::fromProfile ($other); $other = Activitypub_profile::from_profile ($other);
} catch (Exception $e) { } catch (Exception $e) {
return true; return true;
} }
@ -148,7 +148,7 @@ class ActivityPubPlugin extends Plugin
} }
try { try {
$other = Activitypub_profile::fromProfile ($other); $other = Activitypub_profile::from_profile ($other);
} catch (Exception $e) { } catch (Exception $e) {
return true; return true;
} }
@ -159,6 +159,66 @@ class ActivityPubPlugin extends Plugin
return true; 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')); $data = json_decode (file_get_contents ('php://input'));
// Validate data // Validate data
if (!(isset($data->type))) { if (!(isset ($data->type))) {
ActivityPubReturn::error ("Type was not specified."); ActivityPubReturn::error ("Type was not specified.");
} }
if (!isset($data->actor)) { if (!isset ($data->actor)) {
ActivityPubReturn::error ("Actor was not specified."); ActivityPubReturn::error ("Actor was not specified.");
} }
if (!isset($data->object)) { if (!isset ($data->object)) {
ActivityPubReturn::error ("Object was not specified."); ActivityPubReturn::error ("Object was not specified.");
} }

View File

@ -72,9 +72,9 @@ class apActorLikedCollectionAction extends ManagedAction
$limit = 80; $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 ()) { while ($fave->fetch ()) {
$faves[] = $this->pretty_fave (clone ($fave)); $faves[] = $this->pretty_fave (clone ($fave));
} }

View File

@ -30,7 +30,7 @@ if (!defined ('GNUSOCIAL')) {
} }
try { 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", $res = array ("@context" => "https://www.w3.org/ns/activitystreams",
"type" => "Like", "type" => "Like",
"actor" => $data->actor, "actor" => $data->actor,

View File

@ -42,7 +42,7 @@ case "Like":
ActivityPubReturn::error ("Object Notice URL was not specified."); 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."); ActivityPubReturn::answer ("Notice disfavorited successfully.");
} catch (Exception $e) { } catch (Exception $e) {
ActivityPubReturn::error ($e->getMessage (), 403); ActivityPubReturn::error ($e->getMessage (), 403);

View File

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

View File

@ -165,9 +165,9 @@ class Activitypub_explorer
$aprofile->inboxuri = $res["inbox"]; $aprofile->inboxuri = $res["inbox"];
$aprofile->sharedInboxuri = $res["sharedInbox"]; $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 * Create a postman to deliver something to someone
* *
* @param Profile of sender
* @param Activitypub_profile $to array of destinataries * @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->actor = $from;
$this->to = $to; $this->to = $to;
$this->headers = array(); $this->headers = array();
@ -67,13 +69,12 @@ class Activitypub_postman
*/ */
public function follow () public function follow ()
{ {
$this->client = new HTTPClient ();
$data = array ("@context" => "https://www.w3.org/ns/activitystreams", $data = array ("@context" => "https://www.w3.org/ns/activitystreams",
"type" => "Follow", "type" => "Follow",
"actor" => $this->actor->getUrl (), "actor" => $this->actor->getUrl (),
"object" => $this->to[0]->getUrl ()); "object" => $this->to[0]->getUrl ());
$this->client->setBody (json_encode ($data)); $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 () public function undo_follow ()
{ {
$this->client = new HTTPClient ();
$data = array ("@context" => "https://www.w3.org/ns/activitystreams", $data = array ("@context" => "https://www.w3.org/ns/activitystreams",
"type" => "Undo", "type" => "Undo",
"actor" => $this->actor->getUrl (), "actor" => $this->actor->getUrl (),
@ -91,6 +91,54 @@ class Activitypub_postman
) )
); );
$this->client->setBody (json_encode ($data)); $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);
} }
} }