]> git.mxchange.org Git - friendica.git/blob - include/like.php
Remove unused parameter in can_write_wall()
[friendica.git] / include / like.php
1 <?php
2
3 use Friendica\App;
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;
9
10 /**
11  * @brief add/remove activity to an item
12  *
13  * Toggle activities as like,dislike,attend of an item
14  *
15  * @param string $item_id
16  * @param string $verb
17  *              Activity verb. One of
18  *                      like, unlike, dislike, undislike, attendyes, unattendyes,
19  *                      attendno, unattendno, attendmaybe, unattendmaybe
20  * @hook 'post_local_end'
21  *              array $arr
22  *                      'post_id' => ID of posted item
23  */
24 function do_like($item_id, $verb) {
25         $a = get_app();
26
27         if (!local_user() && !remote_user()) {
28                 return false;
29         }
30
31         switch ($verb) {
32                 case 'like':
33                         $bodyverb = t('%1$s likes %2$s\'s %3$s');
34                         $activity = ACTIVITY_LIKE;
35                         break;
36                 case 'unlike':
37                         $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
38                         $activity = ACTIVITY_LIKE;
39                         break;
40                 case 'dislike':
41                 case 'undislike':
42                         $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
43                         $activity = ACTIVITY_DISLIKE;
44                         break;
45                 case 'attendyes':
46                 case 'unattendyes':
47                         $bodyverb = t('%1$s is attending %2$s\'s %3$s');
48                         $activity = ACTIVITY_ATTEND;
49                         break;
50                 case 'attendno':
51                 case 'unattendno':
52                         $bodyverb = t('%1$s is not attending %2$s\'s %3$s');
53                         $activity = ACTIVITY_ATTENDNO;
54                         break;
55                 case 'attendmaybe':
56                 case 'unattendmaybe':
57                         $bodyverb = t('%1$s may attend %2$s\'s %3$s');
58                         $activity = ACTIVITY_ATTENDMAYBE;
59                         break;
60                 default:
61                         logger('like: unknown verb ' . $verb . ' for item ' . $item_id);
62                         return false;
63         }
64
65         // Enable activity toggling instead of on/off
66         $event_verb_flag = $activity === ACTIVITY_ATTEND || $activity === ACTIVITY_ATTENDNO || $activity === ACTIVITY_ATTENDMAYBE;
67
68         logger('like: verb ' . $verb . ' item ' . $item_id);
69
70         // Retrieve item
71         $items = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
72                 dbesc($item_id),
73                 dbesc($item_id)
74         );
75
76         if (!$item_id || !DBM::is_result($items)) {
77                 logger('like: unknown item ' . $item_id);
78                 return false;
79         }
80
81         $item = $items[0];
82         $uid = $item['uid'];
83
84         if (($uid == 0) && local_user()) {
85                 $uid = local_user();
86         }
87
88         if (!can_write_wall($uid)) {
89                 logger('like: unable to write on wall ' . $uid);
90                 return false;
91         }
92
93         // Retrieves the local post owner
94         $owners = q("SELECT `contact`.* FROM `contact`
95                 WHERE `contact`.`self`
96                 AND `contact`.`uid` = %d",
97                 intval($uid)
98         );
99         if (DBM::is_result($owners)) {
100                 $owner_self_contact = $owners[0];
101         } else {
102                 logger('like: unknown owner ' . $uid);
103                 return false;
104         }
105
106         // Retrieve the current logged in user's public contact
107         $author_id = public_contact();
108
109         $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
110                 intval($author_id)
111         );
112         if (DBM::is_result($contacts)) {
113                 $author_contact = $contacts[0];
114         } else {
115                 logger('like: unknown author ' . $author_id);
116                 return false;
117         }
118
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;
123         } else {
124                 $item_contact_id = Contact::getIdForURL($author_contact['url'], $uid);
125
126                 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d",
127                         intval($item_contact_id)
128                 );
129                 if (DBM::is_result($contacts)) {
130                         $item_contact = $contacts[0];
131                 } else {
132                         logger('like: unknown item contact ' . $item_contact_id);
133                         return false;
134                 }
135         }
136
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) . "'";
142         } else {
143                 $verbs = "'".dbesc($activity)."'";
144         }
145
146         $existing_like = q("SELECT `id`, `guid`, `verb` FROM `item`
147                 WHERE `verb` IN ($verbs)
148                 AND `deleted` = 0
149                 AND `author-id` = %d
150                 AND `uid` = %d
151                 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s')
152                 LIMIT 1",
153                 intval($author_contact['id']),
154                 intval($item['uid']),
155                 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
156         );
157
158         // If it exists, mark it as deleted
159         if (DBM::is_result($existing_like)) {
160                 $like_item = $existing_like[0];
161
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'])
166                 );
167
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']));
172
173                 $like_item_id = $like_item['id'];
174                 Worker::add(PRIORITY_HIGH, "Notifier", "like", $like_item_id);
175
176                 if (!$event_verb_flag || $like_item['verb'] == $activity) {
177                         return true;
178                 }
179         }
180
181         // Verb is "un-something", just trying to delete existing entries
182         if (strpos($verb, 'un') === 0) {
183                 return true;
184         }
185
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');
190         }
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'];
194
195         $obj = <<< EOT
196
197         <object>
198                 <type>$objtype</type>
199                 <local>1</local>
200                 <id>{$item['uri']}</id>
201                 <link>$link</link>
202                 <title></title>
203                 <content>$body</content>
204         </object>
205 EOT;
206
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]';
210
211         $new_item = array(
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'],
218                 'origin'        => 1,
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),
232                 'verb'          => $activity,
233                 'object-type'   => $objtype,
234                 'object'        => $obj,
235                 'allow_cid'     => $item['allow_cid'],
236                 'allow_gid'     => $item['allow_gid'],
237                 'deny_cid'      => $item['deny_cid'],
238                 'deny_gid'      => $item['deny_gid'],
239                 'visible'       => 1,
240                 'unseen'        => 1,
241                 'last-child'    => 0
242         );
243
244         $new_item_id = item_store($new_item);
245
246         // @todo: Explain this block
247         if (! $item['visible']) {
248                 q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d",
249                         intval($item['id'])
250                 );
251         }
252
253         // Save the author information for the like in case we need to relay to Diaspora
254         Diaspora::storeLikeSignature($item_contact, $new_item_id);
255
256         $new_item['id'] = $new_item_id;
257
258         call_hooks('post_local_end', $new_item);
259
260         Worker::add(PRIORITY_HIGH, "Notifier", "like", $new_item_id);
261
262         return true;
263 }