aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Module/Like.php
blob: 4460900a8e36394c968737030bbaffe14aa486b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<?php

namespace Zotlabs\Module;

use App;
use Zotlabs\Lib\Activity;
use Zotlabs\Lib\Libsync;
use Zotlabs\Web\Controller;
use Zotlabs\Daemon\Master;


require_once('include/security.php');
require_once('include/bbcode.php');
require_once('include/items.php');
require_once('include/conversation.php');

class Like extends Controller {

	private function reaction_to_activity($reaction) {

		$acts = [
			'like'        => 'Like',
			'dislike'     => 'Dislike',
			'announce'    => ACTIVITY_SHARE,
			'attendyes'   => 'Accept',
			'attendno'    => 'Reject',
			'attendmaybe' => 'TentativeAccept'
		];

		// unlike (etc.) reactions are an undo of positive reactions, rather than a negative action.
		// The activity is the same in undo actions and will have the same activity mapping

		if (substr($reaction, 0, 2) === 'un') {
			$reaction = substr($reaction, 2);
		}

		if (array_key_exists($reaction, $acts)) {
			return $acts[$reaction];
		}

		return EMPTY_STR;

	}

	private function like_response($arr) {

		$page_mode = (($arr['item']['item_thread_top'] && $_REQUEST['page_mode']) ? $_REQUEST['page_mode'] : 'r_preview');
		$conv_mode = (($_REQUEST['conv_mode']) ? $_REQUEST['conv_mode'] : 'network');

		if ($conv_mode === 'channel') {
			$parts = explode('@', $arr['owner_xchan']['xchan_addr']);
			profile_load($parts[0]);
		}

		$item_normal = item_normal();

		if ($page_mode === 'list') {
			$items = q("SELECT item.*, item.id AS item_id FROM item
				WHERE uid = %d $item_normal
				AND parent = %d",
				intval($arr['item']['uid']),
				intval($arr['item']['parent'])
			);
			xchan_query($items, true);
			$items = fetch_post_tags($items, true);
			$items = conv_sort($items, 'commented');
		}
		else {
			$activities = q("SELECT item.*, item.id AS item_id FROM item
				WHERE uid = %d $item_normal
				AND thr_parent = '%s'
				AND verb IN ('%s', '%s', '%s', '%s', '%s', '%s', 'Accept', 'Reject', 'TentativeAccept')",
				intval($arr['item']['uid']),
				dbesc($arr['item']['mid']),
				dbesc('Like'),
				dbesc('Dislike'),
				dbesc(ACTIVITY_SHARE),
				dbesc(ACTIVITY_ATTEND),
				dbesc(ACTIVITY_ATTENDNO),
				dbesc(ACTIVITY_ATTENDMAYBE)
			);
			xchan_query($activities, true);
			$items = array_merge([$arr['item']], $activities);
			$items = fetch_post_tags($items, true);
		}

		$ret = [
			'success' => 1,
			'orig_id' => $arr['orig_item_id'], //this is required for pubstream items where $item_id != $item['id']
			'id'      => $arr['item']['id'],
			'html'    => conversation($items, $conv_mode, true, $page_mode),
		];

		// mod photos
		if (isset($_REQUEST['reload']) && $_REQUEST['reload']) {
			$ret['reload'] = 1;
		}

		return $ret;

	}

