3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @package OStatusPlugin
22 * @author James Walker <james@status.net>
25 if (!defined('STATUSNET')) {
29 class SalmonAction extends Action
34 function prepare($args)
36 StatusNet::setApi(true); // Send smaller error pages
38 parent::prepare($args);
40 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
41 $this->clientError(_('This method requires a POST.'));
44 if ($_SERVER['CONTENT_TYPE'] != 'application/atom+xml') {
45 $this->clientError(_('Salmon requires application/atom+xml'));
48 $xml = file_get_contents('php://input');
50 $dom = DOMDocument::loadXML($xml);
52 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
53 $dom->documentElement->localName != 'entry') {
54 $this->clientError(_m('Salmon post must be an Atom entry.'));
56 // XXX: check the signature
58 $this->act = new Activity($dom->documentElement);
63 * Check the posted activity type and break out to appropriate processing.
66 function handle($args)
68 StatusNet::setApi(true); // Send smaller error pages
70 // TODO : Insert new $xml -> notice code
72 if (Event::handle('StartHandleSalmon', array($this->activity))) {
73 switch ($this->act->verb)
75 case ActivityVerb::POST:
78 case ActivityVerb::SHARE:
81 case ActivityVerb::FAVORITE:
82 $this->handleFavorite();
84 case ActivityVerb::FOLLOW:
85 case ActivityVerb::FRIEND:
86 $this->handleFollow();
88 case ActivityVerb::UNFOLLOW:
89 $this->handleUnfollow();
91 case ActivityVerb::JOIN:
95 throw new ClientException(_("Unimplemented."));
97 Event::handle('EndHandleSalmon', array($this->activity));
101 function handlePost()
103 throw new ClientException(_("Unimplemented!"));
106 function handleFollow()
108 throw new ClientException(_("Unimplemented!"));
111 function handleUnfollow()
113 throw new ClientException(_("Unimplemented!"));
116 function handleFavorite()
118 throw new ClientException(_("Unimplemented!"));
122 * Remote user doesn't like one of our posts after all!
123 * Confirm the post is ours, and delete a local favorite event.
126 function handleUnfavorite()
128 throw new ClientException(_("Unimplemented!"));
134 function handleShare()
136 throw new ClientException(_("Unimplemented!"));
142 function handleJoin()
144 throw new ClientException(_("Unimplemented!"));
148 * @return Ostatus_profile
150 function ensureProfile()
152 $actor = $this->act->actor;
153 if (empty($actor->id)) {
154 common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
155 throw new Exception("Received a salmon slap from unidentified actor.");
158 return Ostatus_profile::ensureActorProfile($this->act);
162 * @fixme merge into Ostatus_profile::ensureActorProfile and friends
164 function createProfile()
166 $actor = $this->act->actor;
168 $profile = new Profile();
170 $profile->nickname = $this->nicknameFromURI($actor->id);
172 if (empty($profile->nickname)) {
173 $profile->nickname = common_nicknamize($actor->title);
176 $profile->fullname = $actor->title;
177 $profile->bio = $actor->summary; // XXX: is that right?
178 $profile->profileurl = $actor->link; // XXX: is that right?
179 $profile->created = common_sql_now();
181 $id = $profile->insert();
184 common_log_db_error($profile, 'INSERT', __FILE__);
185 throw new Exception("Couldn't save new profile for $actor->id\n");
190 $op = new Ostatus_profile();
192 $op->profile_id = $id;
193 $op->homeuri = $actor->id;
194 $op->created = $profile->created;
196 // XXX: determine feed URI from source or Webfinger or whatever
201 common_log_db_error($op, 'INSERT', __FILE__);
202 throw new Exception("Couldn't save new ostatus profile for $actor->id\n");
209 * @fixme should be merged into Ostatus_profile
211 function nicknameFromURI($uri)
213 preg_match('/(\w+):/', $uri, $matches);
215 $protocol = $matches[1];
220 if (preg_match("/^$protocol:(.*)?@.*\$/", $uri, $matches)) {
221 return common_canonical_nickname($matches[1]);
225 return common_url_to_nickname($uri);