2 require_once("include/diaspora.php");
5 * @brief add/remove activity to an item
7 * Toggle activities as like,dislike,attend of an item
9 * @param string $item_id
11 * Activity verb. One of
12 * like, unlike, dislike, undislike, attendyes, unattendyes,
13 * attendno, unattendno, attendmaybe, unattendmaybe
14 * @hook 'post_local_end'
16 * 'post_id' => ID of posted item
18 function do_like($item_id, $verb) {
21 if (! local_user() && ! remote_user()) {
27 $bodyverb = t('%1$s likes %2$s\'s %3$s');
28 $activity = ACTIVITY_LIKE;
31 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
32 $activity = ACTIVITY_LIKE;
36 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
37 $activity = ACTIVITY_DISLIKE;
41 $bodyverb = t('%1$s is attending %2$s\'s %3$s');
42 $activity = ACTIVITY_ATTEND;
46 $bodyverb = t('%1$s is not attending %2$s\'s %3$s');
47 $activity = ACTIVITY_ATTENDNO;
51 $bodyverb = t('%1$s may attend %2$s\'s %3$s');
52 $activity = ACTIVITY_ATTENDMAYBE;
55 logger('like: unknown verb ' . $verb . ' for item ' . $item_id);
59 // Enable activity toggling instead of on/off
60 $event_verb_flag = $activity === ACTIVITY_ATTEND || $activity === ACTIVITY_ATTENDNO || $activity === ACTIVITY_ATTENDMAYBE;
62 logger('like: verb ' . $verb . ' item ' . $item_id);
65 $items = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
70 if (! $item_id || ! dbm::is_result($items)) {
71 logger('like: unknown item ' . $item_id);
77 if (! can_write_wall($a, $item['uid'])) {
78 logger('like: unable to write on wall ' . $item['uid']);
82 // Retrieves the local post owner
83 $owners = q("SELECT `contact`.* FROM `contact`
84 WHERE `contact`.`self` = 1
85 AND `contact`.`uid` = %d",
88 if (dbm::is_result($owners)) {
89 $owner_self_contact = $owners[0];
91 logger('like: unknown owner ' . $item['uid']);
95 // Retrieve the current logged in user's public contact
96 $author_id = public_contact();
98 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
101 if (dbm::is_result($contacts)) {
102 $author_contact = $contacts[0];
104 logger('like: unknown author ' . $author_id);
108 // Contact-id is the uid-dependant author contact
109 if (local_user() == $item['uid']) {
110 $item_contact_id = $owner_self_contact['id'];
111 $item_contact = $owner_self_contact;
113 $item_contact_id = get_contact($author_contact['url'], $item['uid']);
115 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
116 intval($item_contact_id)
118 if (dbm::is_result($contacts)) {
119 $item_contact = $contacts[0];
121 logger('like: unknown item contact ' . $item_contact_id);
126 // Look for an existing verb row
127 // event participation are essentially radio toggles. If you make a subsequent choice,
128 // we need to eradicate your first choice.
129 if ($event_verb_flag) {
130 $verbs = "'" . dbesc(ACTIVITY_ATTEND) . "', '" . dbesc(ACTIVITY_ATTENDNO) . "', '" . dbesc(ACTIVITY_ATTENDMAYBE) . "'";
132 $verbs = "'".dbesc($activity)."'";
135 $existing_like = q("SELECT `id`, `guid`, `verb` FROM `item`
136 WHERE `verb` IN ($verbs)
140 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s')
142 intval($author_contact['id']),
143 intval($item['uid']),
144 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
147 // If it exists, mark it as deleted
148 if (dbm::is_result($existing_like)) {
149 $like_item = $existing_like[0];
151 // Already voted, undo it
152 q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `id` = %d",
153 dbesc(datetime_convert()),
154 intval($like_item['id'])
157 // Clean up the Diaspora signatures for this like
158 // Go ahead and do it even if Diaspora support is disabled. We still want to clean up
159 // if it had been enabled in the past
160 q("DELETE FROM `sign` WHERE `iid` = %d",
161 intval($like_item['id'])
164 $like_item_id = $like_item['id'];
165 proc_run(PRIORITY_HIGH, "include/notifier.php", "like", $like_item_id);
167 if (!$event_verb_flag || $like_item['verb'] == $activity) {
172 // Verb is "un-something", just trying to delete existing entries
173 if (strpos($verb, 'un') === 0) {
177 // Else or if event verb different from existing row, create a new item row
178 $post_type = (($item['resource-id']) ? t('photo') : t('status'));
179 if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
180 $post_type = t('event');
182 $objtype = $item['resource-id'] ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE ;
183 $link = xmlify('<link rel="alternate" type="text/html" href="' . App::get_baseurl() . '/display/' . $owner_self_contact['nick'] . '/' . $item['id'] . '" />' . "\n") ;
184 $body = $item['body'];
189 <type>$objtype</type>
191 <id>{$item['uri']}</id>
194 <content>$body</content>
198 $ulink = '[url=' . $author_contact['url'] . ']' . $author_contact['name'] . '[/url]';
199 $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
200 $plink = '[url=' . App::get_baseurl() . '/display/' . $owner_self_contact['nick'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
203 'guid' => get_guid(32),
204 'uri' => item_new_uri($a->get_hostname(), $item['uid']),
205 'uid' => $item['uid'],
206 'contact-id' => $item_contact_id,
207 'type' => 'activity',
208 'wall' => $item['wall'],
210 'gravity' => GRAVITY_LIKE,
211 'parent' => $item['id'],
212 'parent-uri' => $item['uri'],
213 'thr-parent' => $item['uri'],
214 'owner-id' => $item['owner-id'],
215 'owner-name' => $item['owner-name'],
216 'owner-link' => $item['owner-link'],
217 'owner-avatar' => $item['owner-avatar'],
218 'author-id' => $author_contact['id'],
219 'author-name' => $author_contact['name'],
220 'author-link' => $author_contact['url'],
221 'author-avatar' => $author_contact['thumb'],
222 'body' => sprintf($bodyverb, $ulink, $alink, $plink),
224 'object-type' => $objtype,
226 'allow_cid' => $item['allow_cid'],
227 'allow_gid' => $item['allow_gid'],
228 'deny_cid' => $item['deny_cid'],
229 'deny_gid' => $item['deny_gid'],
235 $new_item_id = item_store($new_item);
237 // @todo: Explain this block
238 if (! $item['visible']) {
239 q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
245 // Save the author information for the like in case we need to relay to Diaspora
246 Diaspora::store_like_signature($item_contact, $new_item_id);
248 $new_item['id'] = $new_item_id;
250 call_hooks('post_local_end', $new_item);
252 proc_run(PRIORITY_HIGH, "include/notifier.php", "like", $new_item_id);