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