Fave.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. public 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 (EmptyIdException $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. static function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
  135. {
  136. $stream = new FaveNoticeStream($user_id, $own);
  137. return $stream->getNotices($offset, $limit, $since_id, $max_id);
  138. }
  139. function idStream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
  140. {
  141. $stream = new FaveNoticeStream($user_id, $own);
  142. return $stream->getNoticeIds($offset, $limit, $since_id, $max_id);
  143. }
  144. function asActivity()
  145. {
  146. $target = $this->getTarget();
  147. $actor = $this->getActor();
  148. $act = new Activity();
  149. $act->verb = ActivityVerb::FAVORITE;
  150. // FIXME: rationalize this with URL below
  151. $act->id = $this->getUri();
  152. $act->time = strtotime($this->created);
  153. // TRANS: Activity title when marking a notice as favorite.
  154. $act->title = _("Favor");
  155. // TRANS: Message that is the "content" of a favorite (%1$s is the actor's nickname, %2$ is the favorited
  156. // notice's nickname and %3$s is the content of the favorited notice.)
  157. $act->content = sprintf(_('%1$s favorited something by %2$s: %3$s'),
  158. $actor->getNickname(), $target->getProfile()->getNickname(),
  159. $target->getRendered());
  160. $act->actor = $actor->asActivityObject();
  161. $act->target = $target->asActivityObject();
  162. $act->objects = array(clone($act->target));
  163. $url = common_local_url('AtomPubShowFavorite',
  164. array('profile' => $actor->id,
  165. 'notice' => $target->id));
  166. $act->selfLink = $url;
  167. $act->editLink = $url;
  168. return $act;
  169. }
  170. static function existsForProfile($notice, Profile $scoped)
  171. {
  172. $fave = self::pkeyGet(array('user_id'=>$scoped->id, 'notice_id'=>$notice->id));
  173. return ($fave instanceof Fave);
  174. }
  175. /**
  176. * Fetch a stream of favorites by profile
  177. *
  178. * @param integer $profileId Profile that faved
  179. * @param integer $offset Offset from last
  180. * @param integer $limit Number to get
  181. *
  182. * @return mixed stream of faves, use fetch() to iterate
  183. *
  184. * @todo Cache results
  185. * @todo integrate with Fave::stream()
  186. */
  187. static function byProfile($profileId, $offset, $limit)
  188. {
  189. $fav = new Fave();
  190. $fav->user_id = $profileId;
  191. $fav->orderBy('modified DESC');
  192. $fav->limit($offset, $limit);
  193. $fav->find();
  194. return $fav;
  195. }
  196. static function countByProfile(Profile $profile)
  197. {
  198. $c = Cache::instance();
  199. if (!empty($c)) {
  200. $cnt = $c->get(Cache::key('fave:count_by_profile:'.$profile->id));
  201. if (is_integer($cnt)) {
  202. return $cnt;
  203. }
  204. }
  205. $faves = new Fave();
  206. $faves->user_id = $profile->id;
  207. $cnt = (int) $faves->count('notice_id');
  208. if (!empty($c)) {
  209. $c->set(Cache::key('fave:count_by_profile:'.$profile->id), $cnt);
  210. }
  211. return $cnt;
  212. }
  213. static protected $_faves = array();
  214. /**
  215. * All faves of this notice
  216. *
  217. * @param Notice $notice A notice we wish to get faves for (may still be ArrayWrapper)
  218. *
  219. * @return array Array of Fave objects
  220. */
  221. static public function byNotice($notice)
  222. {
  223. if (!isset(self::$_faves[$notice->id])) {
  224. self::fillFaves(array($notice->id));
  225. }
  226. return self::$_faves[$notice->id];
  227. }
  228. static public function fillFaves(array $notice_ids)
  229. {
  230. $faveMap = Fave::listGet('notice_id', $notice_ids);
  231. self::$_faves = array_replace(self::$_faves, $faveMap);
  232. }
  233. static public function blowCacheForProfileId($profile_id)
  234. {
  235. $cache = Cache::instance();
  236. if ($cache) {
  237. // Faves don't happen chronologically, so we need to blow
  238. // ;last cache, too
  239. $cache->delete(Cache::key('fave:ids_by_user:'.$profile_id));
  240. $cache->delete(Cache::key('fave:ids_by_user:'.$profile_id.';last'));
  241. $cache->delete(Cache::key('fave:ids_by_user_own:'.$profile_id));
  242. $cache->delete(Cache::key('fave:ids_by_user_own:'.$profile_id.';last'));
  243. $cache->delete(Cache::key('fave:count_by_profile:'.$profile_id));
  244. }
  245. }
  246. static public function blowCacheForNoticeId($notice_id)
  247. {
  248. $cache = Cache::instance();
  249. if ($cache) {
  250. $cache->delete(Cache::key('fave:list-ids:notice_id:'.$notice_id));
  251. }
  252. }
  253. // Remember that we want the _activity_ notice here, not faves applied
  254. // to the supplied Notice (as with byNotice)!
  255. static public function fromStored(Notice $stored)
  256. {
  257. $class = get_called_class();
  258. $object = new $class;
  259. $object->uri = $stored->uri;
  260. if (!$object->find(true)) {
  261. throw new NoResultException($object);
  262. }
  263. return $object;
  264. }
  265. /**
  266. * Retrieves the _targeted_ notice of a verb (such as the notice that was
  267. * _favorited_, but not the favorite activity itself).
  268. *
  269. * @param Notice $stored The activity notice.
  270. *
  271. * @throws NoResultException when it can't find what it's looking for.
  272. */
  273. static public function getTargetFromStored(Notice $stored)
  274. {
  275. return self::fromStored($stored)->getTarget();
  276. }
  277. static public function getObjectType()
  278. {
  279. return ActivityObject::ACTIVITY;
  280. }
  281. public function asActivityObject(Profile $scoped=null)
  282. {
  283. $actobj = new ActivityObject();
  284. $actobj->id = $this->getUri();
  285. $actobj->type = ActivityUtils::resolveUri(self::getObjectType());
  286. $actobj->actor = $this->getActorObject();
  287. $actobj->target = $this->getTargetObject();
  288. $actobj->objects = array(clone($actobj->target));
  289. $actobj->verb = ActivityVerb::FAVORITE;
  290. $actobj->title = ActivityUtils::verbToTitle($actobj->verb);
  291. $actobj->content = $this->getTarget()->getRendered();
  292. return $actobj;
  293. }
  294. /**
  295. * @param ActivityObject $actobj The _favored_ notice (which we're "in-reply-to")
  296. * @param Notice $stored The _activity_ notice, i.e. the favor itself.
  297. */
  298. static public function parseActivityObject(ActivityObject $actobj, Notice $stored)
  299. {
  300. // throws exception if nothing was found, but it could also be a non-Notice...
  301. // FIXME: This should only test _one_ URI (and not the links etc.) though a function like this could be useful in other cases
  302. $local = ActivityUtils::findLocalObject($actobj->getIdentifiers());
  303. if (!$local instanceof Notice) {
  304. // $local always returns something, but this was not what we expected. Something is wrong.
  305. throw new Exception('Something other than a Notice was returned from findLocalObject');
  306. }
  307. $actor = $stored->getProfile();
  308. $object = new Fave();
  309. $object->user_id = $stored->getProfile()->id;
  310. $object->notice_id = $local->id;
  311. $object->uri = $stored->uri;
  312. $object->created = $stored->created;
  313. $object->modified = $stored->modified;
  314. return $object;
  315. }
  316. static public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
  317. {
  318. $target = self::getTargetFromStored($stored);
  319. // The following logic was copied from StatusNet's Activity plugin
  320. if (ActivityUtils::compareVerbs($target->verb, array(ActivityVerb::POST))) {
  321. // "I like the thing you posted"
  322. $act->objects = $target->asActivity()->objects;
  323. } else {
  324. // "I like that you did whatever you did"
  325. $act->target = $target->asActivityObject();
  326. $act->objects = array(clone($act->target));
  327. }
  328. $act->context->replyToID = $target->getUri();
  329. $act->context->replyToUrl = $target->getUrl();
  330. $act->title = ActivityUtils::verbToTitle($act->verb);
  331. }
  332. static function saveActivityObject(ActivityObject $actobj, Notice $stored)
  333. {
  334. $object = self::parseActivityObject($actobj, $stored);
  335. $object->insert(); // exception throwing in Fave's case!
  336. self::blowCacheForProfileId($object->user_id);
  337. self::blowCacheForNoticeId($object->notice_id);
  338. self::blow('popular');
  339. Event::handle('EndFavorNotice', array($stored->getProfile(), $object->getTarget()));
  340. return $object;
  341. }
  342. public function getTarget()
  343. {
  344. return Notice::getByID($this->notice_id);
  345. }
  346. public function getTargetObject()
  347. {
  348. return $this->getTarget()->asActivityObject();
  349. }
  350. protected $_stored = array();
  351. public function getStored()
  352. {
  353. if (!isset($this->_stored[$this->uri])) {
  354. $stored = new Notice();
  355. $stored->uri = $this->uri;
  356. if (!$stored->find(true)) {
  357. throw new NoResultException($stored);
  358. }
  359. $this->_stored[$this->uri] = $stored;
  360. }
  361. return $this->_stored[$this->uri];
  362. }
  363. public function getActor()
  364. {
  365. return Profile::getByID($this->user_id);
  366. }
  367. public function getActorObject()
  368. {
  369. return $this->getActor()->asActivityObject();
  370. }
  371. public function getUri()
  372. {
  373. if (!empty($this->uri)) {
  374. return $this->uri;
  375. }
  376. // We (should've in this case) created it ourselves, so we tag it ourselves
  377. return self::newUri($this->getActor(), $this->getTarget(), $this->created);
  378. }
  379. }