summaryrefslogtreecommitdiff
path: root/src/web/_model/people.php
blob: bf540cf84c0b3bf26a0e7b3d6ef594f92d1cbbc9 (plain)
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
<?php /* Copyright (c) 2024 Freya Murphy */

class People_model extends XSS_Model {

	private function get_filted_query($select): DatabaseQuery {
		$filter_type = $this->get_string('filter');
		$filter_uid  = $this->get_int('uid');
		$max         = $this->get_int('max');
		$query       = $this->db()
			->select($select)
			->from('api.user u');

		if ($filter_type && $filter_uid) {
			switch ($filter_type) {
			// only show followers
			case 'follower':
				$query = $query
					->join('xssbook.follow f', 'f.follower_id = u.id AND f.followee_id', 'INNER')
					->eq($filter_uid)
					->where('f.value = TRUE');
				break;
			// only show followees
			case 'followee':
				$query = $query
					->join('xssbook.follow f', 'f.followee_id = u.id AND f.follower_id', 'INNER')
					->eq($filter_uid)
					->where('f.value = TRUE');
				break;
			}
		}

		if ($max) {
			$query = $query
				->where('u.id')
				->le($max);
		}

		return $query;
	}

	public function get_people(): array {
		$filter_type = $this->get_string('filter');
		$filter_uid  = $this->get_int('uid');
		$page        = $this->get_int('page', 0);

		$page_size = PEOPLE_PAGE_SIZE;
		$offset = $page_size * $page;

		$users = $this->get_filted_query('u.*')
			->order_by('u.id', 'DESC')
			->offset($offset)
			->limit($page_size)
			->rows();

		$count = $this->get_filted_query('COUNT(u.id) AS count')
			->row()['count'];

		$max = 0;

		foreach ($users as $user)
			$max = max($max, $user['id']);

		return array(
			'users' => $users,
			'count' => $count,
			'page_size' => $page_size,
			'max_id' => $max,
			'filter_type' => $filter_type,
			'filter_uid' => $filter_uid,
		);
	}
}