Removing Evan's obscure attentionType solution and directly using the attention array
// Check for optional attributes...
- if (!empty($activity->context)) {
+ if ($activity->context instanceof ActivityContext) {
- foreach ($activity->context->attention as $uri) {
+ foreach ($activity->context->attention as $uri=>$type) {
$profile = Profile::fromURI($uri);
throw new Exception(sprintf("Receiver profile not found: %d", $this->to_profile));
}
- $ctx->attention[] = $rprofile->getUri();
- $ctx->attentionType[$rprofile->getUri()] = ActivityObject::PERSON;
+ $ctx->attention[$rprofile->getUri()] = ActivityObject::PERSON;
$act->context = $ctx;
foreach ($reply_ids as $id) {
$rprofile = Profile::getKV('id', $id);
if ($rprofile instanceof Profile) {
- $ctx->attention[] = $rprofile->getUri();
- $ctx->attentionType[$rprofile->getUri()] = ActivityObject::PERSON;
+ $ctx->attention[$rprofile->getUri()] = ActivityObject::PERSON;
}
}
$groups = $this->getGroups();
foreach ($groups as $group) {
- $ctx->attention[] = $group->getUri();
- $ctx->attentionType[$group->getUri()] = ActivityObject::GROUP;
+ $ctx->attention[$group->getUri()] = ActivityObject::GROUP;
}
switch ($this->scope) {
case Notice::PUBLIC_SCOPE:
- $ctx->attention[] = "http://activityschema.org/collection/public";
- $ctx->attentionType["http://activityschema.org/collection/public"] = ActivityObject::COLLECTION;
+ $ctx->attention[ActivityContext::ATTN_PUBLIC] = ActivityObject::COLLECTION;
break;
case Notice::FOLLOWER_SCOPE:
$surl = common_local_url("subscribers", array('nickname' => $profile->nickname));
- $ctx->attention[] = $surl;
- $ctx->attentionType[$surl] = ActivityObject::COLLECTION;
+ $ctx->attention[$surl] = ActivityObject::COLLECTION;
break;
}
'href' => $this->context->conversation));
}
- foreach ($this->context->attention as $attnURI) {
+ foreach ($this->context->attention as $attnURI=>$type) {
$xs->element('link', array('rel' => ActivityContext::MENTIONED,
+ ActivityContext::OBJECTTYPE => $type, // FIXME: undocumented
'href' => $attnURI));
}
public $replyToID;
public $replyToUrl;
public $location;
- public $attention = array();
- public $attentionType = array();
+ public $attention = array(); // 'uri' => 'type'
public $conversation;
public $scope;
const GEORSS = 'http://www.georss.org/georss';
const OSTATUS = 'http://ostatus.org/schema/1.0';
- const INREPLYTO = 'in-reply-to';
- const REF = 'ref';
- const HREF = 'href';
+ const INREPLYTO = 'in-reply-to';
+ const REF = 'ref';
+ const HREF = 'href';
+ const OBJECTTYPE = 'ostatus:object-type'; // FIXME: Undocumented!
const POINT = 'point';
const MENTIONED = 'mentioned';
const CONVERSATION = 'ostatus:conversation';
+ const ATTN_PUBLIC = 'http://activityschema.org/collection/public';
+
function __construct($element = null)
{
if (empty($element)) {
$links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
- $attention = array();
for ($i = 0; $i < $links->length; $i++) {
$link = $links->item($i);
- $linkRel = $link->getAttribute(ActivityUtils::REL);
-
- if ($linkRel == self::MENTIONED) {
- $attention[] = $link->getAttribute(self::HREF);
+ $linkRel = $link->getAttribute(ActivityUtils::REL);
+ $linkHref = $link->getAttribute(self::HREF);
+ if ($linkRel == self::MENTIONED && $linkHref !== '') {
+ $this->attention[$linkHref] = $link->getAttribute(ActivityContext::OBJECTTYPE);
}
}
- $this->attention = array_unique($attention);
}
/**
{
$tos = array();
- foreach ($this->attention as $attnUrl) {
- if (array_key_exists($attnUrl, $this->attentionType)) {
- $type = ActivityObject::canonicalType($this->attentionType[$attnUrl]);
- } else {
- $type = ActivityObject::canonicalType(ActivityObject::PERSON);
- }
+ foreach ($this->attention as $attnUrl => $attnType) {
$to = array(
- 'objectType' => $type,
- 'id' => $attnUrl
+ 'objectType' => $attnType, // can be empty
+ 'id' => $attnUrl,
);
$tos[] = $to;
}
return $saved;
}
- function filterAttention($attn)
+ protected function filterAttention(array $attn)
{
- $groups = array();
- $replies = array();
+ $groups = array(); // TODO: context->attention
+ $replies = array(); // TODO: context->attention
- foreach (array_unique($attn) as $recipient) {
+ foreach ($attn as $recipient=>$type) {
// Is the recipient a local user?
$user = User::getKV('uri', $recipient);
- if ($user) {
- // @fixme sender verification, spam etc?
+ if ($user instanceof User) {
+ // TODO: @fixme sender verification, spam etc?
$replies[] = $recipient;
continue;
}
}
// Is the recipient a local group?
- // @fixme uri on user_group isn't reliable yet
+ // TODO: @fixme uri on user_group isn't reliable yet
// $group = User_group::getKV('uri', $recipient);
$id = OStatusPlugin::localGroupFromUrl($recipient);
if ($target instanceof User_group) {
$uri = $target->getUri();
- if (!in_array($uri, $activity->context->attention)) {
+ if (!array_key_exists($uri, $activity->context->attention)) {
// @todo FIXME: please document (i18n).
// TRANS: Client exception thrown when ...
- throw new ClientException(_('Bookmark not posted to this group.'));
+ throw new ClientException(_('Object not posted to this group.'));
}
} else if ($target instanceof User) {
$uri = $target->uri;
$original = Notice::getKV('uri',
$activity->context->replyToID);
}
- if (!in_array($uri, $activity->context->attention) &&
+ if (!array_key_exists($uri, $activity->context->attention) &&
(empty($original) ||
$original->profile_id != $target->id)) {
// @todo FIXME: Please document (i18n).
}
}
- $replies = $activity->context->attention;
-
$options['groups'] = array();
- $options['replies'] = array();
+ $options['replies'] = array(); // TODO: context->attention
- foreach ($replies as $replyURI) {
- $other = Profile::fromURI($replyURI);
- if (!empty($other)) {
- $options['replies'][] = $replyURI;
+ foreach ($activity->context->attention as $attnUrl=>$type) {
+ $other = Profile::fromURI($attnUrl);
+ if ($other instanceof Profile) {
+ $options['replies'][] = $attnUrl;
} else {
- $group = User_group::getKV('uri', $replyURI);
- if (!empty($group)) {
- $options['groups'][] = $replyURI;
+ // Maybe we can get rid of this since every User_group got a Profile?
+ // TODO: Make sure the above replies get sorted properly for groups (or handled afterwards)
+ $group = User_group::getKV('uri', $attnUrl);
+ if ($group instanceof User_group) {
+ $options['groups'][] = $attnUrl;
}
}
}
}
// Notice must be to the attention of this group
- $context = $this->activity->context;
-
- if (empty($context->attention)) {
+ if (empty($this->activity->context->attention)) {
// TRANS: Client exception.
throw new ClientException("Not to the attention of anyone.");
} else {
$uri = common_local_url('groupbyid', array('id' => $this->group->id));
- if (!in_array($uri, $context->attention)) {
+
+ if (!array_key_exists($uri, $this->activity->context->attention)) {
// TRANS: Client exception.
throw new ClientException("Not to the attention of this group.");
}
throw new ClientException(_m('In reply to a notice not by this user and not mentioning this user.'));
}
} else if (!empty($context->attention)) {
- if (!in_array($this->user->uri, $context->attention) &&
- !in_array(common_profile_url($this->user->nickname), $context->attention)) {
- common_log(LOG_ERR, "{$this->user->uri} not in attention list (".implode(',', $context->attention).")");
+ if (!array_key_exists($this->user->uri, $context->attention) &&
+ !array_key_exists(common_profile_url($this->user->nickname), $context->attention)) {
+ common_log(LOG_ERR, "{$this->user->uri} not in attention list (".implode(',', array_keys($context->attention)).')');
// TRANS: Client exception.
throw new ClientException(_m('To the attention of user(s), not including this one.'));
}
}
if ($activity->context) {
- // Any individual or group attn: targets?
- $replies = $activity->context->attention;
- $options['groups'] = $this->filterReplies($oprofile, $replies);
- $options['replies'] = $replies;
+ // TODO: context->attention
+ list($options['groups'], $options['replies'])
+ = $this->filterReplies($oprofile, $activity->context->attention);
// Maintain direct reply associations
// @todo FIXME: What about conversation ID?
}
if ($activity->context) {
- // Any individual or group attn: targets?
- $replies = $activity->context->attention;
- $options['groups'] = $this->filterReplies($oprofile, $replies);
- $options['replies'] = $replies;
+ // TODO: context->attention
+ list($options['groups'], $options['replies'])
+ = $this->filterReplies($oprofile, $activity->context->attention);
// Maintain direct reply associations
// @todo FIXME: What about conversation ID?
* @param array in/out &$attention_uris set of URIs, will be pruned on output
* @return array of group IDs
*/
- protected function filterReplies($sender, &$attention_uris)
+ protected function filterReplies($sender, array $attention)
{
- common_log(LOG_DEBUG, "Original reply recipients: " . implode(', ', $attention_uris));
+ common_log(LOG_DEBUG, "Original reply recipients: " . implode(', ', $attention));
$groups = array();
$replies = array();
- foreach (array_unique($attention_uris) as $recipient) {
+ foreach ($attention as $recipient=>$type) {
// Is the recipient a local user?
$user = User::getKV('uri', $recipient);
- if ($user) {
+ if ($user instanceof User) {
// @todo FIXME: Sender verification, spam etc?
$replies[] = $recipient;
continue;
}
// Is the recipient a local group?
- // $group = User_group::getKV('uri', $recipient);
+ // TODO: $group = User_group::getKV('uri', $recipient);
$id = OStatusPlugin::localGroupFromUrl($recipient);
if ($id) {
$group = User_group::getKV('id', $id);
- if ($group) {
+ if ($group instanceof User_group) {
// Deliver to all members of this local group if allowed.
$profile = $sender->localProfile();
if ($profile->isMember($group)) {
}
}
- $attention_uris = $replies;
common_log(LOG_DEBUG, "Local reply recipients: " . implode(', ', $replies));
common_log(LOG_DEBUG, "Local group recipients: " . implode(', ', $groups));
- return $groups;
+ return array($groups, $replies);
}
/**
$this->assertEquals('http://example.net/notice/12', $act->context->replyToID);
$this->assertEquals('http://example.net/notice/12', $act->context->replyToUrl);
$this->assertEquals('http://example.net/conversation/11', $act->context->conversation);
- $this->assertEquals(array('http://example.net/user/1'), $act->context->attention);
+ $this->assertEquals(array('http://example.net/user/1'), array_keys($act->context->attention));
$this->assertFalse(empty($act->objects[0]));
$this->assertEquals($act->objects[0]->content,