Type testing instead of just empty() in OStatusPlugin

This commit is contained in:
Mikael Nordfeldth 2014-05-06 14:36:52 +02:00
parent 261778f6de
commit 0fa00d5106

View File

@ -350,7 +350,7 @@ class OStatusPlugin extends Plugin
$this->log(LOG_INFO, "Checking webfinger '$target'"); $this->log(LOG_INFO, "Checking webfinger '$target'");
try { try {
$oprofile = Ostatus_profile::ensureWebfinger($target); $oprofile = Ostatus_profile::ensureWebfinger($target);
if ($oprofile && !$oprofile->isGroup()) { if ($oprofile instanceof Ostatus_profile && !$oprofile->isGroup()) {
$profile = $oprofile->localProfile(); $profile = $oprofile->localProfile();
$matches[$pos] = array('mentioned' => array($profile), $matches[$pos] = array('mentioned' => array($profile),
'type' => 'mention', 'type' => 'mention',
@ -377,7 +377,7 @@ class OStatusPlugin extends Plugin
$this->log(LOG_INFO, "Checking profile address '$url'"); $this->log(LOG_INFO, "Checking profile address '$url'");
try { try {
$oprofile = Ostatus_profile::ensureProfileURL($url); $oprofile = Ostatus_profile::ensureProfileURL($url);
if ($oprofile && !$oprofile->isGroup()) { if ($oprofile instanceof Ostatus_profile && !$oprofile->isGroup()) {
$profile = $oprofile->localProfile(); $profile = $oprofile->localProfile();
$matches[$pos] = array('mentioned' => array($profile), $matches[$pos] = array('mentioned' => array($profile),
'type' => 'mention', 'type' => 'mention',
@ -422,7 +422,7 @@ class OStatusPlugin extends Plugin
function onStartCommandGetProfile($command, $arg, &$profile) function onStartCommandGetProfile($command, $arg, &$profile)
{ {
$oprofile = $this->pullRemoteProfile($arg); $oprofile = $this->pullRemoteProfile($arg);
if ($oprofile && !$oprofile->isGroup()) { if ($oprofile instanceof Ostatus_profile && !$oprofile->isGroup()) {
$profile = $oprofile->localProfile(); $profile = $oprofile->localProfile();
return false; return false;
} else { } else {
@ -444,7 +444,7 @@ class OStatusPlugin extends Plugin
function onStartCommandGetGroup($command, $arg, &$group) function onStartCommandGetGroup($command, $arg, &$group)
{ {
$oprofile = $this->pullRemoteProfile($arg); $oprofile = $this->pullRemoteProfile($arg);
if ($oprofile && $oprofile->isGroup()) { if ($oprofile instanceof Ostatus_profile && $oprofile->isGroup()) {
$group = $oprofile->localGroup(); $group = $oprofile->localGroup();
return false; return false;
} else { } else {
@ -580,7 +580,7 @@ class OStatusPlugin extends Plugin
function onFeedSubSubscriberCount($feedsub, &$count) function onFeedSubSubscriberCount($feedsub, &$count)
{ {
$oprofile = Ostatus_profile::getKV('feeduri', $feedsub->uri); $oprofile = Ostatus_profile::getKV('feeduri', $feedsub->uri);
if ($oprofile) { if ($oprofile instanceof Ostatus_profile) {
$count += $oprofile->subscriberCount(); $count += $oprofile->subscriberCount();
} }
return true; return true;
@ -607,8 +607,7 @@ class OStatusPlugin extends Plugin
} }
$oprofile = Ostatus_profile::getKV('profile_id', $other->id); $oprofile = Ostatus_profile::getKV('profile_id', $other->id);
if (!$oprofile instanceof Ostatus_profile) {
if (empty($oprofile)) {
return true; return true;
} }
@ -636,8 +635,7 @@ class OStatusPlugin extends Plugin
} }
$oprofile = Ostatus_profile::getKV('profile_id', $other->id); $oprofile = Ostatus_profile::getKV('profile_id', $other->id);
if (!$oprofile instanceof Ostatus_profile) {
if (empty($oprofile)) {
return true; return true;
} }
@ -666,8 +664,7 @@ class OStatusPlugin extends Plugin
} }
$oprofile = Ostatus_profile::getKV('profile_id', $other->id); $oprofile = Ostatus_profile::getKV('profile_id', $other->id);
if (!$oprofile instanceof Ostatus_profile) {
if (empty($oprofile)) {
return true; return true;
} }
@ -713,41 +710,43 @@ class OStatusPlugin extends Plugin
function onStartJoinGroup($group, $profile) function onStartJoinGroup($group, $profile)
{ {
$oprofile = Ostatus_profile::getKV('group_id', $group->id); $oprofile = Ostatus_profile::getKV('group_id', $group->id);
if ($oprofile) { if (!$oprofile instanceof Ostatus_profile) {
if (!$oprofile->subscribe()) { return true;
// TRANS: Exception thrown when setup of remote group membership fails. }
throw new Exception(_m('Could not set up remote group membership.'));
}
// NOTE: we don't use Group_member::asActivity() since that record if (!$oprofile->subscribe()) {
// has not yet been created. // TRANS: Exception thrown when setup of remote group membership fails.
throw new Exception(_m('Could not set up remote group membership.'));
}
$act = new Activity(); // NOTE: we don't use Group_member::asActivity() since that record
$act->id = TagURI::mint('join:%d:%d:%s', // has not yet been created.
$profile->id,
$group->id,
common_date_iso8601(time()));
$act->actor = ActivityObject::fromProfile($profile); $act = new Activity();
$act->verb = ActivityVerb::JOIN; $act->id = TagURI::mint('join:%d:%d:%s',
$act->object = $oprofile->asActivityObject(); $profile->id,
$group->id,
common_date_iso8601(time()));
$act->time = time(); $act->actor = ActivityObject::fromProfile($profile);
// TRANS: Title for joining a remote groep. $act->verb = ActivityVerb::JOIN;
$act->title = _m('TITLE','Join'); $act->object = $oprofile->asActivityObject();
// TRANS: Success message for subscribe to group attempt through OStatus.
// TRANS: %1$s is the member name, %2$s is the subscribed group's name.
$act->content = sprintf(_m('%1$s has joined group %2$s.'),
$profile->getBestName(),
$oprofile->getBestName());
if ($oprofile->notifyActivity($act, $profile)) { $act->time = time();
return true; // TRANS: Title for joining a remote groep.
} else { $act->title = _m('TITLE','Join');
$oprofile->garbageCollect(); // TRANS: Success message for subscribe to group attempt through OStatus.
// TRANS: Exception thrown when joining a remote group fails. // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
throw new Exception(_m('Failed joining remote group.')); $act->content = sprintf(_m('%1$s has joined group %2$s.'),
} $profile->getBestName(),
$oprofile->getBestName());
if ($oprofile->notifyActivity($act, $profile)) {
return true;
} else {
$oprofile->garbageCollect();
// TRANS: Exception thrown when joining a remote group fails.
throw new Exception(_m('Failed joining remote group.'));
} }
} }
@ -768,33 +767,35 @@ class OStatusPlugin extends Plugin
function onEndLeaveGroup($group, $profile) function onEndLeaveGroup($group, $profile)
{ {
$oprofile = Ostatus_profile::getKV('group_id', $group->id); $oprofile = Ostatus_profile::getKV('group_id', $group->id);
if ($oprofile) { if (!$oprofile instanceof Ostatus_profile) {
// Drop the PuSH subscription if there are no other subscribers. return true;
$oprofile->garbageCollect();
$member = $profile;
$act = new Activity();
$act->id = TagURI::mint('leave:%d:%d:%s',
$member->id,
$group->id,
common_date_iso8601(time()));
$act->actor = ActivityObject::fromProfile($member);
$act->verb = ActivityVerb::LEAVE;
$act->object = $oprofile->asActivityObject();
$act->time = time();
// TRANS: Title for leaving a remote group.
$act->title = _m('TITLE','Leave');
// TRANS: Success message for unsubscribe from group attempt through OStatus.
// TRANS: %1$s is the member name, %2$s is the unsubscribed group's name.
$act->content = sprintf(_m('%1$s has left group %2$s.'),
$member->getBestName(),
$oprofile->getBestName());
$oprofile->notifyActivity($act, $member);
} }
// Drop the PuSH subscription if there are no other subscribers.
$oprofile->garbageCollect();
$member = $profile;
$act = new Activity();
$act->id = TagURI::mint('leave:%d:%d:%s',
$member->id,
$group->id,
common_date_iso8601(time()));
$act->actor = ActivityObject::fromProfile($member);
$act->verb = ActivityVerb::LEAVE;
$act->object = $oprofile->asActivityObject();
$act->time = time();
// TRANS: Title for leaving a remote group.
$act->title = _m('TITLE','Leave');
// TRANS: Success message for unsubscribe from group attempt through OStatus.
// TRANS: %1$s is the member name, %2$s is the unsubscribed group's name.
$act->content = sprintf(_m('%1$s has left group %2$s.'),
$member->getBestName(),
$oprofile->getBestName());
$oprofile->notifyActivity($act, $member);
} }
/** /**
@ -811,42 +812,44 @@ class OStatusPlugin extends Plugin
function onStartSubscribePeopletag($peopletag, $user) function onStartSubscribePeopletag($peopletag, $user)
{ {
$oprofile = Ostatus_profile::getKV('peopletag_id', $peopletag->id); $oprofile = Ostatus_profile::getKV('peopletag_id', $peopletag->id);
if ($oprofile) { if (!$oprofile instanceof Ostatus_profile) {
if (!$oprofile->subscribe()) { return true;
// TRANS: Exception thrown when setup of remote list subscription fails. }
throw new Exception(_m('Could not set up remote list subscription.'));
}
$sub = $user->getProfile(); if (!$oprofile->subscribe()) {
$tagger = Profile::getKV($peopletag->tagger); // TRANS: Exception thrown when setup of remote list subscription fails.
throw new Exception(_m('Could not set up remote list subscription.'));
}
$act = new Activity(); $sub = $user->getProfile();
$act->id = TagURI::mint('subscribe_peopletag:%d:%d:%s', $tagger = Profile::getKV($peopletag->tagger);
$sub->id,
$peopletag->id,
common_date_iso8601(time()));
$act->actor = ActivityObject::fromProfile($sub); $act = new Activity();
$act->verb = ActivityVerb::FOLLOW; $act->id = TagURI::mint('subscribe_peopletag:%d:%d:%s',
$act->object = $oprofile->asActivityObject(); $sub->id,
$peopletag->id,
common_date_iso8601(time()));
$act->time = time(); $act->actor = ActivityObject::fromProfile($sub);
// TRANS: Title for following a remote list. $act->verb = ActivityVerb::FOLLOW;
$act->title = _m('TITLE','Follow list'); $act->object = $oprofile->asActivityObject();
// TRANS: Success message for remote list follow through OStatus.
// TRANS: %1$s is the subscriber name, %2$s is the list, %3$s is the lister's name.
$act->content = sprintf(_m('%1$s is now following people listed in %2$s by %3$s.'),
$sub->getBestName(),
$oprofile->getBestName(),
$tagger->getBestName());
if ($oprofile->notifyActivity($act, $sub)) { $act->time = time();
return true; // TRANS: Title for following a remote list.
} else { $act->title = _m('TITLE','Follow list');
$oprofile->garbageCollect(); // TRANS: Success message for remote list follow through OStatus.
// TRANS: Exception thrown when subscription to remote list fails. // TRANS: %1$s is the subscriber name, %2$s is the list, %3$s is the lister's name.
throw new Exception(_m('Failed subscribing to remote list.')); $act->content = sprintf(_m('%1$s is now following people listed in %2$s by %3$s.'),
} $sub->getBestName(),
$oprofile->getBestName(),
$tagger->getBestName());
if ($oprofile->notifyActivity($act, $sub)) {
return true;
} else {
$oprofile->garbageCollect();
// TRANS: Exception thrown when subscription to remote list fails.
throw new Exception(_m('Failed subscribing to remote list.'));
} }
} }
@ -863,35 +866,37 @@ class OStatusPlugin extends Plugin
function onEndUnsubscribePeopletag($peopletag, $user) function onEndUnsubscribePeopletag($peopletag, $user)
{ {
$oprofile = Ostatus_profile::getKV('peopletag_id', $peopletag->id); $oprofile = Ostatus_profile::getKV('peopletag_id', $peopletag->id);
if ($oprofile) { if (!$oprofile instanceof Ostatus_profile) {
// Drop the PuSH subscription if there are no other subscribers. return true;
$oprofile->garbageCollect();
$sub = Profile::getKV($user->id);
$tagger = Profile::getKV($peopletag->tagger);
$act = new Activity();
$act->id = TagURI::mint('unsubscribe_peopletag:%d:%d:%s',
$sub->id,
$peopletag->id,
common_date_iso8601(time()));
$act->actor = ActivityObject::fromProfile($member);
$act->verb = ActivityVerb::UNFOLLOW;
$act->object = $oprofile->asActivityObject();
$act->time = time();
// TRANS: Title for unfollowing a remote list.
$act->title = _m('Unfollow list');
// TRANS: Success message for remote list unfollow through OStatus.
// TRANS: %1$s is the subscriber name, %2$s is the list, %3$s is the lister's name.
$act->content = sprintf(_m('%1$s stopped following the list %2$s by %3$s.'),
$sub->getBestName(),
$oprofile->getBestName(),
$tagger->getBestName());
$oprofile->notifyActivity($act, $user);
} }
// Drop the PuSH subscription if there are no other subscribers.
$oprofile->garbageCollect();
$sub = Profile::getKV($user->id);
$tagger = Profile::getKV($peopletag->tagger);
$act = new Activity();
$act->id = TagURI::mint('unsubscribe_peopletag:%d:%d:%s',
$sub->id,
$peopletag->id,
common_date_iso8601(time()));
$act->actor = ActivityObject::fromProfile($member);
$act->verb = ActivityVerb::UNFOLLOW;
$act->object = $oprofile->asActivityObject();
$act->time = time();
// TRANS: Title for unfollowing a remote list.
$act->title = _m('Unfollow list');
// TRANS: Success message for remote list unfollow through OStatus.
// TRANS: %1$s is the subscriber name, %2$s is the list, %3$s is the lister's name.
$act->content = sprintf(_m('%1$s stopped following the list %2$s by %3$s.'),
$sub->getBestName(),
$oprofile->getBestName(),
$tagger->getBestName());
$oprofile->notifyActivity($act, $user);
} }
/** /**
@ -903,23 +908,23 @@ class OStatusPlugin extends Plugin
*/ */
function onEndFavorNotice(Profile $profile, Notice $notice) function onEndFavorNotice(Profile $profile, Notice $notice)
{ {
$user = User::getKV('id', $profile->id); // Only distribute local users' favor actions, remote users
// will have already distributed theirs.
if (empty($user)) { if (!$profile->isLocal()) {
return true; return true;
} }
$oprofile = Ostatus_profile::getKV('profile_id', $notice->profile_id); $oprofile = Ostatus_profile::getKV('profile_id', $notice->profile_id);
if (!$oprofile instanceof Ostatus_profile) {
if (empty($oprofile)) {
return true; return true;
} }
$fav = Fave::pkeyGet(array('user_id' => $user->id, $fav = Fave::pkeyGet(array('user_id' => $user->id,
'notice_id' => $notice->id)); 'notice_id' => $notice->id));
if (empty($fav)) { if (!$fav instanceof Fave) {
// That's weird. // That's weird.
// TODO: Make pkeyGet throw exception, since this is a critical failure.
return true; return true;
} }
@ -941,8 +946,7 @@ class OStatusPlugin extends Plugin
function onEndTagProfile($ptag) function onEndTagProfile($ptag)
{ {
$oprofile = Ostatus_profile::getKV('profile_id', $ptag->tagged); $oprofile = Ostatus_profile::getKV('profile_id', $ptag->tagged);
if (!$oprofile instanceof Ostatus_profile) {
if (empty($oprofile)) {
return true; return true;
} }
@ -998,8 +1002,7 @@ class OStatusPlugin extends Plugin
function onEndUntagProfile($ptag) function onEndUntagProfile($ptag)
{ {
$oprofile = Ostatus_profile::getKV('profile_id', $ptag->tagged); $oprofile = Ostatus_profile::getKV('profile_id', $ptag->tagged);
if (!$oprofile instanceof Ostatus_profile) {
if (empty($oprofile)) {
return true; return true;
} }
@ -1049,15 +1052,14 @@ class OStatusPlugin extends Plugin
*/ */
function onEndDisfavorNotice(Profile $profile, Notice $notice) function onEndDisfavorNotice(Profile $profile, Notice $notice)
{ {
$user = User::getKV('id', $profile->id); // Only distribute local users' disfavor actions, remote users
// will have already distributed theirs.
if (empty($user)) { if (!$profile->isLocal()) {
return true; return true;
} }
$oprofile = Ostatus_profile::getKV('profile_id', $notice->profile_id); $oprofile = Ostatus_profile::getKV('profile_id', $notice->profile_id);
if (!$oprofile instanceof Ostatus_profile) {
if (empty($oprofile)) {
return true; return true;
} }
@ -1103,7 +1105,7 @@ class OStatusPlugin extends Plugin
function onStartUserGroupPermalink($group, &$url) function onStartUserGroupPermalink($group, &$url)
{ {
$oprofile = Ostatus_profile::getKV('group_id', $group->id); $oprofile = Ostatus_profile::getKV('group_id', $group->id);
if ($oprofile) { if ($oprofile instanceof Ostatus_profile) {
// @fixme this should probably be in the user_group table // @fixme this should probably be in the user_group table
// @fixme this uri not guaranteed to be a profile page // @fixme this uri not guaranteed to be a profile page
$url = $oprofile->uri; $url = $oprofile->uri;
@ -1290,7 +1292,7 @@ class OStatusPlugin extends Plugin
{ {
$oprofile = Ostatus_profile::getKV('profile_id', $profile->id); $oprofile = Ostatus_profile::getKV('profile_id', $profile->id);
if (empty($oprofile)) { if (!$oprofile instanceof Ostatus_profile) {
return true; return true;
} }