2024-05-17 19:14:34 +00:00
|
|
|
import { Features } from 'app/common/Features';
|
2021-10-25 16:54:04 +00:00
|
|
|
import { normalizeEmail } from 'app/common/emails';
|
|
|
|
import { PermissionData, PermissionDelta } from 'app/common/UserAPI';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark that the share is share number #at of a maximum of #top. The #at values
|
|
|
|
* start at 1.
|
|
|
|
*/
|
|
|
|
export interface ShareLimitAnnotation {
|
|
|
|
at: number;
|
|
|
|
top?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some facts about a share.
|
|
|
|
*/
|
|
|
|
export interface ShareAnnotation {
|
|
|
|
isMember?: boolean; // Is the share for a team member.
|
|
|
|
isSupport?: boolean; // Is the share for a support user.
|
|
|
|
collaboratorLimit?: ShareLimitAnnotation; // Does the share count towards a collaborator limit.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Facts about all shares for a resource.
|
|
|
|
*/
|
|
|
|
export interface ShareAnnotations {
|
|
|
|
hasTeam?: boolean; // Is the resource in a team site?
|
|
|
|
users: Map<string, ShareAnnotation>; // Annotations keyed by normalized user email.
|
|
|
|
}
|
|
|
|
|
2022-12-13 01:06:15 +00:00
|
|
|
export interface ShareAnnotatorOptions {
|
|
|
|
supportEmail?: string; // Known email address of the support user (e.g. support@getgrist.com).
|
|
|
|
}
|
|
|
|
|
2021-10-25 16:54:04 +00:00
|
|
|
/**
|
|
|
|
* Helper for annotating users mentioned in a proposed change of shares, given the
|
|
|
|
* current shares in place.
|
|
|
|
*/
|
|
|
|
export class ShareAnnotator {
|
2022-12-13 01:06:15 +00:00
|
|
|
private _supportEmail = this._options.supportEmail;
|
2022-07-26 17:49:35 +00:00
|
|
|
|
2022-12-13 01:06:15 +00:00
|
|
|
constructor(
|
2024-05-17 19:14:34 +00:00
|
|
|
private _features: Features|null,
|
2022-12-13 01:06:15 +00:00
|
|
|
private _state: PermissionData,
|
|
|
|
private _options: ShareAnnotatorOptions = {}
|
|
|
|
) {
|
2021-10-25 16:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public updateState(state: PermissionData) {
|
|
|
|
this._state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
public annotateChanges(change: PermissionDelta): ShareAnnotations {
|
2024-05-17 19:14:34 +00:00
|
|
|
const features = this._features ?? {};
|
2021-10-25 16:54:04 +00:00
|
|
|
const annotations: ShareAnnotations = {
|
2024-05-17 19:14:34 +00:00
|
|
|
hasTeam: !this._features || this._features.vanityDomain,
|
2021-10-25 16:54:04 +00:00
|
|
|
users: new Map(),
|
|
|
|
};
|
|
|
|
if (features.maxSharesPerDocPerRole || features.maxSharesPerWorkspace) {
|
|
|
|
// For simplicity, don't try to annotate if limits not used at the time of writing
|
|
|
|
// are in place.
|
|
|
|
return annotations;
|
|
|
|
}
|
|
|
|
const top = features.maxSharesPerDoc;
|
|
|
|
let at = 0;
|
2022-10-14 15:39:15 +00:00
|
|
|
const makeAnnotation = (user: {email: string, isMember?: boolean, isSupport?: boolean, access: string|null}) => {
|
2021-10-25 16:54:04 +00:00
|
|
|
const annotation: ShareAnnotation = {
|
|
|
|
isMember: user.isMember,
|
|
|
|
};
|
2022-10-14 15:39:15 +00:00
|
|
|
if (user.isSupport) {
|
2021-10-25 16:54:04 +00:00
|
|
|
return { isSupport: true };
|
|
|
|
}
|
|
|
|
if (!annotation.isMember && user.access) {
|
|
|
|
at++;
|
|
|
|
annotation.collaboratorLimit = {
|
|
|
|
at,
|
|
|
|
top
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return annotation;
|
|
|
|
};
|
|
|
|
const removed = new Set(
|
|
|
|
Object.entries(change?.users||{}).filter(([, v]) => v === null)
|
|
|
|
.map(([k, ]) => normalizeEmail(k)));
|
|
|
|
for (const user of this._state.users) {
|
|
|
|
if (removed.has(user.email)) { continue; }
|
2021-12-09 16:35:51 +00:00
|
|
|
if (!user.isMember && !user.access) { continue; }
|
2021-10-25 16:54:04 +00:00
|
|
|
annotations.users.set(user.email, makeAnnotation(user));
|
|
|
|
}
|
|
|
|
const tweaks = new Set(
|
|
|
|
Object.entries(change?.users||{}).filter(([, v]) => v !== null)
|
|
|
|
.map(([k, ]) => normalizeEmail(k)));
|
|
|
|
for (const email of tweaks) {
|
|
|
|
const annotation = annotations.users.get(email) || makeAnnotation({
|
2022-12-13 01:06:15 +00:00
|
|
|
email,
|
|
|
|
isMember: false,
|
|
|
|
isSupport: Boolean(email.trim() !== '' && email === this._supportEmail),
|
|
|
|
access: '<set>',
|
2021-10-25 16:54:04 +00:00
|
|
|
});
|
|
|
|
annotations.users.set(email, annotation);
|
|
|
|
}
|
|
|
|
return annotations;
|
|
|
|
}
|
|
|
|
}
|