3 * GNU Social - a federating social network
4 * Copyright (C) 2014, Free Software Foundation, 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/>.
20 if (!defined('GNUSOCIAL')) { exit(1); }
24 * @maintainer Mikael Nordfeldth <mmn@hethane.se>
26 class SharePlugin extends ActivityVerbHandlerPlugin
28 const PLUGIN_VERSION = '2.0.0';
35 public function types()
40 public function verbs()
42 return array(ActivityVerb::SHARE);
45 // Share is a bit special and $act->objects[0] should be an Activity
46 // instead of ActivityObject! Therefore also $act->objects[0]->type is not set.
47 public function isMyActivity(Activity $act) {
48 return (count($act->objects) == 1
49 && ($act->objects[0] instanceof Activity)
50 && $this->isMyVerb($act->verb));
53 public function onRouterInitialized(URLMapper $m)
56 $m->connect('main/repeat', array('action' => 'repeat'));
58 // Share for Twitter API ("Retweet")
59 $m->connect('api/statuses/retweeted_by_me.:format',
60 array('action' => 'ApiTimelineRetweetedByMe',
61 'format' => '(xml|json|atom|as)'));
63 $m->connect('api/statuses/retweeted_to_me.:format',
64 array('action' => 'ApiTimelineRetweetedToMe',
65 'format' => '(xml|json|atom|as)'));
67 $m->connect('api/statuses/retweets_of_me.:format',
68 array('action' => 'ApiTimelineRetweetsOfMe',
69 'format' => '(xml|json|atom|as)'));
71 $m->connect('api/statuses/retweet/:id.:format',
72 array('action' => 'ApiStatusesRetweet',
74 'format' => '(xml|json)'));
76 $m->connect('api/statuses/retweets/:id.:format',
77 array('action' => 'ApiStatusesRetweets',
79 'format' => '(xml|json)'));
82 // FIXME: Set this to abstract public in lib/activityhandlerplugin.php when all plugins have migrated!
83 protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
85 assert($this->isMyActivity($act));
87 // The below algorithm is mainly copied from the previous Ostatus_profile->processShare()
89 if (count($act->objects) !== 1) {
90 // TRANS: Client exception thrown when trying to share multiple activities at once.
91 throw new ClientException(_m('Can only handle share activities with exactly one object.'));
94 $shared = $act->objects[0];
95 if (!$shared instanceof Activity) {
96 // TRANS: Client exception thrown when trying to share a non-activity object.
97 throw new ClientException(_m('Can only handle shared activities.'));
100 $sharedUri = $shared->id;
101 if (!empty($shared->objects[0]->id)) {
102 // Because StatusNet since commit 8cc4660 sets $shared->id to a TagURI which
103 // fucks up federation, because the URI is no longer recognised by the origin.
104 // So we set it to the object ID if it exists, otherwise we trust $shared->id
105 $sharedUri = $shared->objects[0]->id;
107 if (empty($sharedUri)) {
108 throw new ClientException(_m('Shared activity does not have an id'));
112 // First check if we have the shared activity. This has to be done first, because
113 // we can't use these functions to "ensureActivityObjectProfile" of a local user,
114 // who might be the creator of the shared activity in question.
115 $sharedNotice = Notice::getByUri($sharedUri);
116 } catch (NoResultException $e) {
117 // If no locally stored notice is found, process it!
118 // TODO: Remember to check Deleted_notice!
119 // TODO: If a post is shared that we can't retrieve - what to do?
120 $other = Ostatus_profile::ensureActivityObjectProfile($shared->actor);
121 $sharedNotice = Notice::saveActivity($shared, $other->localProfile(), array('source'=>'share'));
122 } catch (FeedSubException $e) {
123 // Remote feed could not be found or verified, should we
124 // transform this into an "RT @user Blah, blah, blah..."?
125 common_log(LOG_INFO, __METHOD__ . ' got a ' . get_class($e) . ': ' . $e->getMessage());
129 // Setting this here because when the algorithm gets back to
130 // Notice::saveActivity it will update the Notice object.
131 $stored->repeat_of = $sharedNotice->getID();
132 $stored->conversation = $sharedNotice->conversation;
134 // We don't have to save a repeat in a separate table, we can
135 // find repeats by just looking at the notice.repeat_of field.
137 // By returning true here instead of something that evaluates
138 // to false, we show that we have processed everything properly.
142 // FIXME: Put this in lib/activityhandlerplugin.php when we're ready
143 // with the other microapps/activityhandlers as well.
144 // Also it should be StartNoticeAsActivity (with a prepped Activity, including ->context etc.)
145 public function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
147 if (!$this->isMyNotice($stored)) {
151 $this->extendActivity($stored, $act, $scoped);
155 public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
157 // TODO: How to handle repeats of deleted notices?
158 $target = Notice::getByID($stored->repeat_of);
159 // TRANS: A repeat activity's title. %1$s is repeater's nickname
160 // and %2$s is the repeated user's nickname.
161 $act->title = sprintf(_('%1$s repeated a notice by %2$s'),
162 $stored->getProfile()->getNickname(),
163 $target->getProfile()->getNickname());
164 $act->objects[] = $target->asActivity($scoped);
167 public function activityObjectFromNotice(Notice $stored)
169 // Repeat is a little bit special. As it's an activity, our
170 // ActivityObject is instead turned into an Activity
171 $object = new Activity();
172 $object->actor = $stored->getProfile()->asActivityObject();
173 $object->verb = ActivityVerb::SHARE;
174 $object->content = $stored->getRendered();
175 $this->extendActivity($stored, $object);
180 public function deleteRelated(Notice $notice)
182 // No action needed as we don't have a separate table for share objects.
189 * show a link to the author of repeat
191 * FIXME: Some repeat stuff still in lib/noticelistitem.php! ($nli->repeat etc.)
193 public function onEndShowNoticeInfo(NoticeListItem $nli)
195 if (!empty($nli->repeat)) {
196 $repeater = $nli->repeat->getProfile();
198 $attrs = array('href' => $repeater->getUrl(),
199 'class' => 'h-card p-author',
200 'title' => $repeater->getFancyName());
202 $nli->out->elementStart('span', 'repeat');
204 // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
205 $nli->out->raw(_('Repeated by').' ');
207 $nli->out->element('a', $attrs, $repeater->getNickname());
209 $nli->out->elementEnd('span');
213 public function onEndShowThreadedNoticeTailItems(NoticeListItem $nli, Notice $notice, &$threadActive)
215 if ($nli instanceof ThreadedNoticeListSubItem) {
216 // The sub-items are replies to a conversation, thus we use different HTML elements etc.
217 $item = new ThreadedNoticeListInlineRepeatsItem($notice, $nli->out);
219 $item = new ThreadedNoticeListRepeatsItem($notice, $nli->out);
221 $threadActive = $item->show() || $threadActive;
226 * show the "repeat" form in the notice options element
227 * FIXME: Don't let a NoticeListItemAdapter slip in here (or extend that from NoticeListItem)
231 public function onEndShowNoticeOptionItems($nli)
233 // FIXME: Use bitmasks (but be aware that PUBLIC_SCOPE is 0!)
234 // Also: AHHH, $scope and $scoped are scarily similar looking.
235 $scope = $nli->notice->getScope();
236 if ($scope === Notice::PUBLIC_SCOPE || $scope === Notice::SITE_SCOPE) {
237 $scoped = Profile::current();
238 if ($scoped instanceof Profile &&
239 $scoped->getID() !== $nli->notice->getProfile()->getID()) {
241 if ($scoped->hasRepeated($nli->notice)) {
242 $nli->out->element('span', array('class' => 'repeated',
243 // TRANS: Title for repeat form status in notice list when a notice has been repeated.
244 'title' => _('Notice repeated.')),
245 // TRANS: Repeat form status in notice list when a notice has been repeated.
248 $repeat = new RepeatForm($nli->out, $nli->notice);
255 protected function showNoticeListItem(NoticeListItem $nli)
259 public function openNoticeListItemElement(NoticeListItem $nli)
263 public function closeNoticeListItemElement(NoticeListItem $nli)
271 * Typically just used to fill out Twitter-compatible API status data.
273 * FIXME: Make all the calls before this end up with a Notice instead of ArrayWrapper please...
275 public function onNoticeSimpleStatusArray($notice, array &$status, Profile $scoped=null, array $args=array())
277 $status['repeated'] = $scoped instanceof Profile
278 ? $scoped->hasRepeated($notice)
281 if ($status['repeated'] === true) {
282 // Qvitter API wants the "repeated_id" value set too.
283 $repeated = Notice::pkeyGet(array('profile_id' => $scoped->getID(),
284 'repeat_of' => $notice->getID(),
285 'verb' => ActivityVerb::SHARE));
286 $status['repeated_id'] = $repeated->getID();
290 public function onTwitterUserArray(Profile $profile, array &$userdata, Profile $scoped=null, array $args=array())
292 $userdata['favourites_count'] = Fave::countByProfile($profile);
298 * EndInterpretCommand for RepeatPlugin will handle the 'repeat' command
299 * using the class RepeatCommand.
301 * @param string $cmd Command being run
302 * @param string $arg Rest of the message (including address)
303 * @param User $user User sending the message
304 * @param Command &$result The resulting command object to be run.
306 * @return boolean hook value
308 public function onStartInterpretCommand($cmd, $arg, $user, &$result)
310 if ($result === false && in_array($cmd, array('repeat', 'rp', 'rt', 'rd'))) {
314 list($other, $extra) = CommandInterpreter::split_arg($arg);
315 if (!empty($extra)) {
318 $result = new RepeatCommand($user, $other);
326 public function onHelpCommandMessages(HelpCommand $help, array &$commands)
328 // TRANS: Help message for IM/SMS command "repeat #<notice_id>".
329 $commands['repeat #<notice_id>'] = _m('COMMANDHELP', "repeat a notice with a given id");
330 // TRANS: Help message for IM/SMS command "repeat <nickname>".
331 $commands['repeat <nickname>'] = _m('COMMANDHELP', "repeat the last notice from user");
335 * Are we allowed to perform a certain command over the API?
337 public function onCommandSupportedAPI(Command $cmd, &$supported)
339 $supported = $supported || $cmd instanceof RepeatCommand;
342 protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
347 protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
352 protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
354 // handle repeat POST
357 protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
359 return new RepeatForm($action, $target);
362 public function onPluginVersion(array &$versions)
364 $versions[] = array('name' => 'Share verb',
365 'version' => self::PLUGIN_VERSION,
366 'author' => 'Mikael Nordfeldth',
367 'homepage' => 'https://gnu.io/',
369 // TRANS: Plugin description.
370 _m('Shares (repeats) using ActivityStreams.'));