-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathNotificationItem.vue
More file actions
371 lines (337 loc) Β· 10.4 KB
/
Copy pathNotificationItem.vue
File metadata and controls
371 lines (337 loc) Β· 10.4 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<!--
- SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<li
class="notification"
:data-id="notification.notificationId"
:data-timestamp="timestamp"
:data-object-type="notification.objectType"
:data-app="notification.app">
<div class="notification-heading">
<NcDateTime
v-if="timestamp"
class="notification-time"
ignoreSeconds
:format="{ timeStyle: 'short', dateStyle: 'long' }"
:timestamp="timestamp" />
<NcButton
v-if="timestamp"
class="notification-dismiss-button"
variant="tertiary"
:aria-label="t('notifications', 'Dismiss')"
@click="onDismissNotification">
<template #icon>
<IconClose :size="20" />
</template>
</NcButton>
</div>
<a
v-if="notification.externalLink"
:href="notification.externalLink"
class="notification-subject full-subject-link external"
target="_blank"
rel="noreferrer noopener">
<span v-if="notification.icon" class="image"><img :src="notification.icon" class="notification-icon" alt=""></span>
<span class="subject">{{ notification.subject }} β</span>
</a>
<a v-else-if="useLink" :href="notification.link" class="notification-subject full-subject-link">
<span v-if="notification.icon" class="image"><img :src="notification.icon" class="notification-icon" alt=""></span>
<NcRichText
v-if="notification.subjectRich"
:text="notification.subjectRich"
:arguments="preparedSubjectParameters" />
<span v-else class="subject">{{ notification.subject }}</span>
</a>
<div v-else class="notification-subject">
<span v-if="notification.icon" class="image"><img :src="notification.icon" class="notification-icon" alt=""></span>
<NcRichText
v-if="notification.subjectRich"
:text="notification.subjectRich"
:arguments="preparedSubjectParameters" />
<span v-else class="subject">{{ notification.subject }}</span>
</div>
<div v-if="notification.message" class="notification-message" @click="onClickMessage">
<div class="message-container" :class="{ collapsed: isCollapsedMessage }">
<NcRichText
v-if="notification.messageRich"
:text="notification.messageRich"
:arguments="preparedMessageParameters"
:autolink="true" />
<span v-else>{{ notification.message }}</span>
</div>
<div v-if="isCollapsedMessage" class="notification-overflow" />
</div>
<div v-if="notification.actions.length" class="notification-actions">
<ActionButton
v-for="(action, i) in notification.actions"
:key="i"
:action="action"
@click="onClickAction"
@remove="$emit('remove')" />
</div>
<div v-else-if="notification.externalLink" class="notification-actions">
<NcButton
variant="primary"
href="https://nextcloud.com/fairusepolicy"
class="action-button pull-right"
target="_blank"
rel="noreferrer noopener">
<template #icon>
<IconMessageOutline :size="20" />
</template>
{{ t('notifications', 'Contact Nextcloud GmbH') }} β
</NcButton>
</div>
<!-- Inline reply for Nextcloud Talk conversations -->
<div v-if="isTalkChat" class="notification-talk-reply">
<div v-if="showReply" class="notification-talk-reply__form">
<textarea
v-model="replyText"
class="notification-talk-reply__input"
:placeholder="t('notifications', 'Write a replyβ¦')"
rows="2"
autofocus
@keydown.ctrl.enter.prevent="sendReply"
@keydown.meta.enter.prevent="sendReply" />
<div class="notification-talk-reply__buttons">
<NcButton variant="tertiary" @click="showReply = false; replyText = ''">
{{ t('notifications', 'Cancel') }}
</NcButton>
<NcButton
variant="primary"
:disabled="!replyText.trim() || sendingReply"
@click="sendReply">
{{ t('notifications', 'Send') }}
</NcButton>
</div>
</div>
<NcButton
v-else
variant="tertiary"
class="notification-talk-reply__toggle"
@click="showReply = true">
<template #icon>
<IconReply :size="16" />
</template>
{{ t('notifications', 'Reply') }}
</NcButton>
</div>
</li>
</template>
<script>
import axios from '@nextcloud/axios'
import { showError } from '@nextcloud/dialogs'
import { emit } from '@nextcloud/event-bus'
import { t } from '@nextcloud/l10n'
import { generateOcsUrl } from '@nextcloud/router'
import NcButton from '@nextcloud/vue/components/NcButton'
import NcDateTime from '@nextcloud/vue/components/NcDateTime'
import NcRichText from '@nextcloud/vue/components/NcRichText'
import IconClose from 'vue-material-design-icons/Close.vue'
import IconMessageOutline from 'vue-material-design-icons/MessageOutline.vue'
import IconReply from 'vue-material-design-icons/Reply.vue'
import ActionButton from './ActionButton.vue'
import DefaultParameter from './Parameters/DefaultParameter.vue'
import FileParameter from './Parameters/FileParameter.vue'
import UserParameter from './Parameters/UserParameter.vue'
/**
* @typedef {object} NotificationItem
* @property {number} notification_id notification id (required)
* @property {string} app app id (required)
* @property {string} user user id (required)
* @property {string} datetime timestamp of notification (required)
* @property {string} object_type object type, e.g. 'room' (required)
* @property {string} object_id object id, e.g. room token (required)
* @property {string} subject notification subject (required)
* @property {string} message notification message (required)
* @property {string} link notification link (required)
* @property {NotificationAction[]} actions notification list of actions (required)
* @property {string} [subjectRich] notification subject with rich parameters
* @property {object} [subjectRichParameters] rich parameters for notification subject
* @property {string} [messageRich] notification message with rich parameters
* @property {object} [messageRichParameters] rich parameters for notification message
* @property {string} [icon] icon to render
* @property {boolean} [shouldNotify] whether a browser notification should be rendered
*/
/**
* @typedef {object} NotificationAction
* @property {string} label action label (required)
* @property {string} link action link (required)
* @property {string} type action type (required)
* @property {boolean} primary action primary (required)
*/
export default {
name: 'NotificationItem',
components: {
ActionButton,
IconClose,
IconMessageOutline,
IconReply,
NcButton,
NcDateTime,
NcRichText,
},
props: {
notification: {
/** @type {import('vue').PropType<NotificationItem>} */
type: Object,
required: true,
},
},
emits: ['remove'],
data() {
return {
showFullMessage: false,
showReply: false,
replyText: '',
sendingReply: false,
}
},
computed: {
timestamp() {
if (this.notification.datetime === 'warning') {
return 0
}
return (new Date(this.notification.datetime)).valueOf()
},
useLink() {
if (!this.notification.link) {
return false
}
let parametersHaveLink = false
Object.keys(Object(this.notification.subjectRichParameters)).forEach((p) => {
if (Object(this.notification.subjectRichParameters)[p].link) {
parametersHaveLink = true
}
})
return !parametersHaveLink
},
preparedSubjectParameters() {
return this.prepareParameters(this.notification.subjectRichParameters)
},
preparedMessageParameters() {
return this.prepareParameters(this.notification.messageRichParameters)
},
isCollapsedMessage() {
return this.notification.message.length > 200 && !this.showFullMessage
},
isTalkChat() {
return this.notification.app === 'spreed'
&& this.notification.objectType !== 'call'
&& !!this.notification.objectId
},
},
methods: {
t,
prepareParameters(parameters = {}) {
const richParameters = {}
Object.keys(parameters).forEach((p) => {
const type = parameters[p].type
if (type === 'user') {
richParameters[p] = {
component: UserParameter,
props: parameters[p],
}
} else if (type === 'file') {
richParameters[p] = {
component: FileParameter,
props: parameters[p],
}
} else {
richParameters[p] = {
component: DefaultParameter,
props: parameters[p],
}
}
})
return richParameters
},
onClickMessage(e) {
if (e.target.closest('.rich-text--wrapper')) {
// Vue RichText
this.showFullMessage = !this.showFullMessage
} else if (!this.notification.messageRich && !!this.notification.message) {
// Plain text
this.showFullMessage = !this.showFullMessage
}
},
async onClickAction({ event, action }) {
try {
const executeEvent = {
cancelAction: false,
notification: this.notification,
action,
}
await emit('notifications:action:execute', executeEvent)
if (action.type === 'WEB') {
if (executeEvent.cancelAction) {
event.preventDefault()
}
return
}
if (executeEvent.cancelAction) {
return
}
// execute action
await axios({
method: action.type,
url: action.url,
})
// emit event to current app
this.$emit('remove')
emit('notifications:action:executed', event)
} catch (error) {
console.error('Failed to perform action', error)
showError(t('notifications', 'Failed to perform action'))
}
},
async sendReply() {
if (!this.replyText.trim() || this.sendingReply) return
this.sendingReply = true
try {
await axios.post(
generateOcsUrl('apps/spreed/api/v1/chat/{token}', { token: this.notification.objectId }),
{ message: this.replyText.trim() },
)
this.$emit('remove')
} catch {
showError(t('notifications', 'Failed to send reply'))
} finally {
this.sendingReply = false
}
},
onDismissNotification() {
axios
.delete(generateOcsUrl('apps/notifications/api/v2/notifications/{id}', { id: this.notification.notificationId }))
.then(() => {
this.$emit('remove')
})
.catch(() => {
showError(t('notifications', 'Failed to dismiss notification'))
})
},
},
}
</script>
<style lang="scss" scoped>
.notification {
:deep(img.notification-icon) {
display: flex;
width: 32px;
height: 32px;
filter: var(--background-invert-if-dark);
}
:deep(.rich-text--wrapper) {
white-space: pre-wrap;
overflow-wrap: break-word;
}
.notification-subject {
padding: 4px;
}
a.notification-subject:focus-visible {
box-shadow: inset 0 0 0 2px var(--color-main-text) !important; // override rule in core/css/headers.scss #header a:focus-visible
}
}
</style>