	public function get() {

		$o           = EMPTY_STR;
		$sys_channel = get_sys_channel();
		$observer    = App::get_observer();
		$interactive = $_REQUEST['interactive'] ?? false;

		if ((!$observer) || ($interactive)) {
			$o .= '<h1>' . t('Like/Dislike') . '</h1>';
			$o .= EOL . EOL;

			if (!$observer) {
				$_SESSION['return_url'] = App::$query_string;

				$o .= t('This action is restricted to members.') . EOL;
				$o .= t('Please <a href="rmagic">login with your $Projectname ID</a> or <a href="register">register as a new $Projectname member</a> to continue.') . EOL;
				return $o;
			}
		}

		$verb = notags(trim($_GET['verb']));

		if (!$verb)
			$verb = 'like';

		$activity = $this->reaction_to_activity($verb);

		if (!$activity) {
			return EMPTY_STR;
		}

		$is_rsvp = false;
		if (in_array($activity, ['Accept', 'Reject', 'TentativeAccept', ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE])) {
			$is_rsvp = true;
		}

		$extended_like = false;
		$object        = $target = null;
		$post_type     = EMPTY_STR;
		$obj_type       = EMPTY_STR;

		if (argc() == 3) {

			if (!$observer)
				killme();

			$extended_like = true;
			$obj_type      = argv(1);
			$obj_id        = argv(2);
			$public        = true;

			if ($obj_type == 'profile') {
				$r = q("select * from profile where profile_guid = '%s' limit 1",
					dbesc(argv(2))
				);
				if (!$r)
					killme();
				$owner_uid = $r[0]['uid'];
				if ($r[0]['is_default'])
					$public = true;
				if (!$public) {
					$d = q("select abook_xchan from abook where abook_profile = '%s' and abook_channel = %d",
						dbesc($r[0]['profile_guid']),
						intval($owner_uid)
					);
					if (!$d) {
						// forgery - illegal
						if ($interactive) {
							notice(t('Invalid request.') . EOL);
							return $o;
						}
						killme();
					}
					// $d now contains a list of those who can see this profile - only send the status notification
					// to them.
					$allow_cid = $allow_gid = $deny_cid = $deny_gid = '';
					foreach ($d as $dd) {
						$allow_cid .= '<' . $dd['abook_xchan'] . '>';
					}
				}
				$post_type = t('channel');
				$obj_type   = 'Profile';

				$profile = $r[0];
			}
			elseif ($obj_type == 'thing') {

				$r = q("select * from obj where obj_type = %d and obj_obj = '%s' limit 1",
					intval(TERM_OBJ_THING),
					dbesc(argv(2))
				);

				if (!$r) {
					if ($interactive) {
						notice(t('Invalid request.') . EOL);
						return $o;
					}
					killme();
				}

				$owner_uid = $r[0]['obj_channel'];

				$allow_cid = $r[0]['allow_cid'];
				$allow_gid = $r[0]['allow_gid'];
				$deny_cid  = $r[0]['deny_cid'];
				$deny_gid  = $r[0]['deny_gid'];
				if ($allow_cid || $allow_gid || $deny_cid || $deny_gid)
					$public = false;

				$post_type = t('thing');
				$obj_type   = 'Profile';
				$tgttype   = 'Page';

				$links   = array();
				$links[] = array('rel'  => 'alternate', 'type' => 'text/html',
								 'href' => z_root() . '/thing/' . $r[0]['obj_obj']);
				if ($r[0]['imgurl'])
					$links[] = array('rel' => 'photo', 'href' => $r[0]['obj_imgurl']);

				$target = Activity::fetch_thing(['id' => $r[0]['obj_obj']]);

				$plink = '[zrl=' . z_root() . '/thing/' . $r[0]['obj_obj'] . ']' . $r[0]['obj_term'] . '[/zrl]';

			}

			if (!($owner_uid && $r)) {
				if ($interactive) {
					notice(t('Invalid request.') . EOL);
					return $o;
				}
				killme();
			}

			// The resultant activity is going to be a wall-to-wall post, so make sure this is allowed

			$perms = get_all_perms($owner_uid, $observer['xchan_hash']);

			if (!($perms['post_like'] && $perms['view_profile'])) {
				if ($interactive) {
					notice(t('Permission denied.') . EOL);
					return $o;
				}
				killme();
			}

			$ch = q("select * from channel left join xchan on channel_hash = xchan_hash where channel_id = %d limit 1",
				intval($owner_uid)
			);
			if (!$ch) {
				if ($interactive) {
					notice(t('Channel unavailable.') . EOL);
					return $o;
				}
				killme();
			}

			if (!$plink)
				$plink = '[zrl=' . z_root() . '/profile/' . $ch[0]['channel_address'] . ']' . $post_type . '[/zrl]';

			$object = json_encode(Activity::fetch_profile(['id' => channel_url($ch[0])]));

			// second like of the same thing is "undo" for the first like

			$z = q("select * from likes where channel_id = %d and liker = '%s' and verb = '%s' and target_type = '%s' and target_id = '%s' limit 1",
				intval($ch[0]['channel_id']),
				dbesc($observer['xchan_hash']),
				dbesc($activity),
				dbesc(($tgttype) ? $tgttype : $obj_type),
				dbesc($obj_id)
			);

			if ($z) {
				$z[0]['deleted'] = 1;
				Libsync::build_sync_packet($ch[0]['channel_id'], array('likes' => $z));

				q("delete from likes where id = %d",
					intval($z[0]['id'])
				);
				if ($z[0]['i_mid']) {
					$r = q("select id from item where mid = '%s' and uid = %d limit 1",
						dbesc($z[0]['i_mid']),
						intval($ch[0]['channel_id'])
					);
					if ($r)
						drop_item($r[0]['id'], false);
					if ($interactive) {
						notice(t('Previous action reversed.') . EOL);
						return $o;
					}
				}
				killme();
			}
		}
		else {

			if (!$observer)
				killme();

			// this is used to like an item or comment

			$item_id = ((argc() == 2) ? notags(trim(argv(1))) : 0);

			logger('like: verb ' . $verb . ' item ' . $item_id, LOGGER_DEBUG);

			// get the item. Allow linked photos (which are normally hidden) to be liked

			$r = q("SELECT * FROM item WHERE id = %d
				and item_type in (0,6,7) and item_deleted = 0 and item_unpublished = 0
				and item_delayed = 0 and item_pending_remove = 0 and item_blocked = 0 LIMIT 1",
				intval($item_id)
			);

			// if interacting with a pubstream item,
			// create a copy of the parent in your stream. If not the conversation
			// parent, copy that as well.

			if ($r) {
				$obj_type = $r[0]['obj_type'];

				if ($r[0]['uid'] === $sys_channel['channel_id'] && local_channel()) {
					$r = [copy_of_pubitem(App::get_channel(), $r[0]['mid'])];
				}
			}

			if (!$item_id || (!$r)) {
				logger('like: no item ' . $item_id);
				killme();
			}

			xchan_query($r, true);

			$item      = $r[0];
			$owner_uid = $r[0]['uid'];
			$owner_aid = $r[0]['aid'];

			if ((array_key_exists('owner', $item)) && intval($item['owner']['abook_self']))
				$can_comment = perm_is_allowed($item['uid'], $observer['xchan_hash'], 'post_comments');
			else
				$can_comment = can_comment_on_post($observer['xchan_hash'], $item);

			if (!$can_comment) {
				notice(t('Permission denied') . EOL);
				killme();
			}

			$r = q("select * from xchan where xchan_hash = '%s' limit 1",
				dbesc($item['owner_xchan'])
			);

			if ($r)
				$thread_owner = $r[0];
			else
				killme();

			$r = q("select * from xchan where xchan_hash = '%s' limit 1",
				dbesc($item['author_xchan'])
			);
			if ($r)
				$item_author = $r[0];
			else
				killme();

			$verbs = " '" . dbesc($activity) . "' ";

			$multi_undo = false;

			// event participation items are essentially radio toggles. If you make a subsequent choice,
			// we need to eradicate your first choice.

			if (in_array($activity, ['Accept', 'Reject', 'TentativeAccept', ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE])) {
				$verbs      = "'Accept','Reject','TentativeAccept','" . dbesc(ACTIVITY_ATTEND) . "','" . dbesc(ACTIVITY_ATTENDNO) . "','" . dbesc(ACTIVITY_ATTENDMAYBE) . "' ";
				$multi_undo = true;
			}

			$item_normal = item_normal();

			$r = q("SELECT id, parent, uid, verb FROM item WHERE verb in ( $verbs ) $item_normal
				AND author_xchan = '%s' AND thr_parent = '%s' and uid = %d ",
				dbesc($observer['xchan_hash']),
				dbesc($item['mid']),
				intval($owner_uid)
			);

			if ($r) {
				// already liked it. Drop that item.
				require_once('include/items.php');
				foreach ($r as $rr) {
					drop_item($rr['id'], false, DROPITEM_PHASE1);
					// set the changed timestamp on the parent so we'll see the update without a page reload
					q("update item set changed = '%s' where id = %d and uid = %d",
						dbesc(datetime_convert()),
						intval($rr['parent']),
						intval($rr['uid'])
					);
					// Prior activity was a duplicate of the one we're submitting, just undo it;
					// don't fall through and create another
					if (activity_match($rr['verb'], $activity))
						$multi_undo = false;

					$d = q("select * from item where id = %d",
						intval($rr['id'])
					);
					if ($d) {
						xchan_query($d);
						$sync_item = fetch_post_tags($d);
						Libsync::build_sync_packet($profile_uid, ['item' => [encode_item($sync_item[0], true)]]);
					}

					// drop_item was not done interactively, so we need to invoke the notifier
					// in order to push the changes to connections

					Master::Summon(array('Notifier', 'drop', $rr['id']));

				}

				if ($interactive)
					return;

				if (!$multi_undo) {
					$ret = self::like_response([
						'item'         => $item,
						'orig_item_id' => $item_id,
						'owner_xchan'  => $thread_owner
					]);
					json_return_and_die($ret);
				}
			}
		}

		$uuid = new_uuid();

		$arr = array();

		$arr['uuid'] = $uuid;
		$arr['mid']  = z_root() . (($is_rsvp) ? '/activity/' : '/item/') . $uuid;

		if ($extended_like) {
			$arr['item_thread_top'] = 1;
			$arr['item_origin']     = 1;
			$arr['item_wall']       = 1;
		}
		else {
			switch ($item['object_type']) {
				case 'Image':
					$post_type = t('image');
					break;
				case 'Invite':
					$post_type = t('event');
					break;
				case 'Profile':
					$post_type = t('profile');
					break;
				default:
					$post_type = t('status');
					break;
			}

			$object = json_encode(Activity::fetch_item(['id' => $item['mid']]));

			if (!intval($item['item_thread_top']))
				$post_type = 'comment';

			$arr['item_origin']   = 1;
			$arr['item_notshown'] = 1;
			$arr['item_type']     = $item['item_type'];

			if (intval($item['item_wall']))
				$arr['item_wall'] = 1;

			// if this was a linked photo and was hidden, unhide it.

			if (intval($item['item_hidden'])) {
				$r = q("update item set item_hidden = 0 where id = %d",
					intval($item['id'])
				);
			}

		}

		if ($verb === 'like')
			$bodyverb = t('%1$s likes %2$s\'s %3$s');
		if ($verb === 'dislike')
			$bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
		if ($verb === 'attendyes')
			$bodyverb = t('%1$s is attending %2$s\'s %3$s');
		if ($verb === 'attendno')
			$bodyverb = t('%1$s is not attending %2$s\'s %3$s');
		if ($verb === 'attendmaybe')
			$bodyverb = t('%1$s may attend %2$s\'s %3$s');

		if (!isset($bodyverb))
			killme();

		if ($extended_like) {
			$ulink   = '[zrl=' . $ch[0]['xchan_url'] . '][bdi]' . $ch[0]['xchan_name'] . '[/bdi][/zrl]';
			$alink   = '[zrl=' . $observer['xchan_url'] . '][bdi]' . $observer['xchan_name'] . '[/bdi][/zrl]';
			$private = (($public) ? 0 : 1);
		}
		else {
			$arr['parent']     = $item['parent'];
			$arr['thr_parent'] = $item['mid'];
			$ulink             = '[zrl=' . $item_author['xchan_url'] . '][bdi]' . $item_author['xchan_name'] . '[/bdi][/zrl]';
			$alink             = '[zrl=' . $observer['xchan_url'] . '][bdi]' . $observer['xchan_name'] . '[/bdi][/zrl]';
			$plink             = '[zrl=' . z_root() . '/display/' . $item['uuid'] . ']' . $post_type . '[/zrl]';
			$allow_cid         = $item['allow_cid'];
			$allow_gid         = $item['allow_gid'];
			$deny_cid          = $item['deny_cid'];
			$deny_gid          = $item['deny_gid'];
			$private           = $item['item_private'];

		}

		$arr['aid']          = (($extended_like) ? $ch[0]['channel_account_id'] : $owner_aid);
		$arr['uid']          = $owner_uid;
		$arr['item_flags']   = $item['item_flags'];
		$arr['item_wall']    = $item['item_wall'];
		$arr['parent_mid']   = (($extended_like) ? $arr['mid'] : $item['parent_mid']);
		$arr['owner_xchan']  = (($extended_like) ? $ch[0]['xchan_hash'] : $thread_owner['xchan_hash']);
		$arr['author_xchan'] = $observer['xchan_hash'];
		$arr['body']         = sprintf($bodyverb, $alink, $ulink, $plink);

		if ($obj_type === 'thing' && $r[0]['imgurl']) {
			$arr['body'] .= "\n\n[zmg=80x80]" . $r[0]['imgurl'] . '[/zmg]';
		}
		if ($obj_type === 'Profile') {
			if ($public) {
				$arr['body'] .= "\n\n" . '[embed]' . z_root() . '/profile/' . $ch[0]['channel_address'] . '[/embed]';
			}
			else
				$arr['body'] .= "\n\n[zmg=80x80]" . $profile['thumb'] . '[/zmg]';
		}

		$arr['verb']     = $activity;
		$arr['obj_type'] = $obj_type;
		$arr['obj']      = $object;

		if ($target) {
			$arr['tgt_type'] = $tgttype;
			$arr['target']   = $target;
		}

		$arr['allow_cid']    = $allow_cid;
		$arr['allow_gid']    = $allow_gid;
		$arr['deny_cid']     = $deny_cid;
		$arr['deny_gid']     = $deny_gid;
		$arr['item_private'] = $private;

		$created = datetime_convert();

		$arr['created'] = $created;
		$arr['edited'] = $created;
		$arr['commented'] = $created;
		$arr['received'] = $created;
		$arr['changed'] = $created;

		call_hooks('post_local', $arr);

		$post    = item_store($arr);
		$post_id = $post['item_id'];

		// save the conversation from expiration

		if (local_channel() && array_key_exists('item', $post) && (intval($post['item']['id']) != intval($post['item']['parent'])))
			retain_item($post['item']['parent']);

		$arr['id'] = $post_id;

		call_hooks('post_local_end', $arr);

		$r = q("select * from item where id = %d",
			intval($post_id)
		);
		if ($r) {
			xchan_query($r);
			$sync_item = fetch_post_tags($r);
			Libsync::build_sync_packet($profile_uid, ['item' => [encode_item($sync_item[0], true)]]);
		}


		if ($extended_like) {
			$r = q("insert into likes (channel_id,liker,likee,iid,i_mid,verb,target_type,target_id,target) values (%d,'%s','%s',%d,'%s','%s','%s','%s','%s')",
				intval($ch[0]['channel_id']),
				dbesc($observer['xchan_hash']),
				dbesc($ch[0]['channel_hash']),
				intval($post_id),
				dbesc($arr['mid']),
				dbesc($activity),
				dbesc(($tgttype) ? $tgttype : $obj_type),
				dbesc($obj_id),
				dbesc(($target) ? $target : $object)
			);
			$r = q("select * from likes where liker = '%s' and likee = '%s' and i_mid = '%s' and verb = '%s' and target_type = '%s' and target_id = '%s' ",
				dbesc($observer['xchan_hash']),
				dbesc($ch[0]['channel_hash']),
				dbesc($arr['mid']),
				dbesc($activity),
				dbesc(($tgttype) ? $tgttype : $obj_type),
				dbesc($obj_id)
			);
			if ($r)
				Libsync::build_sync_packet($ch[0]['channel_id'], array('likes' => $r));

		}

		Master::Summon(array('Notifier', 'like', $post_id));

		if ($interactive) {
			notice(t('Action completed.') . EOL);
			$o .= t('Thank you.');
			return $o;
		}

		$ret = self::like_response([
			'item'         => $item,
			'orig_item_id' => $item_id,
			'owner_xchan'  => $thread_owner
		]);
		json_return_and_die($ret);

	}

}