-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathprincipal.js
More file actions
163 lines (152 loc) Β· 5.43 KB
/
principal.js
File metadata and controls
163 lines (152 loc) Β· 5.43 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
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import {
PRINCIPAL_PREFIX_CALENDAR_RESOURCE,
PRINCIPAL_PREFIX_CALENDAR_ROOM,
PRINCIPAL_PREFIX_CIRCLE,
PRINCIPAL_PREFIX_GROUP,
PRINCIPAL_PREFIX_USER,
} from './consts.js'
/**
* Creates a complete principal-object based on given props
*
* @param {object} props Principal-props already provided
* @return {any}
*/
function getDefaultPrincipalObject(props) {
return { // Id of the principal
id: null,
// Calendar-user-type. This can be INDIVIDUAL, GROUP, RESOURCE or ROOM
calendarUserType: 'INDIVIDUAL',
// E-Mail address of principal used for scheduling
emailAddress: null,
// The principals display-name
// TODO: this should be renamed to displayName
displayname: null,
// principalScheme
principalScheme: null,
// The internal user-id in case it is of type INDIVIDUAL and a user
// TODO: userId is deprecrated, use principalId instead
userId: null,
// url to the DAV-principal-resource
url: null,
// The cdav-library object
dav: null,
// Whether or not this principal represents a circle
isCircle: false,
// Whether or not this principal represents a user
isUser: false,
// Whether or not this principal represents a group
isGroup: false,
// Whether or not this principal represents a calendar-resource
isCalendarResource: false,
// Whether or not this principal represents a calendar-room
isCalendarRoom: false,
// The id of the principal without prefix. e.g. userId / groupId / etc.
principalId: null,
// The url of the default calendar for invitations
scheduleDefaultCalendarUrl: null,
// Room-specific properties (only for calendar-rooms)
roomSeatingCapacity: null,
roomType: null,
roomAddress: null,
roomFeatures: null,
roomBuildingName: null,
roomBuildingAddress: null,
roomNumber: null,
...props,
}
}
/**
* converts a dav principal into a vuex object
*
* @param {object} dav cdav-library Principal object
* @return {object}
*/
function mapDavToPrincipal(dav) {
const id = btoa(encodeURI(dav.url))
const calendarUserType = dav.calendarUserType
const principalScheme = dav.principalScheme
const emailAddress = dav.email
const displayname = dav.displayname
const scheduleDefaultCalendarUrl = dav.scheduleDefaultCalendarUrl
const isUser = dav.principalScheme.startsWith(PRINCIPAL_PREFIX_USER)
const isGroup = dav.principalScheme.startsWith(PRINCIPAL_PREFIX_GROUP)
const isCircle = dav.principalScheme.startsWith(PRINCIPAL_PREFIX_CIRCLE)
const isCalendarResource = dav.principalScheme.startsWith(PRINCIPAL_PREFIX_CALENDAR_RESOURCE)
const isCalendarRoom = dav.principalScheme.startsWith(PRINCIPAL_PREFIX_CALENDAR_ROOM)
let principalId = null
if (isUser) {
principalId = dav.principalScheme.substring(PRINCIPAL_PREFIX_USER.length)
} else if (isGroup) {
principalId = dav.principalScheme.substring(PRINCIPAL_PREFIX_GROUP.length)
} else if (isCircle) {
principalId = dav.principalScheme.substring(PRINCIPAL_PREFIX_CIRCLE.length)
} else if (isCalendarResource) {
principalId = dav.principalScheme.substring(PRINCIPAL_PREFIX_CALENDAR_RESOURCE.length)
} else if (isCalendarRoom) {
principalId = dav.principalScheme.substring(PRINCIPAL_PREFIX_CALENDAR_ROOM.length)
}
const url = dav.principalUrl
const userId = dav.userId
// Extract room-specific properties from DAV object, trimming string values defensively
const roomSeatingCapacity = dav.roomSeatingCapacity ?? null
const roomType = (dav.roomType ?? '').toString().trim() || null
const roomFeatures = (dav.roomFeatures ?? '').toString().trim() || null
// Strip leading/trailing whitespace and commas from building address to handle empty
// building-name fields, e.g. ", Science Park 140, 1098 XG, Amsterdam" β "Science Park 140, 1098 XG, Amsterdam"
const rawBuildingAddress = dav.roomBuildingAddress ?? null
const roomBuildingAddress = rawBuildingAddress
? rawBuildingAddress.replace(/^[\s,]+|[\s,]+$/g, '').trim() || null
: null
// Derive building name from address (everything before first comma): "Poppodium, Kerkstraat 10" β "Poppodium"
const roomBuildingName = roomBuildingAddress ? roomBuildingAddress.split(',')[0].trim() || null : null
// Room number (floor.room format, e.g. "2.17") is stored in room-building-room-number
const roomNumber = (dav.roomBuildingRoomNumber ?? '').toString().trim() || null
// Construct roomAddress for event LOCATION field from available properties
// Format: "Street (Building, Room X.XX)" β street-first for map/navigation apps
let roomAddress = null
if (roomBuildingAddress) {
const commaIdx = roomBuildingAddress.indexOf(',')
if (commaIdx > 0) {
const building = roomBuildingAddress.substring(0, commaIdx).trim()
const street = roomBuildingAddress.substring(commaIdx + 1).trim()
const detail = roomNumber ? building + ', Room ' + roomNumber : building
roomAddress = street + ' (' + detail + ')'
} else {
roomAddress = roomNumber
? roomBuildingAddress + ' (Room ' + roomNumber + ')'
: roomBuildingAddress
}
}
return getDefaultPrincipalObject({
id,
calendarUserType,
principalScheme,
emailAddress,
displayname,
url,
dav,
isUser,
isGroup,
isCircle,
isCalendarResource,
isCalendarRoom,
principalId,
userId,
scheduleDefaultCalendarUrl,
roomSeatingCapacity,
roomType,
roomAddress,
roomFeatures,
roomBuildingName,
roomBuildingAddress,
roomNumber,
})
}
export {
getDefaultPrincipalObject,
mapDavToPrincipal,
}