aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Module/Pin.php
blob: 352a056ed9b073bf8cdef17e40a9a83d8c011aa5 (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
<?php
namespace Zotlabs\Module;

/*
 * Pinned post processing
 */

use App;

class Pin extends \Zotlabs\Web\Controller {


	function init() {

		if(argc() !== 2)
			http_status_exit(400, 'Bad request');
	}


	function post() {

		$item_id = intval($_POST['id']);

		if ($item_id <= 0)
			http_status_exit(404, 'Not found');

		$observer = \App::get_observer();
		if(! $observer)
			http_status_exit(403, 'Forbidden');

		$r = q("SELECT * FROM item WHERE id = %d AND id = parent AND item_private = 0 LIMIT 1",
			$item_id
		);
		if(! $r) {
			notice(t('Unable to locate original post.'));
			http_status_exit(404, 'Not found');
		}

		$midb64 = 'b64.' . base64url_encode($r[0]['mid']);
		$pinned = (in_array($midb64, get_pconfig($r[0]['uid'], 'pinned', $r[0]['item_type'], [])) ? true : false);

		switch(argv(1)) {

			case 'pin':
				if(! local_channel() || local_channel() != $r[0]['uid'])
					http_status_exit(403, 'Forbidden');
				// Currently allow only one pinned item for each type
				set_pconfig($r[0]['uid'], 'pinned', $r[0]['item_type'], ($pinned ? [] : [ $midb64 ]));
				if($pinned)
					del_pconfig($r[0]['uid'], 'pinned_hide', $midb64);
				break;

			case 'hide':
				if($pinned) {
					$hidden = get_pconfig($r[0]['uid'], 'pinned_hide', $midb64, []);
					if(! in_array($observer['xchan_hash'], $hidden)) {
						$hidden[] = $observer['xchan_hash'];
						set_pconfig($r[0]['uid'], 'pinned_hide', $midb64, $hidden);
					}
				}
				break;

			default:
				http_status_exit(404, 'Not found');
		}

		build_sync_packet($r[0]['uid'], [ 'config' ]);
	}
}