]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Fave.php
Show a single favorite for AtomPub
[quix0rs-gnu-social.git] / classes / Fave.php
1 <?php
2 /**
3  * Table Definition for fave
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Fave extends Memcached_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'fave';                            // table name
13     public $notice_id;                       // int(4)  primary_key not_null
14     public $user_id;                         // int(4)  primary_key not_null
15     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
16
17     /* Static get */
18     function staticGet($k,$v=null)
19     { return Memcached_DataObject::staticGet('Fave',$k,$v); }
20
21     /* the code above is auto generated do not remove the tag below */
22     ###END_AUTOCODE
23
24     /**
25      * Save a favorite record.
26      * @fixme post-author notification should be moved here
27      *
28      * @param Profile $profile the local or remote user who likes
29      * @param Notice $notice the notice that is liked
30      * @return mixed false on failure, or Fave record on success
31      */
32     static function addNew(Profile $profile, Notice $notice) {
33
34         $fave = null;
35
36         if (Event::handle('StartFavorNotice', array($profile, $notice, &$fave))) {
37
38             $fave = new Fave();
39
40             $fave->user_id   = $profile->id;
41             $fave->notice_id = $notice->id;
42
43             if (!$fave->insert()) {
44                 common_log_db_error($fave, 'INSERT', __FILE__);
45                 return false;
46             }
47
48             Event::handle('EndFavorNotice', array($profile, $notice));
49         }
50
51         return $fave;
52     }
53
54     function delete()
55     {
56         $profile = Profile::staticGet('id', $this->user_id);
57         $notice  = Notice::staticGet('id', $this->notice_id);
58
59         $result = null;
60
61         if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) {
62
63             $result = parent::delete();
64
65             if ($result) {
66                 Event::handle('EndDisfavorNotice', array($profile, $notice));
67             }
68         }
69
70         return $result;
71     }
72
73     function pkeyGet($kv)
74     {
75         return Memcached_DataObject::pkeyGet('Fave', $kv);
76     }
77
78     function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
79     {
80         $ids = Notice::stream(array('Fave', '_streamDirect'),
81                               array($user_id, $own),
82                               ($own) ? 'fave:ids_by_user_own:'.$user_id :
83                               'fave:ids_by_user:'.$user_id,
84                               $offset, $limit, $since_id, $max_id);
85         return $ids;
86     }
87
88     function _streamDirect($user_id, $own, $offset, $limit, $since_id, $max_id)
89     {
90         $fav = new Fave();
91         $qry = null;
92
93         if ($own) {
94             $qry  = 'SELECT fave.* FROM fave ';
95             $qry .= 'WHERE fave.user_id = ' . $user_id . ' ';
96         } else {
97              $qry =  'SELECT fave.* FROM fave ';
98              $qry .= 'INNER JOIN notice ON fave.notice_id = notice.id ';
99              $qry .= 'WHERE fave.user_id = ' . $user_id . ' ';
100              $qry .= 'AND notice.is_local != ' . Notice::GATEWAY . ' ';
101         }
102
103         if ($since_id != 0) {
104             $qry .= 'AND notice_id > ' . $since_id . ' ';
105         }
106
107         if ($max_id != 0) {
108             $qry .= 'AND notice_id <= ' . $max_id . ' ';
109         }
110
111         // NOTE: we sort by fave time, not by notice time!
112
113         $qry .= 'ORDER BY modified DESC ';
114
115         if (!is_null($offset)) {
116             $qry .= "LIMIT $limit OFFSET $offset";
117         }
118
119         $fav->query($qry);
120
121         $ids = array();
122
123         while ($fav->fetch()) {
124             $ids[] = $fav->notice_id;
125         }
126
127         $fav->free();
128         unset($fav);
129
130         return $ids;
131     }
132
133     function asActivity()
134     {
135         $notice  = Notice::staticGet('id', $this->notice_id);
136         $profile = Profile::staticGet('id', $this->user_id);
137
138         $act = new Activity();
139
140         $act->verb = ActivityVerb::FAVORITE;
141
142         // FIXME: rationalize this with URL below
143
144         $act->id   = TagURI::mint('favor:%d:%d:%s',
145                                   $profile->id,
146                                   $notice->id,
147                                   common_date_iso8601($this->modified));
148
149         $act->time    = strtotime($this->modified);
150         // TRANS: Activity title when marking a notice as favorite.
151         $act->title   = _("Favor");
152         // TRANS: Ntofication given when a user marks a notice as favorite.
153         // TRANS: %1$s is a user nickname or full name, %2$s is a notice URI.
154         $act->content = sprintf(_('%1$s marked notice %2$s as a favorite.'),
155                                $profile->getBestName(),
156                                $notice->uri);
157
158         $act->actor     = ActivityObject::fromProfile($profile);
159         $act->objects[] = ActivityObject::fromNotice($notice);
160
161         $url = common_local_url('AtomPubShowFavorite',
162                                           array('profile' => $this->user_id,
163                                                 'notice'  => $this->notice_id));
164
165         $act->selfLink = $url;
166         $act->editLink = $url;
167
168         return $act;
169     }
170 }