-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathConversationListView.class.php
More file actions
180 lines (162 loc) · 6.99 KB
/
ConversationListView.class.php
File metadata and controls
180 lines (162 loc) · 6.99 KB
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
<?php
namespace wcf\system\listView\user;
use wcf\data\conversation\Conversation;
use wcf\data\conversation\label\ConversationLabel;
use wcf\data\conversation\UserConversationList;
use wcf\data\DatabaseObjectList;
use wcf\event\listView\user\ConversationListViewInitialized;
use wcf\system\form\builder\field\AbstractFormField;
use wcf\system\form\builder\field\ConversationLabelFormField;
use wcf\system\interaction\bulk\user\ConversationBulkInteractions;
use wcf\system\interaction\user\ConversationInteractions;
use wcf\system\listView\AbstractListView;
use wcf\system\listView\filter\AbstractFilter;
use wcf\system\listView\filter\TextFilter;
use wcf\system\listView\filter\UserFilter;
use wcf\system\listView\ListViewSortField;
use wcf\system\WCF;
/**
* @author Olaf Braun
* @copyright 2001-2025 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.2
*
* @extends AbstractListView<Conversation, UserConversationList>
*/
final class ConversationListView extends AbstractListView
{
public readonly string $filter;
public function __construct(string $filter = '')
{
if ($filter === '' || \in_array($filter, UserConversationList::$availableFilters)) {
$this->filter = $filter;
} else {
$this->filter = '';
}
$this->addAvailableSortFields([
new ListViewSortField('time', 'wcf.global.date'),
new ListViewSortField('subject', 'wcf.global.title'),
new ListViewSortField('lastPostTime', 'wcf.conversation.lastPostTime'),
new ListViewSortField('username', 'wcf.user.username'),
new ListViewSortField('replies', 'wcf.conversation.replies'),
new ListViewSortField('participants', 'wcf.conversation.participants'),
]);
$this->addAvailableFilter(new TextFilter('subject', 'wcf.global.title'));
// Participants in draft conversations are not yet stored in the `wcf1_conversation_to_user` table,
// they are serialized in `draftData`.
if ($filter !== 'draft') {
$this->addAvailableFilter($this->getParticipantFilter());
}
if (ConversationLabel::getUserLabels() !== []) {
$this->addAvailableFilter($this->getLabelFilter());
}
$this->setInteractionProvider(new ConversationInteractions());
$this->setBulkInteractionProvider(new ConversationBulkInteractions());
$this->setItemsPerPage(WCF::getUser()->conversationsPerPage ?: \CONVERSATIONS_PER_PAGE);
$this->setSortField(\CONVERSATION_LIST_DEFAULT_SORT_FIELD);
$this->setSortOrder(\CONVERSATION_LIST_DEFAULT_SORT_ORDER);
$this->setCssClassName("discussionList conversationList");
$this->setContainerCssClassName('discussionList__container conversationList__container');
$this->setMarkAsReadEndpoints('core/conversations/%s/mark-as-read');
}
#[\Override]
protected function createObjectList(): UserConversationList
{
return new UserConversationList(WCF::getUser()->userID, $this->filter);
}
#[\Override]
public function renderItems(): string
{
return WCF::getTPL()->render('wcf', 'conversationListItems', ['view' => $this]);
}
#[\Override]
protected function getInitializedEvent(): ConversationListViewInitialized
{
return new ConversationListViewInitialized($this);
}
#[\Override]
public function getParameters(): array
{
return ['filter' => $this->filter];
}
private function getParticipantFilter(): AbstractFilter
{
return new class(id: 'participants', languageItem: 'wcf.conversation.participants') extends UserFilter {
#[\Override]
public function applyFilter(DatabaseObjectList $list, string $value): void
{
// The condition is split into two branches in order to account for invisible participants.
// Invisible participants are only visible to the conversation starter and remain invisible
// until they write their first message.
//
// We need to protect these users from being exposed as participants by including them for
// any conversation that the current user has started. For all other conversations, users
// flagged with `isInvisible = 0` must be excluded.
//
// See https://github.com/WoltLab/com.woltlab.wcf.conversation/issues/131
$list->getConditionBuilder()->add(
"(
(
conversation.userID = ?
AND conversation.conversationID IN (
SELECT conversationID
FROM wcf1_conversation_to_user
WHERE participantID = ?
)
)
OR
(
conversation.userID <> ?
AND conversation.conversationID IN (
SELECT conversationID
FROM wcf1_conversation_to_user
WHERE participantID = ?
AND isInvisible = ?
)
)
)",
[
// Parameters for the first condition.
WCF::getUser()->userID,
$value,
// Parameters for the second condition.
WCF::getUser()->userID,
$value,
0,
]
);
}
};
}
private function getLabelFilter(): AbstractFilter
{
return new class extends AbstractFilter {
public function __construct()
{
parent::__construct('label', 'wcf.label.label');
}
public function getFormField(): AbstractFormField
{
return ConversationLabelFormField::create('label')
->label($this->languageItem)
->labels(ConversationLabel::getUserLabels());
}
public function applyFilter(DatabaseObjectList $list, string $value): void
{
$list->getConditionBuilder()->add(
"{$list->getDatabaseTableAlias()}.{$list->getDatabaseTableIndexName()} IN (
SELECT conversationID
FROM wcf1_conversation_label_to_object
WHERE labelID = ?
)",
[$value]
);
}
#[\Override]
public function renderValue(string $value): string
{
return ConversationLabel::getUserLabels()[$value]->label;
}
};
}
}