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);
83 if (! can_write_wall($a, $item['uid'])) {
84 logger('like: unable to write on wall ' . $item['uid']);
88 // Retrieves the local post owner
89 $owners = q("SELECT `contact`.* FROM `contact`
90 WHERE `contact`.`self` = 1
91 AND `contact`.`uid` = %d",
94 if (DBM::is_result($owners)) {
95 $owner_self_contact = $owners[0];
97 logger('like: unknown owner ' . $item['uid']);
101 // Retrieve the current logged in user's public contact
102 $author_id = public_contact();
104 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
107 if (DBM::is_result($contacts)) {
108 $author_contact = $contacts[0];
110 logger('like: unknown author ' . $author_id);
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;
119 $item_contact_id = Contact::getIdForURL($author_contact['url'], $item['uid']);
121 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
122 intval($item_contact_id)
124 if (DBM::is_result($contacts)) {
125 $item_contact = $contacts[0];
127 logger('like: unknown item contact ' . $item_contact_id);
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) . "'";
138 $verbs = "'".dbesc($activity)."'";
141 $existing_like = q("SELECT `id`, `guid`, `verb` FROM `item`
142 WHERE `verb` IN ($verbs)
146 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s')
148 intval($author_contact['id']),
149 intval($item['uid']),
150 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
153 // If it exists, mark it as deleted
154 if (DBM::is_result($existing_like)) {
155 $like_item = $existing_like[0];
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'])
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']));
168 $like_item_id = $like_item['id'];
169 Worker::add(PRIORITY_HIGH, "Notifier", "like", $like_item_id);
171 if (!$event_verb_flag || $like_item['verb'] == $activity) {
176 // Verb is "un-something", just trying to delete existing entries
177 if (strpos($verb, 'un') === 0) {
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');
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'];
193 <type>$objtype</type>
195 <id>{$item['uri']}</id>
198 <content>$body</content>
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]';
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'],
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),
228 'object-type' => $objtype,
230 'allow_cid' => $item['allow_cid'],
231 'allow_gid' => $item['allow_gid'],
232 'deny_cid' => $item['deny_cid'],
233 'deny_gid' => $item['deny_gid'],
239 $new_item_id = item_store($new_item);
241 // @todo: Explain this block
242 if (! $item['visible']) {
243 q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
249 // Save the author information for the like in case we need to relay to Diaspora
250 Diaspora::storeLikeSignature($item_contact, $new_item_id);
252 $new_item['id'] = $new_item_id;
254 call_hooks('post_local_end', $new_item);
256 Worker::add(PRIORITY_HIGH, "Notifier", "like", $new_item_id);