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