One function for producing Atom entry for a Notice

Took the various places that we create an atom entry for a notice, and
jammed them together into one function of the notice class, and then
used that function. Also, added Atom threading extension and
categories for hashtags.
This commit is contained in:
Evan Prodromou 2009-03-22 16:58:38 -04:00
parent e947d9fdd1
commit 3ef4f251ac
3 changed files with 117 additions and 62 deletions

View File

@ -799,4 +799,98 @@ class Notice extends Memcached_DataObject
} }
} }
} }
function asAtomEntry($namespace=false, $source=false)
{
$profile = $this->getProfile();
$xs = new XMLStringer(true);
if ($namespace) {
$attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
} else {
$attrs = array();
}
$xs->elementStart('entry', $attrs);
if ($source) {
$xs->elementStart('source');
$xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
$xs->element('link', array('href' => $profile->profileurl));
$user = User::staticGet('id', $profile->id);
if (!empty($user)) {
$atom_feed = common_local_url('api',
array('apiaction' => 'statuses',
'method' => 'user_timeline',
'argument' => $profile->nickname.'.atom'));
$xs->element('link', array('rel' => 'self',
'type' => 'application/atom+xml',
'href' => $profile->profileurl));
$xs->element('link', array('rel' => 'license',
'href' => common_config('license', 'url')));
}
$xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
}
$xs->elementStart('author');
$xs->element('name', null, $profile->nickname);
$xs->element('uri', null, $profile->profileurl);
$xs->elementEnd('author');
if ($source) {
$xs->elementEnd('source');
}
$xs->element('title', null, $this->content);
$xs->element('summary', null, $this->content);
$xs->element('link', array('rel' => 'alternate',
'href' => $this->bestUrl()));
$xs->element('id', null, $this->uri);
$xs->element('published', null, common_date_w3dtf($this->created));
$xs->element('updated', null, common_date_w3dtf($this->modified));
if ($this->reply_to) {
$reply_notice = Notice::staticGet('id', $this->reply_to);
if (!empty($reply_notice)) {
$xs->element('link', array('rel' => 'related',
'href' => $reply_notice->bestUrl()));
$xs->element('thr:in-reply-to',
array('ref' => $reply_notice->uri,
'href' => $reply_notice->bestUrl()));
}
}
$xs->element('content', array('type' => 'html'), $this->rendered);
$tag = new Notice_tag();
$tag->notice_id = $this->id;
if ($tag->find()) {
while ($tag->fetch()) {
$xs->element('category', array('term' => $tag->tag));
}
}
$tag->free();
$xs->elementEnd('entry');
return $xs->getString();
}
function bestUrl()
{
if (!empty($this->url)) {
return $this->url;
} else if (!empty($this->uri) && preg_match('/^https?:/', $this->uri)) {
return $this->uri;
} else {
return common_local_url('shownotice',
array('notice' => $this->id));
}
}
} }

View File

