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::UNFAVORITE:
85 $this->handleUnfavorite();
87 case ActivityVerb::FOLLOW:
88 case ActivityVerb::FRIEND:
89 $this->handleFollow();
91 case ActivityVerb::UNFOLLOW:
92 $this->handleUnfollow();
94 case ActivityVerb::JOIN:
98 throw new ClientException(_("Unimplemented."));
100 Event::handle('EndHandleSalmon', array($this->activity));
104 function handlePost()
106 throw new ClientException(_("Unimplemented!"));
109 function handleFollow()
111 throw new ClientException(_("Unimplemented!"));
114 function handleUnfollow()
116 throw new ClientException(_("Unimplemented!"));
119 function handleFavorite()
121 throw new ClientException(_("Unimplemented!"));
125 * Remote user doesn't like one of our posts after all!
126 * Confirm the post is ours, and delete a local favorite event.
129 function handleUnfavorite()
131 throw new ClientException(_("Unimplemented!"));
137 function handleShare()
139 throw new ClientException(_("Unimplemented!"));
145 function handleJoin()
147 throw new ClientException(_("Unimplemented!"));
151 * @return Ostatus_profile
153 function ensureProfile()
155 $actor = $this->act->actor;
156 if (empty($actor->id)) {
157 common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
158 throw new Exception("Received a salmon slap from unidentified actor.");
161 return Ostatus_profile::ensureActorProfile($this->act);
165 * @fixme merge into Ostatus_profile::ensureActorProfile and friends
167 function createProfile()
169 $actor = $this->act->actor;
171 $profile = new Profile();
173 $profile->nickname = $this->nicknameFromURI($actor->id);
175 if (empty($profile->nickname)) {
176 $profile->nickname = common_nicknamize($actor->title);
179 $profile->fullname = $actor->title;
180 $profile->bio = $actor->summary; // XXX: is that right?
181 $profile->profileurl = $actor->link; // XXX: is that right?
182 $profile->created = common_sql_now();
184 $id = $profile->insert();
187 common_log_db_error($profile, 'INSERT', __FILE__);
188 throw new Exception("Couldn't save new profile for $actor->id\n");
193 $op = new Ostatus_profile();
195 $op->profile_id = $id;
196 $op->homeuri = $actor->id;
197 $op->created = $profile->created;
199 // XXX: determine feed URI from source or Webfinger or whatever
204 common_log_db_error($op, 'INSERT', __FILE__);
205 throw new Exception("Couldn't save new ostatus profile for $actor->id\n");
212 * @fixme should be merged into Ostatus_profile
214 function nicknameFromURI($uri)
216 preg_match('/(\w+):/', $uri, $matches);
218 $protocol = $matches[1];
223 if (preg_match("/^$protocol:(.*)?@.*\$/", $uri, $matches)) {
224 return common_canonical_nickname($matches[1]);
228 return common_url_to_nickname($uri);