2020-02-18 08:57:00 +01:00
|
|
|
import { IEvent } from "@/types/event.model";
|
2019-12-20 13:04:34 +01:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
const ANONYMOUS_PARTICIPATIONS_LOCALSTORAGE_KEY = "ANONYMOUS_PARTICIPATIONS";
|
2019-12-20 13:04:34 +01:00
|
|
|
|
|
|
|
interface IAnonymousParticipation {
|
2020-02-18 08:57:00 +01:00
|
|
|
token: string;
|
2019-12-20 13:04:34 +01:00
|
|
|
expiration: Date;
|
|
|
|
confirmed: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AnonymousParticipationNotFoundError extends Error {
|
|
|
|
constructor(message?: string) {
|
|
|
|
super(message);
|
|
|
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
|
|
this.name = AnonymousParticipationNotFoundError.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 19:27:44 +01:00
|
|
|
function jsonToMap(jsonStr: string): Map<string, IAnonymousParticipation> {
|
|
|
|
return new Map(JSON.parse(jsonStr));
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapToJson(map: Map<any, any>): string {
|
|
|
|
return JSON.stringify([...map]);
|
|
|
|
}
|
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
/**
|
|
|
|
* Fetch existing anonymous participations saved inside this browser
|
|
|
|
*/
|
2020-11-30 10:24:11 +01:00
|
|
|
function getLocalAnonymousParticipations(): Map<
|
|
|
|
string,
|
|
|
|
IAnonymousParticipation
|
|
|
|
> {
|
2020-02-18 08:57:00 +01:00
|
|
|
return jsonToMap(
|
2020-11-30 10:24:11 +01:00
|
|
|
localStorage.getItem(ANONYMOUS_PARTICIPATIONS_LOCALSTORAGE_KEY) ||
|
|
|
|
mapToJson(new Map())
|
2020-02-18 08:57:00 +01:00
|
|
|
);
|
2019-12-20 13:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Purge participations which expiration has been reached
|
|
|
|
* @param participations Map
|
|
|
|
*/
|
2020-02-18 08:57:00 +01:00
|
|
|
function purgeOldParticipations(
|
|
|
|
participations: Map<string, IAnonymousParticipation>
|
|
|
|
): Map<string, IAnonymousParticipation> {
|
2020-11-27 19:27:44 +01:00
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2019-12-20 13:04:34 +01:00
|
|
|
for (const [hashedUUID, { expiration }] of participations) {
|
|
|
|
if (expiration < new Date()) {
|
|
|
|
participations.delete(hashedUUID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return participations;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a participation in the list of anonymous participations
|
|
|
|
* @param hashedUUID
|
|
|
|
* @param participation
|
|
|
|
*/
|
2020-02-18 08:57:00 +01:00
|
|
|
function insertLocalAnonymousParticipation(
|
|
|
|
hashedUUID: string,
|
|
|
|
participation: IAnonymousParticipation
|
|
|
|
) {
|
2020-11-30 10:24:11 +01:00
|
|
|
const participations = purgeOldParticipations(
|
|
|
|
getLocalAnonymousParticipations()
|
|
|
|
);
|
2019-12-20 13:04:34 +01:00
|
|
|
participations.set(hashedUUID, participation);
|
2020-11-30 10:24:11 +01:00
|
|
|
localStorage.setItem(
|
|
|
|
ANONYMOUS_PARTICIPATIONS_LOCALSTORAGE_KEY,
|
|
|
|
mapToJson(participations)
|
|
|
|
);
|
2019-12-20 13:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function buildExpiration(event: IEvent): Date {
|
|
|
|
const expiration = event.endsOn || event.beginsOn;
|
2020-09-30 10:40:10 +02:00
|
|
|
expiration.setMonth(expiration.getMonth() + 1);
|
2019-12-20 13:04:34 +01:00
|
|
|
return expiration;
|
|
|
|
}
|
|
|
|
|
2020-11-27 19:27:44 +01:00
|
|
|
async function digestMessage(message: string): Promise<string> {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const data = encoder.encode(message);
|
|
|
|
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
|
|
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
|
|
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addLocalUnconfirmedAnonymousParticipation(
|
|
|
|
event: IEvent,
|
|
|
|
cancellationToken: string
|
|
|
|
): Promise<void> {
|
2020-02-18 08:57:00 +01:00
|
|
|
/**
|
2020-11-30 10:24:11 +01:00
|
|
|
* We hash the event UUID so that we can't know which events
|
|
|
|
* an anonymous user goes by looking up it's localstorage
|
2020-02-18 08:57:00 +01:00
|
|
|
*/
|
2019-12-20 13:04:34 +01:00
|
|
|
const hashedUUID = await digestMessage(event.uuid);
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
/**
|
2020-11-30 10:24:11 +01:00
|
|
|
* We round expiration to first day of next 3 months so that
|
|
|
|
* it's difficult to find event from date
|
2020-02-18 08:57:00 +01:00
|
|
|
*/
|
2019-12-20 13:04:34 +01:00
|
|
|
const expiration = buildExpiration(event);
|
2020-02-18 08:57:00 +01:00
|
|
|
insertLocalAnonymousParticipation(hashedUUID, {
|
|
|
|
token: cancellationToken,
|
|
|
|
expiration,
|
|
|
|
confirmed: false,
|
|
|
|
});
|
2019-12-20 13:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 19:27:44 +01:00
|
|
|
async function confirmLocalAnonymousParticipation(uuid: string): Promise<void> {
|
2020-11-30 10:24:11 +01:00
|
|
|
const participations = purgeOldParticipations(
|
|
|
|
getLocalAnonymousParticipations()
|
|
|
|
);
|
2019-12-20 13:04:34 +01:00
|
|
|
const hashedUUID = await digestMessage(uuid);
|
|
|
|
const participation = participations.get(hashedUUID);
|
|
|
|
if (participation) {
|
|
|
|
participation.confirmed = true;
|
|
|
|
participations.set(hashedUUID, participation);
|
2020-11-30 10:24:11 +01:00
|
|
|
localStorage.setItem(
|
|
|
|
ANONYMOUS_PARTICIPATIONS_LOCALSTORAGE_KEY,
|
|
|
|
mapToJson(participations)
|
|
|
|
);
|
2019-12-20 13:04:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 10:24:11 +01:00
|
|
|
async function getParticipation(
|
|
|
|
eventUUID: string
|
|
|
|
): Promise<IAnonymousParticipation> {
|
2019-12-20 13:04:34 +01:00
|
|
|
const hashedUUID = await digestMessage(eventUUID);
|
2020-11-30 10:24:11 +01:00
|
|
|
const participation = purgeOldParticipations(
|
|
|
|
getLocalAnonymousParticipations()
|
|
|
|
).get(hashedUUID);
|
2019-12-20 13:04:34 +01:00
|
|
|
if (participation) {
|
|
|
|
return participation;
|
|
|
|
}
|
2020-02-18 08:57:00 +01:00
|
|
|
throw new AnonymousParticipationNotFoundError("Participation not found");
|
2019-12-20 13:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 19:27:44 +01:00
|
|
|
async function isParticipatingInThisEvent(eventUUID: string): Promise<boolean> {
|
|
|
|
const participation = await getParticipation(eventUUID);
|
|
|
|
return participation !== undefined && participation.confirmed;
|
|
|
|
}
|
|
|
|
|
2020-11-30 10:24:11 +01:00
|
|
|
async function getLeaveTokenForParticipation(
|
|
|
|
eventUUID: string
|
|
|
|
): Promise<string> {
|
2019-12-20 13:04:34 +01:00
|
|
|
return (await getParticipation(eventUUID)).token;
|
|
|
|
}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
async function removeAnonymousParticipation(eventUUID: string): Promise<void> {
|
2019-12-20 13:04:34 +01:00
|
|
|
const hashedUUID = await digestMessage(eventUUID);
|
2020-11-30 10:24:11 +01:00
|
|
|
const participations = purgeOldParticipations(
|
|
|
|
getLocalAnonymousParticipations()
|
|
|
|
);
|
2019-12-20 13:04:34 +01:00
|
|
|
participations.delete(hashedUUID);
|
2020-11-30 10:24:11 +01:00
|
|
|
localStorage.setItem(
|
|
|
|
ANONYMOUS_PARTICIPATIONS_LOCALSTORAGE_KEY,
|
|
|
|
mapToJson(participations)
|
|
|
|
);
|
2019-12-20 13:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-12-04 16:44:37 +01:00
|
|
|
function removeAllAnonymousParticipations(): void {
|
|
|
|
localStorage.removeItem(ANONYMOUS_PARTICIPATIONS_LOCALSTORAGE_KEY);
|
|
|
|
}
|
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
export {
|
|
|
|
addLocalUnconfirmedAnonymousParticipation,
|
|
|
|
confirmLocalAnonymousParticipation,
|
|
|
|
getLeaveTokenForParticipation,
|
|
|
|
isParticipatingInThisEvent,
|
|
|
|
removeAnonymousParticipation,
|
2020-12-04 16:44:37 +01:00
|
|
|
removeAllAnonymousParticipations,
|
2019-12-20 13:04:34 +01:00
|
|
|
AnonymousParticipationNotFoundError,
|
|
|
|
};
|