@@ -2,7 +2,7 @@ import { initializeApp } from "firebase-admin/app";
22import { getFirestore } from "firebase-admin/firestore" ;
33import { onSchedule } from "firebase-functions/v2/scheduler" ;
44import { onRequest } from "firebase-functions/v2/https" ;
5- import { Streamer } from "../types" ;
5+ import { Streamer , Stream } from "../types" ;
66import { YoutubeClient , TwitchClient , TwitCastingClient } from "./api" ;
77import { defineConfig , sortStreams } from "./utils" ;
88import { createMaster } from "./createMater" ;
@@ -73,7 +73,7 @@ export const getStreamers = onSchedule(
7373
7474 return result ;
7575 } ,
76- baseStreamers as Record < string , Streamer > ,
76+ baseStreamers as Record < string , Streamer >
7777 ) ;
7878
7979 // create and update db
@@ -84,7 +84,7 @@ export const getStreamers = onSchedule(
8484 batch . set ( streamerRef . doc ( key ) , data ) ;
8585
8686 await batch . commit ( ) ;
87- } ,
87+ }
8888) ;
8989
9090export const getStreams = onSchedule (
@@ -98,8 +98,6 @@ export const getStreams = onSchedule(
9898 region : "asia-northeast1" ,
9999 } ,
100100 async ( ) => {
101- const endTime = new Date ( ) . toISOString ( ) ;
102-
103101 // init
104102 const master = await getStreamerMaster ( ) ;
105103 const tokenDoc = db
@@ -115,15 +113,20 @@ export const getStreams = onSchedule(
115113 twitchClient . getStreams ( [ ...master . twitch . keys ( ) ] ) ,
116114 twitClient . getStreams ( [ ...master . twitCasting . keys ( ) ] ) ,
117115 ] ;
118- const streams = ( await Promise . all ( getStreams ) ) . flat ( ) ;
116+ const currentStreams = ( await Promise . all ( getStreams ) ) . flat ( ) ;
119117
120118 // create and update db
121119 const batch = db . batch ( ) ;
122120 const streamRef = db . collection ( config . collection . streams . value ( ) ) ;
123121 const snap = await streamRef . get ( ) ;
124- const { endedStreams, newStreams } = sortStreams ( streams , snap . docs ) ;
122+ const pastStreams = snap . docs . map ( ( doc ) => ( {
123+ id : doc . id ,
124+ data : doc . data ( ) as Stream ,
125+ } ) ) ;
126+ const streams = sortStreams ( currentStreams , pastStreams ) ;
125127
126- for await ( const { id, data } of endedStreams ) {
128+ const endTime = new Date ( ) . toISOString ( ) ;
129+ for await ( const { id, data } of streams . ended ) {
127130 let stream = data ;
128131
129132 // if twitch stream, update stream info
@@ -139,13 +142,16 @@ export const getStreams = onSchedule(
139142 batch . update ( streamRef . doc ( id ) , { ...stream , endTime } ) ;
140143 }
141144
142- for ( const newStream of newStreams ) {
145+ for ( const { id, data } of streams . updated )
146+ batch . update ( streamRef . doc ( id ) , data ) ;
147+
148+ for ( const newStream of streams . new ) {
143149 const streamerId = master [ newStream . platform ] . get ( newStream . channelId ) ;
144150 batch . set ( streamRef . doc ( ) , { ...newStream , streamerId } ) ;
145151 }
146152
147153 await batch . commit ( ) ;
148- } ,
154+ }
149155) ;
150156
151157export const createStreamerMaster = onRequest (
@@ -155,5 +161,5 @@ export const createStreamerMaster = onRequest(
155161 async ( _ , res ) => {
156162 createMaster ( db , config . collection . master . value ( ) ) ;
157163 res . status ( 200 ) . send ( "updated" ) ;
158- } ,
164+ }
159165) ;
0 commit comments