5 require_once("include/diaspora.php");
8 * @brief add/remove activity to an item
10 * Toggle activities as like,dislike,attend of an item
12 * @param string $item_id
14 * Activity verb. One of
15 * like, unlike, dislike, undislike, attendyes, unattendyes,
16 * attendno, unattendno, attendmaybe, unattendmaybe
17 * @hook 'post_local_end'
19 * 'post_id' => ID of posted item
21 function do_like($item_id, $verb) {
24 if (! local_user() && ! remote_user()) {
30 $bodyverb = t('%1$s likes %2$s\'s %3$s');
31 $activity = ACTIVITY_LIKE;
34 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
35 $activity = ACTIVITY_LIKE;
39 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
40 $activity = ACTIVITY_DISLIKE;
44 $bodyverb = t('%1$s is attending %2$s\'s %3$s');
45 $activity = ACTIVITY_ATTEND;
49 $bodyverb = t('%1$s is not attending %2$s\'s %3$s');
50 $activity = ACTIVITY_ATTENDNO;
54 $bodyverb = t('%1$s may attend %2$s\'s %3$s');
55 $activity = ACTIVITY_ATTENDMAYBE;
58 logger('like: unknown verb ' . $verb . ' for item ' . $item_id);
62 // Enable activity toggling instead of on/off
63 $event_verb_flag = $activity === ACTIVITY_ATTEND || $activity === ACTIVITY_ATTENDNO || $activity === ACTIVITY_ATTENDMAYBE;
65 logger('like: verb ' . $verb . ' item ' . $item_id);
68 $items = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
73 if (! $item_id || ! dbm::is_result($items)) {
74 logger('like: unknown item ' . $item_id);
80 if (! can_write_wall($a, $item['uid'])) {
81 logger('like: unable to write on wall ' . $item['uid']);
85 // Retrieves the local post owner
86 $owners = q("SELECT `contact`.* FROM `contact`
87 WHERE `contact`.`self` = 1
88 AND `contact`.`uid` = %d",
91 if (dbm::is_result($owners)) {
92 $owner_self_contact = $owners[0];
94 logger('like: unknown owner ' . $item['uid']);
98 // Retrieve the current logged in user's public contact
99 $author_id = public_contact();
101 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
104 if (dbm::is_result($contacts)) {
105 $author_contact = $contacts[0];
107 logger('like: unknown author ' . $author_id);
111 // Contact-id is the uid-dependant author contact
112 if (local_user() == $item['uid']) {
113 $item_contact_id = $owner_self_contact['id'];
114 $item_contact = $owner_self_contact;
116 $item_contact_id = get_contact($author_contact['url'], $item['uid']);
118 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
119 intval($item_contact_id)
121 if (dbm::is_result($contacts)) {
122 $item_contact = $contacts[0];
124 logger('like: unknown item contact ' . $item_contact_id);
129 // Look for an existing verb row
130 // event participation are essentially radio toggles. If you make a subsequent choice,
131 // we need to eradicate your first choice.
132 if ($event_verb_flag) {
133 $verbs = "'" . dbesc(ACTIVITY_ATTEND) . "', '" . dbesc(ACTIVITY_ATTENDNO) . "', '" . dbesc(ACTIVITY_ATTENDMAYBE) . "'";
135 $verbs = "'".dbesc($activity)."'";
138 $existing_like = q("SELECT `id`, `guid`, `verb` FROM `item`
139 WHERE `verb` IN ($verbs)
143 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s')
145 intval($author_contact['id']),
146 intval($item['uid']),
147 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
150 // If it exists, mark it as deleted
151 if (dbm::is_result($existing_like)) {
152 $like_item = $existing_like[0];
154 // Already voted, undo it
155 q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `id` = %d",
156 dbesc(datetime_convert()),
157 intval($like_item['id'])
160 // Clean up the Diaspora signatures for this like
161 // Go ahead and do it even if Diaspora support is disabled. We still want to clean up
162 // if it had been enabled in the past
163 q("DELETE FROM `sign` WHERE `iid` = %d",
164 intval($like_item['id'])
167 $like_item_id = $like_item['id'];
168 proc_run(PRIORITY_HIGH, "include/notifier.php", "like", $like_item_id);
170 if (!$event_verb_flag || $like_item['verb'] == $activity) {
175 // Verb is "un-something", just trying to delete existing entries
176 if (strpos($verb, 'un') === 0) {
180 // Else or if event verb different from existing row, create a new item row
181 $post_type = (($item['resource-id']) ? t('photo') : t('status'));
182 if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
183 $post_type = t('event');
185 $objtype = $item['resource-id'] ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE ;
186 $link = xmlify('<link rel="alternate" type="text/html" href="' . App::get_baseurl() . '/display/' . $owner_self_contact['nick'] . '/' . $item['id'] . '" />' . "\n") ;
187 $body = $item['body'];
192 <type>$objtype</type>
194 <id>{$item['uri']}</id>
197 <content>$body</content>
201 $ulink = '[url=' . $author_contact['url'] . ']' . $author_contact['name'] . '[/url]';
202 $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
203 $plink = '[url=' . App::get_baseurl() . '/display/' . $owner_self_contact['nick'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
206 'guid' => get_guid(32),
207 'uri' => item_new_uri($a->get_hostname(), $item['uid']),
208 'uid' => $item['uid'],
209 'contact-id' => $item_contact_id,
210 'type' => 'activity',
211 'wall' => $item['wall'],
213 'gravity' => GRAVITY_LIKE,
214 'parent' => $item['id'],
215 'parent-uri' => $item['uri'],
216 'thr-parent' => $item['uri'],
217 'owner-id' => $item['owner-id'],
218 'owner-name' => $item['owner-name'],
219 'owner-link' => $item['owner-link'],
220 'owner-avatar' => $item['owner-avatar'],
221 'author-id' => $author_contact['id'],
222 'author-name' => $author_contact['name'],
223 'author-link' => $author_contact['url'],
224 'author-avatar' => $author_contact['thumb'],
225 'body' => sprintf($bodyverb, $ulink, $alink, $plink),
227 'object-type' => $objtype,
229 'allow_cid' => $item['allow_cid'],
230 'allow_gid' => $item['allow_gid'],
231 'deny_cid' => $item['deny_cid'],
232 'deny_gid' => $item['deny_gid'],
238 $new_item_id = item_store($new_item);
240 // @todo: Explain this block
241 if (! $item['visible']) {
242 q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
248 // Save the author information for the like in case we need to relay to Diaspora
249 Diaspora::store_like_signature($item_contact, $new_item_id);
251 $new_item['id'] = $new_item_id;
253 call_hooks('post_local_end', $new_item);
255 proc_run(PRIORITY_HIGH, "include/notifier.php", "like", $new_item_id);