/
utils.ts
236 lines (226 loc) · 6.7 KB
/
utils.ts
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
import { IGroup, IItem } from "@esri/arcgis-rest-types";
import { IHubContent, IHubItemEntity } from "../core";
import { CANNOT_DISCUSS } from "./constants";
import {
AclCategory,
AclSubCategory,
IChannel,
SharingAccess,
} from "./api/types";
import { IFilter, IHubSearchResult, IPredicate, IQuery } from "../search";
/**
* Utility to determine if a given IGroup, IItem, IHubContent, or IHubItemEntity
* is discussable.
* @param {IGroup|IItem|IHubContent|IHubItemEntity} subject
* @return {boolean}
*/
export function isDiscussable(
subject: Partial<IGroup | IItem | IHubContent | IHubItemEntity>
) {
let result = false;
if (subject) {
const typeKeywords = subject.typeKeywords || [];
result = !typeKeywords.includes(CANNOT_DISCUSS);
}
return result;
}
/**
* Adds or removes CANNOT_DISCUSS type keyword and returns the updated list
* @param {IGroup|IHubContent|IHubItemEntity} subject
* @param {boolean} discussable
* @returns {string[]} updated list of type keywords
*/
export function setDiscussableKeyword(
typeKeywords: string[],
discussable: boolean
): string[] {
const updatedTypeKeywords = (typeKeywords || []).filter(
(typeKeyword: string) => typeKeyword !== CANNOT_DISCUSS
);
if (!discussable) {
updatedTypeKeywords.push(CANNOT_DISCUSS);
}
return updatedTypeKeywords;
}
/**
* Determines if the given channel is considered to be a `public` channel, supporting both
* legacy permissions and V2 ACL model.
* @param channel An IChannel record
* @returns true if the channel is considered `public`
*/
export function isPublicChannel(channel: IChannel): boolean {
return channel.channelAcl
? channel.channelAcl.some(
({ category }) => category === AclCategory.AUTHENTICATED_USER
)
: channel.access === SharingAccess.PUBLIC;
}
/**
* Determines if the given channel is considered to be an `org` channel, supporting both
* legacy permissions and V2 ACL model.
* @param channel An IChannel record
* @returns true if the channel is considered `org`
*/
export function isOrgChannel(channel: IChannel): boolean {
return channel.channelAcl
? !isPublicChannel(channel) &&
channel.channelAcl.some(
({ category, subCategory }) =>
category === AclCategory.ORG &&
subCategory === AclSubCategory.MEMBER
)
: channel.access === SharingAccess.ORG;
}
/**
* Determines if the given channel is considered to be a `private` channel, supporting both
* legacy permissions and V2 ACL model.
* @param channel An IChannel record
* @returns true if the channel is considered `private`
*/
export function isPrivateChannel(channel: IChannel): boolean {
return !isPublicChannel(channel) && !isOrgChannel(channel);
}
/**
* Determines the given channel's access, supporting both legacy permissions and V2 ACL
* model.
* @param channel An IChannel record
* @returns `public`, `org` or `private`
*/
export function getChannelAccess(channel: IChannel): SharingAccess {
let access = SharingAccess.PRIVATE;
if (isPublicChannel(channel)) {
access = SharingAccess.PUBLIC;
} else if (isOrgChannel(channel)) {
access = SharingAccess.ORG;
}
return access;
}
/**
* Returns an array of org ids configured for the channel, supporting both legacy permissions
* and V2 ACL model.
* @param channel An IChannel record
* @returns an array of org ids for the given channel
*/
export function getChannelOrgIds(channel: IChannel): string[] {
return channel.channelAcl
? channel.channelAcl.reduce(
(acc, permission) =>
permission.category === AclCategory.ORG &&
permission.subCategory === AclSubCategory.MEMBER
? [...acc, permission.key]
: acc,
[]
)
: channel.orgs;
}
/**
* Returns an array of group ids configured for the channel, supporting both legacy permissions
* and V2 ACL model.
* @param channel An IChannel record
* @returns an array of group ids for the given channel
*/
export function getChannelGroupIds(channel: IChannel): string[] {
return channel.channelAcl
? channel.channelAcl.reduce(
(acc, permission) =>
permission.category === AclCategory.GROUP &&
permission.subCategory === AclSubCategory.MEMBER
? [...acc, permission.key]
: acc,
[]
)
: channel.groups;
}
/**
* A utility method used to build an IQuery to search for users that are permitted to be at-mentioned for the given channel.
* @param input An array of strings to search for. Each string is mapped to `username` and `fullname`, filters as an OR condition
* @param channel An IChannel record
* @param currentUsername The currently authenticated user's username
* @param options An IHubSearchOptions object
* @returns a promise that resolves an IHubSearchResponse<IHubSearchResult>
*/
export function getChannelUsersQuery(
inputs: string[],
channel: IChannel,
currentUsername?: string
): IQuery {
const groupIds = getChannelGroupIds(channel);
const orgIds = getChannelOrgIds(channel);
const groupsPredicate = { group: groupIds };
let filters: IFilter[];
if (isPublicChannel(channel)) {
filters = [
{
operation: "OR",
predicates: [{ orgid: { from: "0", to: "{" } }],
},
];
} else if (isOrgChannel(channel)) {
const additional = groupIds.length ? [groupsPredicate] : [];
filters = [
{
operation: "OR",
predicates: [{ orgid: orgIds }, ...additional],
},
];
} else {
filters = [
{
operation: "AND",
predicates: [groupsPredicate],
},
];
}
if (currentUsername) {
filters.push({
operation: "AND",
predicates: [{ username: { not: currentUsername } }],
});
}
const query: IQuery = {
targetEntity: "communityUser",
filters: [
{
operation: "OR",
predicates: inputs.reduce<IPredicate[]>(
(acc, input) => [...acc, { username: input }, { fullname: input }],
[]
),
},
...filters,
],
};
return query;
}
/**
* Transforms a given channel and optional channel groups array into a IHubSearchResult
* @param channel
* @param groups
* @returns
*/
export const channelToSearchResult = (
channel: IChannel,
groups?: IGroup[]
): IHubSearchResult => {
return {
...channel,
id: channel.id,
name: channel.name,
createdDate: new Date(channel.createdAt),
createdDateSource: "channel",
updatedDate: new Date(channel.updatedAt),
updatedDateSource: "channel",
type: "channel",
access: channel.access,
family: "channel",
owner: channel.creator,
links: {
// TODO: add links?
thumbnail: null,
self: null,
siteRelative: null,
},
includes: { groups },
rawResult: channel,
};
};