summaryrefslogtreecommitdiff
path: root/src/web/_model/profile.php
blob: d35cc4631b0e22176b8fe31fa09fca23b46493df (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
<?php /* Copyright (c) 2024 Freya Murphy */
class Profile_model extends XSS_Model {

	private $auth_model;

	function __construct() {
		$this->auth_model = $this->load_model('auth');
	}

	public function get_data(): ?array {
		$uid = $this->get_int('id');
		$session = $this->auth_model->session();

		if (!$uid && $session)
			$uid = $session['id'];
		if (!$uid)
			return NULL;

		$user = $this->db()
			->select('*')
			->from('api.user u')
			->where('u.id')
			->eq($uid)
			->row();

		if (!$user)
			return NULL;

		// am i following $uid?
		$following = FALSE;
		$following_id = NULL;
		// is $uid following me?
		$followed = FALSE;

		if ($session) {
			$sid = $session['id'];
			// am i following $uid?
			$res = $this->db()
				->select('f.value, f.id')
				->from('xssbook.follow f')
				->where('f.follower_id')
				->eq($sid)
				->where('f.followee_id')
				->eq($uid)
				->row();
			$following = $res ? $res['value'] : FALSE;
			$following_id = $res ? $res['id'] : NULL;
			// is $uid following me?
			$res = $this->db()
				->select('f.value')
				->from('xssbook.follow f')
				->where('f.follower_id')
				->eq($uid)
				->where('f.followee_id')
				->eq($sid)
				->row();
			$followed = $res ? $res['value'] : FALSE;
		}

		$data = parent::get_data();
		$data['user'] = $user;
		$data['following'] = $following;
		$data['following_id'] = $following_id;
		$data['followed'] = $followed;

		$name = $this->format_name($user);
		$data['title'] .= " - $name";
		return $data;
	}
}