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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
<!--
- @copyright Copyright (c) 2023 Maksim Sukharev <antreesy.web@gmail.com>
-
- @author Maksim Sukharev <antreesy.web@gmail.com>
-
- @license AGPL-3.0-or-later
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<template>
<li class="wrapper wrapper--system">
<div v-for="messagesCollapsed in messagesGroupedBySystemMessage"
:key="messagesCollapsed.id"
class="messages-group__system">
<ul v-if="messagesCollapsed.messages?.length > 1"
class="messages messages--header">
<Message v-bind="createCombinedSystemMessage(messagesCollapsed)"
:token="token"
is-combined-system-message
:is-combined-system-message-collapsed="messagesCollapsed.collapsed"
:next-message-id="getNextMessageId(messagesCollapsed.messages.at(-1))"
:previous-message-id="getPrevMessageId(messagesCollapsed.messages.at(0))"
:last-collapsed-message-id="messagesCollapsed.lastId"
@toggle-combined-system-message="toggleCollapsed(messagesCollapsed)" />
</ul>
<ul v-show="messagesCollapsed.messages?.length === 1 || !messagesCollapsed.collapsed"
class="messages"
:class="{'messages--collapsed': messagesCollapsed.messages?.length > 1}">
<Message v-for="message in messagesCollapsed.messages"
:key="message.id"
v-bind="message"
:token="token"
:is-collapsed-system-message="messagesCollapsed.messages?.length > 1"
:last-collapsed-message-id="messagesCollapsed.lastId"
:next-message-id="getNextMessageId(message)"
:previous-message-id="getPrevMessageId(message)" />
</ul>
</div>
</li>
</template>
<script>
import Message from './Message/Message.vue'
import { useCombinedSystemMessage } from '../../../composables/useCombinedSystemMessage.js'
export default {
name: 'MessagesSystemGroup',
components: {
Message,
},
inheritAttrs: false,
props: {
/**
* The conversation token.
*/
token: {
type: String,
required: true,
},
/**
* The messages object.
*/
messages: {
type: Array,
required: true,
},
previousMessageId: {
type: [String, Number],
default: 0,
},
nextMessageId: {
type: [String, Number],
default: 0,
},
},
setup() {
const { createCombinedSystemMessage } = useCombinedSystemMessage()
return {
createCombinedSystemMessage,
}
},
data() {
return {
groupIsCollapsed: {},
messagesGroupedBySystemMessage: [],
}
},
computed: {
lastReadMessageId() {
return this.$store.getters.conversation(this.token)?.lastReadMessage
}
},
watch: {
messages: {
deep: true,
immediate: true,
handler(value) {
this.messagesGroupedBySystemMessage = this.groupMessages(value)
this.updateCollapsedState()
},
},
lastReadMessageId() {
this.updateCollapsedState()
},
},
methods: {
/**
* Compare two messages to decide if they should be grouped
*
* @param {object} message1 The new message
* @param {string} message1.id The ID of the new message
* @param {string} message1.actorType Actor type of the new message
* @param {string} message1.actorId Actor id of the new message
* @param {string} message1.systemMessage System message of the new message
* @param {number} message1.timestamp Timestamp of the new message
* @param {null|object} message2 The previous message
* @param {string} message2.id The ID of the second message
* @param {string} message2.actorType Actor type of the previous message
* @param {string} message2.actorId Actor id of the previous message
* @param {string} message2.systemMessage System message of the second message
* @param {number} message2.timestamp Timestamp of the second message
* @return {string} Type of grouping if the messages should be grouped
*/
messagesShouldBeGrouped(message1, message2) {
if (!message2) {
return '' // No previous message
}
// Group users added by one actor
if (message1.systemMessage === 'user_added'
&& message1.systemMessage === message2.systemMessage
&& message1.actorId === message2.actorId
&& message1.actorType === message2.actorType) {
return 'user_added'
}
// Group users removed by one actor
if (message1.systemMessage === 'user_removed'
&& message1.systemMessage === message2.systemMessage
&& message1.actorId === message2.actorId
&& message1.actorType === message2.actorType) {
return 'user_removed'
}
// Group users reconnected in a minute
if (message1.systemMessage === 'call_joined'
&& message2.systemMessage === 'call_left'
&& message1.timestamp - message2.timestamp < 60 * 1000
&& message1.actorId === message2.actorId
&& message1.actorType === message2.actorType) {
return 'call_reconnected'
}
// Group users joined call one by one
if (message1.systemMessage === 'call_joined'
&& message1.systemMessage === message2.systemMessage) {
return 'call_joined'
}
// Group users left call one by one
if (message1.systemMessage === 'call_left'
&& message1.systemMessage === message2.systemMessage) {
return 'call_left'
}
// Group users promoted one by one
if ((message1.systemMessage === 'moderator_promoted' || message1.systemMessage === 'guest_moderator_promoted')
&& (message2.systemMessage === 'moderator_promoted' || message2.systemMessage === 'guest_moderator_promoted')) {
return 'moderator_promoted'
}
// Group users demoted one by one
if ((message1.systemMessage === 'moderator_demoted' || message1.systemMessage === 'guest_moderator_demoted')
&& (message2.systemMessage === 'moderator_demoted' || message2.systemMessage === 'guest_moderator_demoted')) {
return 'moderator_demoted'
}
return ''
},
updateCollapsedState() {
for (const group of this.messagesGroupedBySystemMessage) {
const isLastReadInsideGroup = this.lastReadMessageId >= group.id && this.lastReadMessageId < group.lastId
if (isLastReadInsideGroup) {
// If the last read message is inside the group, we should show the group expanded
group.collapsed = false
} else if (this.groupIsCollapsed[group.id] !== undefined) {
// If the group was collapsed before, we should keep it collapsed
group.collapsed = this.groupIsCollapsed[group.id]
} else {
// If the group is not collapsed, we should collapse it if it contains more than one message
group.collapsed = group.messages.length > 1
}
}
},
groupMessages(messages) {
const groups = []
let lastMessage = null
let forceNextGroup = false
for (const message of messages) {
const groupingType = this.messagesShouldBeGrouped(message, lastMessage)
if (!groupingType || forceNextGroup) {
// Adding a new group
groups.push({ id: message.id, lastId: message.id, messages: [message], type: '', collapsed: undefined })
forceNextGroup = false
} else {
// Adding a message to the existing group
if (groupingType === 'call_reconnected') {
groups.push({ id: message.id, lastId: message.id, messages: [groups.at(-1).messages.pop()], type: '', collapsed: undefined })
groups.at(-1).lastId = groups.at(-1).messages.at(-1).id
forceNextGroup = true
}
groups.at(-1).messages.push(message)
groups.at(-1).lastId = message.id
groups.at(-1).type = groupingType
}
lastMessage = message
}
return groups
},
toggleCollapsed(group) {
this.$set(group, 'collapsed', !group.collapsed)
this.groupIsCollapsed[group.id] = group.collapsed
},
getNextMessageId(message) {
const nextMessage = this.messages[this.messages.findIndex(searchedMessage => searchedMessage.id === message.id) + 1]
return nextMessage?.id || this.nextMessageId
},
getPrevMessageId(message) {
const prevMessage = this.messages[this.messages.findIndex(searchedMessage => searchedMessage.id === message.id) - 1]
return prevMessage?.id || this.previousMessageId
},
},
}
</script>
<style lang="scss" scoped>
@import '../../../assets/variables';
.messages-group__system {
display: flex;
flex-direction: column;
}
.wrapper {
max-width: $messages-list-max-width;
display: flex;
margin: auto;
padding: 0;
&--system {
flex-direction: column;
padding-left: calc(var(--default-clickable-area) + 8px);
}
&:focus {
background-color: rgba(47, 47, 47, 0.068);
}
}
.messages {
flex: auto;
display: flex;
flex-direction: column;
width: 100%;
min-width: 0;
&--header {
}
&--collapsed {
border-radius: var(--border-radius-large);
background-color: var(--color-background-hover);
}
}
</style>
|