3 * @file include/like.php
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;
15 * @brief add/remove activity to an item
17 * Toggle activities as like,dislike,attend of an item
19 * @param string $item_id
21 * Activity verb. One of
22 * like, unlike, dislike, undislike, attendyes, unattendyes,
23 * attendno, unattendno, attendmaybe, unattendmaybe
24 * @hook 'post_local_end'
26 * 'post_id' => ID of posted item
28 function do_like($item_id, $verb) {
31 if (!local_user() && !remote_user()) {
37 $bodyverb = L10n::t('%1$s likes %2$s\'s %3$s');
38 $activity = ACTIVITY_LIKE;
41 $bodyverb = L10n::t('%1$s doesn\'t like %2$s\'s %3$s');
42 $activity = ACTIVITY_LIKE;
46 $bodyverb = L10n::t('%1$s doesn\'t like %2$s\'s %3$s');
47 $activity = ACTIVITY_DISLIKE;
51 $bodyverb = L10n::t('%1$s is attending %2$s\'s %3$s');
52 $activity = ACTIVITY_ATTEND;
56 $bodyverb = L10n::t('%1$s is not attending %2$s\'s %3$s');
57 $activity = ACTIVITY_ATTENDNO;
61 $bodyverb = L10n::t('%1$s may attend %2$s\'s %3$s');
62 $activity = ACTIVITY_ATTENDMAYBE;
65 logger('like: unknown verb ' . $verb . ' for item ' . $item_id);
69 // Enable activity toggling instead of on/off
70 $event_verb_flag = $activity === ACTIVITY_ATTEND || $activity === ACTIVITY_ATTENDNO || $activity === ACTIVITY_ATTENDMAYBE;
72 logger('like: verb ' . $verb . ' item ' . $item_id);
75 $items = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
80 if (!$item_id || !DBM::is_result($items)) {
81 logger('like: unknown item ' . $item_id);
88 if (($uid == 0) && local_user()) {
92 if (!can_write_wall($uid)) {
93 logger('like: unable to write on wall ' . $uid);
97 // Retrieves the local post owner
98 $owners = q("SELECT `contact`.* FROM `contact`
99 WHERE `contact`.`self`
100 AND `contact`.`uid` = %d",
103 if (DBM::is_result($owners)) {
104 $owner_self_contact = $owners[0];
106 logger('like: unknown owner ' . $uid);
110 // Retrieve the current logged in user's public contact
111 $author_id = public_contact();
113 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
116 if (DBM::is_result($contacts)) {
117 $author_contact = $contacts[0];
119 logger('like: unknown author ' . $author_id);
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;
128 $item_contact_id = Contact::getIdForURL($author_contact['url'], $uid);
130 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
131 intval($item_contact_id)
133 if (DBM::is_result($contacts)) {
134 $item_contact = $contacts[0];
136 logger('like: unknown item contact ' . $item_contact_id);
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) . "'";
147 $verbs = "'".dbesc($activity)."'";
150 $existing_like = q("SELECT `id`, `guid`, `verb` FROM `item`
151 WHERE `verb` IN ($verbs)
155 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s')
157 intval($author_contact['id']),
158 intval($item['uid']),
159 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
162 // If it exists, mark it as deleted
163 if (DBM::is_result($existing_like)) {
164 $like_item = $existing_like[0];
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'])
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']]);
177 $like_item_id = $like_item['id'];
178 Worker::add(PRIORITY_HIGH, "Notifier", "like", $like_item_id);
180 if (!$event_verb_flag || $like_item['verb'] == $activity) {
185 // Verb is "un-something", just trying to delete existing entries
186 if (strpos($verb, 'un') === 0) {
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');
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'];
202 <type>$objtype</type>
204 <id>{$item['uri']}</id>
207 <content>$body</content>
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]';
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'],
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),
237 'object-type' => $objtype,
239 'allow_cid' => $item['allow_cid'],
240 'allow_gid' => $item['allow_gid'],
241 'deny_cid' => $item['deny_cid'],
242 'deny_gid' => $item['deny_gid'],
247 $new_item_id = item_store($new_item);
249 // @todo: Explain this block
250 if (! $item['visible']) {
251 q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d",
256 // Save the author information for the like in case we need to relay to Diaspora
257 Diaspora::storeLikeSignature($item_contact, $new_item_id);
259 $new_item['id'] = $new_item_id;
261 Addon::callHooks('post_local_end', $new_item);
263 Worker::add(PRIORITY_HIGH, "Notifier", "like", $new_item_id);