@@ -3,125 +3,121 @@ import { AssemblyAI } from "assemblyai"
3
3
import { db , Timestamp } from "../firebase"
4
4
import { sha256 } from "js-sha256"
5
5
6
-
7
6
export const transcription = functions
8
7
. runWith ( { secrets : [ "ASSEMBLY_API_KEY" ] } )
9
8
. 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 ( )
28
25
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" ] ) )
36
33
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 ]
39
36
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 ( )
46
43
47
- const tokenDataInDb =
48
- tokenDocInDb . data ( ) ?. videoAssemblyWebhookToken
44
+ const tokenDataInDb = tokenDocInDb . data ( ) ?. videoAssemblyWebhookToken
49
45
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 )
64
48
}
49
+ }
65
50
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
+ }
74
61
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 )
81
70
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
+ } )
90
77
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
100
86
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
+ )
102
95
}
103
96
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
+ } )
118
111
}
112
+ } catch ( error ) {
113
+ console . log ( error )
119
114
}
120
- } else {
121
- res . status ( 404 ) . send ( "Not Found" )
122
115
}
116
+ } else {
117
+ res . status ( 404 ) . send ( "Not Found" )
123
118
}
124
119
}
125
120
}
126
- res . status ( 200 ) . send ( )
127
- } )
121
+ }
122
+ res . status ( 200 ) . send ( )
123
+ } )
0 commit comments