]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Fave.php
Annihilate profile_tag_inbox.
[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             self::blow('fave:by_notice:%d', $fave->notice_id);
48
49             Event::handle('EndFavorNotice', array($profile, $notice));
50         }
51
52         return $fave;
53     }
54
55     function delete()
56     {
57         $profile = Profile::staticGet('id', $this->user_id);
58         $notice  = Notice::staticGet('id', $this->notice_id);
59
60         $result = null;
61
62         if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) {
63
64             $result = parent::delete();
65             self::blow('fave:by_notice:%d', $this->notice_id);
66
67             if ($result) {
68                 Event::handle('EndDisfavorNotice', array($profile, $notice));
69             }
70         }
71
72         return $result;
73     }
74
75     function pkeyGet($kv)
76     {
77         return Memcached_DataObject::pkeyGet('Fave', $kv);
78     }
79
80     function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
81     {
82         $stream = new FaveNoticeStream($user_id, $own);
83
84         return $stream->getNotices($offset, $limit, $since_id, $max_id);
85     }
86
87     function idStream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
88     {
89         $stream = new FaveNoticeStream($user_id, $own);
90
91         return $stream->getNoticeIds($offset, $limit, $since_id, $max_id);
92     }
93
94     function asActivity()
95     {
96         $notice  = Notice::staticGet('id', $this->notice_id);
97         $profile = Profile::staticGet('id', $this->user_id);
98
99         $act = new Activity();
100
101         $act->verb = ActivityVerb::FAVORITE;
102
103         // FIXME: rationalize this with URL below
104
105         $act->id   = TagURI::mint('favor:%d:%d:%s',
106                                   $profile->id,
107                                   $notice->id,
108                                   common_date_iso8601($this->modified));
109
110         $act->time    = strtotime($this->modified);
111         // TRANS: Activity title when marking a notice as favorite.
112         $act->title   = _("Favor");
113         // TRANS: Ntofication given when a user marks a notice as favorite.
114         // TRANS: %1$s is a user nickname or full name, %2$s is a notice URI.
115         $act->content = sprintf(_('%1$s marked notice %2$s as a favorite.'),
116                                $profile->getBestName(),
117                                $notice->uri);
118
119         $act->actor     = ActivityObject::fromProfile($profile);
120         $act->objects[] = ActivityObject::fromNotice($notice);
121
122         $url = common_local_url('AtomPubShowFavorite',
123                                           array('profile' => $this->user_id,
124                                                 'notice'  => $this->notice_id));
125
126         $act->selfLink = $url;
127         $act->editLink = $url;
128
129         return $act;
130     }
131
132     /**
133      * Fetch a stream of favorites by profile
134      *
135      * @param integer $profileId Profile that faved
136      * @param integer $offset    Offset from last
137      * @param integer $limit     Number to get
138      *
139      * @return mixed stream of faves, use fetch() to iterate
140      *
141      * @todo Cache results
142      * @todo integrate with Fave::stream()
143      */
144
145     static function byProfile($profileId, $offset, $limit)
146     {
147         $fav = new Fave();
148
149         $fav->user_id = $profileId;
150
151         $fav->orderBy('modified DESC');
152
153         $fav->limit($offset, $limit);
154
155         $fav->find();
156
157         return $fav;
158     }
159
160     /**
161      * Grab a list of profile who have favored this notice.
162      *
163      * @return ArrayWrapper masquerading as a Fave
164      */
165     static function byNotice($noticeId)
166     {
167         $c = self::memcache();
168         $key = Cache::key('fave:by_notice:' . $noticeId);
169
170         $wrapper = $c->get($key);
171         if (!$wrapper) {
172             // @fixme caching & scalability!
173             $fave = new Fave();
174             $fave->notice_id = $noticeId;
175             $fave->find();
176
177             $list = array();
178             while ($fave->fetch()) {
179                 $list[] = clone($fave);
180             }
181             $wrapper = new ArrayWrapper($list);
182             $c->set($key, $wrapper);
183         }
184         return $wrapper;
185     }
186 }