Fave.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. * Table Definition for fave
  18. */
  19. defined('GNUSOCIAL') || die();
  20. class Fave extends Managed_DataObject
  21. {
  22. public $__table = 'fave'; // table name
  23. public $notice_id; // int(4) primary_key not_null
  24. public $user_id; // int(4) primary_key not_null
  25. public $uri; // varchar(191) not 255 because utf8mb4 takes more space not 255 because utf8mb4 takes more space
  26. public $created; // datetime multiple_key not_null
  27. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  28. public static function schemaDef()
  29. {
  30. return array(
  31. 'fields' => array(
  32. 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice that is the favorite'),
  33. 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'user who likes this notice'),
  34. 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universally unique identifier, usually a tag URI'),
  35. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  36. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  37. ),
  38. 'primary key' => array('notice_id', 'user_id'),
  39. 'unique keys' => array(
  40. 'fave_uri_key' => array('uri'),
  41. ),
  42. 'foreign keys' => array(
  43. 'fave_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  44. 'fave_user_id_fkey' => array('profile', array('user_id' => 'id')), // note: formerly referenced notice.id, but we can now record remote users' favorites
  45. ),
  46. 'indexes' => array(
  47. 'fave_user_id_modified_notice_id_idx' => array('user_id', 'modified', 'notice_id'),
  48. 'fave_notice_id_modified_user_id_idx' => array('notice_id', 'modified', 'user_id'),
  49. ),
  50. );
  51. }
  52. /**
  53. * Save a favorite record.
  54. * @fixme post-author notification should be moved here
  55. *
  56. * @param Profile $actor the local or remote Profile who favorites
  57. * @param Notice $target the notice that is favorited
  58. * @return Fave record on success
  59. * @throws Exception on failure
  60. */
  61. public static function addNew(Profile $actor, Notice $target)
  62. {
  63. if (self::existsForProfile($target, $actor)) {
  64. // TRANS: Client error displayed when trying to mark a notice as favorite that already is a favorite.
  65. throw new AlreadyFulfilledException(_('You have already favorited this!'));
  66. }
  67. $act = new Activity();
  68. $act->type = ActivityObject::ACTIVITY;
  69. $act->verb = ActivityVerb::FAVORITE;
  70. $act->time = time();
  71. $act->id = self::newUri($actor, $target, common_sql_date($act->time));
  72. $act->title = _("Favor");
  73. // TRANS: Message that is the "content" of a favorite (%1$s is the actor's nickname, %2$ is the favorited
  74. // notice's nickname and %3$s is the content of the favorited notice.)
  75. $act->content = sprintf(
  76. _('%1$s favorited something by %2$s: %3$s'),
  77. $actor->getNickname(),
  78. $target->getProfile()->getNickname(),
  79. $target->getRendered()
  80. );
  81. $act->actor = $actor->asActivityObject();
  82. $act->target = $target->asActivityObject();
  83. $act->objects = array(clone($act->target));
  84. $url = common_local_url('AtomPubShowFavorite', array('profile'=>$actor->id, 'notice'=>$target->id));
  85. $act->selfLink = $url;
  86. $act->editLink = $url;
  87. // saveActivity will in turn also call Fave::saveActivityObject which does
  88. // what this function used to do before this commit.
  89. $stored = Notice::saveActivity($act, $actor);
  90. return $stored;
  91. }
  92. public static function removeEntry(Profile $actor, Notice $target)
  93. {
  94. $fave = new Fave();
  95. $fave->user_id = $actor->getID();
  96. $fave->notice_id = $target->getID();
  97. if (!$fave->find(true)) {
  98. // TRANS: Client error displayed when trying to remove a 'favor' when there is none in the first place.
  99. throw new AlreadyFulfilledException(_('This is already not favorited.'));
  100. }
  101. $result = $fave->delete();
  102. if ($result === false) {
  103. common_log_db_error($fave, 'DELETE', __FILE__);
  104. // TRANS: Server error displayed when removing a favorite from the database fails.
  105. throw new ServerException(_('Could not delete favorite.'));
  106. }
  107. Fave::blowCacheForProfileId($actor->getID());
  108. Fave::blowCacheForNoticeId($target->getID());
  109. }
  110. // exception throwing takeover!
  111. public function insert()
  112. {
  113. if (parent::insert()===false) {
  114. common_log_db_error($this, 'INSERT', __FILE__);
  115. throw new ServerException(sprintf(_m('Could not store new object of type %s'), get_called_class()));
  116. }
  117. self::blowCacheForProfileId($this->user_id);
  118. self::blowCacheForNoticeId($this->notice_id);
  119. return $this;
  120. }
  121. public function delete($useWhere=false)
  122. {
  123. $result = null;
  124. try {
  125. $profile = $this->getActor();
  126. $notice = $this->getTarget();
  127. if (Event::handle('StartDisfavorNotice', [$profile, $notice, &$result])) {
  128. $result = parent::delete($useWhere);
  129. if ($result !== false) {
  130. Event::handle('EndDisfavorNotice', [$profile, $notice]);
  131. }
  132. }
  133. } catch (NoResultException $e) {
  134. // In case there's some inconsistency where the profile or notice was deleted without losing the fave db entry
  135. common_log(LOG_INFO, '"'.get_class($e->obj).'" with id=='.var_export($e->obj->id, true).' object not found when deleting favorite, ignoring...');
  136. } catch (EmptyPkeyValueException $e) {
  137. // Some buggy instances of GNU social have had favorites with notice id==0 stored in the database
  138. common_log(LOG_INFO, _ve($e->getMessage()));
  139. }
  140. // If we catch an exception above, then $result===null because parent::delete only returns an int>=0 or boolean false
  141. if (is_null($result)) {
  142. // Delete it without the event, as something is wrong and we don't want it anyway.
  143. $result = parent::delete($useWhere);
  144. }
  145. // Err, apparently we can reference $this->user_id after parent::delete,
  146. // I guess it's safe because this is the order it was before!
  147. self::blowCacheForProfileId($this->user_id);
  148. self::blowCacheForNoticeId($this->notice_id);
  149. self::blow('popular');
  150. return $result;
  151. }
  152. // FIXME: Instead of $own, send the scoped Profile so we can pass it along
  153. // directly to FaveNoticeStream and preferrably we should get a Profile
  154. // instead of $user_id
  155. public static function stream(
  156. $user_id,
  157. $offset = 0,
  158. $limit = NOTICES_PER_PAGE,
  159. $own = false,
  160. $since_id = 0,
  161. $max_id = 0
  162. ) {
  163. $target = Profile::getByID($user_id);
  164. $stream = new FaveNoticeStream($target, ($own ? $target : null));
  165. return $stream->getNotices($offset, $limit, $since_id, $max_id);
  166. }
  167. // FIXME: Instead of $own, send the scoped Profile so we can pass it along
  168. // directly to FaveNoticeStream and preferrably we should get a Profile
  169. // instead of $user_id
  170. public function idStream(
  171. $user_id,
  172. $offset = 0,
  173. $limit = NOTICES_PER_PAGE,
  174. $own = false,
  175. $since_id = 0,
  176. $max_id = 0
  177. ) {
  178. $target = Profile::getByID($user_id);
  179. $stream = new FaveNoticeStream($target, ($own ? $target : null));
  180. return $stream->getNoticeIds($offset, $limit, $since_id, $max_id);
  181. }
  182. public function asActivity()
  183. {
  184. $target = $this->getTarget();
  185. $actor = $this->getActor();
  186. $act = new Activity();
  187. $act->verb = ActivityVerb::FAVORITE;
  188. // FIXME: rationalize this with URL below
  189. $act->id = $this->getUri();
  190. $act->time = strtotime($this->created);
  191. // TRANS: Activity title when marking a notice as favorite.
  192. $act->title = _("Favor");
  193. // TRANS: Message that is the "content" of a favorite (%1$s is the actor's nickname, %2$ is the favorited
  194. // notice's nickname and %3$s is the content of the favorited notice.)
  195. $act->content = sprintf(
  196. _('%1$s favorited something by %2$s: %3$s'),
  197. $actor->getNickname(),
  198. $target->getProfile()->getNickname(),
  199. $target->getRendered()
  200. );
  201. $act->context = new ActivityContext();
  202. $act->context->replyToID = $target->getUri();
  203. try {
  204. $act->context->replyToURL = $target->getUrl();
  205. } catch (InvalidUrlException $e) {
  206. // ok, no replyToURL, i.e. the href="" in <thr:in-reply-to/>
  207. }
  208. $act->actor = $actor->asActivityObject();
  209. $act->target = $target->asActivityObject();
  210. $act->objects = array(clone($act->target));
  211. $url = common_local_url(
  212. 'AtomPubShowFavorite',
  213. [
  214. 'profile' => $actor->id,
  215. 'notice' => $target->id,
  216. ]
  217. );
  218. $act->selfLink = $url;
  219. $act->editLink = $url;
  220. return $act;
  221. }
  222. public static function existsForProfile($notice, Profile $scoped)
  223. {
  224. $fave = self::pkeyGet(array('user_id'=>$scoped->id, 'notice_id'=>$notice->id));
  225. return ($fave instanceof Fave);
  226. }
  227. /**
  228. * Fetch a stream of favorites by profile
  229. *
  230. * @param integer $profileId Profile that faved
  231. * @param integer $offset Offset from last
  232. * @param integer $limit Number to get
  233. *
  234. * @return mixed stream of faves, use fetch() to iterate
  235. *
  236. * @todo Cache results
  237. * @todo integrate with Fave::stream()
  238. */
  239. public static function byProfile($profileId, $offset, $limit)
  240. {
  241. $fav = new Fave();
  242. $fav->user_id = $profileId;
  243. $fav->orderBy('modified DESC, notice_id DESC');
  244. $fav->limit($offset, $limit);
  245. $fav->find();
  246. return $fav;
  247. }
  248. public static function countByProfile(Profile $profile)
  249. {
  250. $c = Cache::instance();
  251. if (!empty($c)) {
  252. $cnt = $c->get(Cache::key('fave:count_by_profile:'.$profile->id));
  253. if (is_integer($cnt)) {
  254. return $cnt;
  255. }
  256. }
  257. $faves = new Fave();
  258. $faves->user_id = $profile->id;
  259. $cnt = (int) $faves->count('notice_id');
  260. if (!empty($c)) {
  261. $c->set(Cache::key('fave:count_by_profile:'.$profile->id), $cnt);
  262. }
  263. return $cnt;
  264. }
  265. protected static $_faves = [];
  266. /**
  267. * All faves of this notice
  268. *
  269. * @param Notice $notice A notice we wish to get faves for (may still be ArrayWrapper)
  270. *
  271. * @return array Array of Fave objects
  272. */
  273. public static function byNotice($notice)
  274. {
  275. if (!isset(self::$_faves[$notice->id])) {
  276. self::fillFaves(array($notice->id));
  277. }
  278. return self::$_faves[$notice->id];
  279. }
  280. public static function fillFaves(array $notice_ids)
  281. {
  282. $faveMap = Fave::listGet('notice_id', $notice_ids);
  283. self::$_faves = array_replace(self::$_faves, $faveMap);
  284. }
  285. public static function blowCacheForProfileId($profile_id)
  286. {
  287. $cache = Cache::instance();
  288. if ($cache) {
  289. // Faves don't happen chronologically, so we need to blow
  290. // ;last cache, too
  291. $cache->delete(Cache::key('fave:ids_by_user:'.$profile_id));
  292. $cache->delete(Cache::key('fave:ids_by_user:'.$profile_id.';last'));
  293. $cache->delete(Cache::key('fave:ids_by_user_own:'.$profile_id));
  294. $cache->delete(Cache::key('fave:ids_by_user_own:'.$profile_id.';last'));
  295. $cache->delete(Cache::key('fave:count_by_profile:'.$profile_id));
  296. }
  297. }
  298. public static function blowCacheForNoticeId($notice_id)
  299. {
  300. $cache = Cache::instance();
  301. if ($cache) {
  302. $cache->delete(Cache::key('fave:list-ids:notice_id:'.$notice_id));
  303. }
  304. }
  305. // Remember that we want the _activity_ notice here, not faves applied
  306. // to the supplied Notice (as with byNotice)!
  307. public static function fromStored(Notice $stored)
  308. {
  309. $class = get_called_class();
  310. $object = new $class;
  311. $object->uri = $stored->uri;
  312. if (!$object->find(true)) {
  313. throw new NoResultException($object);
  314. }
  315. return $object;
  316. }
  317. /**
  318. * Retrieves the _targeted_ notice of a verb (such as the notice that was
  319. * _favorited_, but not the favorite activity itself).
  320. *
  321. * @param Notice $stored The activity notice.
  322. *
  323. * @throws NoResultException when it can't find what it's looking for.
  324. */
  325. public static function getTargetFromStored(Notice $stored)
  326. {
  327. return self::fromStored($stored)->getTarget();
  328. }
  329. public static function getObjectType()
  330. {
  331. return ActivityObject::ACTIVITY;
  332. }
  333. public function asActivityObject(Profile $scoped=null)
  334. {
  335. $actobj = new ActivityObject();
  336. $actobj->id = $this->getUri();
  337. $actobj->type = ActivityUtils::resolveUri(self::getObjectType());
  338. $actobj->actor = $this->getActorObject();
  339. $actobj->target = $this->getTargetObject();
  340. $actobj->objects = array(clone($actobj->target));
  341. $actobj->verb = ActivityVerb::FAVORITE;
  342. $actobj->title = ActivityUtils::verbToTitle($actobj->verb);
  343. $actobj->content = $this->getTarget()->getRendered();
  344. return $actobj;
  345. }
  346. /**
  347. * @param ActivityObject $actobj The _favored_ notice (which we're "in-reply-to")
  348. * @param Notice $stored The _activity_ notice, i.e. the favor itself.
  349. */
  350. public static function parseActivityObject(ActivityObject $actobj, Notice $stored)
  351. {
  352. // throws exception if nothing was found, but it could also be a non-Notice...
  353. // FIXME: This should only test _one_ URI (and not the links etc.) though a function like this could be useful in other cases
  354. $local = ActivityUtils::findLocalObject($actobj->getIdentifiers());
  355. if (!$local instanceof Notice) {
  356. // $local always returns something, but this was not what we expected. Something is wrong.
  357. throw new Exception('Something other than a Notice was returned from findLocalObject');
  358. }
  359. $actor = $stored->getProfile();
  360. $object = new Fave();
  361. $object->user_id = $stored->getProfile()->id;
  362. $object->notice_id = $local->id;
  363. $object->uri = $stored->uri;
  364. $object->created = $stored->created;
  365. $object->modified = $stored->modified;
  366. return $object;
  367. }
  368. public static function extendActivity(
  369. Notice $stored,
  370. Activity $act,
  371. Profile $scoped = null
  372. ) {
  373. $target = self::getTargetFromStored($stored);
  374. // The following logic was copied from StatusNet's Activity plugin
  375. if (ActivityUtils::compareVerbs($target->verb, array(ActivityVerb::POST))) {
  376. // "I like the thing you posted"
  377. $act->objects = $target->asActivity()->objects;
  378. } else {
  379. // "I like that you did whatever you did"
  380. $act->target = $target->asActivityObject();
  381. $act->objects = array(clone($act->target));
  382. }
  383. $act->context->replyToID = $target->getUri();
  384. $act->context->replyToUrl = $target->getUrl();
  385. $act->title = ActivityUtils::verbToTitle($act->verb);
  386. }
  387. public static function saveActivityObject(ActivityObject $actobj, Notice $stored)
  388. {
  389. $object = self::parseActivityObject($actobj, $stored);
  390. $object->insert(); // exception throwing in Fave's case!
  391. self::blowCacheForProfileId($object->user_id);
  392. self::blowCacheForNoticeId($object->notice_id);
  393. self::blow('popular');
  394. Event::handle('EndFavorNotice', array($stored->getProfile(), $object->getTarget()));
  395. return $object;
  396. }
  397. public function getTarget()
  398. {
  399. return Notice::getByID($this->notice_id);
  400. }
  401. public function getTargetObject()
  402. {
  403. return $this->getTarget()->asActivityObject();
  404. }
  405. protected $_stored = array();
  406. public function getStored()
  407. {
  408. if (!isset($this->_stored[$this->uri])) {
  409. $stored = new Notice();
  410. $stored->uri = $this->uri;
  411. if (!$stored->find(true)) {
  412. throw new NoResultException($stored);
  413. }
  414. $this->_stored[$this->uri] = $stored;
  415. }
  416. return $this->_stored[$this->uri];
  417. }
  418. public function getActor()
  419. {
  420. return Profile::getByID($this->user_id);
  421. }
  422. public function getActorObject()
  423. {
  424. return $this->getActor()->asActivityObject();
  425. }
  426. public function getUri()
  427. {
  428. if (!empty($this->uri)) {
  429. return $this->uri;
  430. }
  431. // We (should've in this case) created it ourselves, so we tag it ourselves
  432. return self::newUri($this->getActor(), $this->getTarget(), $this->created);
  433. }
  434. }