]> git.mxchange.org Git - friendica.git/blob - include/like.php
Merge remote-tracking branch 'upstream/develop' into develop
[friendica.git] / include / like.php
1 <?php
2 require_once("include/diaspora.php");
3
4 /**
5  * @brief add/remove activity to an item
6  *
7  * Toggle activities as like,dislike,attend of an item
8  *
9  * @param string $item_id
10  * @param string $verb
11  *              Activity verb. One of
12  *                      like, unlike, dislike, undislike, attendyes, unattendyes,
13  *                      attendno, unattendno, attendmaybe, unattendmaybe
14  * @hook 'post_local_end'
15  *              array $arr
16  *                      'post_id' => ID of posted item
17  */
18 function do_like($item_id, $verb) {
19         $a = get_app();
20
21         if(! local_user() && ! remote_user()) {
22                 return false;
23         }
24
25         switch($verb) {
26                 case 'like':
27                 case 'unlike':
28                         $activity = ACTIVITY_LIKE;
29                         break;
30                 case 'dislike':
31                 case 'undislike':
32                         $activity = ACTIVITY_DISLIKE;
33                         break;
34                 case 'attendyes':
35                 case 'unattendyes':
36                         $activity = ACTIVITY_ATTEND;
37                         break;
38                 case 'attendno':
39                 case 'unattendno':
40                         $activity = ACTIVITY_ATTENDNO;
41                         break;
42                 case 'attendmaybe':
43                 case 'unattendmaybe':
44                         $activity = ACTIVITY_ATTENDMAYBE;
45                         break;
46                 default:
47                         return false;
48                         break;
49         }
50
51
52         logger('like: verb ' . $verb . ' item ' . $item_id);
53
54
55         $r = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
56                 dbesc($item_id),
57                 dbesc($item_id)
58         );
59
60         if(! $item_id || (! dbm::is_result($r))) {
61                 logger('like: no item ' . $item_id);
62                 return false;
63         }
64
65         $item = $r[0];
66
67         $owner_uid = $item['uid'];
68
69         if (! can_write_wall($a,$owner_uid)) {
70                 return false;
71         }
72
73         $remote_owner = null;
74
75         if(! $item['wall']) {
76                 // The top level post may have been written by somebody on another system
77                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
78                         intval($item['contact-id']),
79                         intval($item['uid'])
80                 );
81                 if (! dbm::is_result($r)) {
82                         return false;
83                 }
84                 if (! $r[0]['self']) {
85                         $remote_owner = $r[0];
86                 }
87         }
88
89         // this represents the post owner on this system.
90
91         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
92                 WHERE `contact`.`self` = 1 AND `contact`.`uid` = %d LIMIT 1",
93                 intval($owner_uid)
94         );
95         if (dbm::is_result($r)) {
96                 $owner = $r[0];
97         }
98
99         if (! $owner) {
100                 logger('like: no owner');
101                 return false;
102         }
103
104         if (! $remote_owner) {
105                 $remote_owner = $owner;
106         }
107
108         // This represents the person posting
109
110         if ((local_user()) && (local_user() == $owner_uid)) {
111                 $contact = $owner;
112         }
113         else {
114                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
115                         intval($_SESSION['visitor_id']),
116                         intval($owner_uid)
117                 );
118                 if (dbm::is_result($r))
119                         $contact = $r[0];
120         }
121         if (! $contact) {
122                 return false;
123         }
124
125
126         $verbs = " '".dbesc($activity)."' ";
127
128         // event participation are essentially radio toggles. If you make a subsequent choice,
129         // we need to eradicate your first choice.
130         if ($activity === ACTIVITY_ATTEND || $activity === ACTIVITY_ATTENDNO || $activity === ACTIVITY_ATTENDMAYBE) {
131                 $verbs = " '" . dbesc(ACTIVITY_ATTEND) . "','" . dbesc(ACTIVITY_ATTENDNO) . "','" . dbesc(ACTIVITY_ATTENDMAYBE) . "' ";
132         }
133
134         $r = q("SELECT `id`, `guid` FROM `item` WHERE `verb` IN ( $verbs ) AND `deleted` = 0
135                 AND `contact-id` = %d AND `uid` = %d
136                 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s') LIMIT 1",
137                 intval($contact['id']), intval($owner_uid),
138                 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
139         );
140
141         if (dbm::is_result($r)) {
142                 $like_item = $r[0];
143
144                 // Already voted, undo it
145                 $r = q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `id` = %d",
146                         dbesc(datetime_convert()),
147                         intval($like_item['id'])
148                 );
149
150
151                 // Clean up the Diaspora signatures for this like
152                 // Go ahead and do it even if Diaspora support is disabled. We still want to clean up
153                 // if it had been enabled in the past
154                 $r = q("DELETE FROM `sign` WHERE `iid` = %d",
155                         intval($like_item['id'])
156                 );
157
158                 $like_item_id = $like_item['id'];
159                 proc_run(PRIORITY_HIGH, "include/notifier.php", "like", $like_item_id);
160
161                 return true;
162         }
163
164         $uri = item_new_uri($a->get_hostname(),$owner_uid);
165
166         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
167         if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
168                 $post_type = t('event');
169         }
170         $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE );
171         $link = xmlify('<link rel="alternate" type="text/html" href="' . App::get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
172         $body = $item['body'];
173
174         $obj = <<< EOT
175
176         <object>
177                 <type>$objtype</type>
178                 <local>1</local>
179                 <id>{$item['uri']}</id>
180                 <link>$link</link>
181                 <title></title>
182                 <content>$body</content>
183         </object>
184 EOT;
185         if ($verb === 'like') {
186                 $bodyverb = t('%1$s likes %2$s\'s %3$s');
187         }
188         if ($verb === 'dislike') {
189                 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
190         }
191         if ($verb === 'attendyes') {
192                 $bodyverb = t('%1$s is attending %2$s\'s %3$s');
193         }
194         if ($verb === 'attendno') {
195                 $bodyverb = t('%1$s is not attending %2$s\'s %3$s');
196         }
197         if ($verb === 'attendmaybe') {
198                 $bodyverb = t('%1$s may attend %2$s\'s %3$s');
199         }
200
201         if (! isset($bodyverb)) {
202                 return false;
203         }
204
205         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
206         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
207         $plink = '[url=' . App::get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
208
209         /// @TODO Or rewrite this to multi-line initialization of the array?
210         $arr = array();
211
212         $arr['guid'] = get_guid(32);
213         $arr['uri'] = $uri;
214         $arr['uid'] = $owner_uid;
215         $arr['contact-id'] = $contact['id'];
216         $arr['type'] = 'activity';
217         $arr['wall'] = $item['wall'];
218         $arr['origin'] = 1;
219         $arr['gravity'] = GRAVITY_LIKE;
220         $arr['parent'] = $item['id'];
221         $arr['parent-uri'] = $item['uri'];
222         $arr['thr-parent'] = $item['uri'];
223         $arr['owner-name'] = $remote_owner['name'];
224         $arr['owner-link'] = $remote_owner['url'];
225         $arr['owner-avatar'] = $remote_owner['thumb'];
226         $arr['author-name'] = $contact['name'];
227         $arr['author-link'] = $contact['url'];
228         $arr['author-avatar'] = $contact['thumb'];
229         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink );
230         $arr['verb'] = $activity;
231         $arr['object-type'] = $objtype;
232         $arr['object'] = $obj;
233         $arr['allow_cid'] = $item['allow_cid'];
234         $arr['allow_gid'] = $item['allow_gid'];
235         $arr['deny_cid'] = $item['deny_cid'];
236         $arr['deny_gid'] = $item['deny_gid'];
237         $arr['visible'] = 1;
238         $arr['unseen'] = 1;
239         $arr['last-child'] = 0;
240
241         $post_id = item_store($arr);
242
243         if (! $item['visible']) {
244                 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
245                         intval($item['id']),
246                         intval($owner_uid)
247                 );
248         }
249
250
251         // Save the author information for the like in case we need to relay to Diaspora
252         diaspora::store_like_signature($contact, $post_id);
253
254         $arr['id'] = $post_id;
255
256         call_hooks('post_local_end', $arr);
257
258         proc_run(PRIORITY_HIGH, "include/notifier.php", "like", $post_id);
259
260         return true;
261 }