]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Fave.php
455e7b089cee57f44ede339cbe8ecab8f5127564
[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 Managed_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 $uri;                             // varchar(255)
16     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
17
18     /* Static get */
19     function staticGet($k,$v=null)
20     { return Memcached_DataObject::staticGet('Fave',$k,$v); }
21
22     /* the code above is auto generated do not remove the tag below */
23     ###END_AUTOCODE
24
25     public static function schemaDef()
26     {
27         return array(
28             'fields' => array(
29                 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice that is the favorite'),
30                 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'user who likes this notice'),
31                 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'universally unique identifier, usually a tag URI'),
32                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
33             ),
34             'primary key' => array('notice_id', 'user_id'),
35             'unique keys' => array(
36                 'fave_uri_key' => array('uri'),
37             ),
38             'foreign keys' => array(
39                 'fave_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
40                 'fave_user_id_fkey' => array('profile', array('user_id' => 'id')), // note: formerly referenced notice.id, but we can now record remote users' favorites
41             ),
42             'indexes' => array(
43                 'fave_notice_id_idx' => array('notice_id'),
44                 'fave_user_id_idx' => array('user_id', 'modified'),
45                 'fave_modified_idx' => array('modified'),
46             ),
47         );
48     }
49
50     /**
51      * Save a favorite record.
52      * @fixme post-author notification should be moved here
53      *
54      * @param Profile $profile the local or remote user who likes
55      * @param Notice $notice the notice that is liked
56      * @return mixed false on failure, or Fave record on success
57      */
58     static function addNew(Profile $profile, Notice $notice) {
59
60         $fave = null;
61
62         if (Event::handle('StartFavorNotice', array($profile, $notice, &$fave))) {
63
64             $fave = new Fave();
65
66             $fave->user_id   = $profile->id;
67             $fave->notice_id = $notice->id;
68             $fave->modified  = common_sql_now();
69             $fave->uri       = self::newURI($fave->user_id,
70                                             $fave->notice_id,
71                                             $fave->modified);
72             if (!$fave->insert()) {
73                 common_log_db_error($fave, 'INSERT', __FILE__);
74                 return false;
75             }
76             self::blow('fave:list-ids:notice_id:%d', $fave->notice_id);
77             self::blow('popular');
78
79             Event::handle('EndFavorNotice', array($profile, $notice));
80         }
81
82         return $fave;
83     }
84
85     function delete()
86     {
87         $profile = Profile::staticGet('id', $this->user_id);
88         $notice  = Notice::staticGet('id', $this->notice_id);
89
90         $result = null;
91
92         if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) {
93
94             $result = parent::delete();
95             self::blow('fave:list-ids:notice_id:%d', $this->notice_id);
96             self::blow('popular');
97
98             if ($result) {
99                 Event::handle('EndDisfavorNotice', array($profile, $notice));
100             }
101         }
102
103         return $result;
104     }
105
106     function pkeyGet($kv)
107     {
108         return Memcached_DataObject::pkeyGet('Fave', $kv);
109     }
110
111     function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
112     {
113         $stream = new FaveNoticeStream($user_id, $own);
114
115         return $stream->getNotices($offset, $limit, $since_id, $max_id);
116     }
117
118     function idStream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
119     {
120         $stream = new FaveNoticeStream($user_id, $own);
121
122         return $stream->getNoticeIds($offset, $limit, $since_id, $max_id);
123     }
124
125     function asActivity()
126     {
127         $notice = Notice::staticGet('id', $this->notice_id);
128
129         if (!$notice) {
130             throw new Exception("Fave for non-existent notice: " . $this->notice_id);
131         }
132
133         $profile = Profile::staticGet('id', $this->user_id);
134
135         if (!$profile) {
136             throw new Exception("Fave by non-existent profile: " . $this->user_id);
137         }
138
139         $act = new Activity();
140
141         $act->verb = ActivityVerb::FAVORITE;
142
143         // FIXME: rationalize this with URL below
144
145         $act->id   = $this->getURI();
146
147         $act->time    = strtotime($this->modified);
148         // TRANS: Activity title when marking a notice as favorite.
149         $act->title   = _("Favor");
150         // TRANS: Ntofication given when a user marks a notice as favorite.
151         // TRANS: %1$s is a user nickname or full name, %2$s is a notice URI.
152         $act->content = sprintf(_('%1$s marked notice %2$s as a favorite.'),
153                                $profile->getBestName(),
154                                $notice->uri);
155
156         $act->actor     = ActivityObject::fromProfile($profile);
157         $act->objects[] = ActivityObject::fromNotice($notice);
158
159         $url = common_local_url('AtomPubShowFavorite',
160                                           array('profile' => $this->user_id,
161                                                 'notice'  => $this->notice_id));
162
163         $act->selfLink = $url;
164         $act->editLink = $url;
165
166         return $act;
167     }
168
169     /**
170      * Fetch a stream of favorites by profile
171      *
172      * @param integer $profileId Profile that faved
173      * @param integer $offset    Offset from last
174      * @param integer $limit     Number to get
175      *
176      * @return mixed stream of faves, use fetch() to iterate
177      *
178      * @todo Cache results
179      * @todo integrate with Fave::stream()
180      */
181
182     static function byProfile($profileId, $offset, $limit)
183     {
184         $fav = new Fave();
185
186         $fav->user_id = $profileId;
187
188         $fav->orderBy('modified DESC');
189
190         $fav->limit($offset, $limit);
191
192         $fav->find();
193
194         return $fav;
195     }
196
197     function getURI()
198     {
199         if (!empty($this->uri)) {
200             return $this->uri;
201         } else {
202             return self::newURI($this->user_id, $this->notice_id, $this->modified);
203         }
204     }
205
206     static function newURI($profile_id, $notice_id, $modified)
207     {
208         return TagURI::mint('favor:%d:%d:%s',
209                             $profile_id,
210                             $notice_id,
211                             common_date_iso8601($modified));
212     }
213 }