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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
<!--
- @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
-
- @author John Molakvoæ <skjnldsv@protonmail.com>
- @author Richard Steinmetz <richard@steinmetz.cloud>
-
- @license GNU AGPL version 3 or any later version
-
- 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>
<div v-observe-visibility="onVisibilityChange"
class="comments"
:class="{ 'icon-loading': isFirstLoading }">
<!-- Editor -->
<Comment v-bind="editorData"
:auto-complete="autoComplete"
:user-data="userData"
:editor="true"
:ressource-id="ressourceId"
class="comments__writer"
@new="onNewComment" />
<template v-if="!isFirstLoading">
<NcEmptyContent v-if="!hasComments && done"
class="comments__empty"
:name="t('comments', 'No comments yet, start the conversation!')">
<template #icon>
<MessageReplyTextIcon />
</template>
</NcEmptyContent>
<ul v-else>
<!-- Comments -->
<Comment v-for="comment in comments"
:key="comment.props.id"
tag="li"
v-bind="comment.props"
:auto-complete="autoComplete"
:message.sync="comment.props.message"
:ressource-id="ressourceId"
:user-data="genMentionsData(comment.props.mentions)"
class="comments__list"
@delete="onDelete" />
</ul>
<!-- Loading more message -->
<div v-if="loading && !isFirstLoading" class="comments__info icon-loading" />
<div v-else-if="hasComments && done" class="comments__info">
{{ t('comments', 'No more messages') }}
</div>
<!-- Error message -->
<template v-else-if="error">
<NcEmptyContent class="comments__error" :name="error">
<template #icon>
<AlertCircleOutlineIcon />
</template>
</NcEmptyContent>
<NcButton class="comments__retry" @click="getComments">
<template #icon>
<RefreshIcon />
</template>
{{ t('comments', 'Retry') }}
</NcButton>
</template>
</template>
</div>
</template>
<script>
import { generateOcsUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { loadState } from '@nextcloud/initial-state'
import { showError } from '@nextcloud/dialogs'
import axios from '@nextcloud/axios'
import VTooltip from 'v-tooltip'
import Vue from 'vue'
import VueObserveVisibility from 'vue-observe-visibility'
import NcEmptyContent from '@nextcloud/vue/dist/Components/NcEmptyContent.js'
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
import RefreshIcon from 'vue-material-design-icons/Refresh.vue'
import MessageReplyTextIcon from 'vue-material-design-icons/MessageReplyText.vue'
import AlertCircleOutlineIcon from 'vue-material-design-icons/AlertCircleOutline.vue'
import Comment from '../components/Comment.vue'
import { getComments, DEFAULT_LIMIT } from '../services/GetComments.ts'
import cancelableRequest from '../utils/cancelableRequest.js'
import { markCommentsAsRead } from '../services/ReadComments.ts'
Vue.use(VTooltip)
Vue.use(VueObserveVisibility)
export default {
name: 'Comments',
components: {
// Avatar,
Comment,
NcEmptyContent,
NcButton,
RefreshIcon,
MessageReplyTextIcon,
AlertCircleOutlineIcon,
},
data() {
return {
error: '',
loading: false,
done: false,
ressourceId: null,
offset: 0,
comments: [],
cancelRequest: () => {},
editorData: {
actorDisplayName: getCurrentUser().displayName,
actorId: getCurrentUser().uid,
key: 'editor',
},
Comment,
userData: {},
}
},
computed: {
hasComments() {
return this.comments.length > 0
},
isFirstLoading() {
return this.loading && this.offset === 0
},
},
methods: {
async onVisibilityChange(isVisible) {
if (isVisible) {
try {
await markCommentsAsRead(this.commentsType, this.ressourceId, new Date())
} catch (e) {
showError(e.message || t('comments', 'Failed to mark comments as read'))
}
}
},
/**
* Update current ressourceId and fetch new data
*
* @param {number} ressourceId the current ressourceId (fileId...)
*/
async update(ressourceId) {
this.ressourceId = ressourceId
this.resetState()
this.getComments()
},
/**
* Ran when the bottom of the tab is reached
*/
onScrollBottomReached() {
/**
* Do not fetch more if we:
* - are showing an error
* - already fetched everything
* - are currently loading
*/
if (this.error || this.done || this.loading) {
return
}
this.getComments()
},
/**
* Make sure we have all mentions as Array of objects
*
* @param {any[]} mentions the mentions list
* @return {Record<string, object>}
*/
genMentionsData(mentions) {
Object.values(mentions)
.flat()
.forEach(mention => {
this.userData[mention.mentionId] = {
// TODO: support groups
icon: 'icon-user',
id: mention.mentionId,
label: mention.mentionDisplayName,
source: 'users',
primary: getCurrentUser().uid === mention.mentionId,
}
})
return this.userData
},
/**
* Get the existing shares infos
*/
async getComments() {
// Cancel any ongoing request
this.cancelRequest('cancel')
try {
this.loading = true
this.error = ''
// Init cancellable request
const { request, abort } = cancelableRequest(getComments)
this.cancelRequest = abort
// Fetch comments
const { data: comments } = await request({
commentsType: this.commentsType,
ressourceId: this.ressourceId,
}, { offset: this.offset }) || { data: [] }
this.logger.debug(`Processed ${comments.length} comments`, { comments })
// We received less than the requested amount,
// we're done fetching comments
if (comments.length < DEFAULT_LIMIT) {
this.done = true
}
// Insert results
this.comments.push(...comments)
// Increase offset for next fetch
this.offset += DEFAULT_LIMIT
} catch (error) {
if (error.message === 'cancel') {
return
}
this.error = t('comments', 'Unable to load the comments list')
console.error('Error loading the comments list', error)
} finally {
this.loading = false
}
},
/**
* Autocomplete @mentions
*
* @param {string} search the query
* @param {Function} callback the callback to process the results with
*/
async autoComplete(search, callback) {
const results = await axios.get(generateOcsUrl('core/autocomplete/get'), {
params: {
search,
itemType: 'files',
itemId: this.ressourceId,
sorter: 'commenters|share-recipients',
limit: loadState('comments', 'maxAutoCompleteResults'),
},
})
// Save user data so it can be used by the editor to replace mentions
results.data.ocs.data.forEach(user => { this.userData[user.id] = user })
return callback(Object.values(this.userData))
},
/**
* Add newly created comment to the list
*
* @param {object} comment the new comment
*/
onNewComment(comment) {
this.comments.unshift(comment)
},
/**
* Remove deleted comment from the list
*
* @param {number} id the deleted comment
*/
onDelete(id) {
const index = this.comments.findIndex(comment => comment.props.id === id)
if (index > -1) {
this.comments.splice(index, 1)
} else {
console.error('Could not find the deleted comment in the list', id)
}
},
/**
* Reset the current view to its default state
*/
resetState() {
this.error = ''
this.loading = false
this.done = false
this.offset = 0
this.comments = []
},
},
}
</script>
<style lang="scss" scoped>
.comments {
// Do not add emptycontent top margin
&__empty,
&__error {
margin-top: 0 !important;
}
&__retry {
margin: 0 auto;
}
&__info {
height: 60px;
color: var(--color-text-maxcontrast);
text-align: center;
line-height: 60px;
}
}
</style>
|