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