4 use Friendica\Core\System;
5 use Friendica\Core\Worker;
6 use Friendica\Database\DBM;
7 use Friendica\Model\Contact;
8 use Friendica\Protocol\Diaspora;
11 * @brief add/remove activity to an item
13 * Toggle activities as like,dislike,attend of an item
15 * @param string $item_id
17 * Activity verb. One of
18 * like, unlike, dislike, undislike, attendyes, unattendyes,
19 * attendno, unattendno, attendmaybe, unattendmaybe
20 * @hook 'post_local_end'
22 * 'post_id' => ID of posted item
24 function do_like($item_id, $verb) {
27 if (!local_user() && !remote_user()) {
33 $bodyverb = t('%1$s likes %2$s\'s %3$s');
34 $activity = ACTIVITY_LIKE;
37 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
38 $activity = ACTIVITY_LIKE;
42 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
43 $activity = ACTIVITY_DISLIKE;
47 $bodyverb = t('%1$s is attending %2$s\'s %3$s');
48 $activity = ACTIVITY_ATTEND;
52 $bodyverb = t('%1$s is not attending %2$s\'s %3$s');
53 $activity = ACTIVITY_ATTENDNO;
57 $bodyverb = t('%1$s may attend %2$s\'s %3$s');
58 $activity = ACTIVITY_ATTENDMAYBE;
61 logger('like: unknown verb ' . $verb . ' for item ' . $item_id);
65 // Enable activity toggling instead of on/off
66 $event_verb_flag = $activity === ACTIVITY_ATTEND || $activity === ACTIVITY_ATTENDNO || $activity === ACTIVITY_ATTENDMAYBE;
68 logger('like: verb ' . $verb . ' item ' . $item_id);
71 $items = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
76 if (!$item_id || !DBM::is_result($items)) {
77 logger('like: unknown item ' . $item_id);
84 if (($uid == 0) && local_user()) {
88 if (!can_write_wall($a, $uid)) {
89 logger('like: unable to write on wall ' . $uid);
93 // Retrieves the local post owner
94 $owners = q("SELECT `contact`.* FROM `contact`
95 WHERE `contact`.`self`
96 AND `contact`.`uid` = %d",
99 if (DBM::is_result($owners)) {
100 $owner_self_contact = $owners[0];
102 logger('like: unknown owner ' . $uid);
106 // Retrieve the current logged in user's public contact
107 $author_id = public_contact();
109 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
112 if (DBM::is_result($contacts)) {
113 $author_contact = $contacts[0];
115 logger('like: unknown author ' . $author_id);
119 // Contact-id is the uid-dependant author contact
120 if (local_user() == $uid) {
121 $item_contact_id = $owner_self_contact['id'];
122 $item_contact = $owner_self_contact;
124 $item_contact_id = Contact::getIdForURL($author_contact['url'], $uid);
126 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
127 intval($item_contact_id)
129 if (DBM::is_result($contacts)) {
130 $item_contact = $contacts[0];
132 logger('like: unknown item contact ' . $item_contact_id);
137 // Look for an existing verb row
138 // event participation are essentially radio toggles. If you make a subsequent choice,
139 // we need to eradicate your first choice.
140 if ($event_verb_flag) {
141 $verbs = "'" . dbesc(ACTIVITY_ATTEND) . "', '" . dbesc(ACTIVITY_ATTENDNO) . "', '" . dbesc(ACTIVITY_ATTENDMAYBE) . "'";
143 $verbs = "'".dbesc($activity)."'";
146 $existing_like = q("SELECT `id`, `guid`, `verb` FROM `item`
147 WHERE `verb` IN ($verbs)
151 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s')
153 intval($author_contact['id']),
154 intval($item['uid']),
155 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
158 // If it exists, mark it as deleted
159 if (DBM::is_result($existing_like)) {
160 $like_item = $existing_like[0];
162 // Already voted, undo it
163 q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `id` = %d",
164 dbesc(datetime_convert()),
165 intval($like_item['id'])
168 // Clean up the Diaspora signatures for this like
169 // Go ahead and do it even if Diaspora support is disabled. We still want to clean up
170 // if it had been enabled in the past
171 dba::delete('sign', array('iid' => $like_item['id']));
173 $like_item_id = $like_item['id'];
174 Worker::add(PRIORITY_HIGH, "Notifier", "like", $like_item_id);
176 if (!$event_verb_flag || $like_item['verb'] == $activity) {
181 // Verb is "un-something", just trying to delete existing entries
182 if (strpos($verb, 'un') === 0) {
186 // Else or if event verb different from existing row, create a new item row
187 $post_type = (($item['resource-id']) ? t('photo') : t('status'));
188 if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
189 $post_type = t('event');
191 $objtype = $item['resource-id'] ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE ;
192 $link = xmlify('<link rel="alternate" type="text/html" href="' . System::baseUrl() . '/display/' . $owner_self_contact['nick'] . '/' . $item['id'] . '" />' . "\n") ;
193 $body = $item['body'];
198 <type>$objtype</type>
200 <id>{$item['uri']}</id>
203 <content>$body</content>
207 $ulink = '[url=' . $author_contact['url'] . ']' . $author_contact['name'] . '[/url]';
208 $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
209 $plink = '[url=' . System::baseUrl() . '/display/' . $owner_self_contact['nick'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
212 'guid' => get_guid(32),
213 'uri' => item_new_uri($a->get_hostname(), $item['uid']),
214 'uid' => $item['uid'],
215 'contact-id' => $item_contact_id,
216 'type' => 'activity',
217 'wall' => $item['wall'],
219 'gravity' => GRAVITY_LIKE,
220 'parent' => $item['id'],
221 'parent-uri' => $item['uri'],
222 'thr-parent' => $item['uri'],
223 'owner-id' => $item['owner-id'],
224 'owner-name' => $item['owner-name'],
225 'owner-link' => $item['owner-link'],
226 'owner-avatar' => $item['owner-avatar'],
227 'author-id' => $author_contact['id'],
228 'author-name' => $author_contact['name'],
229 'author-link' => $author_contact['url'],
230 'author-avatar' => $author_contact['thumb'],
231 'body' => sprintf($bodyverb, $ulink, $alink, $plink),
233 'object-type' => $objtype,
235 'allow_cid' => $item['allow_cid'],
236 'allow_gid' => $item['allow_gid'],
237 'deny_cid' => $item['deny_cid'],
238 '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 call_hooks('post_local_end', $new_item);
260 Worker::add(PRIORITY_HIGH, "Notifier", "like", $new_item_id);