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
|
<?php
namespace JsonApi\Routes\Blubber;
use JsonApi\Errors\BadRequestException;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\JsonApiController;
use JsonApi\Routes\TimestampTrait;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
/**
* Displays all blubber threads of a certain context ID.
*/
class ThreadsIndex extends JsonApiController
{
use TimestampTrait, FilterTrait;
protected $allowedFilteringParameters = ['since', 'before', 'search', 'context-type', 'context-id'];
protected $allowedIncludePaths = ['author', 'comments', 'context', 'mentions'];
protected $allowedPagingParameters = ['offset', 'limit'];
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __invoke(Request $request, Response $response, $args)
{
$this->validateFilters();
$filters = $this->getFilters();
$contextType = $args['type'];
$contextId = isset($args['id']) ? $args['id'] : null;
if ($contextType === 'all') {
if (isset($filters['context-type'])) {
$contextType = $filters['context-type'];
}
if (isset($filters['context-id'])) {
$contextId = $filters['context-id'];
}
}
if (!in_array($contextType, ['all', 'public', 'private', 'course', 'institute'])) {
throw new BadRequestException('Wrong context type.');
}
[$threads, $total] = match ($contextType) {
'all' => $this->getAllThreads($filters, $this->getUser($request)),
'public' => $this->getPublicThreads($this->getUser($request)),
'private' => $this->getPrivateThreads($this->getUser($request), $contextId),
'course' => $this->getCourseThreads($this->getUser($request), $contextId),
'institute' => $this->getInstituteThreads($this->getUser($request), $contextId),
};
return $this->getPaginatedContentResponse($threads, $total);
}
private function getAllThreads(array $filters, \User $observer)
{
list($offset, $limit) = $this->getOffsetAndLimit();
$threads = \BlubberThread::findMyGlobalThreads(
$offset + $limit + 1,
$filters['since'],
$filters['before'],
$observer->id,
$filters['search']
);
$hasMore = count($threads) < $offset + $limit + 1;
$total = $hasMore ? count($threads) : null;
return [array_slice($threads, $offset, $limit), $total];
}
private function getPublicThreads(\User $observer)
{
return $this->paginateThreads(
$this->upgradeAndFilterThreads($observer, \BlubberThread::findBySQL("context_type = 'public'"))
);
}
private function getPrivateThreads(\User $observer, string $userID)
{
if (!($user = \User::find($userID))) {
throw new RecordNotFoundException();
}
$query = 'SELECT a.thread_id FROM blubber_mentions a
JOIN blubber_mentions b
ON a.thread_id = b.thread_id
WHERE a.user_id = ? AND b.user_id = ?
AND a.external_contact = 0 AND b.external_contact = 0';
$statement = \DBManager::get()->prepare($query);
$statement->execute([$observer->id, $user->id]);
$threadIDs = $statement->fetchAll(\PDO::FETCH_COLUMN, 0);
$threads = $this->upgradeAndFilterThreads($observer, \BlubberThread::findMany($threadIDs, 'ORDER BY mkdate'));
return $this->paginateThreads($threads);
}
private function getCourseThreads(\User $observer, string $courseID)
{
if (!($course = \Course::find($courseID))) {
throw new RecordNotFoundException();
}
return $this->paginateThreads(\BlubberThread::findBySeminar($course->id, false, $observer->id));
}
private function getInstituteThreads(\User $observer, string $instituteID)
{
if (!($institute = \Institute::find($instituteID))) {
throw new RecordNotFoundException();
}
return $this->paginateThreads(\BlubberThread::findByInstitut($institute->id, false, $observer->id));
}
private function upgradeAndFilterThreads(\User $user, array $threads)
{
return array_filter(
array_map(function ($thread) {
return \BlubberThread::upgradeThread($thread);
}, $threads),
function ($thread) use ($user) {
return $thread->isVisibleInStream() && $thread->isReadable($user->id);
}
);
}
private function paginateThreads($threads)
{
list($offset, $limit) = $this->getOffsetAndLimit();
$total = count($threads);
$threads = array_slice($threads, $offset, $limit);
return [$threads, $total];
}
}
|