}
/**
- * @fixme probably call Ostatus_profile::processFeed
+ * We've gotten a follow/subscribe notification from a remote user.
+ * Save a subscription relationship for them.
*/
+
function handleFollow()
{
- $object = $this->act->object;
-
- if ($object->id != $this->user->uri) {
- throw new ClientException("Subscription notice not for this user.");
- }
-
- $profile = $this->ensureProfile();
-
- $sub = Subscription::pkeyGet(array('subscriber' => $profile->id,
- 'subscribed' => $this->user->id));
-
- if (!empty($sub)) {
- throw new ClientException("Already subscribed.");
+ $oprofile = $this->ensureProfile();
+ if ($oprofile) {
+ common_log(LOG_INFO, "Setting up subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
+ $oprofile->subscribeRemoteToLocal($this->user);
+ } else {
+ common_log(LOG_INFO, "Can't set up subscription from remote; missing profile.");
}
+ }
- if ($this->user->hasBlocked($profile)) {
- throw new ClientException("Already subscribed.");
+ /**
+ * We've gotten an unfollow/unsubscribe notification from a remote user.
+ * Check if we have a subscription relationship for them and kill it.
+ *
+ * @fixme probably catch exceptions on fail?
+ */
+ function handleUnfollow()
+ {
+ $oprofile = $this->ensureProfile();
+ if ($oprofile) {
+ common_log(LOG_INFO, "Canceling subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
+ Subscription::cancel($oprofile->localProfile(), $this->user->getProfile());
+ } else {
+ common_log(LOG_ERR, "Can't cancel subscription from remote, didn't find the profile");
}
-
}
/**
- * @fixme probably call Ostatus_profile::processFeed
+ * Remote user likes one of our posts.
+ * Confirm the post is ours, and save a local favorite event.
*/
+
function handleFavorite()
{
+ // WORST VARIABLE NAME EVER
+ $object = $this->act->object;
+
+ switch ($this->act->object->type) {
+ case ActivityObject::ARTICLE:
+ case ActivityObject::BLOGENTRY:
+ case ActivityObject::NOTE:
+ case ActivityObject::STATUS:
+ case ActivityObject::COMMENT:
+ break;
+ default:
+ throw new ClientException("Can't handle that kind of object for liking/faving.");
+ }
+
+ $notice = Notice::staticGet('uri', $object->id);
+
+ if (empty($notice)) {
+ throw new ClientException("Notice with ID $object->id unknown.");
+ }
+
+ if ($notice->profile_id != $this->user->id) {
+ throw new ClientException("Notice with ID $object->id not posted by $this->user->id.");
+ }
+
+ $profile = $this->ensureProfile();
+
+ $old = Fave::pkeyGet(array('user_id' => $profile->id,
+ 'notice_id' => $notice->id));
+
+ if (!empty($old)) {
+ throw new ClientException("We already know that's a fave!");
+ }
+
+ $fave = new Fave();
+
+ // @fixme need to change this attribute name, maybe references
+ $fave->user_id = $profile->id;
+ $fave->notice_id = $notice->id;
+
+ $result = $fave->insert();
+
+ if (!$result) {
+ common_log_db_error($fave, 'INSERT', __FILE__);
+ throw new ServerException('Could not save new favorite.');
+ }
}
/**