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;
12 * @brief add/remove activity to an item
14 * Toggle activities as like,dislike,attend of an item
16 * @param string $item_id
18 * Activity verb. One of
19 * like, unlike, dislike, undislike, attendyes, unattendyes,
20 * attendno, unattendno, attendmaybe, unattendmaybe
21 * @hook 'post_local_end'
23 * 'post_id' => ID of posted item
25 function do_like($item_id, $verb) {
28 if (!local_user() && !remote_user()) {
34 $bodyverb = t('%1$s likes %2$s\'s %3$s');
35 $activity = ACTIVITY_LIKE;
38 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
39 $activity = ACTIVITY_LIKE;
43 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
44 $activity = ACTIVITY_DISLIKE;
48 $bodyverb = t('%1$s is attending %2$s\'s %3$s');
49 $activity = ACTIVITY_ATTEND;
53 $bodyverb = t('%1$s is not attending %2$s\'s %3$s');
54 $activity = ACTIVITY_ATTENDNO;
58 $bodyverb = t('%1$s may attend %2$s\'s %3$s');
59 $activity = ACTIVITY_ATTENDMAYBE;
62 logger('like: unknown verb ' . $verb . ' for item ' . $item_id);
66 // Enable activity toggling instead of on/off
67 $event_verb_flag = $activity === ACTIVITY_ATTEND || $activity === ACTIVITY_ATTENDNO || $activity === ACTIVITY_ATTENDMAYBE;
69 logger('like: verb ' . $verb . ' item ' . $item_id);
72 $items = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
77 if (!$item_id || !DBM::is_result($items)) {
78 logger('like: unknown item ' . $item_id);
85 if (($uid == 0) && local_user()) {
89 if (!can_write_wall($uid)) {
90 logger('like: unable to write on wall ' . $uid);
94 // Retrieves the local post owner
95 $owners = q("SELECT `contact`.* FROM `contact`
96 WHERE `contact`.`self`
97 AND `contact`.`uid` = %d",
100 if (DBM::is_result($owners)) {
101 $owner_self_contact = $owners[0];
103 logger('like: unknown owner ' . $uid);
107 // Retrieve the current logged in user's public contact
108 $author_id = public_contact();
110 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
113 if (DBM::is_result($contacts)) {
114 $author_contact = $contacts[0];
116 logger('like: unknown author ' . $author_id);
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;
125 $item_contact_id = Contact::getIdForURL($author_contact['url'], $uid);
127 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
128 intval($item_contact_id)
130 if (DBM::is_result($contacts)) {
131 $item_contact = $contacts[0];
133 logger('like: unknown item contact ' . $item_contact_id);
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) . "'";
144 $verbs = "'".dbesc($activity)."'";
147 $existing_like = q("SELECT `id`, `guid`, `verb` FROM `item`
148 WHERE `verb` IN ($verbs)
152 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s')
154 intval($author_contact['id']),
155 intval($item['uid']),
156 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
159 // If it exists, mark it as deleted
160 if (DBM::is_result($existing_like)) {
161 $like_item = $existing_like[0];
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'])
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']]);
174 $like_item_id = $like_item['id'];
175 Worker::add(PRIORITY_HIGH, "Notifier", "like", $like_item_id);
177 if (!$event_verb_flag || $like_item['verb'] == $activity) {
182 // Verb is "un-something", just trying to delete existing entries
183 if (strpos($verb, 'un') === 0) {
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');
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'];
199 <type>$objtype</type>
201 <id>{$item['uri']}</id>
204 <content>$body</content>
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]';
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'],
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),
234 'object-type' => $objtype,
236 'allow_cid' => $item['allow_cid'],
237 'allow_gid' => $item['allow_gid'],
238 'deny_cid' => $item['deny_cid'],
239 'deny_gid' => $item['deny_gid'],
244 $new_item_id = item_store($new_item);
246 // @todo: Explain this block
247 if (! $item['visible']) {
248 q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d",
253 // Save the author information for the like in case we need to relay to Diaspora
254 Diaspora::storeLikeSignature($item_contact, $new_item_id);
256 $new_item['id'] = $new_item_id;
258 Addon::callHooks('post_local_end', $new_item);
260 Worker::add(PRIORITY_HIGH, "Notifier", "like", $new_item_id);