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