-
Notifications
You must be signed in to change notification settings - Fork 241
/
InviteesListSearch.vue
303 lines (279 loc) Β· 7.82 KB
/
InviteesListSearch.vue
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
<!--
- SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<NcSelect class="invitees-search__multiselect"
:options="matches"
:searchable="true"
:max-height="600"
:placeholder="placeholder"
:class="{ 'showContent': inputGiven, 'icon-loading': isLoading }"
:clearable="false"
:label-outside="true"
input-id="uid"
label="dropdownName"
@search="findAttendees"
@option:selected="addAttendee">
<template #option="option">
<div class="invitees-search-list-item">
<!-- We need to specify a unique key here for the avatar to be reactive. -->
<Avatar v-if="option.isUser"
:key="option.uid"
:user="option.avatar"
:display-name="option.dropdownName" />
<Avatar v-else-if="option.type === 'circle'">
<template #icon>
<GoogleCirclesCommunitiesIcon :size="20" />
</template>
</Avatar>
<Avatar v-if="!option.isUser && option.type !== 'circle'"
:key="option.uid"
:url="option.avatar"
:display-name="option.commonName" />
<div class="invitees-search-list-item__label">
<div>
{{ option.commonName }}
</div>
<div v-if="option.email !== option.commonName && option.type !== 'circle'">
{{ option.email }}
</div>
<div v-if="option.type === 'circle'">
{{ option.subtitle }}
</div>
</div>
</div>
</template>
</NcSelect>
</template>
<script>
import {
NcAvatar as Avatar,
NcSelect,
} from '@nextcloud/vue'
import { principalPropertySearchByDisplaynameOrEmail } from '../../../services/caldavService.js'
import isCirclesEnabled from '../../../services/isCirclesEnabled.js'
import {
circleSearchByName,
circleGetMembers,
} from '../../../services/circleService.js'
import HttpClient from '@nextcloud/axios'
import debounce from 'debounce'
import { linkTo } from '@nextcloud/router'
import { randomId } from '../../../utils/randomId.js'
import GoogleCirclesCommunitiesIcon from 'vue-material-design-icons/GoogleCirclesCommunities.vue'
import { showInfo } from '@nextcloud/dialogs'
export default {
name: 'InviteesListSearch',
components: {
Avatar,
NcSelect,
GoogleCirclesCommunitiesIcon,
},
props: {
alreadyInvitedEmails: {
type: Array,
required: true,
},
organizer: {
type: Object,
required: false,
},
},
data() {
return {
isLoading: false,
inputGiven: false,
matches: [],
isCirclesEnabled,
}
},
computed: {
placeholder() {
return this.$t('calendar', 'Search for emails, users, contacts, teams or groups')
},
noResult() {
return this.$t('calendar', 'No match found')
},
},
methods: {
findAttendees: debounce(async function(query) {
this.isLoading = true
const matches = []
if (query.length > 0) {
const promises = [
this.findAttendeesFromContactsAPI(query),
this.findAttendeesFromDAV(query),
]
if (isCirclesEnabled) {
promises.push(this.findAttendeesFromCircles(query))
}
const [contactsResults, davResults, circleResults] = await Promise.all(promises)
matches.push(...contactsResults)
matches.push(...davResults)
if (isCirclesEnabled) {
matches.push(...circleResults)
}
// Source of the Regex: https://stackoverflow.com/a/46181
// eslint-disable-next-line
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
if (emailRegex.test(query)) {
const alreadyInList = matches.find((attendee) => attendee.email.toLowerCase() === query.toLowerCase())
if (!alreadyInList) {
matches.unshift({
calendarUserType: 'INDIVIDUAL',
commonName: query,
email: query,
isUser: false,
avatar: null,
language: null,
timezoneId: null,
hasMultipleEMails: false,
dropdownName: query,
})
}
}
// Generate a unique id for every result to make the avatar components reactive
for (const match of matches) {
match.uid = randomId()
}
this.isLoading = false
this.inputGiven = true
} else {
this.inputGiven = false
this.isLoading = false
}
this.matches = matches
}, 500),
addAttendee(selectedValue) {
if (selectedValue.type === 'circle') {
showInfo(this.$t('calendar', 'Note that members of circles get invited but are not synced yet.'))
this.resolveCircleMembers(selectedValue.id, selectedValue.email)
}
this.$emit('add-attendee', selectedValue)
},
async resolveCircleMembers(circleId, groupId) {
let results
try {
// Going to query custom backend to fetch Circle members since we're going to use
// mail addresses of local circle members. The Circles API doesn't expose member
// emails yet. Change approved by @miaulalala and @ChristophWurst.
results = await circleGetMembers(circleId)
} catch (error) {
console.debug(error)
return []
}
results.data.forEach((member) => {
if (!this.organizer || member.email !== this.organizer.uri) {
this.$emit('add-attendee', member)
}
})
},
async findAttendeesFromContactsAPI(query) {
let response
try {
response = await HttpClient.post(linkTo('calendar', 'index.php') + '/v1/autocompletion/attendee', {
search: query,
})
} catch (error) {
console.debug(error)
return []
}
const data = response.data
return data.reduce((arr, result) => {
const hasMultipleEMails = result.emails.length > 1
result.emails.forEach((email) => {
let name
if (result.name && !hasMultipleEMails) {
name = result.name
} else if (result.name && hasMultipleEMails) {
name = `${result.name} (${email})`
} else {
name = email
}
if (email && this.alreadyInvitedEmails.includes(email)) {
return
}
arr.push({
calendarUserType: 'INDIVIDUAL',
commonName: result.name,
email,
isUser: false,
avatar: result.photo,
language: result.lang,
timezoneId: result.tzid,
hasMultipleEMails,
dropdownName: name,
})
})
return arr
}, [])
},
async findAttendeesFromDAV(query) {
let results
try {
results = await principalPropertySearchByDisplaynameOrEmail(query)
} catch (error) {
console.debug(error)
return []
}
return results.filter((principal) => {
if (!principal.email) {
return false
}
if (this.alreadyInvitedEmails.includes(principal.email)) {
return false
}
// We do not support GROUPS for now
if (principal.calendarUserType === 'GROUP') {
return false
}
// Do not include resources and rooms
if (['ROOM', 'RESOURCE'].includes(principal.calendarUserType)) {
return false
}
return true
}).map((principal) => {
return {
commonName: principal.displayname,
calendarUserType: principal.calendarUserType,
email: principal.email,
language: principal.language,
isUser: principal.calendarUserType === 'INDIVIDUAL',
avatar: decodeURIComponent(principal.userId),
hasMultipleEMails: false,
dropdownName: principal.displayname ? [principal.displayname, principal.email].join(' ') : principal.email,
}
})
},
async findAttendeesFromCircles(query) {
let results
try {
results = await circleSearchByName(query)
} catch (error) {
console.debug(error)
return []
}
return results.filter((circle) => {
return true
}).map((circle) => {
return {
commonName: circle.displayname,
calendarUserType: 'GROUP',
email: 'circle+' + circle.id + '@' + circle.instance,
isUser: false,
dropdownName: circle.displayname,
type: 'circle',
id: circle.id,
subtitle: this.$n('calendar', '%n member', '%n members', circle.population),
}
})
},
},
}
</script>
<style scoped>
:deep(.avatardiv) {
overflow: visible !important;
}
</style>