Skip to content

Commit

Permalink
#70 getting rid of event-family-tokens useless firestore collection
Browse files Browse the repository at this point in the history
  • Loading branch information
fcamblor committed May 15, 2024
1 parent 4321722 commit 1892fe1
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 65 deletions.
4 changes: 0 additions & 4 deletions cloud/firestore/firestore.default.rules
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ service cloud.firestore {
allow read, write: if false;
}

match /event-family-tokens/{familyId} {
allow read, write: if false;
}

match /public-tokens/{publicSecretToken} {
allow get: if true;
allow list, write: if false;
Expand Down
22 changes: 0 additions & 22 deletions cloud/firestore/firestore.default.rules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,6 @@ const FIREBASE_MANAGED_COLLECTIONS = [
data: () => ({dayId: 'monday', feedbacks: []}),
updatedData: () => ({dayId: 'tuesday', feedbacks: []})
}]
}, {
name: '/event-family-tokens/{familyId}',
docInitializations: [{
name: 'default',
collection: '/event-family-tokens',
path: '/event-family-tokens/a-family',
newDocPath: '/event-family-tokens/another-family',
data: () => ({families: ['devoxx'], token: 'ffffffff-ffff-ffff-ffff-ffffffffffff'}),
updatedData: () => ({families: ['devoxx'], token: 'ffffffff-ffff-ffff-ffff-fffffffffff0'})
}]
}, {
name: '/public-tokens/{tokenId}',
docInitializations: [{
Expand Down Expand Up @@ -692,18 +682,6 @@ const COLLECTIONS: CollectionDescriptor[] = [{
get: false, update: false, createDoc: false,
}, 'alice')
}
}, {
name: "/event-family-tokens",
aroundTests: (_: UserContext) => ({
beforeEach: [],
afterEach: [],
}),
tests: (userContext: UserContext) => {
ensureCollectionFollowAccessPermissions('/event-family-tokens/{familyId}', userContext,
{
read: false, write: false
})
}
}, {
name: "/public-tokens",
aroundTests: (_: UserContext) => ({
Expand Down
24 changes: 1 addition & 23 deletions cloud/functions/src/functions/firestore/firestore-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function getSecretTokenDoc<T>(path: string) {

export async function getOrganizerSpaceByToken(
eventId: string,
tokenType: 'organizerSecretToken'|'familyToken',
tokenType: 'organizerSecretToken',
secretToken: string
) {
const organizerSpace: ConferenceOrganizerSpace = await getSecretTokenDoc(`events/${eventId}/organizer-space`);
Expand All @@ -40,13 +40,6 @@ export async function getOrganizerSpaceByToken(
throw new Error(`Invalid organizer token for eventId=${eventId}: ${secretToken}`);
}

if(tokenType === 'familyToken') {
const familyTokenValid = await checkEventFamilyTokenIsValid(eventId, secretToken);
if(!familyTokenValid) {
throw new Error(`Invalid family token for eventId=${eventId}: ${secretToken}`);
}
}

return organizerSpace;
}

Expand Down Expand Up @@ -132,18 +125,3 @@ export async function checkEventLastUpdate(

return { cachedHash, updatesDetected: true };
}

export async function checkEventFamilyTokenIsValid(eventId: string, token: string) {
const listableEvent = (await db.collection("events").doc(eventId).get())?.data() as ListableEvent|undefined;

if(!listableEvent || !listableEvent.eventFamily) {
return false;
}

const familyTokenSnapshots = await db.collection("event-family-tokens")
.where('families', 'array-contains', listableEvent.eventFamily)
.where("token", '==', token)
.get()

return !familyTokenSnapshots.empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {db} from "../../../firebase";


export async function cleaningUnusedFirestoreDocs(): Promise<"OK"|"Error"> {
const eventFamilyTokensColl = await db.collection(`event-family-tokens`).get()
if(!eventFamilyTokensColl.empty) {
await Promise.all(eventFamilyTokensColl.docs.map(async eventFamilyToken => {
return eventFamilyToken.ref.delete()
}))
}

return "OK"
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ export async function legacyAttendeesFeedbacks(request: functions.https.Request,

const organizerSecretToken = extractSingleQueryParam(request, 'organizerSecretToken');
const talkIds = extractMultiQueryParam(request, 'talkIds');
// deprecated
const familyToken = extractSingleQueryParam(request, 'familyToken');
const familyOrganizerSecretToken = extractSingleQueryParam(request, 'familyOrganizerSecretToken');
const eventId = extractSingleQueryParam(request, 'eventId');

if(!talkIds || !talkIds.length) { return sendResponseMessage(response, 400, `Missing [talkIds] (multi) query parameter !`) }
if(!eventId) { return sendResponseMessage(response, 400, `Missing [eventId] query parameter !`) }
if(!organizerSecretToken && !familyToken && !familyOrganizerSecretToken) { return sendResponseMessage(response, 400, `Missing either [organizerSecretToken] or [familyToken] or [familyOrganizerSecretToken] query parameter !`) }
if(!organizerSecretToken && !familyOrganizerSecretToken) { return sendResponseMessage(response, 400, `Missing either [organizerSecretToken] or [familyOrganizerSecretToken] query parameter !`) }

const eventDescriptor = await getEventDescriptor(eventId);
if(familyOrganizerSecretToken) {
Expand All @@ -48,13 +46,11 @@ export async function legacyAttendeesFeedbacks(request: functions.https.Request,
// }

try {
const organizerSpace = await match([organizerSecretToken, familyToken, familyOrganizerSecretToken])
.with([ P.nullish, P.nullish, P.nullish ], async ([_1, _2]) => { throw new Error(`Unexpected state: (undefined,undefined)`); })
.with([ P.not(P.nullish), P.any, P.any ], async ([organizerSecretToken, _]) => {
const organizerSpace = await match([organizerSecretToken, familyOrganizerSecretToken])
.with([ P.nullish, P.nullish ], async ([_1, _2]) => { throw new Error(`Unexpected state: (undefined,undefined)`); })
.with([ P.not(P.nullish), P.any ], async ([organizerSecretToken, _]) => {
return getOrganizerSpaceByToken(eventId, 'organizerSecretToken', organizerSecretToken);
}).with([ P.any, P.not(P.nullish), P.any ], async ([_, familyToken]) => {
return getOrganizerSpaceByToken(eventId, 'familyToken', familyToken);
}).with([ P.any, P.any, P.not(P.nullish) ], async ([_1, _2, familyOrganizerSecretToken]) => {
}).with([ P.any, P.not(P.nullish) ], async ([_1]) => {
return getSecretTokenDoc<ConferenceOrganizerSpace>(`/events/${eventId}/organizer-space`);
}).run()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import * as express from "express";
export async function deprecatedEventStats(request: functions.https.Request, response: express.Response) {

const organizerSecretToken = extractSingleQueryParam(request, 'organizerSecretToken');
const familyToken = extractSingleQueryParam(request, 'familyToken');
const eventId = extractSingleQueryParam(request, 'eventId');

if(!eventId) { return sendResponseMessage(response, 400, `Missing [eventId] query parameter !`) }
if(!organizerSecretToken && !familyToken) { return sendResponseMessage(response, 400, `Missing either [organizerSecretToken] or [familyToken] query parameter !`) }
if(!organizerSecretToken) { return sendResponseMessage(response, 400, `Missing either [organizerSecretToken] or [familyToken] query parameter !`) }

const { cachedHash, updatesDetected } = await checkEventLastUpdate(eventId, [
root => root.favorites,
Expand All @@ -27,12 +26,10 @@ export async function deprecatedEventStats(request: functions.https.Request, res
}

try {
const organizerSpace = await match([organizerSecretToken, familyToken])
.with([ P.nullish, P.nullish ], async ([_1, _2]) => { throw new Error(`Unexpected state: (undefined,undefined)`); })
.with([ P.not(P.nullish), P.any ], async ([organizerSecretToken, _]) => {
const organizerSpace = await match([organizerSecretToken])
.with([ P.nullish ], async ([_1]) => { throw new Error(`Unexpected state: (undefined,undefined)`); })
.with([ P.not(P.nullish) ], async ([organizerSecretToken]) => {
return getOrganizerSpaceByToken(eventId, 'organizerSecretToken', organizerSecretToken);
}).with([ P.any, P.not(P.nullish) ], async ([_, familyToken]) => {
return getOrganizerSpaceByToken(eventId, 'familyToken', familyToken);
}).run()

const talksStats = await eventTalkStatsFor(eventId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const MIGRATIONS: Migration[] = [
{ name: "migrateFamilyEventsStatsAccessTokenTypes", exec: async () => (await import("../firestore/migrations/017-migrateSomePublicTokenTypes")).migrateFamilyEventsStatsAccessTokenTypes() },
{ name: "introduceTalkAssets", exec: async () => (await import("../firestore/migrations/018-introduceTalkAssets")).introduceTalkAssets() },
{ name: "introduceFormattingsFeature", exec: async () => (await import("../firestore/migrations/019-introduceFormattingsFeature")).introduceFormattingsFeature() },
{ name: "cleaningUnusedFirestoreDocs", exec: async () => (await import("../firestore/migrations/020-cleaningUnusedFirestoreDocs")).cleaningUnusedFirestoreDocs() },
];

export type MigrationResult = "OK"|"Error";
Expand Down

0 comments on commit 1892fe1

Please sign in to comment.