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