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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch existing anonymous participations saved inside this browser
|
|
|
|
*/
|
2020-02-18 08:57:00 +01:00
|
|
|
function getLocalAnonymousParticipations(): Map<string, IAnonymousParticipation> {
|
|
|
|
return jsonToMap(
|
|
|
|
localStorage.getItem(ANONYMOUS_PARTICIPATIONS_LOCALSTORAGE_KEY) || mapToJson(new Map())
|
|
|
|
);
|
2019-12-20 13:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
function mapToJson(map: Map<any, any>): string {
|
2019-12-20 13:04:34 +01:00
|
|
|
return JSON.stringify([...map]);
|
|
|
|
}
|
2020-02-18 08:57:00 +01:00
|
|
|
function jsonToMap(jsonStr: string): Map<string, IAnonymousParticipation> {
|
2019-12-20 13:04:34 +01:00
|
|
|
return new Map(JSON.parse(jsonStr));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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> {
|
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
|
|
|
|
) {
|
2019-12-20 13:04:34 +01:00
|
|
|
const participations = purgeOldParticipations(getLocalAnonymousParticipations());
|
|
|
|
participations.set(hashedUUID, participation);
|
|
|
|
localStorage.setItem(ANONYMOUS_PARTICIPATIONS_LOCALSTORAGE_KEY, mapToJson(participations));
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildExpiration(event: IEvent): Date {
|
|
|
|
const expiration = event.endsOn || event.beginsOn;
|
|
|
|
expiration.setMonth(expiration.getMonth() + 3);
|
|
|
|
expiration.setDate(1);
|
|
|
|
return expiration;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addLocalUnconfirmedAnonymousParticipation(event: IEvent, cancellationToken: string) {
|
2020-02-18 08:57:00 +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
|
|
|
|
*/
|
2019-12-20 13:04:34 +01:00
|
|
|
const hashedUUID = await digestMessage(event.uuid);
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
/**
|
|
|
|
* We round expiration to first day of next 3 months so that it's difficult to find event from date
|
|
|
|
*/
|
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-02-18 08:57:00 +01:00
|
|
|
async function confirmLocalAnonymousParticipation(uuid: string) {
|
2019-12-20 13:04:34 +01:00
|
|
|
const participations = purgeOldParticipations(getLocalAnonymousParticipations());
|
|
|
|
const hashedUUID = await digestMessage(uuid);
|
|
|
|
const participation = participations.get(hashedUUID);
|
|
|
|
if (participation) {
|
|
|
|
participation.confirmed = true;
|
|
|
|
participations.set(hashedUUID, participation);
|
|
|
|
localStorage.setItem(ANONYMOUS_PARTICIPATIONS_LOCALSTORAGE_KEY, mapToJson(participations));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
async function isParticipatingInThisEvent(eventUUID: string): Promise<boolean> {
|
2019-12-20 13:04:34 +01:00
|
|
|
const participation = await getParticipation(eventUUID);
|
|
|
|
return participation !== undefined && participation.confirmed;
|
|
|
|
}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
async function getParticipation(eventUUID: string): Promise<IAnonymousParticipation> {
|
2019-12-20 13:04:34 +01:00
|
|
|
const hashedUUID = await digestMessage(eventUUID);
|
|
|
|
const participation = purgeOldParticipations(getLocalAnonymousParticipations()).get(hashedUUID);
|
|
|
|
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-02-18 08:57:00 +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);
|
|
|
|
const participations = purgeOldParticipations(getLocalAnonymousParticipations());
|
|
|
|
participations.delete(hashedUUID);
|
|
|
|
localStorage.setItem(ANONYMOUS_PARTICIPATIONS_LOCALSTORAGE_KEY, mapToJson(participations));
|
|
|
|
}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
async function digestMessage(message: string): Promise<string> {
|
2019-12-20 13:04:34 +01:00
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const data = encoder.encode(message);
|
2020-02-18 08:57:00 +01:00
|
|
|
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
2019-12-20 13:04:34 +01:00
|
|
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
2020-02-18 08:57:00 +01:00
|
|
|
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
2019-12-20 13:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
addLocalUnconfirmedAnonymousParticipation,
|
|
|
|
confirmLocalAnonymousParticipation,
|
|
|
|
getLeaveTokenForParticipation,
|
|
|
|
isParticipatingInThisEvent,
|
|
|
|
removeAnonymousParticipation,
|
|
|
|
AnonymousParticipationNotFoundError,
|
|
|
|
};
|