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