Live video on the AT Protocol
1package model
2
3import (
4 "fmt"
5
6 "github.com/google/uuid"
7)
8
9type Thumbnail struct {
10 ID string `json:"id" gorm:"primaryKey"`
11 Format string `json:"format"`
12 SegmentID string `json:"segmentId" gorm:"index"`
13 Segment Segment `json:"segment,omitempty" gorm:"foreignKey:SegmentID;references:id"`
14}
15
16func (m *DBModel) CreateThumbnail(thumb *Thumbnail) error {
17 uu, err := uuid.NewV7()
18 if err != nil {
19 return err
20 }
21 if thumb.SegmentID == "" {
22 return fmt.Errorf("segmentID is required")
23 }
24 thumb.ID = uu.String()
25 err = m.DB.Model(Thumbnail{}).Create(thumb).Error
26 if err != nil {
27 return err
28 }
29 return nil
30}
31
32// return the most recent thumbnail for a user
33func (m *DBModel) LatestThumbnailForUser(user string) (*Thumbnail, error) {
34 var thumbnail Thumbnail
35
36 res := m.DB.Table("thumbnails AS t").
37 Select("t.*").
38 Joins("JOIN segments AS s ON t.segment_id = s.id").
39 Where("s.repo_did = ?", user).
40 Order("s.start_time DESC").
41 Limit(1).
42 Scan(&thumbnail)
43
44 if res.RowsAffected == 0 {
45 return nil, nil
46 }
47 if res.Error != nil {
48 return nil, res.Error
49 }
50
51 var seg Segment
52 err := m.DB.First(&seg, "id = ?", thumbnail.SegmentID).Error
53 if err != nil {
54 return nil, fmt.Errorf("could not find segment for thumbnail SegmentID=%s", thumbnail.SegmentID)
55 }
56
57 thumbnail.Segment = seg
58
59 return &thumbnail, nil
60}