4 use Friendica\Core\System;
5 use Friendica\Core\Worker;
6 use Friendica\Database\DBM;
7 use Friendica\Protocol\Diaspora;
10 * @brief add/remove activity to an item
12 * Toggle activities as like,dislike,attend of an item
14 * @param string $item_id
16 * Activity verb. One of
17 * like, unlike, dislike, undislike, attendyes, unattendyes,
18 * attendno, unattendno, attendmaybe, unattendmaybe
19 * @hook 'post_local_end'
21 * 'post_id' => ID of posted item
23 function do_like($item_id, $verb) {
26 if (! local_user() && ! remote_user()) {
32 $bodyverb = t('%1$s likes %2$s\'s %3$s');
33 $activity = ACTIVITY_LIKE;
36 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
37 $activity = ACTIVITY_LIKE;
41 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
42 $activity = ACTIVITY_DISLIKE;
46 $bodyverb = t('%1$s is attending %2$s\'s %3$s');
47 $activity = ACTIVITY_ATTEND;
51 $bodyverb = t('%1$s is not attending %2$s\'s %3$s');
52 $activity = ACTIVITY_ATTENDNO;
56 $bodyverb = t('%1$s may attend %2$s\'s %3$s');
57 $activity = ACTIVITY_ATTENDMAYBE;
60 logger('like: unknown verb ' . $verb . ' for item ' . $item_id);
64 // Enable activity toggling instead of on/off
65 $event_verb_flag = $activity === ACTIVITY_ATTEND || $activity === ACTIVITY_ATTENDNO || $activity === ACTIVITY_ATTENDMAYBE;
67 logger('like: verb ' . $verb . ' item ' . $item_id);
70 $items = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
75 if (! $item_id || ! DBM::is_result($items)) {
76 logger('like: unknown item ' . $item_id);
82 if (! can_write_wall($a, $item['uid'])) {
83 logger('like: unable to write on wall ' . $item['uid']);
87 // Retrieves the local post owner
88 $owners = q("SELECT `contact`.* FROM `contact`
89 WHERE `contact`.`self` = 1
90 AND `contact`.`uid` = %d",
93 if (DBM::is_result($owners)) {
94 $owner_self_contact = $owners[0];
96 logger('like: unknown owner ' . $item['uid']);
100 // Retrieve the current logged in user's public contact
101 $author_id = public_contact();
103 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
106 if (DBM::is_result($contacts)) {
107 $author_contact = $contacts[0];
109 logger('like: unknown author ' . $author_id);
113 // Contact-id is the uid-dependant author contact
114 if (local_user() == $item['uid']) {
115 $item_contact_id = $owner_self_contact['id'];
116 $item_contact = $owner_self_contact;
118 $item_contact_id = get_contact($author_contact['url'], $item['uid']);
120 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
121 intval($item_contact_id)
123 if (DBM::is_result($contacts)) {
124 $item_contact = $contacts[0];
126 logger('like: unknown item contact ' . $item_contact_id);
131 // Look for an existing verb row
132 // event participation are essentially radio toggles. If you make a subsequent choice,
133 // we need to eradicate your first choice.
134 if ($event_verb_flag) {
135 $verbs = "'" . dbesc(ACTIVITY_ATTEND) . "', '" . dbesc(ACTIVITY_ATTENDNO) . "', '" . dbesc(ACTIVITY_ATTENDMAYBE) . "'";
137 $verbs = "'".dbesc($activity)."'";
140 $existing_like = q("SELECT `id`, `guid`, `verb` FROM `item`
141 WHERE `verb` IN ($verbs)
145 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s')
147 intval($author_contact['id']),
148 intval($item['uid']),
149 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
152 // If it exists, mark it as deleted
153 if (DBM::is_result($existing_like)) {
154 $like_item = $existing_like[0];
156 // Already voted, undo it
157 q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `id` = %d",
158 dbesc(datetime_convert()),
159 intval($like_item['id'])
162 // Clean up the Diaspora signatures for this like
163 // Go ahead and do it even if Diaspora support is disabled. We still want to clean up
164 // if it had been enabled in the past
165 q("DELETE FROM `sign` WHERE `iid` = %d",
166 intval($like_item['id'])
169 $like_item_id = $like_item['id'];
170 Worker::add(PRIORITY_HIGH, "Notifier", "like", $like_item_id);
172 if (!$event_verb_flag || $like_item['verb'] == $activity) {
177 // Verb is "un-something", just trying to delete existing entries
178 if (strpos($verb, 'un') === 0) {
182 // Else or if event verb different from existing row, create a new item row
183 $post_type = (($item['resource-id']) ? t('photo') : t('status'));
184 if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
185 $post_type = t('event');
187 $objtype = $item['resource-id'] ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE ;
188 $link = xmlify('<link rel="alternate" type="text/html" href="' . System::baseUrl() . '/display/' . $owner_self_contact['nick'] . '/' . $item['id'] . '" />' . "\n") ;
189 $body = $item['body'];
194 <type>$objtype</type>
196 <id>{$item['uri']}</id>
199 <content>$body</content>
203 $ulink = '[url=' . $author_contact['url'] . ']' . $author_contact['name'] . '[/url]';
204 $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
205 $plink = '[url=' . System::baseUrl() . '/display/' . $owner_self_contact['nick'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
208 'guid' => get_guid(32),
209 'uri' => item_new_uri($a->get_hostname(), $item['uid']),
210 'uid' => $item['uid'],
211 'contact-id' => $item_contact_id,
212 'type' => 'activity',
213 'wall' => $item['wall'],
215 'gravity' => GRAVITY_LIKE,
216 'parent' => $item['id'],
217 'parent-uri' => $item['uri'],
218 'thr-parent' => $item['uri'],
219 'owner-id' => $item['owner-id'],
220 'owner-name' => $item['owner-name'],
221 'owner-link' => $item['owner-link'],
222 'owner-avatar' => $item['owner-avatar'],
223 'author-id' => $author_contact['id'],
224 'author-name' => $author_contact['name'],
225 'author-link' => $author_contact['url'],
226 'author-avatar' => $author_contact['thumb'],
227 'body' => sprintf($bodyverb, $ulink, $alink, $plink),
229 'object-type' => $objtype,
231 'allow_cid' => $item['allow_cid'],
232 'allow_gid' => $item['allow_gid'],
233 'deny_cid' => $item['deny_cid'],
234 'deny_gid' => $item['deny_gid'],
240 $new_item_id = item_store($new_item);
242 // @todo: Explain this block
243 if (! $item['visible']) {
244 q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
250 // Save the author information for the like in case we need to relay to Diaspora
251 Diaspora::store_like_signature($item_contact, $new_item_id);
253 $new_item['id'] = $new_item_id;
255 call_hooks('post_local_end', $new_item);
257 Worker::add(PRIORITY_HIGH, "Notifier", "like", $new_item_id);