Skip to content

Commit 9f9d39d

Browse files
committed
fix formatting once more
1 parent b485aa8 commit 9f9d39d

File tree

1 file changed

+96
-100
lines changed

1 file changed

+96
-100
lines changed

functions/src/webhooks/transcription.ts

Lines changed: 96 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -3,125 +3,121 @@ import { AssemblyAI } from "assemblyai"
33
import { db, Timestamp } from "../firebase"
44
import { sha256 } from "js-sha256"
55

6-
76
export const transcription = functions
87
.runWith({ secrets: ["ASSEMBLY_API_KEY"] })
98
.https.onRequest(async (req, res) => {
10-
if (req.headers["x-maple-webhook"]) {
11-
if (req.body.status === "completed") {
12-
// If we get a request with the right header and status, get the
13-
// transcription from the assembly API.
14-
const assembly = new AssemblyAI({
15-
apiKey: process.env.ASSEMBLY_API_KEY ? process.env.ASSEMBLY_API_KEY : ""
16-
})
17-
18-
const transcript = await assembly.transcripts.get(
19-
req.body.transcript_id
20-
)
21-
if (transcript && transcript.webhook_auth) {
22-
// If there is a transcript and the transcript has an auth property,
23-
// look for an event (aka Hearing) in the DB with a matching ID.
24-
const maybeEventsInDb = await db
25-
.collection("events")
26-
.where("videoTranscriptionId", "==", transcript.id)
27-
.get()
9+
if (req.headers["x-maple-webhook"]) {
10+
if (req.body.status === "completed") {
11+
// If we get a request with the right header and status, get the
12+
// transcription from the assembly API.
13+
const assembly = new AssemblyAI({
14+
apiKey: process.env.ASSEMBLY_API_KEY ? process.env.ASSEMBLY_API_KEY : ""
15+
})
16+
17+
const transcript = await assembly.transcripts.get(req.body.transcript_id)
18+
if (transcript && transcript.webhook_auth) {
19+
// If there is a transcript and the transcript has an auth property,
20+
// look for an event (aka Hearing) in the DB with a matching ID.
21+
const maybeEventsInDb = await db
22+
.collection("events")
23+
.where("videoTranscriptionId", "==", transcript.id)
24+
.get()
2825

29-
if (maybeEventsInDb.docs.length) {
30-
// If we have a match look for one that matches a hash of the token
31-
// we gave Assembly. There should only be one of these but firestore
32-
// gives us an array. If there is more than one member, something is
33-
// wrong
34-
const authenticatedEventIds = [] as string[]
35-
const hashedToken = sha256(String(req.headers["x-maple-webhook"]))
26+
if (maybeEventsInDb.docs.length) {
27+
// If we have a match look for one that matches a hash of the token
28+
// we gave Assembly. There should only be one of these but firestore
29+
// gives us an array. If there is more than one member, something is
30+
// wrong
31+
const authenticatedEventIds = [] as string[]
32+
const hashedToken = sha256(String(req.headers["x-maple-webhook"]))
3633

37-
for (const index in maybeEventsInDb.docs) {
38-
const doc = maybeEventsInDb.docs[index]
34+
for (const index in maybeEventsInDb.docs) {
35+
const doc = maybeEventsInDb.docs[index]
3936

40-
const tokenDocInDb = await db
41-
.collection("events")
42-
.doc(doc.id)
43-
.collection("private")
44-
.doc("webhookAuth")
45-
.get()
37+
const tokenDocInDb = await db
38+
.collection("events")
39+
.doc(doc.id)
40+
.collection("private")
41+
.doc("webhookAuth")
42+
.get()
4643

47-
const tokenDataInDb =
48-
tokenDocInDb.data()?.videoAssemblyWebhookToken
44+
const tokenDataInDb = tokenDocInDb.data()?.videoAssemblyWebhookToken
4945

50-
if (hashedToken === tokenDataInDb) {
51-
authenticatedEventIds.push(doc.id)
52-
}
53-
}
54-
55-
// Log edge cases
56-
if (maybeEventsInDb.docs.length === 0) {
57-
console.log("No matching event in db.")
58-
}
59-
if (authenticatedEventIds.length === 0) {
60-
console.log("No authenticated events in db.")
61-
}
62-
if (authenticatedEventIds.length > 1) {
63-
console.log("More than one matching event in db.")
46+
if (hashedToken === tokenDataInDb) {
47+
authenticatedEventIds.push(doc.id)
6448
}
49+
}
6550

66-
if (authenticatedEventIds.length === 1) {
67-
// If there is one authenticated event, pull out the parts we want to
68-
// save and try to save them in the db.
69-
const { id, text, audio_url, utterances } = transcript
70-
try {
71-
const transcriptionInDb = await db
72-
.collection("transcriptions")
73-
.doc(id)
51+
// Log edge cases
52+
if (maybeEventsInDb.docs.length === 0) {
53+
console.log("No matching event in db.")
54+
}
55+
if (authenticatedEventIds.length === 0) {
56+
console.log("No authenticated events in db.")
57+
}
58+
if (authenticatedEventIds.length > 1) {
59+
console.log("More than one matching event in db.")
60+
}
7461

75-
await transcriptionInDb.set({
76-
id,
77-
text,
78-
createdAt: Timestamp.now(),
79-
audio_url
80-
})
62+
if (authenticatedEventIds.length === 1) {
63+
// If there is one authenticated event, pull out the parts we want to
64+
// save and try to save them in the db.
65+
const { id, text, audio_url, utterances } = transcript
66+
try {
67+
const transcriptionInDb = await db
68+
.collection("transcriptions")
69+
.doc(id)
8170

82-
// Put each `utterance` in a separate doc in an utterances
83-
// collection. Previously had done the same for `words` but
84-
// got worried about collection size and write times since
85-
// `words` can be tens of thousands of members.
86-
if (utterances) {
87-
const writer = db.bulkWriter()
88-
for (let utterance of utterances) {
89-
const { speaker, confidence, start, end, text } = utterance
71+
await transcriptionInDb.set({
72+
id,
73+
text,
74+
createdAt: Timestamp.now(),
75+
audio_url
76+
})
9077

91-
writer.set(
92-
db
93-
.collection("transcriptions")
94-
.doc(`${transcript.id}`)
95-
.collection("utterances")
96-
.doc(),
97-
{ speaker, confidence, start, end, text }
98-
)
99-
}
78+
// Put each `utterance` in a separate doc in an utterances
79+
// collection. Previously had done the same for `words` but
80+
// got worried about collection size and write times since
81+
// `words` can be tens of thousands of members.
82+
if (utterances) {
83+
const writer = db.bulkWriter()
84+
for (let utterance of utterances) {
85+
const { speaker, confidence, start, end, text } = utterance
10086

101-
await writer.close()
87+
writer.set(
88+
db
89+
.collection("transcriptions")
90+
.doc(`${transcript.id}`)
91+
.collection("utterances")
92+
.doc(),
93+
{ speaker, confidence, start, end, text }
94+
)
10295
}
10396

104-
// Delete the hashed webhook auth token from our db now that
105-
// we're done.
106-
for (const index in authenticatedEventIds) {
107-
await db
108-
.collection("events")
109-
.doc(authenticatedEventIds[index])
110-
.collection("private")
111-
.doc("webhookAuth")
112-
.set({
113-
videoAssemblyWebhookToken: null
114-
})
115-
}
116-
} catch (error) {
117-
console.log(error)
97+
await writer.close()
98+
}
99+
100+
// Delete the hashed webhook auth token from our db now that
101+
// we're done.
102+
for (const index in authenticatedEventIds) {
103+
await db
104+
.collection("events")
105+
.doc(authenticatedEventIds[index])
106+
.collection("private")
107+
.doc("webhookAuth")
108+
.set({
109+
videoAssemblyWebhookToken: null
110+
})
118111
}
112+
} catch (error) {
113+
console.log(error)
119114
}
120-
} else {
121-
res.status(404).send("Not Found")
122115
}
116+
} else {
117+
res.status(404).send("Not Found")
123118
}
124119
}
125120
}
126-
res.status(200).send()
127-
})
121+
}
122+
res.status(200).send()
123+
})

0 commit comments

Comments
 (0)