]> git.mxchange.org Git - friendica.git/blob - include/like.php
Merge pull request #3096 from annando/1701-index-again
[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         logger('like: verb ' . $verb . ' item ' . $item_id);
52
53         $r = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
54                 dbesc($item_id),
55                 dbesc($item_id)
56         );
57
58         if(! $item_id || (! dbm::is_result($r))) {
59                 logger('like: no item ' . $item_id);
60                 return false;
61         }
62
63         $item = $r[0];
64
65         $owner_uid = $item['uid'];
66
67         if (! can_write_wall($a,$owner_uid)) {
68                 return false;
69         }
70
71         $remote_owner = null;
72
73         if(! $item['wall']) {
74                 // The top level post may have been written by somebody on another system
75                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
76                         intval($item['contact-id']),
77                         intval($item['uid'])
78                 );
79                 if (! dbm::is_result($r)) {
80                         return false;
81                 }
82                 if (! $r[0]['self']) {
83                         $remote_owner = $r[0];
84                 }
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 (dbm::is_result($r)) {
94                 $owner = $r[0];
95         }
96
97         if (! $owner) {
98                 logger('like: no owner');
99                 return false;
100         }
101
102         if (! $remote_owner) {
103                 $remote_owner = $owner;
104         }
105
106         // This represents the person posting
107
108         if ((local_user()) && (local_user() == $owner_uid)) {
109                 $contact = $owner;
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 (dbm::is_result($r)) {
116                         $contact = $r[0];
117                 }
118         }
119         if (! $contact) {
120                 return false;
121         }
122
123
124         $verbs = " '".dbesc($activity)."' ";
125
126         // event participation are essentially radio toggles. If you make a subsequent choice,
127         // we need to eradicate your first choice.
128         if ($activity === ACTIVITY_ATTEND || $activity === ACTIVITY_ATTENDNO || $activity === ACTIVITY_ATTENDMAYBE) {
129                 $verbs = " '" . dbesc(ACTIVITY_ATTEND) . "','" . dbesc(ACTIVITY_ATTENDNO) . "','" . dbesc(ACTIVITY_ATTENDMAYBE) . "' ";
130         }
131
132         $r = q("SELECT `id`, `guid` FROM `item` WHERE `verb` IN ( $verbs ) AND `deleted` = 0
133                 AND `contact-id` = %d AND `uid` = %d
134                 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s') LIMIT 1",
135                 intval($contact['id']), intval($owner_uid),
136                 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
137         );
138
139         if (dbm::is_result($r)) {
140                 $like_item = $r[0];
141
142                 // Already voted, undo it
143                 $r = q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `id` = %d",
144                         dbesc(datetime_convert()),
145                         intval($like_item['id'])
146                 );
147
148
149                 // Clean up the Diaspora signatures for this like
150                 // Go ahead and do it even if Diaspora support is disabled. We still want to clean up
151                 // if it had been enabled in the past
152                 $r = q("DELETE FROM `sign` WHERE `iid` = %d",
153                         intval($like_item['id'])
154                 );
155
156                 $like_item_id = $like_item['id'];
157                 proc_run(PRIORITY_HIGH, "include/notifier.php", "like", $like_item_id);
158
159                 return true;
160         }
161
162         $uri = item_new_uri($a->get_hostname(),$owner_uid);
163
164         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
165         if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
166                 $post_type = t('event');
167         }
168         $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE );
169         $link = xmlify('<link rel="alternate" type="text/html" href="' . App::get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
170         $body = $item['body'];
171
172         $obj = <<< EOT
173
174         <object>
175                 <type>$objtype</type>
176                 <local>1</local>
177                 <id>{$item['uri']}</id>
178                 <link>$link</link>
179                 <title></title>
180                 <content>$body</content>
181         </object>
182 EOT;
183         if ($verb === 'like') {
184                 $bodyverb = t('%1$s likes %2$s\'s %3$s');
185         }
186         if ($verb === 'dislike') {
187                 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
188         }
189         if ($verb === 'attendyes') {
190                 $bodyverb = t('%1$s is attending %2$s\'s %3$s');
191         }
192         if ($verb === 'attendno') {
193                 $bodyverb = t('%1$s is not attending %2$s\'s %3$s');
194         }
195         if ($verb === 'attendmaybe') {
196                 $bodyverb = t('%1$s may attend %2$s\'s %3$s');
197         }
198
199         if (! isset($bodyverb)) {
200                 return false;
201         }
202
203         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
204         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
205         $plink = '[url=' . App::get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
206
207         /// @TODO Or rewrite this to multi-line initialization of the array?
208         $arr = array();
209
210         $arr['guid'] = get_guid(32);
211         $arr['uri'] = $uri;
212         $arr['uid'] = $owner_uid;
213         $arr['contact-id'] = $contact['id'];
214         $arr['type'] = 'activity';
215         $arr['wall'] = $item['wall'];
216         $arr['origin'] = 1;
217         $arr['gravity'] = GRAVITY_LIKE;
218         $arr['parent'] = $item['id'];
219         $arr['parent-uri'] = $item['uri'];
220         $arr['thr-parent'] = $item['uri'];
221         $arr['owner-name'] = $remote_owner['name'];
222         $arr['owner-link'] = $remote_owner['url'];
223         $arr['owner-avatar'] = $remote_owner['thumb'];
224         $arr['author-name'] = $contact['name'];
225         $arr['author-link'] = $contact['url'];
226         $arr['author-avatar'] = $contact['thumb'];
227         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink );
228         $arr['verb'] = $activity;
229         $arr['object-type'] = $objtype;
230         $arr['object'] = $obj;
231         $arr['allow_cid'] = $item['allow_cid'];
232         $arr['allow_gid'] = $item['allow_gid'];
233         $arr['deny_cid'] = $item['deny_cid'];
234         $arr['deny_gid'] = $item['deny_gid'];
235         $arr['visible'] = 1;
236         $arr['unseen'] = 1;
237         $arr['last-child'] = 0;
238
239         $post_id = item_store($arr);
240
241         if (! $item['visible']) {
242                 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
243                         intval($item['id']),
244                         intval($owner_uid)
245                 );
246         }
247
248
249         // Save the author information for the like in case we need to relay to Diaspora
250         Diaspora::store_like_signature($contact, $post_id);
251
252         $arr['id'] = $post_id;
253
254         call_hooks('post_local_end', $arr);
255
256         proc_run(PRIORITY_HIGH, "include/notifier.php", "like", $post_id);
257
258         return true;
259 }