@@ -6,118 +6,123 @@ import { sha256 } from "js-sha256"
6
6
export const transcription = functions
7
7
. runWith ( { secrets : [ "ASSEMBLY_API_KEY" ] } )
8
8
. https . onRequest ( async ( req , res ) => {
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 ( )
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
15
+ ? process . env . ASSEMBLY_API_KEY
16
+ : ""
17
+ } )
25
18
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" ] ) )
19
+ const transcript = await assembly . transcripts . get (
20
+ req . body . transcript_id
21
+ )
22
+ if ( transcript && transcript . webhook_auth ) {
23
+ // If there is a transcript and the transcript has an auth property,
24
+ // look for an event (aka Hearing) in the DB with a matching ID.
25
+ const maybeEventsInDb = await db
26
+ . collection ( "events" )
27
+ . where ( "videoTranscriptionId" , "==" , transcript . id )
28
+ . get ( )
33
29
34
- for ( const index in maybeEventsInDb . docs ) {
35
- const doc = maybeEventsInDb . docs [ index ]
30
+ if ( maybeEventsInDb . docs . length ) {
31
+ // If we have a match look for one that matches a hash of the token
32
+ // we gave Assembly. There should only be one of these but firestore
33
+ // gives us an array. If there is more than one member, something is
34
+ // wrong
35
+ const authenticatedEventIds = [ ] as string [ ]
36
+ const hashedToken = sha256 ( String ( req . headers [ "x-maple-webhook" ] ) )
36
37
37
- const tokenDocInDb = await db
38
- . collection ( "events" )
39
- . doc ( doc . id )
40
- . collection ( "private" )
41
- . doc ( "webhookAuth" )
42
- . get ( )
38
+ for ( const index in maybeEventsInDb . docs ) {
39
+ const doc = maybeEventsInDb . docs [ index ]
43
40
44
- const tokenDataInDb = tokenDocInDb . data ( ) ?. videoAssemblyWebhookToken
41
+ const tokenDocInDb = await db
42
+ . collection ( "events" )
43
+ . doc ( doc . id )
44
+ . collection ( "private" )
45
+ . doc ( "webhookAuth" )
46
+ . get ( )
45
47
46
- if ( hashedToken === tokenDataInDb ) {
47
- authenticatedEventIds . push ( doc . id )
48
+ const tokenDataInDb =
49
+ tokenDocInDb . data ( ) ?. videoAssemblyWebhookToken
50
+
51
+ if ( hashedToken === tokenDataInDb ) {
52
+ authenticatedEventIds . push ( doc . id )
53
+ }
48
54
}
49
- }
50
55
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
- }
56
+ // Log edge cases
57
+ if ( maybeEventsInDb . docs . length === 0 ) {
58
+ console . log ( "No matching event in db." )
59
+ }
60
+ if ( authenticatedEventIds . length === 0 ) {
61
+ console . log ( "No authenticated events in db." )
62
+ }
63
+ if ( authenticatedEventIds . length > 1 ) {
64
+ console . log ( "More than one matching event in db." )
65
+ }
61
66
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 )
67
+ if ( authenticatedEventIds . length === 1 ) {
68
+ // If there is one authenticated event, pull out the parts we want to
69
+ // save and try to save them in the db.
70
+ const { id, text, audio_url, utterances } = transcript
71
+ try {
72
+ const transcriptionInDb = await db
73
+ . collection ( "transcriptions" )
74
+ . doc ( id )
70
75
71
- await transcriptionInDb . set ( {
72
- id,
73
- text,
74
- createdAt : Timestamp . now ( ) ,
75
- audio_url
76
- } )
76
+ await transcriptionInDb . set ( {
77
+ id,
78
+ text,
79
+ createdAt : Timestamp . now ( ) ,
80
+ audio_url
81
+ } )
77
82
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
83
+ // Put each `utterance` in a separate doc in an utterances
84
+ // collection. Previously had done the same for `words` but
85
+ // got worried about collection size and write times since
86
+ // `words` can be tens of thousands of members.
87
+ if ( utterances ) {
88
+ const writer = db . bulkWriter ( )
89
+ for ( let utterance of utterances ) {
90
+ const { speaker, confidence, start, end, text } = utterance
86
91
87
- writer . set (
88
- db
89
- . collection ( "transcriptions" )
90
- . doc ( `${ transcript . id } ` )
91
- . collection ( "utterances" )
92
- . doc ( ) ,
93
- { speaker, confidence, start, end, text }
94
- )
95
- }
92
+ writer . set (
93
+ db
94
+ . collection ( "transcriptions" )
95
+ . doc ( `${ transcript . id } ` )
96
+ . collection ( "utterances" )
97
+ . doc ( ) ,
98
+ { speaker, confidence, start, end, text }
99
+ )
100
+ }
96
101
97
- await writer . close ( )
98
- }
102
+ await writer . close ( )
103
+ }
99
104
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
- } )
105
+ // Delete the hashed webhook auth token from our db now that
106
+ // we're done.
107
+ for ( const index in authenticatedEventIds ) {
108
+ await db
109
+ . collection ( "events" )
110
+ . doc ( authenticatedEventIds [ index ] )
111
+ . collection ( "private" )
112
+ . doc ( "webhookAuth" )
113
+ . set ( {
114
+ videoAssemblyWebhookToken : null
115
+ } )
116
+ }
117
+ } catch ( error ) {
118
+ console . log ( error )
111
119
}
112
- } catch ( error ) {
113
- console . log ( error )
114
120
}
121
+ } else {
122
+ res . status ( 404 ) . send ( "Not Found" )
115
123
}
116
- } else {
117
- res . status ( 404 ) . send ( "Not Found" )
118
124
}
119
125
}
120
126
}
121
- }
122
- res . status ( 200 ) . send ( )
123
- } )
127
+ res . status ( 200 ) . send ( )
128
+ } )
0 commit comments