Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cloud/functions/src/model/data/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface TaskData {
deckId: string;
token: string | firestore.FieldValue;

type: 'publish-deck' | 'push-github';
type: 'publish-all' | 'publish-deck' | 'push-github';

status: 'scheduled' | 'failure' | 'successful';

Expand Down
55 changes: 31 additions & 24 deletions cloud/functions/src/request/publish/schedule-publish-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export function schedulePublish(request: functions.Request): Promise<ScheduledPu
try {
const token: string | undefined = await geToken(request);
const deckId: string | undefined = request.body.deckId;
const ownerId: string | undefined = request.body.ownerId;

if (!deckId) {
reject('No deck information provided.');
Expand All @@ -30,11 +29,6 @@ export function schedulePublish(request: functions.Request): Promise<ScheduledPu
return;
}

if (!ownerId) {
reject('No owner ID provided.');
return;
}

const publish: boolean = request.body.publish !== undefined && request.body.publish;
const github: boolean = request.body.github !== undefined && request.body.github;

Expand All @@ -44,25 +38,10 @@ export function schedulePublish(request: functions.Request): Promise<ScheduledPu
}

// We tell the frontend to wait
await updateDeckDeploy(deckId, ownerId, publish, github);
await updateDeckDeploy(deckId, publish, github);

// We schedule internally / cloud the job so we keep secret the token

if (publish) {
await scheduleTask({
deckId,
token,
type: 'publish-deck',
});
}

if (github) {
await scheduleTask({
deckId,
token,
type: 'push-github',
});
}
await schedule(deckId, publish, github, token);

resolve({
deckId,
Expand All @@ -76,7 +55,35 @@ export function schedulePublish(request: functions.Request): Promise<ScheduledPu
});
}

function updateDeckDeploy(deckId: string, ownerId: string, publish: boolean, github: boolean): Promise<void> {
async function schedule(deckId: string, publish: boolean, github: boolean, token: string) {
if (publish && github) {
await scheduleTask({
deckId,
token,
type: 'publish-all',
});

return;
}

if (publish) {
await scheduleTask({
deckId,
token,
type: 'publish-deck',
});
}

if (github) {
await scheduleTask({
deckId,
token,
type: 'push-github',
});
}
}

function updateDeckDeploy(deckId: string, publish: boolean, github: boolean): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
try {
if (!deckId || deckId === undefined || !deckId) {
Expand Down
36 changes: 33 additions & 3 deletions cloud/functions/src/watch/publish/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,45 @@ function publishJob(snap: DocumentSnapshot): Promise<void> {
return;
}

if (task.type === 'publish-deck') {
if (task.type === 'publish-all') {
const newPublish: boolean = deck.data.api_id === undefined || deck.data.api_id === null;

// If we do both, currently we need the API first as we are getting the content from the published deck
await publishToApi(deck, task.token as string);

// Even if we fixed the delay to publish to Cloudfare CDN (#195), sometimes if too quick, the presentation will not be correctly published
// Therefore, to avoid such problem, we add a bit of delay in the process but only for the first publish
setTimeout(
async () => {
await delayPublishToGitHub(deck.id);
resolve();
},
newPublish ? 7000 : 0
);
} else if (task.type === 'publish-deck') {
await publishToApi(deck, task.token as string);
resolve();
} else if (task.type === 'push-github') {
await publishToGitHub(deck.id, deck.data);
resolve();
}

resolve();
} catch (err) {
reject(err);
}
});
}

async function delayPublishToGitHub(deckId: string) {
// It has been changed by the publish to the API
const refreshDeck: Deck = await findDeck(deckId);

if (!refreshDeck || !refreshDeck.data) {
throw new Error('Updated published deck cannot be found');
}

try {
await publishToGitHub(refreshDeck.id, refreshDeck.data);
} catch (err) {
throw err;
}
}