FavoriteModule.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * @package Favorite
  18. * @author Mikael Nordfeldth <mmn@hethane.se>
  19. * @copyright 2014 Free Software Foundation, Inc http://www.fsf.org
  20. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  21. */
  22. defined('GNUSOCIAL') || die();
  23. include("classes/Fave.php");
  24. include("lib/threadednoticelistfavesitem.php");
  25. include("lib/favcommand.php");
  26. include("lib/popularnoticestream.php");
  27. include("lib/favenoticestream.php");
  28. include("lib/popularnoticesection.php");
  29. include("lib/threadednoticelistinlinefavesitem.php");
  30. class FavoriteModule extends ActivityVerbHandlerModule
  31. {
  32. const MODULE_VERSION = '2.0.0';
  33. protected $email_notify_fave = 1;
  34. public function tag()
  35. {
  36. return 'favorite';
  37. }
  38. public function types()
  39. {
  40. return array();
  41. }
  42. public function verbs()
  43. {
  44. return array(ActivityVerb::FAVORITE, ActivityVerb::LIKE,
  45. ActivityVerb::UNFAVORITE, ActivityVerb::UNLIKE);
  46. }
  47. public function onCheckSchema()
  48. {
  49. $schema = Schema::get();
  50. $schema->ensureTable('fave', Fave::schemaDef());
  51. return true;
  52. }
  53. public function initialize()
  54. {
  55. common_config_set('email', 'notify_fave', $this->email_notify_fave);
  56. }
  57. public function onStartUpgrade()
  58. {
  59. // This is a migration feature that will make sure we move
  60. // certain User preferences to the Profile_prefs table.
  61. // Introduced after commit b5fd2a048fc621ea05d756caba17275ab3dd0af4
  62. // on Sun Jul 13 16:30:37 2014 +0200
  63. $user = new User();
  64. $user->whereAdd('emailnotifyfav IS NOT NULL');
  65. if ($user->find()) {
  66. printfnq("Detected old User table (emailnotifyfav IS NOT NULL). Moving 'emailnotifyfav' property to Profile_prefs...");
  67. // First we'll make sure Profile_prefs exists
  68. $schema = Schema::get();
  69. $schema->ensureTable('profile_prefs', Profile_prefs::schemaDef());
  70. // Make sure we have our own tables setup properly
  71. while ($user->fetch()) {
  72. $user->setPref('email', 'notify_fave', $user->emailnotifyfav);
  73. $orig = clone($user);
  74. $user->emailnotifyfav = $user->sqlValue('NULL'); // flag this preference as migrated
  75. $user->update($orig);
  76. }
  77. printfnq("DONE.\n");
  78. }
  79. }
  80. public function onEndUpgrade()
  81. {
  82. printfnq("Ensuring all faves have a URI...");
  83. $fave = new Fave();
  84. $fave->whereAdd('uri IS NULL');
  85. if ($fave->find()) {
  86. while ($fave->fetch()) {
  87. try {
  88. $fave->decache();
  89. $fave->query(sprintf(
  90. "UPDATE fave SET uri = '%s', modified = TIMESTAMP '%s' " .
  91. 'WHERE user_id = %d AND notice_id = %d',
  92. Fave::newUri($fave->getActor(), $fave->getTarget(), $fave->modified),
  93. common_sql_date(strtotime($fave->modified)),
  94. $fave->user_id,
  95. $fave->notice_id
  96. ));
  97. } catch (Exception $e) {
  98. common_log(LOG_ERR, "Error updating fave URI: " . $e->getMessage());
  99. }
  100. }
  101. }
  102. printfnq("DONE.\n");
  103. }
  104. public function onRouterInitialized(URLMapper $m)
  105. {
  106. // Web UI actions
  107. $m->connect(
  108. 'main/favor',
  109. ['action' => 'favor']
  110. );
  111. $m->connect(
  112. 'main/disfavor',
  113. ['action' => 'disfavor']
  114. );
  115. if (common_config('singleuser', 'enabled')) {
  116. $nickname = User::singleUserNickname();
  117. $m->connect(
  118. 'favorites',
  119. [
  120. 'action' => 'showfavorites',
  121. 'nickname' => $nickname,
  122. ]
  123. );
  124. $m->connect(
  125. 'favoritesrss',
  126. [
  127. 'action' => 'favoritesrss',
  128. 'nickname' => $nickname,
  129. ]
  130. );
  131. } else {
  132. $m->connect(
  133. 'favoritedrss',
  134. ['action' => 'favoritedrss']
  135. );
  136. $m->connect(
  137. 'favorited/',
  138. ['action' => 'favorited']
  139. );
  140. $m->connect(
  141. 'favorited',
  142. ['action' => 'favorited']
  143. );
  144. $m->connect(
  145. ':nickname/favorites',
  146. ['action' => 'showfavorites'],
  147. ['nickname' => Nickname::DISPLAY_FMT]
  148. );
  149. $m->connect(
  150. ':nickname/favorites/rss',
  151. ['action' => 'favoritesrss'],
  152. ['nickname' => Nickname::DISPLAY_FMT]
  153. );
  154. }
  155. // Favorites for API
  156. $m->connect(
  157. 'api/favorites/create.:format',
  158. ['action' => 'ApiFavoriteCreate'],
  159. ['format' => '(xml|json)']
  160. );
  161. $m->connect(
  162. 'api/favorites/destroy.:format',
  163. ['action' => 'ApiFavoriteDestroy'],
  164. ['format' => '(xml|json)']
  165. );
  166. $m->connect(
  167. 'api/favorites/list.:format',
  168. ['action' => 'ApiTimelineFavorites'],
  169. ['format' => '(xml|json|rss|atom|as)']
  170. );
  171. $m->connect(
  172. 'api/favorites/:id.:format',
  173. ['action' => 'ApiTimelineFavorites'],
  174. [
  175. 'id' => Nickname::INPUT_FMT,
  176. 'format' => '(xml|json|rss|atom|as)',
  177. ]
  178. );
  179. $m->connect(
  180. 'api/favorites.:format',
  181. ['action' => 'ApiTimelineFavorites'],
  182. ['format' => '(xml|json|rss|atom|as)']
  183. );
  184. $m->connect(
  185. 'api/favorites/create/:id.:format',
  186. ['action' => 'ApiFavoriteCreate'],
  187. [
  188. 'id' => '[0-9]+',
  189. 'format' => '(xml|json)',
  190. ]
  191. );
  192. $m->connect(
  193. 'api/favorites/destroy/:id.:format',
  194. ['action' => 'ApiFavoriteDestroy'],
  195. [
  196. 'id' => '[0-9]+',
  197. 'format' => '(xml|json)',
  198. ]
  199. );
  200. // AtomPub API
  201. $m->connect(
  202. 'api/statusnet/app/favorites/:profile/:notice.atom',
  203. ['action' => 'AtomPubShowFavorite'],
  204. [
  205. 'profile' => '[0-9]+',
  206. 'notice' => '[0-9]+',
  207. ]
  208. );
  209. $m->connect(
  210. 'api/statusnet/app/favorites/:profile.atom',
  211. ['action' => 'AtomPubFavoriteFeed'],
  212. ['profile' => '[0-9]+']
  213. );
  214. // Required for qvitter API
  215. $m->connect(
  216. 'api/statuses/favs/:id.:format',
  217. ['action' => 'ApiStatusesFavs'],
  218. [
  219. 'id' => '[0-9]+',
  220. 'format' => '(xml|json)',
  221. ]
  222. );
  223. }
  224. // FIXME: Set this to abstract public in lib/modules/ActivityHandlerPlugin.php when all plugins have migrated!
  225. protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
  226. {
  227. if (!$this->isMyActivity($act)) {
  228. doom("あなたのアクティビティではありません。", __FILE__, __LINE__);
  229. }
  230. // We must have an objects[0] here because in isMyActivity we require the count to be == 1
  231. $actobj = $act->objects[0];
  232. $object = Fave::saveActivityObject($actobj, $stored);
  233. return $object;
  234. }
  235. // FIXME: Put this in lib/modules/ActivityHandlerPlugin.php when we're ready
  236. // with the other microapps/activityhandlers as well.
  237. // Also it should be StartNoticeAsActivity (with a prepped Activity, including ->context etc.)
  238. public function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
  239. {
  240. if (!$this->isMyNotice($stored)) {
  241. return true;
  242. }
  243. $this->extendActivity($stored, $act, $scoped);
  244. return false;
  245. }
  246. public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
  247. {
  248. Fave::extendActivity($stored, $act, $scoped);
  249. }
  250. public function activityObjectFromNotice(Notice $notice)
  251. {
  252. $fave = Fave::fromStored($notice);
  253. return $fave->asActivityObject();
  254. }
  255. public function deleteRelated(Notice $notice)
  256. {
  257. try {
  258. $fave = Fave::fromStored($notice);
  259. $fave->delete();
  260. } catch (NoResultException $e) {
  261. // Cool, no problem. We wanted to get rid of it anyway.
  262. }
  263. }
  264. // API stuff
  265. /**
  266. * Typically just used to fill out Twitter-compatible API status data.
  267. *
  268. * FIXME: Make all the calls before this end up with a Notice instead of ArrayWrapper please...
  269. */
  270. public function onNoticeSimpleStatusArray($notice, array &$status, Profile $scoped=null, array $args=array())
  271. {
  272. if ($scoped instanceof Profile) {
  273. $status['favorited'] = Fave::existsForProfile($notice, $scoped);
  274. } else {
  275. $status['favorited'] = false;
  276. }
  277. return true;
  278. }
  279. public function onTwitterUserArray(Profile $profile, array &$userdata, Profile $scoped=null, array $args=array())
  280. {
  281. $userdata['favourites_count'] = Fave::countByProfile($profile);
  282. }
  283. /**
  284. * Typically just used to fill out StatusNet specific data in API calls in the referenced $info array.
  285. */
  286. public function onStatusNetApiNoticeInfo(Notice $notice, array &$info, Profile $scoped=null, array $args=array())
  287. {
  288. if ($scoped instanceof Profile) {
  289. $info['favorite'] = Fave::existsForProfile($notice, $scoped) ? 'true' : 'false';
  290. }
  291. return true;
  292. }
  293. public function onNoticeDeleteRelated(Notice $notice)
  294. {
  295. parent::onNoticeDeleteRelated($notice);
  296. // The below algorithm is because we want to delete fave
  297. // activities on any notice which _has_ faves, and not as
  298. // in the parent function only ones that _are_ faves.
  299. $fave = new Fave();
  300. $fave->notice_id = $notice->id;
  301. if ($fave->find()) {
  302. while ($fave->fetch()) {
  303. $fave->delete();
  304. }
  305. }
  306. $fave->free();
  307. }
  308. public function onProfileDeleteRelated(Profile $profile, array &$related)
  309. {
  310. $fave = new Fave();
  311. $fave->user_id = $profile->id;
  312. // Will perform a DELETE matching "user_id = {$user->id}"
  313. if ($fave->find()) {
  314. while ($fave->fetch()) {
  315. $fave->delete();
  316. }
  317. }
  318. $fave->free();
  319. Fave::blowCacheForProfileId($profile->id);
  320. return true;
  321. }
  322. public function onStartNoticeListPrefill(array &$notices, array $notice_ids, Profile $scoped=null)
  323. {
  324. // prefill array of objects, before pluginfication it was Notice::fillFaves($notices)
  325. Fave::fillFaves($notice_ids);
  326. // DB caching
  327. if ($scoped instanceof Profile) {
  328. Fave::pivotGet('notice_id', $notice_ids, array('user_id' => $scoped->id));
  329. }
  330. }
  331. /**
  332. * show the "favorite" form in the notice options element
  333. * FIXME: Don't let a NoticeListItemAdapter slip in here (or extend that from NoticeListItem)
  334. *
  335. * @return void
  336. */
  337. public function onStartShowNoticeOptionItems($nli)
  338. {
  339. if (Event::handle('StartShowFaveForm', array($nli))) {
  340. $scoped = Profile::current();
  341. if ($scoped instanceof Profile) {
  342. if (Fave::existsForProfile($nli->notice, $scoped)) {
  343. $disfavor = new DisfavorForm($nli->out, $nli->notice);
  344. $disfavor->show();
  345. } else {
  346. $favor = new FavorForm($nli->out, $nli->notice);
  347. $favor->show();
  348. }
  349. }
  350. Event::handle('EndShowFaveForm', array($nli));
  351. }
  352. }
  353. protected function showNoticeListItem(NoticeListItem $nli)
  354. {
  355. // pass
  356. }
  357. public function openNoticeListItemElement(NoticeListItem $nli)
  358. {
  359. // pass
  360. }
  361. public function closeNoticeListItemElement(NoticeListItem $nli)
  362. {
  363. // pass
  364. }
  365. public function onAppendUserActivityStreamObjects(UserActivityStream $uas, array &$objs)
  366. {
  367. $fave = new Fave();
  368. $fave->user_id = $uas->getUser()->id;
  369. if (!empty($uas->after)) {
  370. $fave->whereAdd("modified > '" . common_sql_date($uas->after) . "'");
  371. }
  372. $fave->orderBy('modified DESC, notice_id DESC');
  373. if ($fave->find()) {
  374. while ($fave->fetch()) {
  375. $objs[] = clone($fave);
  376. }
  377. }
  378. return true;
  379. }
  380. public function onEndShowThreadedNoticeTailItems(NoticeListItem $nli, Notice $notice, &$threadActive)
  381. {
  382. if ($nli instanceof ThreadedNoticeListSubItem) {
  383. // The sub-items are replies to a conversation, thus we use different HTML elements etc.
  384. $item = new ThreadedNoticeListInlineFavesItem($notice, $nli->out);
  385. } else {
  386. $item = new ThreadedNoticeListFavesItem($notice, $nli->out);
  387. }
  388. $threadActive = $item->show() || $threadActive;
  389. return true;
  390. }
  391. public function onEndFavorNotice(Profile $actor, Notice $target)
  392. {
  393. try {
  394. $notice_author = $target->getProfile();
  395. // Don't notify ourselves of our own favorite on our own notice,
  396. // or if it's a remote user (since we don't know their email addresses etc.)
  397. if ($notice_author->id == $actor->id || !$notice_author->isLocal()) {
  398. return true;
  399. }
  400. $local_user = $notice_author->getUser();
  401. mail_notify_fave($local_user, $actor, $target);
  402. } catch (Exception $e) {
  403. // Mm'kay, probably not a local user. Let's skip this favor notification.
  404. }
  405. }
  406. /**
  407. * EndInterpretCommand for FavoritePlugin will handle the 'fav' command
  408. * using the class FavCommand.
  409. *
  410. * @param string $cmd Command being run
  411. * @param string $arg Rest of the message (including address)
  412. * @param User $user User sending the message
  413. * @param Command &$result The resulting command object to be run.
  414. *
  415. * @return boolean hook value
  416. */
  417. public function onStartInterpretCommand($cmd, $arg, $user, &$result)
  418. {
  419. if ($result === false && $cmd == 'fav') {
  420. if (empty($arg)) {
  421. $result = null;
  422. } else {
  423. list($other, $extra) = CommandInterpreter::split_arg($arg);
  424. if (!empty($extra)) {
  425. $result = null;
  426. } else {
  427. $result = new FavCommand($user, $other);
  428. }
  429. }
  430. return false;
  431. }
  432. return true;
  433. }
  434. public function onHelpCommandMessages(HelpCommand $help, array &$commands)
  435. {
  436. // TRANS: Help message for IM/SMS command "fav <nickname>".
  437. $commands['fav <nickname>'] = _m('COMMANDHELP', "add user's last notice as a 'fave'");
  438. // TRANS: Help message for IM/SMS command "fav #<notice_id>".
  439. $commands['fav #<notice_id>'] = _m('COMMANDHELP', "add notice with the given id as a 'fave'");
  440. }
  441. /**
  442. * Are we allowed to perform a certain command over the API?
  443. */
  444. public function onCommandSupportedAPI(Command $cmd, &$supported)
  445. {
  446. $supported = $supported || $cmd instanceof FavCommand;
  447. }
  448. // Form stuff (settings etc.)
  449. public function onEndEmailFormData(Action $action, Profile $scoped)
  450. {
  451. $emailfave = $scoped->getConfigPref('email', 'notify_fave') ? 1 : 0;
  452. $action->elementStart('li');
  453. $action->checkbox(
  454. 'email-notify_fave',
  455. // TRANS: Checkbox label in e-mail preferences form.
  456. _('Send me email when someone adds my notice as a favorite.'),
  457. $emailfave
  458. );
  459. $action->elementEnd('li');
  460. return true;
  461. }
  462. public function onStartEmailSaveForm(Action $action, Profile $scoped)
  463. {
  464. $emailfave = $action->boolean('email-notify_fave');
  465. try {
  466. if ($emailfave == (bool) $scoped->getPref('email', 'notify_fave')) {
  467. // No need to update setting
  468. return true;
  469. }
  470. } catch (NoResultException $e) {
  471. // Apparently there's no previously stored setting, then continue to save it as it is now.
  472. }
  473. $scoped->setPref('email', 'notify_fave', $emailfave);
  474. return true;
  475. }
  476. // Layout stuff
  477. public function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
  478. {
  479. $menu->out->menuItem(
  480. common_local_url('showfavorites', ['nickname' => $target->getNickname()]),
  481. // TRANS: Menu item in personal group navigation menu.
  482. _m('MENU', 'Favorites'),
  483. // @todo i18n FIXME: Need to make this two messages.
  484. // TRANS: Menu item title in personal group navigation menu.
  485. // TRANS: %s is a username.
  486. sprintf(_('%s\'s favorite notices'), $target->getBestName()),
  487. ($scoped instanceof Profile && $target->id === $scoped->id && $menu->actionName === 'showfavorites'),
  488. 'nav_timeline_favorites'
  489. );
  490. }
  491. public function onEndPublicGroupNav(Menu $menu)
  492. {
  493. if (!common_config('singleuser', 'enabled')) {
  494. // TRANS: Menu item in search group navigation panel.
  495. $menu->out->menuItem(
  496. common_local_url('favorited'),
  497. _m('MENU', 'Popular'),
  498. // TRANS: Menu item title in search group navigation panel.
  499. _('Popular notices'),
  500. ($menu->actionName === 'favorited'),
  501. 'nav_timeline_favorited'
  502. );
  503. }
  504. }
  505. public function onEndShowSections(Action $action)
  506. {
  507. if (!$action->isAction(array('all', 'public'))) {
  508. return true;
  509. }
  510. if (!common_config('performance', 'high')) {
  511. $section = new PopularNoticeSection($action, $action->getScoped());
  512. $section->show();
  513. }
  514. }
  515. protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  516. {
  517. return Fave::existsForProfile($target, $scoped)
  518. // TRANS: Page/dialog box title when a notice is marked as favorite already
  519. ? _m('TITLE', 'Unmark notice as favorite')
  520. // TRANS: Page/dialog box title when a notice is not marked as favorite
  521. : _m('TITLE', 'Mark notice as favorite');
  522. }
  523. protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  524. {
  525. if ($action->isPost()) {
  526. // The below tests are only for presenting to the user. POSTs which inflict
  527. // duplicate favorite entries are handled with AlreadyFulfilledException.
  528. return false;
  529. }
  530. $exists = Fave::existsForProfile($target, $scoped);
  531. $expected_verb = $exists ? ActivityVerb::UNFAVORITE : ActivityVerb::FAVORITE;
  532. switch (true) {
  533. case $exists && ActivityUtils::compareVerbs($verb, array(ActivityVerb::FAVORITE, ActivityVerb::LIKE)):
  534. case !$exists && ActivityUtils::compareVerbs($verb, array(ActivityVerb::UNFAVORITE, ActivityVerb::UNLIKE)):
  535. common_redirect(common_local_url(
  536. 'activityverb',
  537. [
  538. 'id' => $target->getID(),
  539. 'verb' => ActivityUtils::resolveUri($expected_verb, true),
  540. ]
  541. ));
  542. break;
  543. default:
  544. // No need to redirect as we are on the correct action already.
  545. }
  546. return false;
  547. }
  548. protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  549. {
  550. switch (true) {
  551. case ActivityUtils::compareVerbs($verb, array(ActivityVerb::FAVORITE, ActivityVerb::LIKE)):
  552. Fave::addNew($scoped, $target);
  553. break;
  554. case ActivityUtils::compareVerbs($verb, array(ActivityVerb::UNFAVORITE, ActivityVerb::UNLIKE)):
  555. Fave::removeEntry($scoped, $target);
  556. break;
  557. default:
  558. throw new ServerException('ActivityVerb POST not handled by plugin that was supposed to do it.');
  559. }
  560. return false;
  561. }
  562. protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  563. {
  564. return Fave::existsForProfile($target, $scoped)
  565. ? new DisfavorForm($action, $target)
  566. : new FavorForm($action, $target);
  567. }
  568. public function onModuleVersion(array &$versions): bool
  569. {
  570. $versions[] = array('name' => 'Favorite',
  571. 'version' => self::MODULE_VERSION,
  572. 'author' => 'Mikael Nordfeldth',
  573. 'homepage' => GNUSOCIAL_ENGINE_URL,
  574. 'rawdescription' =>
  575. // TRANS: Plugin description.
  576. _m('Favorites (likes) using ActivityStreams.'));
  577. return true;
  578. }
  579. }
  580. /**
  581. * Notify a user that one of their notices has been chosen as a 'fave'
  582. *
  583. * @param User $rcpt The user whose notice was faved
  584. * @param Profile $sender The user who faved the notice
  585. * @param Notice $notice The notice that was faved
  586. *
  587. * @return void
  588. */
  589. function mail_notify_fave(User $rcpt, Profile $sender, Notice $notice)
  590. {
  591. if (!$rcpt->receivesEmailNotifications() || !$rcpt->getConfigPref('email', 'notify_fave')) {
  592. return;
  593. }
  594. // This test is actually "if the sender is sandboxed"
  595. if (!$sender->hasRight(Right::EMAILONFAVE)) {
  596. return;
  597. }
  598. if ($rcpt->hasBlocked($sender)) {
  599. // If the author has blocked us, don't spam them with a notification.
  600. return;
  601. }
  602. // We need the global mail.php for various mail related functions below.
  603. require_once INSTALLDIR . '/lib/util/mail.php';
  604. $bestname = $sender->getBestName();
  605. common_switch_locale($rcpt->language);
  606. // TRANS: Subject for favorite notification e-mail.
  607. // TRANS: %1$s is the adding user's long name, %2$s is the adding user's nickname.
  608. $subject = sprintf(_('%1$s (@%2$s) added your notice as a favorite'), $bestname, $sender->getNickname());
  609. // TRANS: Body for favorite notification e-mail.
  610. // TRANS: %1$s is the adding user's long name, $2$s is the date the notice was created,
  611. // TRANS: %3$s is a URL to the faved notice, %4$s is the faved notice text,
  612. // TRANS: %5$s is a URL to all faves of the adding user, %6$s is the StatusNet sitename,
  613. // TRANS: %7$s is the adding user's nickname.
  614. $body = sprintf(
  615. _("%1\$s (@%7\$s) just added your notice from %2\$s".
  616. " as one of their favorites.\n\n" .
  617. "The URL of your notice is:\n\n" .
  618. "%3\$s\n\n" .
  619. "The text of your notice is:\n\n" .
  620. "%4\$s\n\n" .
  621. "You can see the list of %1\$s's favorites here:\n\n" .
  622. "%5\$s"),
  623. $bestname,
  624. common_exact_date($notice->created),
  625. common_local_url('shownotice', ['notice' => $notice->id]),
  626. $notice->content,
  627. common_local_url('showfavorites', ['nickname' => $sender->getNickname()]),
  628. common_config('site', 'name'),
  629. $sender->getNickname()
  630. ) .
  631. mail_footer_block();
  632. $headers = _mail_prepare_headers('fave', $rcpt->getNickname(), $sender->getNickname());
  633. common_switch_locale();
  634. mail_to_user($rcpt, $subject, $body, $headers);
  635. }