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