-
Notifications
You must be signed in to change notification settings - Fork 4
/
episode.go
128 lines (110 loc) · 3.27 KB
/
episode.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
package model
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/bcc-code/bcc-media-platform/backend/common"
"github.com/bcc-code/bcc-media-platform/backend/user"
"github.com/bcc-code/bcc-media-platform/backend/utils"
)
// EpisodeFrom coverts a common.Episode into an GQL episode type
func EpisodeFrom(ctx context.Context, e *common.Episode) *Episode {
ginCtx, _ := utils.GinCtx(ctx)
languages := user.GetLanguagesFromCtx(ginCtx)
var season *Season
if e.SeasonID.Valid {
season = &Season{
ID: strconv.Itoa(int(e.SeasonID.Int64)),
}
}
var extraDescription string
if v := e.ExtraDescription.GetValueOrNil(languages); v != nil {
extraDescription = *v
}
var legacyID *string
if e.LegacyID.Valid {
strID := strconv.Itoa(int(e.LegacyID.Int64))
legacyID = &strID
}
var legacyProgramID *string
if e.LegacyProgramID.Valid {
strID := strconv.Itoa(int(e.LegacyProgramID.Int64))
legacyProgramID = &strID
}
var image *string
if image == nil {
image = e.Images.GetDefault(languages, common.ImageStyleDefault)
}
var images []*Image
for style, img := range e.Images.GetForLanguages(languages) {
if img == nil {
continue
}
images = append(images, &Image{
Style: style,
URL: *img,
})
}
var episodeType EpisodeType
if e.Type == "episode" {
episodeType = EpisodeTypeEpisode
} else {
episodeType = EpisodeTypeStandalone
}
var title = e.Title.Get(languages)
if e.ProductionDateInTitle {
title = fmt.Sprintf("%s %s", strings.TrimSpace(title), utils.FormatInLocale(e.ProductionDate, languages))
}
episode := &Episode{
Chapters: []*Chapter{}, // Currently not supported
ID: strconv.Itoa(e.ID),
Status: statusFrom(e),
UUID: e.UUID.String(),
Type: episodeType,
PublishDate: e.PublishDate.Format(time.RFC3339),
LegacyID: legacyID,
LegacyProgramID: legacyProgramID,
ProductionDate: e.ProductionDate.Format(time.RFC3339),
ProductionDateInTitle: e.ProductionDateInTitle,
AvailableFrom: e.AvailableFrom.Format(time.RFC3339),
AvailableTo: e.AvailableTo.Format(time.RFC3339),
Title: title,
Description: e.Description.Get(languages),
ExtraDescription: extraDescription,
Season: season,
Duration: e.Duration,
AgeRating: e.AgeRating,
ImageURL: image,
Images: images,
AssetVersion: e.AssetVersion,
}
if e.Number.Valid {
num := int(e.Number.Int64)
episode.Number = &num
}
return episode
}
// EpisodeSectionItemFrom returns a SectionItem
func EpisodeSectionItemFrom(ctx context.Context, s *common.Episode, sort int, sectionStyle string, numberInTitle bool) *SectionItem {
ginCtx, _ := utils.GinCtx(ctx)
languages := user.GetLanguagesFromCtx(ginCtx)
episode := EpisodeFrom(ctx, s)
img := s.Images.GetDefault(languages, sectionStyle)
if img == nil {
img = episode.ImageURL
}
title := episode.Title
if numberInTitle && episode.Number != nil {
title = fmt.Sprintf("%d. %s", *episode.Number, title)
}
return &SectionItem{
ID: episode.ID,
Item: episode,
Title: title,
Description: episode.Description,
Image: img,
Sort: sort,
}
}