-
Notifications
You must be signed in to change notification settings - Fork 38
/
job.go
122 lines (98 loc) · 2.17 KB
/
job.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
package public
import (
"fmt"
)
type JobType string
const (
JobTypeRecording JobType = "recording"
JobTypeTranscribing = "transcribing"
)
type JobStatusType string
const (
JobStatusTypeStarted JobStatusType = "started"
JobStatusTypeFailed = "failed"
)
type JobStatus struct {
JobType JobType
Status JobStatusType
Error string `json:"omitempty"`
}
// We need aliases so that we can have different validation rules.
type RecordingJobInfo struct {
// Recording job ID
JobID string
// Call post ID
PostID string
// Recording files IDs
FileIDs []string
}
type Transcription struct {
Title string
Language string
FileIDs []string
}
type Transcriptions []Transcription
type TranscribingJobInfo struct {
// Transcribing job ID
JobID string
// Call post ID
PostID string
// Transcription metadata
Transcriptions Transcriptions
}
func (i RecordingJobInfo) IsValid() error {
if i.PostID == "" {
return fmt.Errorf("PostID should not be empty")
}
if len(i.FileIDs) == 0 {
return fmt.Errorf("invalid FileIDs length")
}
if i.JobID == "" {
return fmt.Errorf("JobID should not be empty")
}
return nil
}
func (i TranscribingJobInfo) IsValid() error {
if i.PostID == "" {
return fmt.Errorf("PostID should not be empty")
}
if len(i.Transcriptions) == 0 {
return fmt.Errorf("invalid Transcriptions length")
}
for _, t := range i.Transcriptions {
if err := t.IsValid(); err != nil {
return err
}
}
if i.JobID == "" {
return fmt.Errorf("JobID should not be empty")
}
return nil
}
func (t Transcription) IsValid() error {
if t.Language == "" {
return fmt.Errorf("Language should not be empty")
}
if len(t.FileIDs) < 2 {
return fmt.Errorf("invalid FileIDs length")
}
return nil
}
// We need to do some magic in order to go through the RCP layer without errors.
func (t Transcription) ToClientMap() map[string]any {
if t.Title == "" {
t.Title = t.Language
}
return map[string]any{
"title": t.Title,
"language": t.Language,
"file_id": t.FileIDs[0],
}
}
func (t Transcriptions) ToClientCaptions() []any {
captions := make([]any, len(t))
for i := range t {
captions[i] = t[i].ToClientMap()
}
return captions
}