SharePlugin.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. /*
  3. * GNU Social - a federating social network
  4. * Copyright (C) 2014, Free Software Foundation, Inc.
  5. *
  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.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. if (!defined('GNUSOCIAL')) { exit(1); }
  20. /**
  21. * @package Activity
  22. * @maintainer Mikael Nordfeldth <mmn@hethane.se>
  23. */
  24. class SharePlugin extends ActivityVerbHandlerPlugin
  25. {
  26. public function tag()
  27. {
  28. return 'share';
  29. }
  30. public function types()
  31. {
  32. return array();
  33. }
  34. public function verbs()
  35. {
  36. return array(ActivityVerb::SHARE);
  37. }
  38. // Share is a bit special and $act->objects[0] should be an Activity
  39. // instead of ActivityObject! Therefore also $act->objects[0]->type is not set.
  40. public function isMyActivity(Activity $act) {
  41. return (count($act->objects) == 1
  42. && ($act->objects[0] instanceof Activity)
  43. && $this->isMyVerb($act->verb));
  44. }
  45. public function onRouterInitialized(URLMapper $m)
  46. {
  47. // Web UI actions
  48. $m->connect('main/repeat', array('action' => 'repeat'));
  49. // Share for Twitter API ("Retweet")
  50. $m->connect('api/statuses/retweeted_by_me.:format',
  51. array('action' => 'ApiTimelineRetweetedByMe',
  52. 'format' => '(xml|json|atom|as)'));
  53. $m->connect('api/statuses/retweeted_to_me.:format',
  54. array('action' => 'ApiTimelineRetweetedToMe',
  55. 'format' => '(xml|json|atom|as)'));
  56. $m->connect('api/statuses/retweets_of_me.:format',
  57. array('action' => 'ApiTimelineRetweetsOfMe',
  58. 'format' => '(xml|json|atom|as)'));
  59. $m->connect('api/statuses/retweet/:id.:format',
  60. array('action' => 'ApiStatusesRetweet',
  61. 'id' => '[0-9]+',
  62. 'format' => '(xml|json)'));
  63. $m->connect('api/statuses/retweets/:id.:format',
  64. array('action' => 'ApiStatusesRetweets',
  65. 'id' => '[0-9]+',
  66. 'format' => '(xml|json)'));
  67. }
  68. // FIXME: Set this to abstract public in lib/activityhandlerplugin.php when all plugins have migrated!
  69. protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
  70. {
  71. assert($this->isMyActivity($act));
  72. // The below algorithm is mainly copied from the previous Ostatus_profile->processShare()
  73. if (count($act->objects) !== 1) {
  74. // TRANS: Client exception thrown when trying to share multiple activities at once.
  75. throw new ClientException(_m('Can only handle share activities with exactly one object.'));
  76. }
  77. $shared = $act->objects[0];
  78. if (!$shared instanceof Activity) {
  79. // TRANS: Client exception thrown when trying to share a non-activity object.
  80. throw new ClientException(_m('Can only handle shared activities.'));
  81. }
  82. $sharedUri = $shared->id;
  83. if (!empty($shared->objects[0]->id)) {
  84. // Because StatusNet since commit 8cc4660 sets $shared->id to a TagURI which
  85. // fucks up federation, because the URI is no longer recognised by the origin.
  86. // So we set it to the object ID if it exists, otherwise we trust $shared->id
  87. $sharedUri = $shared->objects[0]->id;
  88. }
  89. if (empty($sharedUri)) {
  90. throw new ClientException(_m('Shared activity does not have an id'));
  91. }
  92. try {
  93. // First check if we have the shared activity. This has to be done first, because
  94. // we can't use these functions to "ensureActivityObjectProfile" of a local user,
  95. // who might be the creator of the shared activity in question.
  96. $sharedNotice = Notice::getByUri($sharedUri);
  97. } catch (NoResultException $e) {
  98. // If no locally stored notice is found, process it!
  99. // TODO: Remember to check Deleted_notice!
  100. // TODO: If a post is shared that we can't retrieve - what to do?
  101. $other = Ostatus_profile::ensureActivityObjectProfile($shared->actor);
  102. $sharedNotice = Notice::saveActivity($shared, $other->localProfile(), array('source'=>'share'));
  103. } catch (FeedSubException $e) {
  104. // Remote feed could not be found or verified, should we
  105. // transform this into an "RT @user Blah, blah, blah..."?
  106. common_log(LOG_INFO, __METHOD__ . ' got a ' . get_class($e) . ': ' . $e->getMessage());
  107. return false;
  108. }
  109. // Setting this here because when the algorithm gets back to
  110. // Notice::saveActivity it will update the Notice object.
  111. $stored->repeat_of = $sharedNotice->getID();
  112. $stored->conversation = $sharedNotice->conversation;
  113. // We don't have to save a repeat in a separate table, we can
  114. // find repeats by just looking at the notice.repeat_of field.
  115. // By returning true here instead of something that evaluates
  116. // to false, we show that we have processed everything properly.
  117. return true;
  118. }
  119. // FIXME: Put this in lib/activityhandlerplugin.php when we're ready
  120. // with the other microapps/activityhandlers as well.
  121. // Also it should be StartNoticeAsActivity (with a prepped Activity, including ->context etc.)
  122. public function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
  123. {
  124. if (!$this->isMyNotice($stored)) {
  125. return true;
  126. }
  127. $this->extendActivity($stored, $act, $scoped);
  128. return false;
  129. }
  130. public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
  131. {
  132. // TODO: How to handle repeats of deleted notices?
  133. $target = Notice::getByID($stored->repeat_of);
  134. // TRANS: A repeat activity's title. %1$s is repeater's nickname
  135. // and %2$s is the repeated user's nickname.
  136. $act->title = sprintf(_('%1$s repeated a notice by %2$s'),
  137. $stored->getProfile()->getNickname(),
  138. $target->getProfile()->getNickname());
  139. $act->objects[] = $target->asActivity($scoped);
  140. }
  141. public function activityObjectFromNotice(Notice $stored)
  142. {
  143. // Repeat is a little bit special. As it's an activity, our
  144. // ActivityObject is instead turned into an Activity
  145. $object = new Activity();
  146. $object->actor = $stored->getProfile()->asActivityObject();
  147. $object->verb = ActivityVerb::SHARE;
  148. $object->content = $stored->getRendered();
  149. $this->extendActivity($stored, $object);
  150. return $object;
  151. }
  152. public function deleteRelated(Notice $notice)
  153. {
  154. // No action needed as we don't have a separate table for share objects.
  155. return true;
  156. }
  157. // Layout stuff
  158. /**
  159. * show a link to the author of repeat
  160. *
  161. * FIXME: Some repeat stuff still in lib/noticelistitem.php! ($nli->repeat etc.)
  162. */
  163. public function onEndShowNoticeInfo(NoticeListItem $nli)
  164. {
  165. if (!empty($nli->repeat)) {
  166. $repeater = $nli->repeat->getProfile();
  167. $attrs = array('href' => $repeater->getUrl(),
  168. 'class' => 'h-card p-author',
  169. 'title' => $repeater->getFancyName());
  170. $nli->out->elementStart('span', 'repeat');
  171. // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
  172. $nli->out->raw(_('Repeated by').' ');
  173. $nli->out->element('a', $attrs, $repeater->getNickname());
  174. $nli->out->elementEnd('span');
  175. }
  176. }
  177. public function onEndShowThreadedNoticeTailItems(NoticeListItem $nli, Notice $notice, &$threadActive)
  178. {
  179. if ($nli instanceof ThreadedNoticeListSubItem) {
  180. // The sub-items are replies to a conversation, thus we use different HTML elements etc.
  181. $item = new ThreadedNoticeListInlineRepeatsItem($notice, $nli->out);
  182. } else {
  183. $item = new ThreadedNoticeListRepeatsItem($notice, $nli->out);
  184. }
  185. $threadActive = $item->show() || $threadActive;
  186. return true;
  187. }
  188. /**
  189. * show the "repeat" form in the notice options element
  190. * FIXME: Don't let a NoticeListItemAdapter slip in here (or extend that from NoticeListItem)
  191. *
  192. * @return void
  193. */
  194. public function onEndShowNoticeOptionItems($nli)
  195. {
  196. // FIXME: Use bitmasks (but be aware that PUBLIC_SCOPE is 0!)
  197. // Also: AHHH, $scope and $scoped are scarily similar looking.
  198. $scope = $nli->notice->getScope();
  199. if ($scope === Notice::PUBLIC_SCOPE || $scope === Notice::SITE_SCOPE) {
  200. $scoped = Profile::current();
  201. if ($scoped instanceof Profile &&
  202. $scoped->getID() !== $nli->notice->getProfile()->getID()) {
  203. if ($scoped->hasRepeated($nli->notice)) {
  204. $nli->out->element('span', array('class' => 'repeated',
  205. // TRANS: Title for repeat form status in notice list when a notice has been repeated.
  206. 'title' => _('Notice repeated.')),
  207. // TRANS: Repeat form status in notice list when a notice has been repeated.
  208. _('Repeated'));
  209. } else {
  210. $repeat = new RepeatForm($nli->out, $nli->notice);
  211. $repeat->show();
  212. }
  213. }
  214. }
  215. }
  216. protected function showNoticeListItem(NoticeListItem $nli)
  217. {
  218. // pass
  219. }
  220. public function openNoticeListItemElement(NoticeListItem $nli)
  221. {
  222. // pass
  223. }
  224. public function closeNoticeListItemElement(NoticeListItem $nli)
  225. {
  226. // pass
  227. }
  228. // API stuff
  229. /**
  230. * Typically just used to fill out Twitter-compatible API status data.
  231. *
  232. * FIXME: Make all the calls before this end up with a Notice instead of ArrayWrapper please...
  233. */
  234. public function onNoticeSimpleStatusArray($notice, array &$status, Profile $scoped=null, array $args=array())
  235. {
  236. $status['repeated'] = $scoped instanceof Profile
  237. ? $scoped->hasRepeated($notice)
  238. : false;
  239. if ($status['repeated'] === true) {
  240. // Qvitter API wants the "repeated_id" value set too.
  241. $repeated = Notice::pkeyGet(array('profile_id' => $scoped->getID(),
  242. 'repeat_of' => $notice->getID(),
  243. 'verb' => ActivityVerb::SHARE));
  244. $status['repeated_id'] = $repeated->getID();
  245. }
  246. }
  247. public function onTwitterUserArray(Profile $profile, array &$userdata, Profile $scoped=null, array $args=array())
  248. {
  249. $userdata['favourites_count'] = Fave::countByProfile($profile);
  250. }
  251. // Command stuff
  252. /**
  253. * EndInterpretCommand for RepeatPlugin will handle the 'repeat' command
  254. * using the class RepeatCommand.
  255. *
  256. * @param string $cmd Command being run
  257. * @param string $arg Rest of the message (including address)
  258. * @param User $user User sending the message
  259. * @param Command &$result The resulting command object to be run.
  260. *
  261. * @return boolean hook value
  262. */
  263. public function onStartInterpretCommand($cmd, $arg, $user, &$result)
  264. {
  265. if ($result === false && in_array($cmd, array('repeat', 'rp', 'rt', 'rd'))) {
  266. if (empty($arg)) {
  267. $result = null;
  268. } else {
  269. list($other, $extra) = CommandInterpreter::split_arg($arg);
  270. if (!empty($extra)) {
  271. $result = null;
  272. } else {
  273. $result = new RepeatCommand($user, $other);
  274. }
  275. }
  276. return false;
  277. }
  278. return true;
  279. }
  280. public function onHelpCommandMessages(HelpCommand $help, array &$commands)
  281. {
  282. // TRANS: Help message for IM/SMS command "repeat #<notice_id>".
  283. $commands['repeat #<notice_id>'] = _m('COMMANDHELP', "repeat a notice with a given id");
  284. // TRANS: Help message for IM/SMS command "repeat <nickname>".
  285. $commands['repeat <nickname>'] = _m('COMMANDHELP', "repeat the last notice from user");
  286. }
  287. /**
  288. * Are we allowed to perform a certain command over the API?
  289. */
  290. public function onCommandSupportedAPI(Command $cmd, &$supported)
  291. {
  292. $supported = $supported || $cmd instanceof RepeatCommand;
  293. }
  294. protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  295. {
  296. // return page title
  297. }
  298. protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  299. {
  300. // prepare Action?
  301. }
  302. protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  303. {
  304. // handle repeat POST
  305. }
  306. protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  307. {
  308. return new RepeatForm($action, $target);
  309. }
  310. public function onPluginVersion(array &$versions)
  311. {
  312. $versions[] = array('name' => 'Share verb',
  313. 'version' => GNUSOCIAL_VERSION,
  314. 'author' => 'Mikael Nordfeldth',
  315. 'homepage' => 'https://gnu.io/',
  316. 'rawdescription' =>
  317. // TRANS: Plugin description.
  318. _m('Shares (repeats) using ActivityStreams.'));
  319. return true;
  320. }
  321. }