OStatusPlugin.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2009-2010, StatusNet, 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. /**
  20. * OStatusPlugin implementation for GNU Social
  21. *
  22. * Depends on: WebFinger plugin
  23. *
  24. * @package OStatusPlugin
  25. * @maintainer Brion Vibber <brion@status.net>
  26. */
  27. if (!defined('GNUSOCIAL')) { exit(1); }
  28. set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/phpseclib');
  29. class FeedSubException extends Exception
  30. {
  31. function __construct($msg=null)
  32. {
  33. $type = get_class($this);
  34. if ($msg) {
  35. parent::__construct("$type: $msg");
  36. } else {
  37. parent::__construct($type);
  38. }
  39. }
  40. }
  41. class OStatusPlugin extends Plugin
  42. {
  43. /**
  44. * Hook for RouterInitialized event.
  45. *
  46. * @param URLMapper $m path-to-action mapper
  47. * @return boolean hook return
  48. */
  49. public function onRouterInitialized(URLMapper $m)
  50. {
  51. // Discovery actions
  52. $m->connect('main/ostatustag',
  53. array('action' => 'ostatustag'));
  54. $m->connect('main/ostatustag?nickname=:nickname',
  55. array('action' => 'ostatustag'), array('nickname' => '[A-Za-z0-9_-]+'));
  56. $m->connect('main/ostatus/nickname/:nickname',
  57. array('action' => 'ostatusinit'), array('nickname' => '[A-Za-z0-9_-]+'));
  58. $m->connect('main/ostatus/group/:group',
  59. array('action' => 'ostatusinit'), array('group' => '[A-Za-z0-9_-]+'));
  60. $m->connect('main/ostatus/peopletag/:peopletag/tagger/:tagger',
  61. array('action' => 'ostatusinit'), array('tagger' => '[A-Za-z0-9_-]+',
  62. 'peopletag' => '[A-Za-z0-9_-]+'));
  63. $m->connect('main/ostatus',
  64. array('action' => 'ostatusinit'));
  65. // Remote subscription actions
  66. $m->connect('main/ostatussub',
  67. array('action' => 'ostatussub'));
  68. $m->connect('main/ostatusgroup',
  69. array('action' => 'ostatusgroup'));
  70. $m->connect('main/ostatuspeopletag',
  71. array('action' => 'ostatuspeopletag'));
  72. // PuSH actions
  73. $m->connect('main/push/hub', array('action' => 'pushhub'));
  74. $m->connect('main/push/callback/:feed',
  75. array('action' => 'pushcallback'),
  76. array('feed' => '[0-9]+'));
  77. // Salmon endpoint
  78. $m->connect('main/salmon/user/:id',
  79. array('action' => 'usersalmon'),
  80. array('id' => '[0-9]+'));
  81. $m->connect('main/salmon/group/:id',
  82. array('action' => 'groupsalmon'),
  83. array('id' => '[0-9]+'));
  84. $m->connect('main/salmon/peopletag/:id',
  85. array('action' => 'peopletagsalmon'),
  86. array('id' => '[0-9]+'));
  87. return true;
  88. }
  89. /**
  90. * Set up queue handlers for outgoing hub pushes
  91. * @param QueueManager $qm
  92. * @return boolean hook return
  93. */
  94. function onEndInitializeQueueManager(QueueManager $qm)
  95. {
  96. // Prepare outgoing distributions after notice save.
  97. $qm->connect('ostatus', 'OStatusQueueHandler');
  98. // Outgoing from our internal PuSH hub
  99. $qm->connect('hubconf', 'HubConfQueueHandler');
  100. $qm->connect('hubprep', 'HubPrepQueueHandler');
  101. $qm->connect('hubout', 'HubOutQueueHandler');
  102. // Outgoing Salmon replies (when we don't need a return value)
  103. $qm->connect('salmon', 'SalmonQueueHandler');
  104. // Incoming from a foreign PuSH hub
  105. $qm->connect('pushin', 'PushInQueueHandler');
  106. return true;
  107. }
  108. /**
  109. * Put saved notices into the queue for pubsub distribution.
  110. */
  111. function onStartEnqueueNotice($notice, &$transports)
  112. {
  113. if ($notice->inScope(null)) {
  114. // put our transport first, in case there's any conflict (like OMB)
  115. array_unshift($transports, 'ostatus');
  116. $this->log(LOG_INFO, "Notice {$notice->id} queued for OStatus processing");
  117. } else {
  118. // FIXME: we don't do privacy-controlled OStatus updates yet.
  119. // once that happens, finer grain of control here.
  120. $this->log(LOG_NOTICE, "Not queueing notice {$notice->id} for OStatus because of privacy; scope = {$notice->scope}");
  121. }
  122. return true;
  123. }
  124. /**
  125. * Set up a PuSH hub link to our internal link for canonical timeline
  126. * Atom feeds for users and groups.
  127. */
  128. function onStartApiAtom($feed)
  129. {
  130. $id = null;
  131. if ($feed instanceof AtomUserNoticeFeed) {
  132. $salmonAction = 'usersalmon';
  133. $user = $feed->getUser();
  134. $id = $user->id;
  135. $profile = $user->getProfile();
  136. } else if ($feed instanceof AtomGroupNoticeFeed) {
  137. $salmonAction = 'groupsalmon';
  138. $group = $feed->getGroup();
  139. $id = $group->id;
  140. } else if ($feed instanceof AtomListNoticeFeed) {
  141. $salmonAction = 'peopletagsalmon';
  142. $peopletag = $feed->getList();
  143. $id = $peopletag->id;
  144. } else {
  145. return true;
  146. }
  147. if (!empty($id)) {
  148. $hub = common_config('ostatus', 'hub');
  149. if (empty($hub)) {
  150. // Updates will be handled through our internal PuSH hub.
  151. $hub = common_local_url('pushhub');
  152. }
  153. $feed->addLink($hub, array('rel' => 'hub'));
  154. // Also, we'll add in the salmon link
  155. $salmon = common_local_url($salmonAction, array('id' => $id));
  156. $feed->addLink($salmon, array('rel' => Salmon::REL_SALMON));
  157. // XXX: these are deprecated, but StatusNet only looks for NS_REPLIES
  158. $feed->addLink($salmon, array('rel' => Salmon::NS_REPLIES));
  159. $feed->addLink($salmon, array('rel' => Salmon::NS_MENTIONS));
  160. }
  161. return true;
  162. }
  163. /**
  164. * Add in an OStatus subscribe button
  165. */
  166. function onStartProfileRemoteSubscribe($output, $profile)
  167. {
  168. $this->onStartProfileListItemActionElements($output, $profile);
  169. return false;
  170. }
  171. function onStartGroupSubscribe($widget, $group)
  172. {
  173. $cur = common_current_user();
  174. if (empty($cur)) {
  175. $widget->out->elementStart('li', 'entity_subscribe');
  176. $url = common_local_url('ostatusinit',
  177. array('group' => $group->nickname));
  178. $widget->out->element('a', array('href' => $url,
  179. 'class' => 'entity_remote_subscribe'),
  180. // TRANS: Link to subscribe to a remote entity.
  181. _m('Subscribe'));
  182. $widget->out->elementEnd('li');
  183. return false;
  184. }
  185. return true;
  186. }
  187. function onStartSubscribePeopletagForm($output, $peopletag)
  188. {
  189. $cur = common_current_user();
  190. if (empty($cur)) {
  191. $output->elementStart('li', 'entity_subscribe');
  192. $profile = $peopletag->getTagger();
  193. $url = common_local_url('ostatusinit',
  194. array('tagger' => $profile->nickname, 'peopletag' => $peopletag->tag));
  195. $output->element('a', array('href' => $url,
  196. 'class' => 'entity_remote_subscribe'),
  197. // TRANS: Link to subscribe to a remote entity.
  198. _m('Subscribe'));
  199. $output->elementEnd('li');
  200. return false;
  201. }
  202. return true;
  203. }
  204. /*
  205. * If the field being looked for is URI look for the profile
  206. */
  207. function onStartProfileCompletionSearch($action, $profile, $search_engine) {
  208. if ($action->field == 'uri') {
  209. $profile->joinAdd(array('id', 'user:id'));
  210. $profile->whereAdd('uri LIKE "%' . $profile->escape($q) . '%"');
  211. $profile->query();
  212. if ($profile->N == 0) {
  213. try {
  214. if (Validate::email($q)) {
  215. $oprofile = Ostatus_profile::ensureWebfinger($q);
  216. } else if (Validate::uri($q)) {
  217. $oprofile = Ostatus_profile::ensureProfileURL($q);
  218. } else {
  219. // TRANS: Exception in OStatus when invalid URI was entered.
  220. throw new Exception(_m('Invalid URI.'));
  221. }
  222. return $this->filter(array($oprofile->localProfile()));
  223. } catch (Exception $e) {
  224. // TRANS: Error message in OStatus plugin. Do not translate the domain names example.com
  225. // TRANS: and example.net, as these are official standard domain names for use in examples.
  226. $this->msg = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.");
  227. return array();
  228. }
  229. }
  230. return false;
  231. }
  232. return true;
  233. }
  234. /**
  235. * Find any explicit remote mentions. Accepted forms:
  236. * Webfinger: @user@example.com
  237. * Profile link: @example.com/mublog/user
  238. * @param Profile $sender
  239. * @param string $text input markup text
  240. * @param array &$mention in/out param: set of found mentions
  241. * @return boolean hook return value
  242. */
  243. function onEndFindMentions(Profile $sender, $text, &$mentions)
  244. {
  245. $matches = array();
  246. // Webfinger matches: @user@example.com
  247. if (preg_match_all('!(?:^|\s+)@((?:\w+\.)*\w+@(?:\w+\-?\w+\.)*\w+(?:\w+\-\w+)*\.\w+)!',
  248. $text,
  249. $wmatches,
  250. PREG_OFFSET_CAPTURE)) {
  251. foreach ($wmatches[1] as $wmatch) {
  252. list($target, $pos) = $wmatch;
  253. $this->log(LOG_INFO, "Checking webfinger '$target'");
  254. try {
  255. $oprofile = Ostatus_profile::ensureWebfinger($target);
  256. if ($oprofile instanceof Ostatus_profile && !$oprofile->isGroup()) {
  257. $profile = $oprofile->localProfile();
  258. $matches[$pos] = array('mentioned' => array($profile),
  259. 'type' => 'mention',
  260. 'text' => $target,
  261. 'position' => $pos,
  262. 'url' => $profile->getUrl());
  263. }
  264. } catch (Exception $e) {
  265. $this->log(LOG_ERR, "Webfinger check failed: " . $e->getMessage());
  266. }
  267. }
  268. }
  269. // Profile matches: @example.com/mublog/user
  270. if (preg_match_all('!(?:^|\s+)@((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)!',
  271. $text,
  272. $wmatches,
  273. PREG_OFFSET_CAPTURE)) {
  274. foreach ($wmatches[1] as $wmatch) {
  275. list($target, $pos) = $wmatch;
  276. $schemes = array('http', 'https');
  277. foreach ($schemes as $scheme) {
  278. $url = "$scheme://$target";
  279. $this->log(LOG_INFO, "Checking profile address '$url'");
  280. try {
  281. $oprofile = Ostatus_profile::ensureProfileURL($url);
  282. if ($oprofile instanceof Ostatus_profile && !$oprofile->isGroup()) {
  283. $profile = $oprofile->localProfile();
  284. $matches[$pos] = array('mentioned' => array($profile),
  285. 'type' => 'mention',
  286. 'text' => $target,
  287. 'position' => $pos,
  288. 'url' => $profile->getUrl());
  289. break;
  290. }
  291. } catch (Exception $e) {
  292. $this->log(LOG_ERR, "Profile check failed: " . $e->getMessage());
  293. }
  294. }
  295. }
  296. }
  297. foreach ($mentions as $i => $other) {
  298. // If we share a common prefix with a local user, override it!
  299. $pos = $other['position'];
  300. if (isset($matches[$pos])) {
  301. $mentions[$i] = $matches[$pos];
  302. unset($matches[$pos]);
  303. }
  304. }
  305. foreach ($matches as $mention) {
  306. $mentions[] = $mention;
  307. }
  308. return true;
  309. }
  310. /**
  311. * Allow remote profile references to be used in commands:
  312. * sub update@status.net
  313. * whois evan@identi.ca
  314. * reply http://identi.ca/evan hey what's up
  315. *
  316. * @param Command $command
  317. * @param string $arg
  318. * @param Profile &$profile
  319. * @return hook return code
  320. */
  321. function onStartCommandGetProfile($command, $arg, &$profile)
  322. {
  323. $oprofile = $this->pullRemoteProfile($arg);
  324. if ($oprofile instanceof Ostatus_profile && !$oprofile->isGroup()) {
  325. try {
  326. $profile = $oprofile->localProfile();
  327. } catch (NoProfileException $e) {
  328. // No locally stored profile found for remote profile
  329. return true;
  330. }
  331. return false;
  332. } else {
  333. return true;
  334. }
  335. }
  336. /**
  337. * Allow remote group references to be used in commands:
  338. * join group+statusnet@identi.ca
  339. * join http://identi.ca/group/statusnet
  340. * drop identi.ca/group/statusnet
  341. *
  342. * @param Command $command
  343. * @param string $arg
  344. * @param User_group &$group
  345. * @return hook return code
  346. */
  347. function onStartCommandGetGroup($command, $arg, &$group)
  348. {
  349. $oprofile = $this->pullRemoteProfile($arg);
  350. if ($oprofile instanceof Ostatus_profile && $oprofile->isGroup()) {
  351. $group = $oprofile->localGroup();
  352. return false;
  353. } else {
  354. return true;
  355. }
  356. }
  357. protected function pullRemoteProfile($arg)
  358. {
  359. $oprofile = null;
  360. if (preg_match('!^((?:\w+\.)*\w+@(?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+)$!', $arg)) {
  361. // webfinger lookup
  362. try {
  363. return Ostatus_profile::ensureWebfinger($arg);
  364. } catch (Exception $e) {
  365. common_log(LOG_ERR, 'Webfinger lookup failed for ' .
  366. $arg . ': ' . $e->getMessage());
  367. }
  368. }
  369. // Look for profile URLs, with or without scheme:
  370. $urls = array();
  371. if (preg_match('!^https?://((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)$!', $arg)) {
  372. $urls[] = $arg;
  373. }
  374. if (preg_match('!^((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)$!', $arg)) {
  375. $schemes = array('http', 'https');
  376. foreach ($schemes as $scheme) {
  377. $urls[] = "$scheme://$arg";
  378. }
  379. }
  380. foreach ($urls as $url) {
  381. try {
  382. return Ostatus_profile::ensureProfileURL($url);
  383. } catch (Exception $e) {
  384. common_log(LOG_ERR, 'Profile lookup failed for ' .
  385. $arg . ': ' . $e->getMessage());
  386. }
  387. }
  388. return null;
  389. }
  390. /**
  391. * Make sure necessary tables are filled out.
  392. */
  393. function onCheckSchema() {
  394. $schema = Schema::get();
  395. $schema->ensureTable('ostatus_profile', Ostatus_profile::schemaDef());
  396. $schema->ensureTable('ostatus_source', Ostatus_source::schemaDef());
  397. $schema->ensureTable('feedsub', FeedSub::schemaDef());
  398. $schema->ensureTable('hubsub', HubSub::schemaDef());
  399. $schema->ensureTable('magicsig', Magicsig::schemaDef());
  400. return true;
  401. }
  402. public function onEndShowStylesheets(Action $action) {
  403. $action->cssLink($this->path('theme/base/css/ostatus.css'));
  404. return true;
  405. }
  406. function onEndShowStatusNetScripts($action) {
  407. $action->script($this->path('js/ostatus.js'));
  408. return true;
  409. }
  410. /**
  411. * Override the "from ostatus" bit in notice lists to link to the
  412. * original post and show the domain it came from.
  413. *
  414. * @param Notice in $notice
  415. * @param string out &$name
  416. * @param string out &$url
  417. * @param string out &$title
  418. * @return mixed hook return code
  419. */
  420. function onStartNoticeSourceLink($notice, &$name, &$url, &$title)
  421. {
  422. // If we don't handle this, keep the event handler going
  423. if ($notice->source != 'ostatus') {
  424. return true;
  425. }
  426. try {
  427. $url = $notice->getUrl();
  428. // If getUrl() throws exception, $url is never set
  429. $bits = parse_url($url);
  430. $domain = $bits['host'];
  431. if (substr($domain, 0, 4) == 'www.') {
  432. $name = substr($domain, 4);
  433. } else {
  434. $name = $domain;
  435. }
  436. // TRANS: Title. %s is a domain name.
  437. $title = sprintf(_m('Sent from %s via OStatus'), $domain);
  438. // Abort event handler, we have a name and URL!
  439. return false;
  440. } catch (InvalidUrlException $e) {
  441. // This just means we don't have the notice source data
  442. return true;
  443. }
  444. }
  445. /**
  446. * Send incoming PuSH feeds for OStatus endpoints in for processing.
  447. *
  448. * @param FeedSub $feedsub
  449. * @param DOMDocument $feed
  450. * @return mixed hook return code
  451. */
  452. function onStartFeedSubReceive($feedsub, $feed)
  453. {
  454. $oprofile = Ostatus_profile::getKV('feeduri', $feedsub->uri);
  455. if ($oprofile instanceof Ostatus_profile) {
  456. $oprofile->processFeed($feed, 'push');
  457. } else {
  458. common_log(LOG_DEBUG, "No ostatus profile for incoming feed $feedsub->uri");
  459. }
  460. }
  461. /**
  462. * Tell the FeedSub infrastructure whether we have any active OStatus
  463. * usage for the feed; if not it'll be able to garbage-collect the
  464. * feed subscription.
  465. *
  466. * @param FeedSub $feedsub
  467. * @param integer $count in/out
  468. * @return mixed hook return code
  469. */
  470. function onFeedSubSubscriberCount($feedsub, &$count)
  471. {
  472. $oprofile = Ostatus_profile::getKV('feeduri', $feedsub->uri);
  473. if ($oprofile instanceof Ostatus_profile) {
  474. $count += $oprofile->subscriberCount();
  475. }
  476. return true;
  477. }
  478. /**
  479. * When about to subscribe to a remote user, start a server-to-server
  480. * PuSH subscription if needed. If we can't establish that, abort.
  481. *
  482. * @fixme If something else aborts later, we could end up with a stray
  483. * PuSH subscription. This is relatively harmless, though.
  484. *
  485. * @param Profile $profile subscriber
  486. * @param Profile $other subscribee
  487. *
  488. * @return hook return code
  489. *
  490. * @throws Exception
  491. */
  492. function onStartSubscribe(Profile $profile, Profile $other)
  493. {
  494. if (!$profile->isLocal()) {
  495. return true;
  496. }
  497. $oprofile = Ostatus_profile::getKV('profile_id', $other->id);
  498. if (!$oprofile instanceof Ostatus_profile) {
  499. return true;
  500. }
  501. $oprofile->subscribe();
  502. }
  503. /**
  504. * Having established a remote subscription, send a notification to the
  505. * remote OStatus profile's endpoint.
  506. *
  507. * @param Profile $profile subscriber
  508. * @param Profile $other subscribee
  509. *
  510. * @return hook return code
  511. *
  512. * @throws Exception
  513. */
  514. function onEndSubscribe(Profile $profile, Profile $other)
  515. {
  516. if (!$profile->isLocal()) {
  517. return true;
  518. }
  519. $oprofile = Ostatus_profile::getKV('profile_id', $other->id);
  520. if (!$oprofile instanceof Ostatus_profile) {
  521. return true;
  522. }
  523. $sub = Subscription::pkeyGet(array('subscriber' => $profile->id,
  524. 'subscribed' => $other->id));
  525. $act = $sub->asActivity();
  526. $oprofile->notifyActivity($act, $profile);
  527. return true;
  528. }
  529. /**
  530. * Notify remote server and garbage collect unused feeds on unsubscribe.
  531. * @todo FIXME: Send these operations to background queues
  532. *
  533. * @param User $user
  534. * @param Profile $other
  535. * @return hook return value
  536. */
  537. function onEndUnsubscribe(Profile $profile, Profile $other)
  538. {
  539. if (!$profile->isLocal()) {
  540. return true;
  541. }
  542. $oprofile = Ostatus_profile::getKV('profile_id', $other->id);
  543. if (!$oprofile instanceof Ostatus_profile) {
  544. return true;
  545. }
  546. // Drop the PuSH subscription if there are no other subscribers.
  547. $oprofile->garbageCollect();
  548. $act = new Activity();
  549. $act->verb = ActivityVerb::UNFOLLOW;
  550. $act->id = TagURI::mint('unfollow:%d:%d:%s',
  551. $profile->id,
  552. $other->id,
  553. common_date_iso8601(time()));
  554. $act->time = time();
  555. // TRANS: Title for unfollowing a remote profile.
  556. $act->title = _m('TITLE','Unfollow');
  557. // TRANS: Success message for unsubscribe from user attempt through OStatus.
  558. // TRANS: %1$s is the unsubscriber's name, %2$s is the unsubscribed user's name.
  559. $act->content = sprintf(_m('%1$s stopped following %2$s.'),
  560. $profile->getBestName(),
  561. $other->getBestName());
  562. $act->actor = $profile->asActivityObject();
  563. $act->object = $other->asActivityObject();
  564. $oprofile->notifyActivity($act, $profile);
  565. return true;
  566. }
  567. /**
  568. * When one of our local users tries to join a remote group,
  569. * notify the remote server. If the notification is rejected,
  570. * deny the join.
  571. *
  572. * @param User_group $group
  573. * @param Profile $profile
  574. *
  575. * @return mixed hook return value
  576. * @throws Exception of various kinds, some from $oprofile->subscribe();
  577. */
  578. function onStartJoinGroup($group, $profile)
  579. {
  580. $oprofile = Ostatus_profile::getKV('group_id', $group->id);
  581. if (!$oprofile instanceof Ostatus_profile) {
  582. return true;
  583. }
  584. $oprofile->subscribe();
  585. // NOTE: we don't use Group_member::asActivity() since that record
  586. // has not yet been created.
  587. $act = new Activity();
  588. $act->id = TagURI::mint('join:%d:%d:%s',
  589. $profile->id,
  590. $group->id,
  591. common_date_iso8601(time()));
  592. $act->actor = $profile->asActivityObject();
  593. $act->verb = ActivityVerb::JOIN;
  594. $act->object = $oprofile->asActivityObject();
  595. $act->time = time();
  596. // TRANS: Title for joining a remote groep.
  597. $act->title = _m('TITLE','Join');
  598. // TRANS: Success message for subscribe to group attempt through OStatus.
  599. // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
  600. $act->content = sprintf(_m('%1$s has joined group %2$s.'),
  601. $profile->getBestName(),
  602. $oprofile->getBestName());
  603. if ($oprofile->notifyActivity($act, $profile)) {
  604. return true;
  605. } else {
  606. $oprofile->garbageCollect();
  607. // TRANS: Exception thrown when joining a remote group fails.
  608. throw new Exception(_m('Failed joining remote group.'));
  609. }
  610. }
  611. /**
  612. * When one of our local users leaves a remote group, notify the remote
  613. * server.
  614. *
  615. * @fixme Might be good to schedule a resend of the leave notification
  616. * if it failed due to a transitory error. We've canceled the local
  617. * membership already anyway, but if the remote server comes back up
  618. * it'll be left with a stray membership record.
  619. *
  620. * @param User_group $group
  621. * @param Profile $profile
  622. *
  623. * @return mixed hook return value
  624. */
  625. function onEndLeaveGroup($group, $profile)
  626. {
  627. $oprofile = Ostatus_profile::getKV('group_id', $group->id);
  628. if (!$oprofile instanceof Ostatus_profile) {
  629. return true;
  630. }
  631. // Drop the PuSH subscription if there are no other subscribers.
  632. $oprofile->garbageCollect();
  633. $member = $profile;
  634. $act = new Activity();
  635. $act->id = TagURI::mint('leave:%d:%d:%s',
  636. $member->id,
  637. $group->id,
  638. common_date_iso8601(time()));
  639. $act->actor = $member->asActivityObject();
  640. $act->verb = ActivityVerb::LEAVE;
  641. $act->object = $oprofile->asActivityObject();
  642. $act->time = time();
  643. // TRANS: Title for leaving a remote group.
  644. $act->title = _m('TITLE','Leave');
  645. // TRANS: Success message for unsubscribe from group attempt through OStatus.
  646. // TRANS: %1$s is the member name, %2$s is the unsubscribed group's name.
  647. $act->content = sprintf(_m('%1$s has left group %2$s.'),
  648. $member->getBestName(),
  649. $oprofile->getBestName());
  650. $oprofile->notifyActivity($act, $member);
  651. }
  652. /**
  653. * When one of our local users tries to subscribe to a remote peopletag,
  654. * notify the remote server. If the notification is rejected,
  655. * deny the subscription.
  656. *
  657. * @param Profile_list $peopletag
  658. * @param User $user
  659. *
  660. * @return mixed hook return value
  661. * @throws Exception of various kinds, some from $oprofile->subscribe();
  662. */
  663. function onStartSubscribePeopletag($peopletag, $user)
  664. {
  665. $oprofile = Ostatus_profile::getKV('peopletag_id', $peopletag->id);
  666. if (!$oprofile instanceof Ostatus_profile) {
  667. return true;
  668. }
  669. $oprofile->subscribe();
  670. $sub = $user->getProfile();
  671. $tagger = Profile::getKV($peopletag->tagger);
  672. $act = new Activity();
  673. $act->id = TagURI::mint('subscribe_peopletag:%d:%d:%s',
  674. $sub->id,
  675. $peopletag->id,
  676. common_date_iso8601(time()));
  677. $act->actor = $sub->asActivityObject();
  678. $act->verb = ActivityVerb::FOLLOW;
  679. $act->object = $oprofile->asActivityObject();
  680. $act->time = time();
  681. // TRANS: Title for following a remote list.
  682. $act->title = _m('TITLE','Follow list');
  683. // TRANS: Success message for remote list follow through OStatus.
  684. // TRANS: %1$s is the subscriber name, %2$s is the list, %3$s is the lister's name.
  685. $act->content = sprintf(_m('%1$s is now following people listed in %2$s by %3$s.'),
  686. $sub->getBestName(),
  687. $oprofile->getBestName(),
  688. $tagger->getBestName());
  689. if ($oprofile->notifyActivity($act, $sub)) {
  690. return true;
  691. } else {
  692. $oprofile->garbageCollect();
  693. // TRANS: Exception thrown when subscription to remote list fails.
  694. throw new Exception(_m('Failed subscribing to remote list.'));
  695. }
  696. }
  697. /**
  698. * When one of our local users unsubscribes to a remote peopletag, notify the remote
  699. * server.
  700. *
  701. * @param Profile_list $peopletag
  702. * @param User $user
  703. *
  704. * @return mixed hook return value
  705. */
  706. function onEndUnsubscribePeopletag($peopletag, $user)
  707. {
  708. $oprofile = Ostatus_profile::getKV('peopletag_id', $peopletag->id);
  709. if (!$oprofile instanceof Ostatus_profile) {
  710. return true;
  711. }
  712. // Drop the PuSH subscription if there are no other subscribers.
  713. $oprofile->garbageCollect();
  714. $sub = Profile::getKV($user->id);
  715. $tagger = Profile::getKV($peopletag->tagger);
  716. $act = new Activity();
  717. $act->id = TagURI::mint('unsubscribe_peopletag:%d:%d:%s',
  718. $sub->id,
  719. $peopletag->id,
  720. common_date_iso8601(time()));
  721. $act->actor = $member->asActivityObject();
  722. $act->verb = ActivityVerb::UNFOLLOW;
  723. $act->object = $oprofile->asActivityObject();
  724. $act->time = time();
  725. // TRANS: Title for unfollowing a remote list.
  726. $act->title = _m('Unfollow list');
  727. // TRANS: Success message for remote list unfollow through OStatus.
  728. // TRANS: %1$s is the subscriber name, %2$s is the list, %3$s is the lister's name.
  729. $act->content = sprintf(_m('%1$s stopped following the list %2$s by %3$s.'),
  730. $sub->getBestName(),
  731. $oprofile->getBestName(),
  732. $tagger->getBestName());
  733. $oprofile->notifyActivity($act, $user);
  734. }
  735. /**
  736. * Notify remote users when their notices get favorited.
  737. *
  738. * @param Profile or User $profile of local user doing the faving
  739. * @param Notice $notice being favored
  740. * @return hook return value
  741. */
  742. function onEndFavorNotice(Profile $profile, Notice $notice)
  743. {
  744. // Only distribute local users' favor actions, remote users
  745. // will have already distributed theirs.
  746. if (!$profile->isLocal()) {
  747. return true;
  748. }
  749. $oprofile = Ostatus_profile::getKV('profile_id', $notice->profile_id);
  750. if (!$oprofile instanceof Ostatus_profile) {
  751. return true;
  752. }
  753. $fav = Fave::pkeyGet(array('user_id' => $profile->id,
  754. 'notice_id' => $notice->id));
  755. if (!$fav instanceof Fave) {
  756. // That's weird.
  757. // TODO: Make pkeyGet throw exception, since this is a critical failure.
  758. return true;
  759. }
  760. $act = $fav->asActivity();
  761. $oprofile->notifyActivity($act, $profile);
  762. return true;
  763. }
  764. /**
  765. * Notify remote user it has got a new people tag
  766. * - tag verb is queued
  767. * - the subscription is done immediately if not present
  768. *
  769. * @param Profile_tag $ptag the people tag that was created
  770. * @return hook return value
  771. * @throws Exception of various kinds, some from $oprofile->subscribe();
  772. */
  773. function onEndTagProfile($ptag)
  774. {
  775. $oprofile = Ostatus_profile::getKV('profile_id', $ptag->tagged);
  776. if (!$oprofile instanceof Ostatus_profile) {
  777. return true;
  778. }
  779. $plist = $ptag->getMeta();
  780. if ($plist->private) {
  781. return true;
  782. }
  783. $act = new Activity();
  784. $tagger = $plist->getTagger();
  785. $tagged = Profile::getKV('id', $ptag->tagged);
  786. $act->verb = ActivityVerb::TAG;
  787. $act->id = TagURI::mint('tag_profile:%d:%d:%s',
  788. $plist->tagger, $plist->id,
  789. common_date_iso8601(time()));
  790. $act->time = time();
  791. // TRANS: Title for listing a remote profile.
  792. $act->title = _m('TITLE','List');
  793. // TRANS: Success message for remote list addition through OStatus.
  794. // TRANS: %1$s is the list creator's name, %2$s is the added list member, %3$s is the list name.
  795. $act->content = sprintf(_m('%1$s listed %2$s in the list %3$s.'),
  796. $tagger->getBestName(),
  797. $tagged->getBestName(),
  798. $plist->getBestName());
  799. $act->actor = $tagger->asActivityObject();
  800. $act->objects = array($tagged->asActivityObject());
  801. $act->target = ActivityObject::fromPeopletag($plist);
  802. $oprofile->notifyDeferred($act, $tagger);
  803. // initiate a PuSH subscription for the person being tagged
  804. $oprofile->subscribe();
  805. return true;
  806. }
  807. /**
  808. * Notify remote user that a people tag has been removed
  809. * - untag verb is queued
  810. * - the subscription is undone immediately if not required
  811. * i.e garbageCollect()'d
  812. *
  813. * @param Profile_tag $ptag the people tag that was deleted
  814. * @return hook return value
  815. */
  816. function onEndUntagProfile($ptag)
  817. {
  818. $oprofile = Ostatus_profile::getKV('profile_id', $ptag->tagged);
  819. if (!$oprofile instanceof Ostatus_profile) {
  820. return true;
  821. }
  822. $plist = $ptag->getMeta();
  823. if ($plist->private) {
  824. return true;
  825. }
  826. $act = new Activity();
  827. $tagger = $plist->getTagger();
  828. $tagged = Profile::getKV('id', $ptag->tagged);
  829. $act->verb = ActivityVerb::UNTAG;
  830. $act->id = TagURI::mint('untag_profile:%d:%d:%s',
  831. $plist->tagger, $plist->id,
  832. common_date_iso8601(time()));
  833. $act->time = time();
  834. // TRANS: Title for unlisting a remote profile.
  835. $act->title = _m('TITLE','Unlist');
  836. // TRANS: Success message for remote list removal through OStatus.
  837. // TRANS: %1$s is the list creator's name, %2$s is the removed list member, %3$s is the list name.
  838. $act->content = sprintf(_m('%1$s removed %2$s from the list %3$s.'),
  839. $tagger->getBestName(),
  840. $tagged->getBestName(),
  841. $plist->getBestName());
  842. $act->actor = $tagger->asActivityObject();
  843. $act->objects = array($tagged->asActivityObject());
  844. $act->target = ActivityObject::fromPeopletag($plist);
  845. $oprofile->notifyDeferred($act, $tagger);
  846. // unsubscribe to PuSH feed if no more required
  847. $oprofile->garbageCollect();
  848. return true;
  849. }
  850. /**
  851. * Notify remote users when their notices get de-favorited.
  852. *
  853. * @param Profile $profile Profile person doing the de-faving
  854. * @param Notice $notice Notice being favored
  855. *
  856. * @return hook return value
  857. */
  858. function onEndDisfavorNotice(Profile $profile, Notice $notice)
  859. {
  860. // Only distribute local users' disfavor actions, remote users
  861. // will have already distributed theirs.
  862. if (!$profile->isLocal()) {
  863. return true;
  864. }
  865. $oprofile = Ostatus_profile::getKV('profile_id', $notice->profile_id);
  866. if (!$oprofile instanceof Ostatus_profile) {
  867. return true;
  868. }
  869. $act = new Activity();
  870. $act->verb = ActivityVerb::UNFAVORITE;
  871. $act->id = TagURI::mint('disfavor:%d:%d:%s',
  872. $profile->id,
  873. $notice->id,
  874. common_date_iso8601(time()));
  875. $act->time = time();
  876. // TRANS: Title for unliking a remote notice.
  877. $act->title = _m('Unlike');
  878. // TRANS: Success message for remove a favorite notice through OStatus.
  879. // TRANS: %1$s is the unfavoring user's name, %2$s is URI to the no longer favored notice.
  880. $act->content = sprintf(_m('%1$s no longer likes %2$s.'),
  881. $profile->getBestName(),
  882. $notice->getUrl());
  883. $act->actor = $profile->asActivityObject();
  884. $act->object = $notice->asActivityObject();
  885. $oprofile->notifyActivity($act, $profile);
  886. return true;
  887. }
  888. function onStartGetProfileUri($profile, &$uri)
  889. {
  890. $oprofile = Ostatus_profile::getKV('profile_id', $profile->id);
  891. if ($oprofile instanceof Ostatus_profile) {
  892. $uri = $oprofile->uri;
  893. return false;
  894. }
  895. return true;
  896. }
  897. function onStartUserGroupHomeUrl($group, &$url)
  898. {
  899. return $this->onStartUserGroupPermalink($group, $url);
  900. }
  901. function onStartUserGroupPermalink($group, &$url)
  902. {
  903. $oprofile = Ostatus_profile::getKV('group_id', $group->id);
  904. if ($oprofile instanceof Ostatus_profile) {
  905. // @fixme this should probably be in the user_group table
  906. // @fixme this uri not guaranteed to be a profile page
  907. $url = $oprofile->uri;
  908. return false;
  909. }
  910. }
  911. function onStartShowSubscriptionsContent($action)
  912. {
  913. $this->showEntityRemoteSubscribe($action);
  914. return true;
  915. }
  916. function onStartShowUserGroupsContent($action)
  917. {
  918. $this->showEntityRemoteSubscribe($action, 'ostatusgroup');
  919. return true;
  920. }
  921. function onEndShowSubscriptionsMiniList($action)
  922. {
  923. $this->showEntityRemoteSubscribe($action);
  924. return true;
  925. }
  926. function onEndShowGroupsMiniList($action)
  927. {
  928. $this->showEntityRemoteSubscribe($action, 'ostatusgroup');
  929. return true;
  930. }
  931. function showEntityRemoteSubscribe($action, $target='ostatussub')
  932. {
  933. $user = common_current_user();
  934. if ($user && ($user->id == $action->profile->id)) {
  935. $action->elementStart('div', 'entity_actions');
  936. $action->elementStart('p', array('id' => 'entity_remote_subscribe',
  937. 'class' => 'entity_subscribe'));
  938. $action->element('a', array('href' => common_local_url($target),
  939. 'class' => 'entity_remote_subscribe'),
  940. // TRANS: Link text for link to remote subscribe.
  941. _m('Remote'));
  942. $action->elementEnd('p');
  943. $action->elementEnd('div');
  944. }
  945. }
  946. /**
  947. * Ping remote profiles with updates to this profile.
  948. * Salmon pings are queued for background processing.
  949. */
  950. function onEndBroadcastProfile(Profile $profile)
  951. {
  952. $user = User::getKV('id', $profile->id);
  953. // Find foreign accounts I'm subscribed to that support Salmon pings.
  954. //
  955. // @fixme we could run updates through the PuSH feed too,
  956. // in which case we can skip Salmon pings to folks who
  957. // are also subscribed to me.
  958. $sql = "SELECT * FROM ostatus_profile " .
  959. "WHERE profile_id IN " .
  960. "(SELECT subscribed FROM subscription WHERE subscriber=%d) " .
  961. "OR group_id IN " .
  962. "(SELECT group_id FROM group_member WHERE profile_id=%d)";
  963. $oprofile = new Ostatus_profile();
  964. $oprofile->query(sprintf($sql, $profile->id, $profile->id));
  965. if ($oprofile->N == 0) {
  966. common_log(LOG_DEBUG, "No OStatus remote subscribees for $profile->nickname");
  967. return true;
  968. }
  969. $act = new Activity();
  970. $act->verb = ActivityVerb::UPDATE_PROFILE;
  971. $act->id = TagURI::mint('update-profile:%d:%s',
  972. $profile->id,
  973. common_date_iso8601(time()));
  974. $act->time = time();
  975. // TRANS: Title for activity.
  976. $act->title = _m('Profile update');
  977. // TRANS: Ping text for remote profile update through OStatus.
  978. // TRANS: %s is user that updated their profile.
  979. $act->content = sprintf(_m('%s has updated their profile page.'),
  980. $profile->getBestName());
  981. $act->actor = $profile->asActivityObject();
  982. $act->object = $act->actor;
  983. while ($oprofile->fetch()) {
  984. $oprofile->notifyDeferred($act, $profile);
  985. }
  986. return true;
  987. }
  988. function onStartProfileListItemActionElements($item, $profile=null)
  989. {
  990. if (!common_logged_in()) {
  991. $profileUser = User::getKV('id', $item->profile->id);
  992. if (!empty($profileUser)) {
  993. if ($item instanceof Action) {
  994. $output = $item;
  995. $profile = $item->profile;
  996. } else {
  997. $output = $item->out;
  998. }
  999. // Add an OStatus subscribe
  1000. $output->elementStart('li', 'entity_subscribe');
  1001. $url = common_local_url('ostatusinit',
  1002. array('nickname' => $profileUser->nickname));
  1003. $output->element('a', array('href' => $url,
  1004. 'class' => 'entity_remote_subscribe'),
  1005. // TRANS: Link text for a user to subscribe to an OStatus user.
  1006. _m('Subscribe'));
  1007. $output->elementEnd('li');
  1008. $output->elementStart('li', 'entity_tag');
  1009. $url = common_local_url('ostatustag',
  1010. array('nickname' => $profileUser->nickname));
  1011. $output->element('a', array('href' => $url,
  1012. 'class' => 'entity_remote_tag'),
  1013. // TRANS: Link text for a user to list an OStatus user.
  1014. _m('List'));
  1015. $output->elementEnd('li');
  1016. }
  1017. }
  1018. return true;
  1019. }
  1020. function onPluginVersion(&$versions)
  1021. {
  1022. $versions[] = array('name' => 'OStatus',
  1023. 'version' => GNUSOCIAL_VERSION,
  1024. 'author' => 'Evan Prodromou, James Walker, Brion Vibber, Zach Copley',
  1025. 'homepage' => 'http://status.net/wiki/Plugin:OStatus',
  1026. // TRANS: Plugin description.
  1027. 'rawdescription' => _m('Follow people across social networks that implement '.
  1028. '<a href="http://ostatus.org/">OStatus</a>.'));
  1029. return true;
  1030. }
  1031. /**
  1032. * Utility function to check if the given URI is a canonical group profile
  1033. * page, and if so return the ID number.
  1034. *
  1035. * @param string $url
  1036. * @return mixed int or false
  1037. */
  1038. public static function localGroupFromUrl($url)
  1039. {
  1040. $group = User_group::getKV('uri', $url);
  1041. if ($group instanceof User_group) {
  1042. if ($group->isLocal()) {
  1043. return $group->id;
  1044. }
  1045. } else {
  1046. // To find local groups which haven't had their uri fields filled out...
  1047. // If the domain has changed since a subscriber got the URI, it'll
  1048. // be broken.
  1049. $template = common_local_url('groupbyid', array('id' => '31337'));
  1050. $template = preg_quote($template, '/');
  1051. $template = str_replace('31337', '(\d+)', $template);
  1052. if (preg_match("/$template/", $url, $matches)) {
  1053. return intval($matches[1]);
  1054. }
  1055. }
  1056. return false;
  1057. }
  1058. public function onStartProfileGetAtomFeed($profile, &$feed)
  1059. {
  1060. $oprofile = Ostatus_profile::getKV('profile_id', $profile->id);
  1061. if (!$oprofile instanceof Ostatus_profile) {
  1062. return true;
  1063. }
  1064. $feed = $oprofile->feeduri;
  1065. return false;
  1066. }
  1067. function onStartGetProfileFromURI($uri, &$profile)
  1068. {
  1069. // Don't want to do Web-based discovery on our own server,
  1070. // so we check locally first.
  1071. $user = User::getKV('uri', $uri);
  1072. if (!empty($user)) {
  1073. $profile = $user->getProfile();
  1074. return false;
  1075. }
  1076. // Now, check remotely
  1077. try {
  1078. $oprofile = Ostatus_profile::ensureProfileURI($uri);
  1079. $profile = $oprofile->localProfile();
  1080. return !($profile instanceof Profile); // localProfile won't throw exception but can return null
  1081. } catch (Exception $e) {
  1082. return true; // It's not an OStatus profile as far as we know, continue event handling
  1083. }
  1084. }
  1085. function onEndWebFingerNoticeLinks(XML_XRD $xrd, Notice $target)
  1086. {
  1087. $author = $target->getProfile();
  1088. $profiletype = $this->profileTypeString($author);
  1089. $salmon_url = common_local_url("{$profiletype}salmon", array('id' => $author->id));
  1090. $xrd->links[] = new XML_XRD_Element_Link(Salmon::REL_SALMON, $salmon_url);
  1091. return true;
  1092. }
  1093. function onEndWebFingerProfileLinks(XML_XRD $xrd, Profile $target)
  1094. {
  1095. if ($target->getObjectType() === ActivityObject::PERSON) {
  1096. $this->addWebFingerPersonLinks($xrd, $target);
  1097. }
  1098. // Salmon
  1099. $profiletype = $this->profileTypeString($target);
  1100. $salmon_url = common_local_url("{$profiletype}salmon", array('id' => $target->id));
  1101. $xrd->links[] = new XML_XRD_Element_Link(Salmon::REL_SALMON, $salmon_url);
  1102. // XXX: these are deprecated, but StatusNet only looks for NS_REPLIES
  1103. $xrd->links[] = new XML_XRD_Element_Link(Salmon::NS_REPLIES, $salmon_url);
  1104. $xrd->links[] = new XML_XRD_Element_Link(Salmon::NS_MENTIONS, $salmon_url);
  1105. // TODO - finalize where the redirect should go on the publisher
  1106. $xrd->links[] = new XML_XRD_Element_Link('http://ostatus.org/schema/1.0/subscribe',
  1107. common_local_url('ostatussub') . '?profile={uri}',
  1108. null, // type not set
  1109. true); // isTemplate
  1110. return true;
  1111. }
  1112. protected function profileTypeString(Profile $target)
  1113. {
  1114. // This is just used to have a definitive string response to "USERsalmon" or "GROUPsalmon"
  1115. switch ($target->getObjectType()) {
  1116. case ActivityObject::PERSON:
  1117. return 'user';
  1118. case ActivityObject::GROUP:
  1119. return 'group';
  1120. default:
  1121. throw new ServerException('Unknown profile type for WebFinger profile links');
  1122. }
  1123. }
  1124. protected function addWebFingerPersonLinks(XML_XRD $xrd, Profile $target)
  1125. {
  1126. $xrd->links[] = new XML_XRD_Element_Link(Discovery::UPDATESFROM,
  1127. common_local_url('ApiTimelineUser',
  1128. array('id' => $target->id, 'format' => 'atom')),
  1129. 'application/atom+xml');
  1130. // Get this profile's keypair
  1131. $magicsig = Magicsig::getKV('user_id', $target->id);
  1132. if (!$magicsig instanceof Magicsig && $target->isLocal()) {
  1133. $magicsig = Magicsig::generate($target->getUser());
  1134. }
  1135. if ($magicsig instanceof Magicsig) {
  1136. $xrd->links[] = new XML_XRD_Element_Link(Magicsig::PUBLICKEYREL,
  1137. 'data:application/magic-public-key,'. $magicsig->toString());
  1138. $xrd->links[] = new XML_XRD_Element_Link(Magicsig::DIASPORA_PUBLICKEYREL,
  1139. base64_encode($magicsig->exportPublicKey()));
  1140. }
  1141. }
  1142. public function onGetLocalAttentions(Profile $actor, array $attention_uris, array &$mentions, array &$groups)
  1143. {
  1144. list($mentions, $groups) = Ostatus_profile::filterAttention($actor, $attention_uris);
  1145. }
  1146. // FIXME: Maybe this shouldn't be so authoritative that it breaks other remote profile lookups?
  1147. static public function onCheckActivityAuthorship(Activity $activity, Profile &$profile)
  1148. {
  1149. try {
  1150. $oprofile = Ostatus_profile::ensureProfileURL($profile->getUrl());
  1151. $profile = $oprofile->checkAuthorship($activity);
  1152. } catch (Exception $e) {
  1153. common_log(LOG_ERR, 'Could not get a profile or check authorship ('.get_class($e).': "'.$e->getMessage().'") for activity ID: '.$activity->id);
  1154. $profile = null;
  1155. return false;
  1156. }
  1157. return true;
  1158. }
  1159. public function onProfileDeleteRelated($profile, &$related)
  1160. {
  1161. // Ostatus_profile has a 'profile_id' property, which will be used to find the object
  1162. $related[] = 'Ostatus_profile';
  1163. // Magicsig has a "user_id" column instead, so we have to delete it more manually:
  1164. $magicsig = Magicsig::getKV('user_id', $profile->id);
  1165. if ($magicsig instanceof Magicsig) {
  1166. $magicsig->delete();
  1167. }
  1168. return true;
  1169. }
  1170. }