示例#1
0
文件: subs.php 项目: himmelex/NTW
function subs_unsubscribe_to($user, $other)
{
    try {
        Subscription::cancel($user->getProfile(), $other);
        return true;
    } catch (Exception $e) {
        return $e->getMessage();
    }
}
示例#2
0
 function handle($args)
 {
     parent::handle($args);
     if (!common_logged_in()) {
         // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
         $this->clientError(_('Not logged in.'));
     }
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         common_redirect(common_local_url('subscriptions', array('nickname' => $this->scoped->nickname)));
     }
     /* Use a session token for CSRF protection. */
     $token = $this->trimmed('token');
     if (!$token || $token != common_session_token()) {
         // TRANS: Client error displayed when the session token does not match or is not given.
         $this->clientError(_('There was a problem with your session token. ' . 'Try again, please.'));
     }
     $other_id = $this->arg('unsubscribeto');
     if (!$other_id) {
         // TRANS: Client error displayed when trying to unsubscribe without providing a profile ID.
         $this->clientError(_('No profile ID in request.'));
     }
     $other = Profile::getKV('id', $other_id);
     if (!$other instanceof Profile) {
         // TRANS: Client error displayed when trying to unsubscribe while providing a non-existing profile ID.
         $this->clientError(_('No profile with that ID.'));
     }
     try {
         Subscription::cancel($this->scoped, $other);
     } catch (Exception $e) {
         $this->clientError($e->getMessage());
     }
     if ($this->boolean('ajax')) {
         $this->startHTML('text/xml;charset=utf-8');
         $this->elementStart('head');
         // TRANS: Page title for page to unsubscribe.
         $this->element('title', null, _('Unsubscribed'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $subscribe = new SubscribeForm($this, $other);
         $subscribe->show();
         $this->elementEnd('body');
         $this->endHTML();
     } else {
         common_redirect(common_local_url('subscriptions', array('nickname' => $this->scoped->nickname)), 303);
     }
 }
 function execute($channel)
 {
     if (!$this->other) {
         $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
         return;
     }
     $result = Subscription::cancel($this->getProfile($this->other), $this->user->getProfile());
     if ($result) {
         $channel->output($this->user, sprintf(_('Unsubscribed  %s'), $this->other));
     } else {
         $channel->error($this->user, $result);
     }
 }
示例#4
0
 function execute($channel)
 {
     if (!$this->other) {
         // TRANS: Error text shown when no username was provided when issuing the command.
         $channel->error($this->user, _('Specify the name of the user to unsubscribe from.'));
         return;
     }
     $result = Subscription::cancel($this->getProfile($this->other), $this->user->getProfile());
     if ($result) {
         // TRANS: Text shown after issuing the lose command successfully (stop another user from following the current user).
         // TRANS: %s is the name of the user the unsubscription was requested for.
         $channel->output($this->user, sprintf(_('Unsubscribed %s.'), $this->other));
     } else {
         $channel->error($this->user, $result);
     }
 }
示例#5
0
 function moveActivity($act, $sink, $user, $remote)
 {
     if (empty($user)) {
         throw new Exception(sprintf(_("No such user %s."), $act->actor->id));
     }
     switch ($act->verb) {
         case ActivityVerb::FAVORITE:
             $this->log(LOG_INFO, "Moving favorite of {$act->objects[0]->id} by " . "{$act->actor->id} to {$remote->nickname}.");
             // push it, then delete local
             $sink->postActivity($act);
             $notice = Notice::staticGet('uri', $act->objects[0]->id);
             if (!empty($notice)) {
                 $fave = Fave::pkeyGet(array('user_id' => $user->id, 'notice_id' => $notice->id));
                 $fave->delete();
             }
             break;
         case ActivityVerb::POST:
             $this->log(LOG_INFO, "Moving notice {$act->objects[0]->id} by " . "{$act->actor->id} to {$remote->nickname}.");
             // XXX: send a reshare, not a post
             $sink->postActivity($act);
             $notice = Notice::staticGet('uri', $act->objects[0]->id);
             if (!empty($notice)) {
                 $notice->delete();
             }
             break;
         case ActivityVerb::JOIN:
             $this->log(LOG_INFO, "Moving group join of {$act->objects[0]->id} by " . "{$act->actor->id} to {$remote->nickname}.");
             $sink->postActivity($act);
             $group = User_group::staticGet('uri', $act->objects[0]->id);
             if (!empty($group)) {
                 Group_member::leave($group->id, $user->id);
             }
             break;
         case ActivityVerb::FOLLOW:
             if ($act->actor->id == $user->uri) {
                 $this->log(LOG_INFO, "Moving subscription to {$act->objects[0]->id} by " . "{$act->actor->id} to {$remote->nickname}.");
                 $sink->postActivity($act);
                 $other = Profile::fromURI($act->objects[0]->id);
                 if (!empty($other)) {
                     Subscription::cancel($user->getProfile(), $other);
                 }
             } else {
                 $otherUser = User::staticGet('uri', $act->actor->id);
                 if (!empty($otherUser)) {
                     $this->log(LOG_INFO, "Changing sub to {$act->objects[0]->id}" . "by {$act->actor->id} to {$remote->nickname}.");
                     $otherProfile = $otherUser->getProfile();
                     Subscription::start($otherProfile, $remote);
                     Subscription::cancel($otherProfile, $user->getProfile());
                 } else {
                     $this->log(LOG_NOTICE, "Not changing sub to {$act->objects[0]->id}" . "by remote {$act->actor->id} " . "to {$remote->nickname}.");
                 }
             }
             break;
     }
 }
示例#6
0
 function block($other)
 {
     // Add a new block record
     // no blocking (and thus unsubbing from) yourself
     if ($this->id == $other->id) {
         common_log(LOG_WARNING, sprintf("Profile ID %d (%s) tried to block themself.", $this->id, $this->nickname));
         return false;
     }
     $block = new Profile_block();
     // Begin a transaction
     $block->query('BEGIN');
     $block->blocker = $this->id;
     $block->blocked = $other->id;
     $result = $block->insert();
     if (!$result) {
         common_log_db_error($block, 'INSERT', __FILE__);
         return false;
     }
     $self = $this->getProfile();
     if (Subscription::exists($other, $self)) {
         Subscription::cancel($other, $self);
     }
     if (Subscription::exists($self, $other)) {
         Subscription::cancel($self, $other);
     }
     $block->query('COMMIT');
     return true;
 }
示例#7
0
 function _deleteSubscriptions()
 {
     $sub = new Subscription();
     $sub->subscriber = $this->id;
     $sub->find();
     while ($sub->fetch()) {
         $other = Profile::staticGet('id', $sub->subscribed);
         if (empty($other)) {
             continue;
         }
         if ($other->id == $this->id) {
             continue;
         }
         Subscription::cancel($this, $other);
     }
     $subd = new Subscription();
     $subd->subscribed = $this->id;
     $subd->find();
     while ($subd->fetch()) {
         $other = Profile::staticGet('id', $subd->subscriber);
         if (empty($other)) {
             continue;
         }
         if ($other->id == $this->id) {
             continue;
         }
         Subscription::cancel($other, $this);
     }
     $self = new Subscription();
     $self->subscriber = $this->id;
     $self->subscribed = $this->id;
     $self->delete();
 }
 /**
  * Handle the request
  *
  * Check the format and show the user info
  *
  * @return void
  */
 protected function handle()
 {
     parent::handle();
     if (!in_array($this->format, array('xml', 'json'))) {
         $this->clientError(_('API method not found.'), 404);
     }
     if (!$this->other instanceof Profile) {
         $this->clientError(_('Could not unfollow user: User not found.'), 403);
     }
     // Don't allow unsubscribing from yourself!
     if ($this->scoped->id == $this->other->id) {
         $this->clientError(_("You cannot unfollow yourself."), 403);
     }
     // throws an exception on error
     Subscription::cancel($this->scoped, $this->other);
     $this->initDocument($this->format);
     $this->showProfile($this->other, $this->format);
     $this->endDocument($this->format);
 }
示例#9
0
function get_blocked_subs()
{
    $query = "SELECT subscription.* " . "FROM subscription " . "INNER JOIN profile_block " . "ON blocker=subscribed " . "AND blocked=subscriber";
    $subscription = new Subscription();
    $subscription->query($query);
    return $subscription;
}
$dry = have_option('dry-run');
$sub = get_blocked_subs();
$count = $sub->N;
while ($sub->fetch()) {
    $subber = Profile::staticGet('id', $sub->subscriber);
    $subbed = Profile::staticGet('id', $sub->subscribed);
    if (!$subber || !$subbed) {
        print "Bogus entry! {$sub->subscriber} subbed to {$sub->subscribed}\n";
        continue;
    }
    print "{$subber->nickname} ({$subber->id}) blocked but subbed to {$subbed->nickname} ({$subbed->id})";
    if ($dry) {
        print ": skipping; dry run\n";
    } else {
        Subscription::cancel($subber, $subbed);
        print ": removed\n";
    }
}
print "\n";
if ($dry && $count > 0) {
    print "Be sure to run without --dry-run to remove the bad entries!\n";
} else {
    print "done.\n";
}
function updateOStatus($user)
{
    if (!have_option('q', 'quiet')) {
        echo "{$user->nickname}...";
    }
    $up = $user->getProfile();
    $sp = $user->getSubscriptions();
    $rps = array();
    while ($sp->fetch()) {
        $remote = Remote_profile::staticGet('id', $sp->id);
        if (!empty($remote)) {
            $rps[] = clone $sp;
        }
    }
    if (!have_option('q', 'quiet')) {
        echo count($rps) . "\n";
    }
    foreach ($rps as $rp) {
        try {
            if (!have_option('q', 'quiet')) {
                echo "Checking {$rp->nickname}...";
            }
            $op = Ostatus_profile::ensureProfileURL($rp->profileurl);
            if (empty($op)) {
                echo "can't convert.\n";
                continue;
            } else {
                if (!have_option('q', 'quiet')) {
                    echo "Converting...";
                }
                Subscription::start($up, $op->localProfile());
                Subscription::cancel($up, $rp);
                if (!have_option('q', 'quiet')) {
                    echo "done.\n";
                }
            }
        } catch (Exception $e) {
            if (!have_option('q', 'quiet')) {
                echo "fail.\n";
            }
            common_log(LOG_NOTICE, "Couldn't convert OMB subscription (" . $up->nickname . ", " . $rp->nickname . ") to OStatus: " . $e->getMessage());
            continue;
        }
    }
}
 /**
  * Delete the subscription
  *
  * @return void
  */
 function deleteSubscription()
 {
     if (!$this->scoped instanceof Profile || $this->scoped->id != $this->_subscriber->id) {
         // TRANS: Client exception thrown when trying to delete a subscription of another user.
         throw new ClientException(_("Cannot delete someone else's subscription."), 403);
     }
     Subscription::cancel($this->_subscriber, $this->_subscribed);
 }
示例#12
0
 function moveActivity($act, $sink, $user, $remote)
 {
     if (empty($user)) {
         // TRANS: Exception thrown if a non-existing user is provided. %s is a user ID.
         throw new Exception(sprintf(_('No such user "%s".'), $act->actor->id));
     }
     switch ($act->verb) {
         /*        case ActivityVerb::FAVORITE:
                   $this->log(LOG_INFO,
                              "Moving favorite of {$act->objects[0]->id} by ".
                              "{$act->actor->id} to {$remote->nickname}.");
                   // push it, then delete local
                   $sink->postActivity($act);
                   $notice = Notice::getKV('uri', $act->objects[0]->id);
                   if (!empty($notice)) {
                       $fave = Fave::pkeyGet(array('user_id' => $user->id,
                                                   'notice_id' => $notice->id));
                       $fave->delete();
                   }
                   break;*/
         case ActivityVerb::POST:
             $this->log(LOG_INFO, "Moving notice {$act->objects[0]->id} by " . "{$act->actor->id} to {$remote->nickname}.");
             // XXX: send a reshare, not a post
             $sink->postActivity($act);
             $notice = Notice::getKV('uri', $act->objects[0]->id);
             if (!empty($notice)) {
                 $notice->delete();
             }
             break;
         case ActivityVerb::JOIN:
             $this->log(LOG_INFO, "Moving group join of {$act->objects[0]->id} by " . "{$act->actor->id} to {$remote->nickname}.");
             $sink->postActivity($act);
             $group = User_group::getKV('uri', $act->objects[0]->id);
             if (!empty($group)) {
                 $user->leaveGroup($group);
             }
             break;
         case ActivityVerb::FOLLOW:
             if ($act->actor->id === $user->getUri()) {
                 $this->log(LOG_INFO, "Moving subscription to {$act->objects[0]->id} by " . "{$act->actor->id} to {$remote->nickname}.");
                 $sink->postActivity($act);
                 try {
                     $other = Profile::fromUri($act->objects[0]->id);
                     Subscription::cancel($user->getProfile(), $other);
                 } catch (UnknownUriException $e) {
                     // Can't cancel subscription if we don't know who to alert
                 }
             } else {
                 $otherUser = User::getKV('uri', $act->actor->id);
                 if (!empty($otherUser)) {
                     $this->log(LOG_INFO, "Changing sub to {$act->objects[0]->id}" . "by {$act->actor->id} to {$remote->nickname}.");
                     $otherProfile = $otherUser->getProfile();
                     Subscription::ensureStart($otherProfile, $remote);
                     Subscription::cancel($otherProfile, $user->getProfile());
                 } else {
                     $this->log(LOG_NOTICE, "Not changing sub to {$act->objects[0]->id}" . "by remote {$act->actor->id} " . "to {$remote->nickname}.");
                 }
             }
             break;
     }
 }
示例#13
0
 /**
  * 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");
     }
 }
示例#14
0
 function _deleteSubscriptions()
 {
     $sub = new Subscription();
     $sub->subscriber = $this->getID();
     $sub->find();
     while ($sub->fetch()) {
         try {
             $other = $sub->getSubscribed();
             if (!$other->sameAs($this)) {
                 Subscription::cancel($this, $other);
             }
         } catch (NoResultException $e) {
             // Profile not found
             common_log(LOG_INFO, 'Subscribed profile id==' . $sub->subscribed . ' not found when deleting profile id==' . $this->getID() . ', ignoring...');
         } catch (ServerException $e) {
             // Subscription cancel failed
             common_log(LOG_INFO, 'Subscribed profile id==' . $other->getID() . ' could not be reached for unsubscription notice when deleting profile id==' . $this->getID() . ', ignoring...');
         }
     }
     $sub = new Subscription();
     $sub->subscribed = $this->getID();
     $sub->find();
     while ($sub->fetch()) {
         try {
             $other = $sub->getSubscriber();
             common_log(LOG_INFO, 'Subscriber profile id==' . $sub->subscribed . ' not found when deleting profile id==' . $this->getID() . ', ignoring...');
             if (!$other->sameAs($this)) {
                 Subscription::cancel($other, $this);
             }
         } catch (NoResultException $e) {
             // Profile not found
             common_log(LOG_INFO, 'Subscribed profile id==' . $sub->subscribed . ' not found when deleting profile id==' . $this->getID() . ', ignoring...');
         } catch (ServerException $e) {
             // Subscription cancel failed
             common_log(LOG_INFO, 'Subscriber profile id==' . $other->getID() . ' could not be reached for unsubscription notice when deleting profile id==' . $this->getID() . ', ignoring...');
         }
     }
     // Finally delete self-subscription
     $self = new Subscription();
     $self->subscriber = $this->getID();
     $self->subscribed = $this->getID();
     $self->delete();
 }
示例#15
0
 /**
  * Handle the request
  *
  * Check the format and show the user info
  *
  * @param array $args $_REQUEST data (unused)
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         $this->clientError(_('This method requires a POST.'), 400, $this->format);
         return;
     }
     if (!in_array($this->format, array('xml', 'json'))) {
         $this->clientError(_('API method not found.'), 404, $this->format);
         return;
     }
     if (empty($this->other)) {
         $this->clientError(_('Could not unfollow user: User not found.'), 403, $this->format);
         return;
     }
     // Don't allow unsubscribing from yourself!
     if ($this->user->id == $this->other->id) {
         $this->clientError(_("You cannot unfollow yourself."), 403, $this->format);
         return;
     }
     // throws an exception on error
     Subscription::cancel($this->user->getProfile(), $this->other);
     $this->initDocument($this->format);
     $this->showProfile($this->other, $this->format);
     $this->endDocument($this->format);
 }
 /**
  * Delete the subscription
  *
  * @return void
  */
 function deleteSubscription()
 {
     if (empty($this->auth_user) || $this->auth_user->id != $this->_subscriber->id) {
         // TRANS: Client exception thrown when trying to delete a subscription of another user.
         throw new ClientException(_("Cannot delete someone else's " . "subscription."), 403);
     }
     Subscription::cancel($this->_subscriber, $this->_subscribed);
     return;
 }
示例#17
0
 /**
  * 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()
 {
     common_log(LOG_INFO, sprintf('Canceling subscription from remote %s to local %s', $this->oprofile->getUri(), $this->target->getNickname()));
     try {
         Subscription::cancel($this->actor, $this->target);
     } catch (NoProfileException $e) {
         common_debug('Could not find profile for Subscription: ' . $e->getMessage());
     }
 }