Fave.php 17 KB

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