-
Notifications
You must be signed in to change notification settings - Fork 23
/
vod.go
868 lines (746 loc) · 28.2 KB
/
vod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
package vod
import (
"context"
"encoding/json"
"fmt"
"math"
"os"
"runtime"
"sort"
"strconv"
"time"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"github.com/zibbp/ganymede/ent"
"github.com/zibbp/ganymede/ent/channel"
"github.com/zibbp/ganymede/ent/vod"
"github.com/zibbp/ganymede/internal/cache"
"github.com/zibbp/ganymede/internal/chat"
"github.com/zibbp/ganymede/internal/database"
"github.com/zibbp/ganymede/internal/utils"
)
type Service struct {
Store *database.Database
}
func NewService(store *database.Database) *Service {
return &Service{Store: store}
}
type Vod struct {
ID uuid.UUID `json:"id"`
ExtID string `json:"ext_id"`
Platform utils.VodPlatform `json:"platform"`
Type utils.VodType `json:"type"`
Title string `json:"title"`
Duration int `json:"duration"`
Views int `json:"views"`
Resolution string `json:"resolution"`
Processing bool `json:"processing"`
ThumbnailPath string `json:"thumbnail_path"`
WebThumbnailPath string `json:"web_thumbnail_path"`
VideoPath string `json:"video_path"`
ChatPath string `json:"chat_path"`
ChatVideoPath string `json:"chat_video_path"`
InfoPath string `json:"info_path"`
CaptionPath string `json:"caption_path"`
StreamedAt time.Time `json:"streamed_at"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time `json:"created_at"`
FolderName string `json:"folder_name"`
FileName string `json:"file_name"`
Locked bool `json:"locked"`
}
type Pagination struct {
Offset int `json:"offset"`
Limit int `json:"limit"`
TotalCount int `json:"total_count"`
Pages int `json:"pages"`
Data []*ent.Vod `json:"data"`
}
func (s *Service) CreateVod(vodDto Vod, cUUID uuid.UUID) (*ent.Vod, error) {
v, err := s.Store.Client.Vod.Create().SetID(vodDto.ID).SetChannelID(cUUID).SetExtID(vodDto.ExtID).SetPlatform(vodDto.Platform).SetType(vodDto.Type).SetTitle(vodDto.Title).SetDuration(vodDto.Duration).SetViews(vodDto.Views).SetResolution(vodDto.Resolution).SetProcessing(vodDto.Processing).SetThumbnailPath(vodDto.ThumbnailPath).SetWebThumbnailPath(vodDto.WebThumbnailPath).SetVideoPath(vodDto.VideoPath).SetChatPath(vodDto.ChatPath).SetChatVideoPath(vodDto.ChatVideoPath).SetInfoPath(vodDto.InfoPath).SetCaptionPath(vodDto.CaptionPath).SetStreamedAt(vodDto.StreamedAt).SetFolderName(vodDto.FolderName).SetFileName(vodDto.FileName).SetLocked(vodDto.Locked).Save(context.Background())
if err != nil {
log.Debug().Err(err).Msg("error creating vod")
if _, ok := err.(*ent.ConstraintError); ok {
return nil, fmt.Errorf("channel does not exist")
}
return nil, fmt.Errorf("error creating vod: %v", err)
}
return v, nil
}
func (s *Service) GetVods(c echo.Context) ([]*ent.Vod, error) {
v, err := s.Store.Client.Vod.Query().WithChannel().Order(ent.Desc(vod.FieldStreamedAt)).All(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error getting vods")
return nil, fmt.Errorf("error getting vods: %v", err)
}
return v, nil
}
func (s *Service) GetVodsByChannel(c echo.Context, cUUID uuid.UUID) ([]*ent.Vod, error) {
v, err := s.Store.Client.Vod.Query().Where(vod.HasChannelWith(channel.ID(cUUID))).Order(ent.Desc(vod.FieldStreamedAt)).All(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error getting vods by channel")
return nil, fmt.Errorf("error getting vods by channel: %v", err)
}
return v, nil
}
func (s *Service) GetVod(vodID uuid.UUID) (*ent.Vod, error) {
v, err := s.Store.Client.Vod.Query().Where(vod.ID(vodID)).Only(context.Background())
if err != nil {
log.Debug().Err(err).Msg("error getting vod")
// if vod not found
if _, ok := err.(*ent.NotFoundError); ok {
return nil, fmt.Errorf("vod not found")
}
return nil, fmt.Errorf("error getting vod: %v", err)
}
return v, nil
}
func (s *Service) GetVodWithChannel(vodID uuid.UUID) (*ent.Vod, error) {
v, err := s.Store.Client.Vod.Query().Where(vod.ID(vodID)).WithChannel().Only(context.Background())
if err != nil {
log.Debug().Err(err).Msg("error getting vod")
// if vod not found
if _, ok := err.(*ent.NotFoundError); ok {
return nil, fmt.Errorf("vod not found")
}
return nil, fmt.Errorf("error getting vod: %v", err)
}
return v, nil
}
func (s *Service) DeleteVod(c echo.Context, vodID uuid.UUID, deleteFiles bool) error {
log.Debug().Msgf("deleting vod %s", vodID)
// delete vod and queue item
v, err := s.Store.Client.Vod.Query().Where(vod.ID(vodID)).WithQueue().WithChannel().Only(c.Request().Context())
if err != nil {
if _, ok := err.(*ent.NotFoundError); ok {
return fmt.Errorf("vod not found")
}
return fmt.Errorf("error deleting vod: %v", err)
}
if v.Edges.Queue != nil {
err = s.Store.Client.Queue.DeleteOneID(v.Edges.Queue.ID).Exec(c.Request().Context())
if err != nil {
return fmt.Errorf("error deleting queue item: %v", err)
}
}
// delete files
if deleteFiles {
log.Debug().Msgf("deleting files for vod %s", v.ID)
path := fmt.Sprintf("/vods/%s/%s", v.Edges.Channel.Name, v.FolderName)
err := utils.DeleteFolder(path)
if err != nil {
log.Debug().Err(err).Msg("error deleting files")
return err
}
}
err = s.Store.Client.Vod.DeleteOneID(vodID).Exec(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error deleting vod")
return fmt.Errorf("error deleting vod: %v", err)
}
return nil
}
func (s *Service) UpdateVod(c echo.Context, vodID uuid.UUID, vodDto Vod, cUUID uuid.UUID) (*ent.Vod, error) {
v, err := s.Store.Client.Vod.UpdateOneID(vodID).SetChannelID(cUUID).SetExtID(vodDto.ExtID).SetPlatform(vodDto.Platform).SetType(vodDto.Type).SetTitle(vodDto.Title).SetDuration(vodDto.Duration).SetViews(vodDto.Views).SetResolution(vodDto.Resolution).SetProcessing(vodDto.Processing).SetThumbnailPath(vodDto.ThumbnailPath).SetWebThumbnailPath(vodDto.WebThumbnailPath).SetVideoPath(vodDto.VideoPath).SetChatPath(vodDto.ChatPath).SetChatVideoPath(vodDto.ChatVideoPath).SetInfoPath(vodDto.InfoPath).SetCaptionPath(vodDto.CaptionPath).SetStreamedAt(vodDto.StreamedAt).SetLocked(vodDto.Locked).Save(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error updating vod")
// if vod not found
if _, ok := err.(*ent.NotFoundError); ok {
return nil, fmt.Errorf("vod not found")
}
return nil, fmt.Errorf("error updating vod: %v", err)
}
return v, nil
}
func (s *Service) CheckVodExists(extID string) (bool, error) {
_, err := s.Store.Client.Vod.Query().Where(vod.ExtID(extID)).Only(context.Background())
if err != nil {
log.Debug().Err(err).Msg("error checking vod exists")
// if vod not found
if _, ok := err.(*ent.NotFoundError); ok {
return false, nil
}
return false, fmt.Errorf("error checking vod exists: %v", err)
}
return true, nil
}
func (s *Service) SearchVods(c echo.Context, term string, limit int, offset int) (Pagination, error) {
var pagination Pagination
v, err := s.Store.Client.Vod.Query().Where(vod.TitleContainsFold(term)).Order(ent.Desc(vod.FieldStreamedAt)).WithChannel().Limit(limit).Offset(offset).All(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error searching vods")
return pagination, fmt.Errorf("error searching vods: %v", err)
}
totalCount, err := s.Store.Client.Vod.Query().Where(vod.TitleContainsFold(term)).Count(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error getting total vod count")
return pagination, fmt.Errorf("error getting total vod count: %v", err)
}
pagination.TotalCount = totalCount
pagination.Limit = limit
pagination.Offset = offset
pagination.Pages = int(math.Ceil(float64(totalCount) / float64(limit)))
pagination.Data = v
return pagination, nil
}
func (s *Service) GetVodPlaylists(c echo.Context, vodID uuid.UUID) ([]*ent.Playlist, error) {
v, err := s.Store.Client.Vod.Query().Where(vod.ID(vodID)).WithPlaylists().Only(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error getting vod playlists")
return nil, fmt.Errorf("error getting vod playlists: %v", err)
}
return v.Edges.Playlists, nil
}
func (s *Service) GetVodsPagination(c echo.Context, limit int, offset int, channelId uuid.UUID, types []utils.VodType) (Pagination, error) {
var pagination Pagination
// Query builder
vodQuery := s.Store.Client.Vod.Query()
// If channel id is not nil
if channelId != uuid.Nil {
vodQuery = vodQuery.Where(vod.HasChannelWith(channel.ID(channelId)))
}
// If types is not nil
if len(types) > 0 {
vodQuery = vodQuery.Where(vod.TypeIn(types...))
}
v, err := vodQuery.Order(ent.Desc(vod.FieldStreamedAt)).Limit(limit).Offset(offset).WithChannel().All(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error getting vods")
return pagination, fmt.Errorf("error getting vods: %v", err)
}
// Get total count
// Amount will differ depending on types supplied
totalCountQuery := s.Store.Client.Vod.Query()
// If channel id is not nil
if channelId != uuid.Nil {
totalCountQuery = totalCountQuery.Where(vod.HasChannelWith(channel.ID(channelId)))
}
// If types is not nil
if len(types) > 0 {
totalCountQuery = totalCountQuery.Where(vod.TypeIn(types...))
}
totalCount, err := totalCountQuery.Count(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error getting total vod count")
return pagination, fmt.Errorf("error getting total vod count: %v", err)
}
pagination.Limit = limit
pagination.Offset = offset
pagination.TotalCount = totalCount
pagination.Pages = int(math.Ceil(float64(totalCount) / float64(limit)))
pagination.Data = v
return pagination, nil
}
func (s *Service) GetUserIdFromChat(c echo.Context, vodID uuid.UUID) (*int64, error) {
v, err := s.Store.Client.Vod.Query().Where(vod.ID(vodID)).Only(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error getting vod")
return nil, fmt.Errorf("error getting vod: %v", err)
}
data, err := utils.ReadChatFile(v.ChatPath)
if err != nil {
log.Debug().Err(err).Msg("error reading chat file")
return nil, fmt.Errorf("error reading chat file: %v", err)
}
var chatData *chat.ChatNoEmotes
err = json.Unmarshal(data, &chatData)
if err != nil {
log.Debug().Err(err).Msg("error unmarshalling chat data")
return nil, fmt.Errorf("error unmarshalling chat data: %v", err)
}
// Older chat files have the streamer ID stored as a string, need to convert to an int64
var sID int64
switch streamerChatId := chatData.Streamer.ID.(type) {
case string:
sID, err = strconv.ParseInt(streamerChatId, 10, 64)
if err != nil {
log.Debug().Err(err).Msg("error parsing streamer chat id")
return nil, fmt.Errorf("error parsing streamer chat id: %v", err)
}
case float64:
sID = int64(streamerChatId)
}
if sID == 0 {
return nil, fmt.Errorf("error getting streamer id from chat")
}
return &sID, nil
}
func (s *Service) GetVodChatComments(c echo.Context, vodID uuid.UUID, start float64, end float64) (*[]chat.Comment, error) {
envDeployment := os.Getenv("ENV")
if envDeployment == "development" {
utils.PrintMemUsage()
}
v, err := s.Store.Client.Vod.Query().Where(vod.ID(vodID)).Only(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error getting vod chat")
return nil, fmt.Errorf("error getting vod chat: %v", err)
}
var chatData *chat.ChatNoEmotes
var comments []chat.Comment
cacheData, exists := cache.Cache().Get(v.ID.String())
if exists {
comments = cacheData.([]chat.Comment)
} else {
data, err := utils.ReadChatFile(v.ChatPath)
if err != nil {
log.Debug().Err(err).Msg("error getting vod chat")
return nil, fmt.Errorf("error getting vod chat: %v", err)
}
err = json.Unmarshal(data, &chatData)
if err != nil {
log.Debug().Err(err).Msg("error getting vod chat")
return nil, fmt.Errorf("error getting vod chat: %v", err)
}
comments = chatData.Comments
chatData = nil
runtime.GC()
// Sort the comments by their content offset seconds
sort.Slice(comments, func(i, j int) bool {
return comments[i].ContentOffsetSeconds < comments[j].ContentOffsetSeconds
})
// Set cache
err = cache.Cache().Set(v.ID.String(), comments, 10*time.Minute)
if err != nil {
log.Debug().Err(err).Msg("error setting cache")
return nil, fmt.Errorf("error setting cache: %v", err)
}
runtime.GC()
}
// Reset the cache
err = cache.Cache().Set(v.ID.String(), comments, 10*time.Minute)
if err != nil {
log.Debug().Err(err).Msg("error setting cache")
return nil, fmt.Errorf("error setting cache: %v", err)
}
var filteredComments []chat.Comment
// Use binary search to find the index of the first comment with an offset greater than the specified offset
// This is much faster than iterating through the entire slice
i := sort.Search(len(comments), func(i int) bool { return comments[i].ContentOffsetSeconds >= start })
// Iterate through the comments starting at the index found above
// Stop when we reach the end of the slice or the offset is greater than the specified end offset
for i < len(comments) && comments[i].ContentOffsetSeconds <= end {
filteredComments = append(filteredComments, comments[i])
i++
}
// Cleanup
chatData = nil
comments = nil
defer runtime.GC()
if envDeployment == "development" {
utils.PrintMemUsage()
}
return &filteredComments, nil
}
func (s *Service) GetNumberOfVodChatCommentsFromTime(c echo.Context, vodID uuid.UUID, start float64, commentCount int64) (*[]chat.Comment, error) {
envDeployment := os.Getenv("ENV")
if envDeployment == "development" {
utils.PrintMemUsage()
}
v, err := s.Store.Client.Vod.Query().Where(vod.ID(vodID)).Only(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error getting vod chat")
return nil, fmt.Errorf("error getting vod chat: %v", err)
}
var chatData *chat.ChatNoEmotes
var comments []chat.Comment
cacheData, exists := cache.Cache().Get(v.ID.String())
if exists {
comments = cacheData.([]chat.Comment)
} else {
data, err := utils.ReadChatFile(v.ChatPath)
if err != nil {
log.Debug().Err(err).Msg("error getting vod chat")
return nil, fmt.Errorf("error getting vod chat: %v", err)
}
err = json.Unmarshal(data, &chatData)
if err != nil {
log.Debug().Err(err).Msg("error getting vod chat")
return nil, fmt.Errorf("error getting vod chat: %v", err)
}
comments = chatData.Comments
chatData = nil
runtime.GC()
// Sort the comments by their content offset seconds
sort.Slice(comments, func(i, j int) bool {
return comments[i].ContentOffsetSeconds < comments[j].ContentOffsetSeconds
})
err = cache.Cache().Set(v.ID.String(), comments, 10*time.Minute)
if err != nil {
log.Debug().Err(err).Msg("error setting cache")
return nil, fmt.Errorf("error setting cache: %v", err)
}
runtime.GC()
}
// Reset the cache
err = cache.Cache().Set(v.ID.String(), comments, 10*time.Minute)
if err != nil {
log.Debug().Err(err).Msg("error setting cache")
return nil, fmt.Errorf("error setting cache: %v", err)
}
var filteredComments []chat.Comment
// Use binary search to find the index of the first comment with an offset greater than the specified offset
// This is much faster than iterating through the entire slice
i := sort.Search(len(comments), func(i int) bool { return comments[i].ContentOffsetSeconds >= start })
// Iterate backwards from the index found above to get the last commentCount comments before the start time
for j := i; len(filteredComments) < int(commentCount); j-- {
if j < 0 || j >= len(comments) {
break
}
comment := comments[j]
filteredComments = append(filteredComments, comment)
}
// Check if the index is less than the number of comments we want to return
if i-int(commentCount) >= 0 {
filteredComments = comments[i-int(commentCount) : i]
}
// Cleanup
chatData = nil
comments = nil
defer runtime.GC()
if envDeployment == "development" {
utils.PrintMemUsage()
}
return &filteredComments, nil
}
func (s *Service) GetVodChatEmotes(c echo.Context, vodID uuid.UUID) (*chat.GanymedeEmotes, error) {
v, err := s.Store.Client.Vod.Query().Where(vod.ID(vodID)).Only(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error getting vod chat emotes")
return nil, fmt.Errorf("error getting vod chat emotes: %v", err)
}
data, err := utils.ReadChatFile(v.ChatPath)
if err != nil {
log.Debug().Err(err).Msg("error getting vod chat emotes")
return nil, fmt.Errorf("error getting vod chat emotes: %v", err)
}
var chatData *chat.ChatOnlyEmotes
err = json.Unmarshal(data, &chatData)
if err != nil {
log.Debug().Err(err).Msg("error getting vod chat emotes")
return nil, fmt.Errorf("error getting vod chat emotes: %v", err)
}
defer runtime.GC()
var ganymedeEmotes chat.GanymedeEmotes
switch {
case len(chatData.Emotes.FirstParty) > 0 && len(chatData.Emotes.ThirdParty) > 0:
log.Debug().Msgf("VOD %s chat playback embedded emotes found in 'emotes'", vodID)
for _, emote := range chatData.Emotes.FirstParty {
var ganymedeEmote chat.GanymedeEmote
ganymedeEmote.Name = fmt.Sprint(emote.Name)
ganymedeEmote.ID = emote.ID
ganymedeEmote.URL = emote.Data
ganymedeEmote.Type = "embed"
ganymedeEmote.Width = emote.Width
ganymedeEmote.Height = emote.Height
ganymedeEmotes.Emotes = append(ganymedeEmotes.Emotes, ganymedeEmote)
}
// Loop through third party emotes
for _, emote := range chatData.Emotes.ThirdParty {
var ganymedeEmote chat.GanymedeEmote
ganymedeEmote.Name = fmt.Sprint(emote.Name)
ganymedeEmote.ID = emote.ID
ganymedeEmote.URL = emote.Data
ganymedeEmote.Type = "embed"
ganymedeEmote.Width = emote.Width
ganymedeEmote.Height = emote.Height
ganymedeEmotes.Emotes = append(ganymedeEmotes.Emotes, ganymedeEmote)
}
case len(chatData.EmbeddedData.FirstParty) > 0 && len(chatData.EmbeddedData.ThirdParty) > 0:
log.Debug().Msgf("VOD %s chat playback embedded emotes found in 'emebeddedData'", vodID)
for _, emote := range chatData.EmbeddedData.FirstParty {
var ganymedeEmote chat.GanymedeEmote
ganymedeEmote.Name = fmt.Sprint(emote.Name)
ganymedeEmote.ID = emote.ID
ganymedeEmote.URL = emote.Data
ganymedeEmote.Type = "embed"
ganymedeEmote.Width = emote.Width
ganymedeEmote.Height = emote.Height
ganymedeEmotes.Emotes = append(ganymedeEmotes.Emotes, ganymedeEmote)
}
// Loop through third party emotes
for _, emote := range chatData.EmbeddedData.ThirdParty {
var ganymedeEmote chat.GanymedeEmote
ganymedeEmote.Name = fmt.Sprint(emote.Name)
ganymedeEmote.ID = emote.ID
ganymedeEmote.URL = emote.Data
ganymedeEmote.Type = "embed"
ganymedeEmote.Width = emote.Width
ganymedeEmote.Height = emote.Height
ganymedeEmotes.Emotes = append(ganymedeEmotes.Emotes, ganymedeEmote)
}
default:
log.Debug().Msgf("VOD %s chat playback embedded emotes not found, fetching emotes from providers", vodID)
twitchGlobalEmotes, err := chat.GetTwitchGlobalEmotes()
if err != nil {
log.Debug().Err(err).Msg("error getting twitch global emotes")
return nil, fmt.Errorf("error getting twitch global emotes: %v", err)
}
// Older chat files have the streamer ID stored as a string, need to convert to an int64
var sID int64
switch streamerChatId := chatData.Streamer.ID.(type) {
case string:
sID, err = strconv.ParseInt(streamerChatId, 10, 64)
if err != nil {
log.Debug().Err(err).Msg("error parsing streamer chat id")
return nil, fmt.Errorf("error parsing streamer chat id: %v", err)
}
case float64:
sID = int64(streamerChatId)
}
twitchChannelEmotes, err := chat.GetTwitchChannelEmotes(sID)
if err != nil {
log.Debug().Err(err).Msg("error getting twitch channel emotes")
return nil, fmt.Errorf("error getting twitch channel emotes: %v", err)
}
sevenTVGlobalEmotes, err := chat.Get7TVGlobalEmotes()
if err != nil {
log.Debug().Err(err).Msg("error getting 7tv global emotes")
return nil, fmt.Errorf("error getting 7tv global emotes: %v", err)
}
sevenTVChannelEmotes, err := chat.Get7TVChannelEmotes(sID)
if err != nil {
log.Debug().Err(err).Msg("error getting 7tv channel emotes")
return nil, fmt.Errorf("error getting 7tv channel emotes: %v", err)
}
bttvGlobalEmotes, err := chat.GetBTTVGlobalEmotes()
if err != nil {
log.Debug().Err(err).Msg("error getting bttv global emotes")
return nil, fmt.Errorf("error getting bttv global emotes: %v", err)
}
bttvChannelEmotes, err := chat.GetBTTVChannelEmotes(sID)
if err != nil {
log.Debug().Err(err).Msg("error getting bttv channel emotes")
return nil, fmt.Errorf("error getting bttv channel emotes: %v", err)
}
ffzGlobalEmotes, err := chat.GetFFZGlobalEmotes()
if err != nil {
log.Debug().Err(err).Msg("error getting ffz global emotes")
return nil, fmt.Errorf("error getting ffz global emotes: %v", err)
}
ffzChannelEmotes, err := chat.GetFFZChannelEmotes(sID)
if err != nil {
log.Debug().Err(err).Msg("error getting ffz channel emotes")
return nil, fmt.Errorf("error getting ffz channel emotes: %v", err)
}
// Loop through twitch global emotes
for _, emote := range twitchGlobalEmotes {
ganymedeEmotes.Emotes = append(ganymedeEmotes.Emotes, *emote)
}
// Loop through twitch channel emotes
for _, emote := range twitchChannelEmotes {
ganymedeEmotes.Emotes = append(ganymedeEmotes.Emotes, *emote)
}
// Loop through 7tv global emotes
for _, emote := range sevenTVGlobalEmotes {
ganymedeEmotes.Emotes = append(ganymedeEmotes.Emotes, *emote)
}
// Loop through 7tv channel emotes
for _, emote := range sevenTVChannelEmotes {
ganymedeEmotes.Emotes = append(ganymedeEmotes.Emotes, *emote)
}
// Loop through bttv global emotes
for _, emote := range bttvGlobalEmotes {
ganymedeEmotes.Emotes = append(ganymedeEmotes.Emotes, *emote)
}
// Loop through bttv channel emotes
for _, emote := range bttvChannelEmotes {
ganymedeEmotes.Emotes = append(ganymedeEmotes.Emotes, *emote)
}
// Loop through ffz global emotes
for _, emote := range ffzGlobalEmotes {
ganymedeEmotes.Emotes = append(ganymedeEmotes.Emotes, *emote)
}
// Loop through ffz channel emotes
for _, emote := range ffzChannelEmotes {
ganymedeEmotes.Emotes = append(ganymedeEmotes.Emotes, *emote)
}
}
chatData = nil
defer runtime.GC()
return &ganymedeEmotes, nil
}
func (s *Service) GetVodChatBadges(c echo.Context, vodID uuid.UUID) (*chat.GanymedeBadges, error) {
envDeployment := os.Getenv("ENV")
if envDeployment == "development" {
utils.PrintMemUsage()
}
v, err := s.Store.Client.Vod.Query().Where(vod.ID(vodID)).Only(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error getting vod chat emotes")
return nil, fmt.Errorf("error getting vod chat emotes: %v", err)
}
data, err := utils.ReadChatFile(v.ChatPath)
if err != nil {
log.Debug().Err(err).Msg("error getting vod chat emotes")
return nil, fmt.Errorf("error getting vod chat emotes: %v", err)
}
var chatData *chat.ChatOnlyBadges
var chatDataOld *chat.ChatOnlyBadgesOld
err = json.Unmarshal(data, &chatData)
if err != nil {
// attempt to unmarshal old format
err = json.Unmarshal(data, &chatDataOld)
if err != nil {
log.Debug().Err(err).Msg("error getting vod chat emotes")
return nil, fmt.Errorf("error getting vod chat emotes: %v", err)
}
}
// convert old format to new format
if chatDataOld != nil {
for _, badge := range chatDataOld.EmbeddedData.TwitchBadges {
var tmpBadges = make(map[string]chat.ChatTwitchBadgeVersion)
for v, imgData := range badge.Versions {
chatTwitchBadgeVersion := chat.ChatTwitchBadgeVersion{
Title: badge.Name,
Description: fmt.Sprintf("%s-%s", badge.Name, v),
Bytes: imgData,
}
tmpBadges[v] = chatTwitchBadgeVersion
}
chatData.EmbeddedData.TwitchBadges = append(chatData.EmbeddedData.TwitchBadges, chat.ChatTwitchBadge{
Name: badge.Name,
Versions: tmpBadges,
})
}
}
var badgeResp chat.GanymedeBadges
// If emebedded badges
if len(chatData.EmbeddedData.TwitchBadges) != 0 {
log.Debug().Msgf("VOD %s chat playback embedded badges found", vodID)
// Emebedded badges have duplicate arrays for each of the below
// So we need to check if we have already added the badge to the response
// To ensure we use the channel's badge and not the global one
subscriberBadgesSet := false
bitsBadgesSet := false
subGiftBadgesSet := false
for _, badge := range chatData.EmbeddedData.TwitchBadges {
if badge.Name == "subscriber" && !subscriberBadgesSet {
empty := false
for v, imgData := range badge.Versions {
// check if empty
if imgData.Title == "" {
empty = true
} else {
badgeResp.Badges = append(badgeResp.Badges, chat.GanymedeBadge{
Name: badge.Name,
Version: v,
Title: fmt.Sprintf("%s %s", badge.Name, v),
ImageUrl1X: fmt.Sprintf("data:image/png;base64,%s", imgData.Bytes),
})
}
}
if empty {
continue
} else {
subscriberBadgesSet = true
continue
}
}
if badge.Name == "bits" && !bitsBadgesSet {
empty := false
for v, imgData := range badge.Versions {
if imgData.Title == "" {
empty = true
} else {
badgeResp.Badges = append(badgeResp.Badges, chat.GanymedeBadge{
Name: badge.Name,
Version: v,
Title: fmt.Sprintf("%s %s", badge.Name, v),
ImageUrl1X: fmt.Sprintf("data:image/png;base64,%s", imgData.Bytes),
})
}
}
if empty {
continue
} else {
bitsBadgesSet = true
continue
}
}
if badge.Name == "sub-gifter" && !subGiftBadgesSet {
empty := false
for v, imgData := range badge.Versions {
if imgData.Title == "" {
empty = true
} else {
badgeResp.Badges = append(badgeResp.Badges, chat.GanymedeBadge{
Name: badge.Name,
Version: v,
Title: fmt.Sprintf("%s %s", badge.Name, v),
ImageUrl1X: fmt.Sprintf("data:image/png;base64,%s", imgData.Bytes),
})
}
}
if empty {
continue
} else {
subGiftBadgesSet = true
continue
}
}
if badge.Name != "subscriber" && badge.Name != "bits" && badge.Name != "sub-gifter" {
for v, imgData := range badge.Versions {
if imgData.Title == "" {
} else {
badgeResp.Badges = append(badgeResp.Badges, chat.GanymedeBadge{
Name: badge.Name,
Version: v,
Title: fmt.Sprintf("%s %s", badge.Name, v),
ImageUrl1X: fmt.Sprintf("data:image/png;base64,%s", imgData.Bytes),
})
}
break
}
}
}
} else {
log.Debug().Msgf("VOD %s chat playback embedded badges not found, fetching badges from providers", vodID)
// Older chat files have the streamer ID stored as a string, need to convert to an int64
var sID int64
switch streamerChatId := chatData.Streamer.ID.(type) {
case string:
sID, err = strconv.ParseInt(streamerChatId, 10, 64)
if err != nil {
log.Debug().Err(err).Msg("error parsing streamer chat id")
return nil, fmt.Errorf("error parsing streamer chat id: %v", err)
}
case float64:
sID = int64(streamerChatId)
}
twitchBadges, err := chat.GetTwitchGlobalBadges()
if err != nil {
log.Error().Err(err).Msg("error getting twitch global badges")
return nil, fmt.Errorf("error getting twitch global badges: %v", err)
}
channelBadges, err := chat.GetTwitchChannelBadges(sID)
if err != nil {
log.Error().Err(err).Msg("error getting twitch channel badges")
return nil, fmt.Errorf("error getting twitch channel badges: %v", err)
}
// Loop through twitch global badges
badgeResp.Badges = append(badgeResp.Badges, twitchBadges.Badges...)
// Loop through twitch channel badges
badgeResp.Badges = append(badgeResp.Badges, channelBadges.Badges...)
twitchBadges = nil
channelBadges = nil
}
chatData = nil
defer runtime.GC()
if envDeployment == "development" {
utils.PrintMemUsage()
}
return &badgeResp, nil
}
func (s *Service) LockVod(c echo.Context, vID uuid.UUID, status bool) error {
_, err := s.Store.Client.Vod.UpdateOneID(vID).SetLocked(status).Save(c.Request().Context())
if err != nil {
log.Debug().Err(err).Msg("error updating vod")
// if vod not found
if _, ok := err.(*ent.NotFoundError); ok {
return fmt.Errorf("vod not found")
}
return fmt.Errorf("error updating vod: %v", err)
}
return nil
}