OStatus: fix group delivery, send reply/group Salmon pings from background.

This commit is contained in:
Brion Vibber 2010-02-24 02:19:13 +00:00
parent 868ae8e62b
commit 2e58802cc9
5 changed files with 140 additions and 51 deletions

View File

@ -78,11 +78,16 @@ class OStatusPlugin extends Plugin
*/ */
function onEndInitializeQueueManager(QueueManager $qm) function onEndInitializeQueueManager(QueueManager $qm)
{ {
// Prepare outgoing distributions after notice save.
$qm->connect('ostatus', 'OStatusQueueHandler');
// Outgoing from our internal PuSH hub // Outgoing from our internal PuSH hub
$qm->connect('hubverify', 'HubVerifyQueueHandler'); $qm->connect('hubverify', 'HubVerifyQueueHandler');
$qm->connect('hubdistrib', 'HubDistribQueueHandler');
$qm->connect('hubout', 'HubOutQueueHandler'); $qm->connect('hubout', 'HubOutQueueHandler');
// Outgoing Salmon replies (when we don't need a return value)
$qm->connect('salmonout', 'SalmonOutQueueHandler');
// Incoming from a foreign PuSH hub // Incoming from a foreign PuSH hub
$qm->connect('pushinput', 'PushInputQueueHandler'); $qm->connect('pushinput', 'PushInputQueueHandler');
return true; return true;
@ -93,7 +98,7 @@ class OStatusPlugin extends Plugin
*/ */
function onStartEnqueueNotice($notice, &$transports) function onStartEnqueueNotice($notice, &$transports)
{ {
$transports[] = 'hubdistrib'; $transports[] = 'ostatus';
return true; return true;
} }
@ -199,25 +204,6 @@ class OStatusPlugin extends Plugin
function onEndNoticeSave($notice) function onEndNoticeSave($notice)
{ {
$mentioned = $notice->getReplies();
foreach ($mentioned as $profile_id) {
$oprofile = Ostatus_profile::staticGet('profile_id', $profile_id);
if (!empty($oprofile) && !empty($oprofile->salmonuri)) {
common_log(LOG_INFO, "Sending notice '{$notice->uri}' to remote profile '{$oprofile->uri}'.");
// FIXME: this needs to go out in a queue handler
$xml = '<?xml version="1.0" encoding="UTF-8" ?' . '>';
$xml .= $notice->asAtomEntry(true, true);
$salmon = new Salmon();
$salmon->post($oprofile->salmonuri, $xml);
}
}
} }
/** /**

View File

@ -46,6 +46,11 @@ class GroupsalmonAction extends SalmonAction
$this->clientError(_('No such group.')); $this->clientError(_('No such group.'));
} }
$oprofile = Ostatus_profile::staticGet('group_id', $id);
if ($oprofile) {
$this->clientError(_m("Can't accept remote posts for a remote group."));
}
return true; return true;
} }
@ -74,13 +79,13 @@ class GroupsalmonAction extends SalmonAction
throw new ClientException("Not to the attention of anyone."); throw new ClientException("Not to the attention of anyone.");
} else { } else {
$uri = common_local_url('groupbyid', array('id' => $this->group->id)); $uri = common_local_url('groupbyid', array('id' => $this->group->id));
if (!in_array($context->attention, $uri)) { if (!in_array($uri, $context->attention)) {
throw new ClientException("Not to the attention of this group."); throw new ClientException("Not to the attention of this group.");
} }
} }
$profile = $this->ensureProfile(); $profile = $this->ensureProfile();
// @fixme save the post $this->saveNotice();
} }
/** /**

View File

@ -650,6 +650,7 @@ class Ostatus_profile extends Memcached_DataObject
*/ */
protected function filterReplies($sender, &$attention_uris) protected function filterReplies($sender, &$attention_uris)
{ {
common_log(LOG_DEBUG, "Original reply recipients: " . implode(', ', $attention_uris));
$groups = array(); $groups = array();
$replies = array(); $replies = array();
foreach ($attention_uris as $recipient) { foreach ($attention_uris as $recipient) {
@ -668,6 +669,8 @@ class Ostatus_profile extends Memcached_DataObject
// Deliver to local members of this remote group. // Deliver to local members of this remote group.
// @fixme sender verification? // @fixme sender verification?
$groups[] = $oprofile->group_id; $groups[] = $oprofile->group_id;
} else {
common_log(LOG_DEBUG, "Skipping reply to remote profile $recipient");
} }
continue; continue;
} }
@ -683,14 +686,24 @@ class Ostatus_profile extends Memcached_DataObject
$group = User_group::staticGet('id', $id); $group = User_group::staticGet('id', $id);
if ($group) { if ($group) {
// Deliver to all members of this local group if allowed. // Deliver to all members of this local group if allowed.
if ($sender->localProfile()->isMember($group)) { $profile = $sender->localProfile();
if ($profile->isMember($group)) {
$groups[] = $group->id; $groups[] = $group->id;
} else {
common_log(LOG_DEBUG, "Skipping reply to local group $group->nickname as sender $profile->id is not a member");
} }
continue; continue;
} else {
common_log(LOG_DEBUG, "Skipping reply to bogus group $recipient");
} }
} }
common_log(LOG_DEBUG, "Skipping reply to unrecognized profile $recipient");
} }
$attention_uris = $replies; $attention_uris = $replies;
common_log(LOG_DEBUG, "Local reply recipients: " . implode(', ', $replies));
common_log(LOG_DEBUG, "Local group recipients: " . implode(', ', $groups));
return $groups; return $groups;
} }