@ -163,50 +163,25 @@ function jabber_send_notice($to, $notice)
function jabber_format_entry($profile, $notice) function jabber_format_entry($profile, $notice)
{ {
// FIXME: notice url might be remote $entry = $notice->asAtomEntry(true, true);
$noticeurl = common_local_url('shownotice', $xs = new XMLStringer();
array('notice' => $notice->id)); $xs->elementStart('html', array('xmlns' => 'http://jabber.org/protocol/xhtml-im'));
$xs->elementStart('body', array('xmlns' => 'http://www.w3.org/1999/xhtml'));
$msg = jabber_format_notice($profile, $notice); $xs->element('a', array('href' => $profile->profileurl),
$profile->nickname);
$self_url = common_local_url('userrss', array('nickname' => $profile->nickname)); $xs->text(": ");
if (!empty($notice->rendered)) {
$entry = "\n<entry xmlns='http://www.w3.org/2005/Atom'>\n"; $xs->raw($notice->rendered);
$entry .= "<source>\n"; } else {
$entry .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n"; $xs->raw(common_render_content($notice->content, $notice));
$entry .= "<link href='" . htmlspecialchars($profile->profileurl) . "'/>\n";
$entry .= "<link rel='self' type='application/rss+xml' href='" . $self_url . "'/>\n";
$entry .= "<author><name>" . $profile->nickname . "</name></author>\n";
$entry .= "<icon>" . $profile->avatarUrl(AVATAR_PROFILE_SIZE) . "</icon>\n";
$entry .= "</source>\n";
$entry .= "<title>" . htmlspecialchars($msg) . "</title>\n";
$entry .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";
$entry .= "<link rel='alternate' href='" . $noticeurl . "' />\n";
$entry .= "<id>". $notice->uri . "</id>\n";
$entry .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
$entry .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
if ($notice->reply_to) {
$replyurl = common_local_url('shownotice',
array('notice' => $notice->reply_to));
$entry .= "<link rel='related' href='" . $replyurl . "'/>\n";
} }
$entry .= "</entry>\n"; $xs->elementEnd('body');
$xs->elementEnd('html');
$html = "\n<html xmlns='http://jabber.org/protocol/xhtml-im'>\n"; $html = $xs->asString();
$html .= "<body xmlns='http://www.w3.org/1999/xhtml'>\n";
$html .= "<a href='".htmlspecialchars($profile->profileurl)."'>".$profile->nickname."</a>: ";
$html .= ($notice->rendered) ? $notice->rendered : common_render_content($notice->content, $notice);
$html .= "\n</body>\n";
$html .= "\n</html>\n";
$address = "<addresses xmlns='http://jabber.org/protocol/address'>\n"; return $html . ' ' . $entry;
$address .= "<address type='replyto' jid='" . jabber_daemon_address() . "' />\n";
$address .= "</addresses>\n";
// FIXME: include a pubsub event, too.
return $html . $entry . $address;
} }
/** /**

View File

@ -238,21 +238,6 @@ class TwitterapiAction extends Action
$this->elementEnd('item'); $this->elementEnd('item');
} }
function show_twitter_atom_entry($entry)
{
$this->elementStart('entry');
$this->element('title', null, $entry['title']);
$this->element('content', array('type' => 'html'), $entry['content']);
$this->element('id', null, $entry['id']);
$this->element('published', null, $entry['published']);
$this->element('updated', null, $entry['updated']);
$this->element('link', array('href' => $entry['link'], 'rel' => 'alternate', 'type' => 'text/html'), null);
$this->elementStart('author');
$this->element('name', null, $entry['author']);
$this->elementEnd('author');
$this->elementEnd('entry');
}
function show_json_objects($objects) function show_json_objects($objects)
{ {
print(json_encode($objects)); print(json_encode($objects));
@ -392,13 +377,11 @@ class TwitterapiAction extends Action
if (is_array($notice)) { if (is_array($notice)) {
foreach ($notice as $n) { foreach ($notice as $n) {
$entry = $this->twitter_rss_entry_array($n); $this->raw($n->asAtomEntry());
$this->show_twitter_atom_entry($entry);
} }
} else { } else {
while ($notice->fetch()) { while ($notice->fetch()) {
$entry = $this->twitter_rss_entry_array($notice); $this->raw($notice->asAtomEntry());
$this->show_twitter_atom_entry($entry);
} }
} }
@ -578,13 +561,16 @@ class TwitterapiAction extends Action
function init_twitter_atom() function init_twitter_atom()
{ {
$this->startXML(); $this->startXML();
$this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US')); // FIXME: don't hardcode the language here!
$this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom',
'xml:lang' => 'en-US',
'xmlns:thr' => 'http://purl.org/syndication/thread/1.0'));
} }
function end_twitter_atom() function end_twitter_atom()
{ {
$this->endXML();
$this->elementEnd('feed'); $this->elementEnd('feed');
$this->endXML();
} }
function show_profile($profile, $content_type='xml', $notice=null) function show_profile($profile, $content_type='xml', $notice=null)