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