]> git.mxchange.org Git - friendica.git/blob - include/like.php
Merge remote-tracking branch 'upstream/develop' into 1604-cookie
[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 || (! count($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(! count($r))
82                         return false;
83                 if(! $r[0]['self'])
84                         $remote_owner = $r[0];
85         }
86
87         // this represents the post owner on this system.
88
89         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
90                 WHERE `contact`.`self` = 1 AND `contact`.`uid` = %d LIMIT 1",
91                 intval($owner_uid)
92         );
93         if(count($r))
94                 $owner = $r[0];
95
96         if(! $owner) {
97                 logger('like: no owner');
98                 return false;
99         }
100
101         if(! $remote_owner)
102                 $remote_owner = $owner;
103
104
105         // This represents the person posting
106
107         if((local_user()) && (local_user() == $owner_uid)) {
108                 $contact = $owner;
109         }
110         else {
111                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
112                         intval($_SESSION['visitor_id']),
113                         intval($owner_uid)
114                 );
115                 if(count($r))
116                         $contact = $r[0];
117         }
118         if(! $contact) {
119                 return false;
120         }
121
122
123         $verbs = " '".dbesc($activity)."' ";
124
125         // event participation are essentially radio toggles. If you make a subsequent choice,
126         // we need to eradicate your first choice.
127         if($activity === ACTIVITY_ATTEND || $activity === ACTIVITY_ATTENDNO || $activity === ACTIVITY_ATTENDMAYBE) {
128                 $verbs = " '" . dbesc(ACTIVITY_ATTEND) . "','" . dbesc(ACTIVITY_ATTENDNO) . "','" . dbesc(ACTIVITY_ATTENDMAYBE) . "' ";
129         }
130
131         $r = q("SELECT `id`, `guid` FROM `item` WHERE `verb` IN ( $verbs ) AND `deleted` = 0
132                 AND `contact-id` = %d AND `uid` = %d
133                 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s') LIMIT 1",
134                 intval($contact['id']), intval($owner_uid),
135                 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
136         );
137
138         if(count($r)) {
139                 $like_item = $r[0];
140
141                 // Already voted, undo it
142                 $r = q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `id` = %d",
143                         dbesc(datetime_convert()),
144                         intval($like_item['id'])
145                 );
146
147
148                 // Clean up the Diaspora signatures for this like
149                 // Go ahead and do it even if Diaspora support is disabled. We still want to clean up
150                 // if it had been enabled in the past
151                 $r = q("DELETE FROM `sign` WHERE `iid` = %d",
152                         intval($like_item['id'])
153                 );
154
155                 $like_item_id = $like_item['id'];
156                 proc_run('php',"include/notifier.php","like","$like_item_id");
157
158                 return true;
159         }
160
161         $uri = item_new_uri($a->get_hostname(),$owner_uid);
162
163         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
164         if($item['obj_type'] === ACTIVITY_OBJ_EVENT)
165                 $post_type = t('event');
166         $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE );
167         $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
168         $body = $item['body'];
169
170         $obj = <<< EOT
171
172         <object>
173                 <type>$objtype</type>
174                 <local>1</local>
175                 <id>{$item['uri']}</id>
176                 <link>$link</link>
177                 <title></title>
178                 <content>$body</content>
179         </object>
180 EOT;
181         if($verb === 'like')
182                 $bodyverb = t('%1$s likes %2$s\'s %3$s');
183         if($verb === 'dislike')
184                 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
185         if($verb === 'attendyes')
186                 $bodyverb = t('%1$s is attending %2$s\'s %3$s');
187         if($verb === 'attendno')
188                 $bodyverb = t('%1$s is not attending %2$s\'s %3$s');
189         if($verb === 'attendmaybe')
190                 $bodyverb = t('%1$s may attend %2$s\'s %3$s');
191
192         if(! isset($bodyverb))
193                         return false;
194
195         $arr = array();
196
197         $arr['guid'] = get_guid(32);
198         $arr['uri'] = $uri;
199         $arr['uid'] = $owner_uid;
200         $arr['contact-id'] = $contact['id'];
201         $arr['type'] = 'activity';
202         $arr['wall'] = $item['wall'];
203         $arr['origin'] = 1;
204         $arr['gravity'] = GRAVITY_LIKE;
205         $arr['parent'] = $item['id'];
206         $arr['parent-uri'] = $item['uri'];
207         $arr['thr-parent'] = $item['uri'];
208         $arr['owner-name'] = $remote_owner['name'];
209         $arr['owner-link'] = $remote_owner['url'];
210         $arr['owner-avatar'] = $remote_owner['thumb'];
211         $arr['author-name'] = $contact['name'];
212         $arr['author-link'] = $contact['url'];
213         $arr['author-avatar'] = $contact['thumb'];
214
215         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
216         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
217         $plink = '[url=' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
218         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink );
219
220         $arr['verb'] = $activity;
221         $arr['object-type'] = $objtype;
222         $arr['object'] = $obj;
223         $arr['allow_cid'] = $item['allow_cid'];
224         $arr['allow_gid'] = $item['allow_gid'];
225         $arr['deny_cid'] = $item['deny_cid'];
226         $arr['deny_gid'] = $item['deny_gid'];
227         $arr['visible'] = 1;
228         $arr['unseen'] = 1;
229         $arr['last-child'] = 0;
230
231         $post_id = item_store($arr);
232
233         if(! $item['visible']) {
234                 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
235                         intval($item['id']),
236                         intval($owner_uid)
237                 );
238         }
239
240
241         // Save the author information for the like in case we need to relay to Diaspora
242         diaspora::store_like_signature($contact, $post_id);
243
244         $arr['id'] = $post_id;
245
246         call_hooks('post_local_end', $arr);
247
248         proc_run('php',"include/notifier.php","like","$post_id");
249
250         return true;
251 }