forked from GNUsocial/gnu-social
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing
This commit is contained in:
commit
d4b47eb699
162
lib/activity.php
162
lib/activity.php
@ -34,6 +34,7 @@ if (!defined('STATUSNET')) {
|
||||
|
||||
class PoCoURL
|
||||
{
|
||||
const URLS = 'urls';
|
||||
const TYPE = 'type';
|
||||
const VALUE = 'value';
|
||||
const PRIMARY = 'primary';
|
||||
@ -55,7 +56,7 @@ class PoCoURL
|
||||
$xs->elementStart('poco:urls');
|
||||
$xs->element('poco:type', null, $this->type);
|
||||
$xs->element('poco:value', null, $this->value);
|
||||
if ($this->primary) {
|
||||
if (!empty($this->primary)) {
|
||||
$xs->element('poco:primary', null, 'true');
|
||||
}
|
||||
$xs->elementEnd('poco:urls');
|
||||
@ -70,21 +71,19 @@ class PoCoAddress
|
||||
|
||||
public $formatted;
|
||||
|
||||
function __construct($formatted)
|
||||
{
|
||||
if (empty($formatted)) {
|
||||
return null;
|
||||
}
|
||||
$this->formatted = $formatted;
|
||||
}
|
||||
// @todo Other address fields
|
||||
|
||||
function asString()
|
||||
{
|
||||
$xs = new XMLStringer(true);
|
||||
$xs->elementStart('poco:address');
|
||||
$xs->element('poco:formatted', null, $this->formatted);
|
||||
$xs->elementEnd('poco:address');
|
||||
return $xs->getString();
|
||||
if (!empty($this->formatted)) {
|
||||
$xs = new XMLStringer(true);
|
||||
$xs->elementStart('poco:address');
|
||||
$xs->element('poco:formatted', null, $this->formatted);
|
||||
$xs->elementEnd('poco:address');
|
||||
return $xs->getString();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -92,26 +91,117 @@ class PoCo
|
||||
{
|
||||
const NS = 'http://portablecontacts.net/spec/1.0';
|
||||
|
||||
const USERNAME = 'preferredUsername';
|
||||
const NOTE = 'note';
|
||||
const URLS = 'urls';
|
||||
const USERNAME = 'preferredUsername';
|
||||
const DISPLAYNAME = 'displayName';
|
||||
const NOTE = 'note';
|
||||
|
||||
public $preferredUsername;
|
||||
public $displayName;
|
||||
public $note;
|
||||
public $address;
|
||||
public $urls = array();
|
||||
|
||||
function __construct($profile)
|
||||
function __construct($element = null)
|
||||
{
|
||||
$this->preferredUsername = $profile->nickname;
|
||||
$this->displayName = $profile->getBestName();
|
||||
if (empty($element)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->note = $profile->bio;
|
||||
$this->address = new PoCoAddress($profile->location);
|
||||
$this->preferredUsername = ActivityUtils::childContent(
|
||||
$element,
|
||||
self::USERNAME,
|
||||
self::NS
|
||||
);
|
||||
|
||||
$this->displayName = ActivityUtils::childContent(
|
||||
$element,
|
||||
self::DISPLAYNAME,
|
||||
self::NS
|
||||
);
|
||||
|
||||
$this->note = ActivityUtils::childContent(
|
||||
$element,
|
||||
self::NOTE,
|
||||
self::NS
|
||||
);
|
||||
|
||||
$this->address = $this->_getAddress($element);
|
||||
$this->urls = $this->_getURLs($element);
|
||||
}
|
||||
|
||||
private function _getURLs($element)
|
||||
{
|
||||
$urlEls = $element->getElementsByTagnameNS(self::NS, PoCoURL::URLS);
|
||||
$urls = array();
|
||||
|
||||
foreach ($urlEls as $urlEl) {
|
||||
|
||||
$type = ActivityUtils::childContent(
|
||||
$urlEl,
|
||||
PoCoURL::TYPE,
|
||||
PoCo::NS
|
||||
);
|
||||
|
||||
$value = ActivityUtils::childContent(
|
||||
$urlEl,
|
||||
PoCoURL::VALUE,
|
||||
PoCo::NS
|
||||
);
|
||||
|
||||
$primary = ActivityUtils::childContent(
|
||||
$urlEl,
|
||||
PoCoURL::PRIMARY,
|
||||
PoCo::NS
|
||||
);
|
||||
|
||||
array_push($urls, new PoCoURL($type, $value, $primary));
|
||||
}
|
||||
return $urls;
|
||||
}
|
||||
|
||||
private function _getAddress($element)
|
||||
{
|
||||
$addressEl = ActivityUtils::child(
|
||||
$element,
|
||||
PoCoAddress::ADDRESS,
|
||||
PoCo::NS
|
||||
);
|
||||
|
||||
$formatted = ActivityUtils::childContent(
|
||||
$addressEl,
|
||||
PoCoAddress::FORMATTED,
|
||||
self::NS
|
||||
);
|
||||
|
||||
if (!empty($formatted)) {
|
||||
$address = new PoCoAddress();
|
||||
$address->formatted = $formatted;
|
||||
return $address;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function fromProfile($profile)
|
||||
{
|
||||
if (empty($profile)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$poco = new PoCo();
|
||||
|
||||
$poco->preferredUsername = $profile->nickname;
|
||||
$poco->displayName = $profile->getBestName();
|
||||
|
||||
$poco->note = $profile->bio;
|
||||
|
||||
$paddy = new PoCoAddress();
|
||||
$paddy->formatted = $profile->location;
|
||||
$poco->address = $paddy;
|
||||
|
||||
if (!empty($profile->homepage)) {
|
||||
array_push(
|
||||
$this->urls,
|
||||
$poco->urls,
|
||||
new PoCoURL(
|
||||
'homepage',
|
||||
$profile->homepage,
|
||||
@ -119,6 +209,8 @@ class PoCo
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $poco;
|
||||
}
|
||||
|
||||
function asString()
|
||||
@ -381,6 +473,8 @@ class ActivityObject
|
||||
public $source;
|
||||
public $avatar;
|
||||
public $geopoint;
|
||||
public $poco;
|
||||
public $displayName;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@ -433,7 +527,6 @@ class ActivityObject
|
||||
|
||||
$this->link = ActivityUtils::getPermalink($element);
|
||||
|
||||
// XXX: grab PoCo stuff
|
||||
}
|
||||
|
||||
// Some per-type attributes...
|
||||
@ -441,8 +534,9 @@ class ActivityObject
|
||||
$this->displayName = $this->title;
|
||||
|
||||
// @fixme we may have multiple avatars with different resolutions specified
|
||||
$this->avatar = ActivityUtils::getLink($element, 'avatar');
|
||||
$this->nickname = ActivityUtils::childContent($element, PoCo::USERNAME, PoCo::NS);
|
||||
$this->avatar = ActivityUtils::getLink($element, 'avatar');
|
||||
|
||||
$this->poco = new PoCo($element);
|
||||
}
|
||||
}
|
||||
|
||||
@ -497,7 +591,7 @@ class ActivityObject
|
||||
$object->geopoint = (float)$profile->lat . ' ' . (float)$profile->lon;
|
||||
}
|
||||
|
||||
$object->poco = new PoCo($profile);
|
||||
$object->poco = PoCo::fromProfile($profile);
|
||||
|
||||
return $object;
|
||||
}
|
||||
@ -526,11 +620,19 @@ class ActivityObject
|
||||
}
|
||||
|
||||
if (!empty($this->link)) {
|
||||
$xs->element('link', array('rel' => 'alternate', 'type' => 'text/html'),
|
||||
$this->link);
|
||||
$xs->element(
|
||||
'link',
|
||||
array(
|
||||
'rel' => 'alternate',
|
||||
'type' => 'text/html',
|
||||
'href' => $this->link
|
||||
),
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->type == ActivityObject::PERSON) {
|
||||
if ($this->type == ActivityObject::PERSON
|
||||
|| $this->type == ActivityObject::GROUP) {
|
||||
$xs->element(
|
||||
'link', array(
|
||||
'type' => empty($this->avatar) ? 'image/png' : $this->avatar->mediatype,
|
||||
@ -539,7 +641,7 @@ class ActivityObject
|
||||
? Avatar::defaultImage(AVATAR_PROFILE_SIZE)
|
||||
: $this->avatar->displayUrl()
|
||||
),
|
||||
''
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -78,11 +78,16 @@ class OStatusPlugin extends Plugin
|
||||
*/
|
||||
function onEndInitializeQueueManager(QueueManager $qm)
|
||||
{
|
||||
// Prepare outgoing distributions after notice save.
|
||||
$qm->connect('ostatus', 'OStatusQueueHandler');
|
||||
|
||||
// Outgoing from our internal PuSH hub
|
||||
$qm->connect('hubverify', 'HubVerifyQueueHandler');
|
||||
$qm->connect('hubdistrib', 'HubDistribQueueHandler');
|
||||
$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
|
||||
$qm->connect('pushinput', 'PushInputQueueHandler');
|
||||
return true;
|
||||
@ -93,7 +98,7 @@ class OStatusPlugin extends Plugin
|
||||
*/
|
||||
function onStartEnqueueNotice($notice, &$transports)
|
||||
{
|
||||
$transports[] = 'hubdistrib';
|
||||
$transports[] = 'ostatus';
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -199,25 +204,6 @@ class OStatusPlugin extends Plugin
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -46,6 +46,11 @@ class GroupsalmonAction extends SalmonAction
|
||||
$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;
|
||||
}
|
||||
|
||||
@ -74,13 +79,13 @@ class GroupsalmonAction extends SalmonAction
|
||||
throw new ClientException("Not to the attention of anyone.");
|
||||
} else {
|
||||
$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.");
|
||||
}
|
||||
}
|
||||
|
||||
$profile = $this->ensureProfile();
|
||||
// @fixme save the post
|
||||
$this->saveNotice();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -650,6 +650,7 @@ class Ostatus_profile extends Memcached_DataObject
|
||||
*/
|
||||
protected function filterReplies($sender, &$attention_uris)
|
||||
{
|
||||
common_log(LOG_DEBUG, "Original reply recipients: " . implode(', ', $attention_uris));
|
||||
$groups = array();
|
||||
$replies = array();
|
||||
foreach ($attention_uris as $recipient) {
|
||||
@ -668,6 +669,8 @@ class Ostatus_profile extends Memcached_DataObject
|
||||
// Deliver to local members of this remote group.
|
||||
// @fixme sender verification?
|
||||
$groups[] = $oprofile->group_id;
|
||||
} else {
|
||||
common_log(LOG_DEBUG, "Skipping reply to remote profile $recipient");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@ -683,14 +686,24 @@ class Ostatus_profile extends Memcached_DataObject
|
||||
$group = User_group::staticGet('id', $id);
|
||||
if ($group) {
|
||||
// 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;
|
||||
} else {
|
||||
common_log(LOG_DEBUG, "Skipping reply to local group $group->nickname as sender $profile->id is not a member");
|
||||
}
|
||||
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;
|
||||
common_log(LOG_DEBUG, "Local reply recipients: " . implode(', ', $replies));
|
||||
common_log(LOG_DEBUG, "Local group recipients: " . implode(', ', $groups));
|
||||
return $groups;
|
||||
}
|
||||
|
||||
|
@ -18,46 +18,89 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* Send a PuSH subscription verification from our internal hub.
|
||||
* Queue up final distribution for
|
||||
* @package Hub
|
||||
* Prepare PuSH and Salmon distributions for an outgoing message.
|
||||
*
|
||||
* @package OStatusPlugin
|
||||
* @author Brion Vibber <brion@status.net>
|
||||
*/
|
||||
class HubDistribQueueHandler extends QueueHandler
|
||||
class OStatusQueueHandler extends QueueHandler
|
||||
{
|
||||
function transport()
|
||||
{
|
||||
return 'hubdistrib';
|
||||
return 'ostatus';
|
||||
}
|
||||
|
||||
function handle($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) {
|
||||
$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;
|
||||
}
|
||||
|
||||
function pushUser($notice)
|
||||
|
||||
function pushUser()
|
||||
{
|
||||
// See if there's any PuSH subscriptions, including OStatus clients.
|
||||
// @fixme handle group subscriptions as well
|
||||
// http://identi.ca/api/statuses/user_timeline/1.atom
|
||||
$feed = common_local_url('ApiTimelineUser',
|
||||
array('id' => $notice->profile_id,
|
||||
'format' => 'atom'));
|
||||
$this->pushFeed($feed, array($this, 'userFeedForNotice'), $notice);
|
||||
if ($this->user) {
|
||||
// For local posts, ping the PuSH hub to update their feed.
|
||||
// http://identi.ca/api/statuses/user_timeline/1.atom
|
||||
$feed = common_local_url('ApiTimelineUser',
|
||||
array('id' => $this->user->id,
|
||||
'format' => 'atom'));
|
||||
$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',
|
||||
array('id' => $group_id,
|
||||
'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)
|
||||
{
|
||||
common_log(LOG_INFO, "Preparing $sub->N PuSH distribution(s) for $sub->topic");
|
||||
$qm = QueueManager::get();
|
||||
while ($sub->fetch()) {
|
||||
$sub->distribute($atom);
|
||||
}
|
||||
@ -130,20 +172,19 @@ class HubDistribQueueHandler extends QueueHandler
|
||||
|
||||
/**
|
||||
* Build a single-item version of the sending user's Atom feed.
|
||||
* @param Notice $notice
|
||||
* @return string
|
||||
*/
|
||||
function userFeedForNotice($notice)
|
||||
function userFeedForNotice()
|
||||
{
|
||||
// @fixme this feels VERY hacky...
|
||||
// should probably be a cleaner way to do it
|
||||
|
||||
ob_start();
|
||||
$api = new ApiTimelineUserAction();
|
||||
$api->prepare(array('id' => $notice->profile_id,
|
||||
$api->prepare(array('id' => $this->notice->profile_id,
|
||||
'format' => 'atom',
|
||||
'max_id' => $notice->id,
|
||||
'since_id' => $notice->id - 1));
|
||||
'max_id' => $this->notice->id,
|
||||
'since_id' => $this->notice->id - 1));
|
||||
$api->showTimeline();
|
||||
$feed = ob_get_clean();
|
||||
|
||||
@ -155,7 +196,7 @@ class HubDistribQueueHandler extends QueueHandler
|
||||
return $feed;
|
||||
}
|
||||
|
||||
function groupFeedForNotice($group_id, $notice)
|
||||
function groupFeedForNotice($group_id)
|
||||
{
|
||||
// @fixme this feels VERY hacky...
|
||||
// should probably be a cleaner way to do it
|
||||
@ -164,8 +205,8 @@ class HubDistribQueueHandler extends QueueHandler
|
||||
$api = new ApiTimelineGroupAction();
|
||||
$args = array('id' => $group_id,
|
||||
'format' => 'atom',
|
||||
'max_id' => $notice->id,
|
||||
'since_id' => $notice->id - 1);
|
||||
'max_id' => $this->notice->id,
|
||||
'since_id' => $this->notice->id - 1);
|
||||
$api->prepare($args);
|
||||
$api->handle($args);
|
||||
$feed = ob_get_clean();
|
44
plugins/OStatus/lib/salmonoutqueuehandler.php
Normal file
44
plugins/OStatus/lib/salmonoutqueuehandler.php
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user