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