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