View File

@ -18,46 +18,89 @@
*/ */
/** /**
* Send a PuSH subscription verification from our internal hub. * Prepare PuSH and Salmon distributions for an outgoing message.
* Queue up final distribution for *
* @package Hub * @package OStatusPlugin
* @author Brion Vibber <brion@status.net> * @author Brion Vibber <brion@status.net>
*/ */
class HubDistribQueueHandler extends QueueHandler class OStatusQueueHandler extends QueueHandler
{ {
function transport() function transport()
{ {
return 'hubdistrib'; return 'ostatus';
} }
function handle($notice) function handle($notice)
{ {
assert($notice instanceof Notice); assert($notice instanceof Notice);
$this->pushUser($notice); $this->notice = $notice;
$this->user = User::staticGet($notice->profile_id);
$this->pushUser();
foreach ($notice->getGroups() as $group) { foreach ($notice->getGroups() as $group) {
$this->pushGroup($notice, $group->id); $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
if ($oprofile) {
$this->pingReply($oprofile);
} else {
$this->pushGroup($group->id);
} }
}
foreach ($notice->getReplies() as $profile_id) {
$oprofile = Ostatus_profile::staticGet('profile_id', $profile_id);
if ($oprofile) {
$this->pingReply($oprofile);
}
}
return true; return true;
} }
function pushUser($notice) function pushUser()
{ {
// See if there's any PuSH subscriptions, including OStatus clients. if ($this->user) {
// @fixme handle group subscriptions as well // For local posts, ping the PuSH hub to update their feed.
// http://identi.ca/api/statuses/user_timeline/1.atom // http://identi.ca/api/statuses/user_timeline/1.atom
$feed = common_local_url('ApiTimelineUser', $feed = common_local_url('ApiTimelineUser',
array('id' => $notice->profile_id, array('id' => $this->user->id,
'format' => 'atom')); 'format' => 'atom'));
$this->pushFeed($feed, array($this, 'userFeedForNotice'), $notice); $this->pushFeed($feed, array($this, 'userFeedForNotice'));
}
} }
function pushGroup($notice, $group_id) function pushGroup($group_id)
{ {
// For a local group, ping the PuSH hub to update its feed.
// Updates may come from either a local or a remote user.
$feed = common_local_url('ApiTimelineGroup', $feed = common_local_url('ApiTimelineGroup',
array('id' => $group_id, array('id' => $group_id,
'format' => 'atom')); 'format' => 'atom'));
$this->pushFeed($feed, array($this, 'groupFeedForNotice'), $group_id, $notice); $this->pushFeed($feed, array($this, 'groupFeedForNotice'), $group_id);
}
function pingReply($oprofile)
{
if ($this->user) {
if (!empty($oprofile->salmonuri)) {
// For local posts, send a Salmon ping to the mentioned
// remote user or group.
// @fixme as an optimization we can skip this if the
// remote profile is subscribed to the author.
common_log(LOG_INFO, "Prepping to send notice '{$this->notice->uri}' to remote profile '{$oprofile->uri}'.");
$xml = '<?xml version="1.0" encoding="UTF-8" ?' . '>';
$xml .= $this->notice->asAtomEntry(true, true);
$data = array('salmonuri' => $oprofile->salmonuri,
'entry' => $xml);
$qm = QueueManager::get();
$qm->enqueue($data, 'salmonout');
}
}
} }
/** /**
@ -122,7 +165,6 @@ class HubDistribQueueHandler extends QueueHandler
function pushFeedInternal($atom, $sub) function pushFeedInternal($atom, $sub)
{ {
common_log(LOG_INFO, "Preparing $sub->N PuSH distribution(s) for $sub->topic"); common_log(LOG_INFO, "Preparing $sub->N PuSH distribution(s) for $sub->topic");
$qm = QueueManager::get();
while ($sub->fetch()) { while ($sub->fetch()) {
$sub->distribute($atom); $sub->distribute($atom);
} }
@ -130,20 +172,19 @@ class HubDistribQueueHandler extends QueueHandler
/** /**
* Build a single-item version of the sending user's Atom feed. * Build a single-item version of the sending user's Atom feed.
* @param Notice $notice
* @return string * @return string
*/ */
function userFeedForNotice($notice) function userFeedForNotice()
{ {
// @fixme this feels VERY hacky... // @fixme this feels VERY hacky...
// should probably be a cleaner way to do it // should probably be a cleaner way to do it
ob_start(); ob_start();
$api = new ApiTimelineUserAction(); $api = new ApiTimelineUserAction();
$api->prepare(array('id' => $notice->profile_id, $api->prepare(array('id' => $this->notice->profile_id,
'format' => 'atom', 'format' => 'atom',
'max_id' => $notice->id, 'max_id' => $this->notice->id,
'since_id' => $notice->id - 1)); 'since_id' => $this->notice->id - 1));
$api->showTimeline(); $api->showTimeline();
$feed = ob_get_clean(); $feed = ob_get_clean();
@ -155,7 +196,7 @@ class HubDistribQueueHandler extends QueueHandler
return $feed; return $feed;
} }
function groupFeedForNotice($group_id, $notice) function groupFeedForNotice($group_id)
{ {
// @fixme this feels VERY hacky... // @fixme this feels VERY hacky...
// should probably be a cleaner way to do it // should probably be a cleaner way to do it
@ -164,8 +205,8 @@ class HubDistribQueueHandler extends QueueHandler
$api = new ApiTimelineGroupAction(); $api = new ApiTimelineGroupAction();
$args = array('id' => $group_id, $args = array('id' => $group_id,
'format' => 'atom', 'format' => 'atom',
'max_id' => $notice->id, 'max_id' => $this->notice->id,
'since_id' => $notice->id - 1); 'since_id' => $this->notice->id - 1);
$api->prepare($args); $api->prepare($args);
$api->handle($args); $api->handle($args);
$feed = ob_get_clean(); $feed = ob_get_clean();

View File

@ -0,0 +1,44 @@
<?php
/*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2010, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Send a Salmon notification in the background.
* @package OStatusPlugin
* @author Brion Vibber <brion@status.net>
*/
class SalmonOutQueueHandler extends QueueHandler
{
function transport()
{
return 'salmonout';
}
function handle($data)
{
assert(is_array($data));
assert(is_string($data['salmonuri']));
assert(is_string($data['entry']));
$salmon = new Salmon();
$salmon->post($data['salmonuri'], $data['entry']);
// @fixme detect failure and attempt to resend
return true;
}
}