9 * @author Evan Prodromou <evan@status.net>
10 * @author Robin Millette <millette@status.net>
11 * @author Mikael Nordfeldth <mmn@hethane.se>
12 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
13 * @link http://www.gnu.org/software/social/
15 * StatusNet - the distributed open-source microblogging tool
16 * Copyright (C) 2008, 2009, StatusNet, Inc.
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU Affero General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Affero General Public License for more details.
28 * You should have received a copy of the GNU Affero General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 if (!defined('GNUSOCIAL')) { exit(1); }
34 require_once INSTALLDIR.'/lib/mail.php';
41 * @author Evan Prodromou <evan@status.net>
42 * @author Robin Millette <millette@status.net>
43 * @author Mikael Nordfeldth <mmn@hethane.se>
44 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
45 * @link http://www.gnu.org/software/social/
47 class FavorAction extends FormAction
49 protected $needPost = true;
51 protected $object = null;
53 protected function prepare(array $args=array())
55 parent::prepare($args);
57 $this->target = Notice::getKV($this->trimmed('notice'));
58 if (!$this->target instanceof Notice) {
59 throw new ServerException(_m('No such notice.'));
61 if (!$this->target->inScope($this->scoped)) {
62 // TRANS: Client error displayed when trying to reply to a notice a the target has no access to.
63 // TRANS: %1$s is a user nickname, %2$d is a notice ID (number).
64 throw new ClientException(sprintf(_m('%1$s has no right to reply to notice %2$d.'), $this->scoped->getNickname(), $this->target->id), 403);
70 protected function handlePost()
74 if (Fave::existsForProfile($this->target, $this->scoped)) {
75 // TRANS: Client error displayed when trying to mark a notice as favorite that already is a favorite.
76 throw new AlreadyFulfilledException(_('You have already favorited this!'));
79 $now = common_sql_now();
81 $act = new Activity();
82 $act->id = Fave::newUri($this->scoped, $this->target, $now);
83 $act->type = Fave::getObjectType();
84 $act->actor = $this->scoped->asActivityObject();
85 $act->target = $this->target->asActivityObject();
86 $act->objects = array(clone($act->target));
87 $act->verb = ActivityVerb::FAVORITE;
88 $act->title = ActivityUtils::verbToTitle($act->verb);
89 $act->time = strtotime($now);
91 $stored = Notice::saveActivity($act, $this->scoped,
92 array('uri'=>$act->id));
94 $this->notify($stored, $this->scoped->getUser());
95 Fave::blowCacheForProfileId($this->scoped->id);
97 return _('Favorited the notice');
100 protected function showContent()
102 if ($this->target instanceof Notice) {
103 $disfavor = new DisfavorForm($this, $this->target);
109 * Notifies a user when their notice is favorited.
111 * @param class $notice favorited notice
112 * @param class $user user declaring a favorite
116 function notify($notice, $user)
118 $other = User::getKV('id', $notice->profile_id);
119 if ($other && $other->id != $user->id) {
120 if ($other->email && $other->emailnotifyfav) {
121 mail_notify_fave($other, $user, $notice);
124 // XXX: notify by SMS