/
live_stream_repository.go
245 lines (206 loc) · 6.17 KB
/
live_stream_repository.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
package stream
import (
"context"
"errors"
"fmt"
"sync"
"github.com/google/uuid"
"github.com/shigde/sfu/internal/activitypub/models"
"github.com/shigde/sfu/internal/auth"
"github.com/shigde/sfu/internal/lobby"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
var ErrStreamNotFound = errors.New("reading unknown live stream from store")
type LiveStreamRepository struct {
locker *sync.RWMutex
store storage
}
func NewLiveStreamRepository(store storage) *LiveStreamRepository {
return &LiveStreamRepository{
&sync.RWMutex{},
store,
}
}
func (r *LiveStreamRepository) Add(ctx context.Context, liveStream *LiveStream) (string, error) {
r.locker.Lock()
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
r.locker.Unlock()
cancel()
}()
if len(liveStream.UUID.String()) == 0 {
liveStream.UUID = uuid.New()
}
if liveStream.UUID == uuid.Nil {
liveStream.UUID = uuid.New()
}
result := tx.Create(liveStream)
if result.Error != nil || result.RowsAffected != 1 {
return "", fmt.Errorf("adding live stream: %w", result.Error)
}
return liveStream.UUID.String(), nil
}
func (r *LiveStreamRepository) AllBySpaceIdentifier(ctx context.Context, identifier string) ([]LiveStream, error) {
r.locker.RLock()
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
defer r.locker.RUnlock()
cancel()
}()
var streams []LiveStream
result := tx.Preload("Video").Joins("Space", tx.Where(&Space{Identifier: identifier})).Model(&LiveStream{}).Limit(100).Find(&streams)
if result.Error != nil {
return nil, fmt.Errorf("fetching all streams %w", result.Error)
}
return streams, nil
}
func (r *LiveStreamRepository) FindByUuid(ctx context.Context, streamUUID string) (*LiveStream, error) {
r.locker.RLock()
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
defer r.locker.RUnlock()
cancel()
}()
UUID, err := uuid.Parse(streamUUID)
if err != nil {
return nil, fmt.Errorf("parsing UUID: %w", err)
}
var liveStream LiveStream
result := tx.Preload("Space").Preload("Lobby").Preload("Account").Preload("Video").Where("uuid=?", UUID).First(&liveStream)
if result.Error != nil {
err := fmt.Errorf("finding stream by uuid %s: %w", streamUUID, result.Error)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, errors.Join(err, ErrStreamNotFound)
}
return nil, err
}
return &liveStream, nil
}
func (r *LiveStreamRepository) Delete(ctx context.Context, streamUUID string) error {
r.locker.Lock()
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
defer r.locker.Unlock()
cancel()
}()
UUID, err := uuid.Parse(streamUUID)
if err != nil {
return fmt.Errorf("parsing UUID: %w", err)
}
result := tx.Where("uuid=?", UUID).Delete(&LiveStream{})
if result.Error != nil {
return fmt.Errorf("deleting stream by id %s: %w", streamUUID, result.Error)
}
return nil
}
func (r *LiveStreamRepository) Contains(ctx context.Context, streamUUID string) bool {
r.locker.RLock()
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
defer r.locker.RUnlock()
cancel()
}()
UUID, err := uuid.Parse(streamUUID)
if err != nil {
return false
}
var count int64
tx.Where("uuid=?", UUID).Find(&LiveStream{}).Count(&count)
return count == 1
}
func (r *LiveStreamRepository) Update(ctx context.Context, stream *LiveStream) error {
r.locker.Lock()
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
defer r.locker.Unlock()
cancel()
}()
result := tx.Save(stream)
if result.Error != nil {
return fmt.Errorf("updating stream %s: %w", stream.UUID, result.Error)
}
return nil
}
func (r *LiveStreamRepository) Len(ctx context.Context) int64 {
r.locker.RLock()
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
defer r.locker.RUnlock()
cancel()
}()
var count int64
tx.Model(&LiveStream{}).Count(&count)
return count
}
func (r *LiveStreamRepository) getStoreWithContext(ctx context.Context) (*gorm.DB, context.CancelFunc) {
ctx, cancel := context.WithTimeout(ctx, queryTimeOut)
db := r.store.GetDatabase()
tx := db.WithContext(ctx)
return tx, cancel
}
func (r *LiveStreamRepository) UpsertLiveStream(ctx context.Context, stream *LiveStream) error {
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
cancel()
}()
account := auth.Account{User: stream.User}
resultAc := tx.Where("user=?", stream.User).First(&account)
if resultAc.Error != nil && !errors.Is(resultAc.Error, gorm.ErrRecordNotFound) {
return fmt.Errorf("serching account: %w", resultAc.Error)
}
space := Space{Identifier: stream.Space.Identifier}
resultSp := tx.Where("identifier=?", stream.Space.Identifier).First(&space)
if resultSp.Error != nil && !errors.Is(resultSp.Error, gorm.ErrRecordNotFound) {
return fmt.Errorf("serching space: %w", resultAc.Error)
}
lobbyEntity := lobby.LobbyEntity{}
resultLE := tx.Where("live_stream_id=?", stream.Lobby.LiveStreamId).First(&lobbyEntity)
if resultLE.Error != nil && !errors.Is(resultLE.Error, gorm.ErrRecordNotFound) {
return fmt.Errorf("serching lobby: %w", resultLE.Error)
}
if resultAc.RowsAffected > 0 {
stream.Account = &account
}
if resultSp.RowsAffected > 0 {
stream.Space = &space
}
if resultLE.RowsAffected > 0 {
stream.Lobby = &lobbyEntity
}
result := tx.Create(stream)
if result.Error != nil || result.RowsAffected == 0 {
return fmt.Errorf("adding live stream: %w", result.Error)
}
return nil
}
func (r *LiveStreamRepository) DeleteByUuid(ctx context.Context, streamUUID string) error {
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
cancel()
}()
result := tx.Unscoped().Delete(&LiveStream{}, "uuid = ?", streamUUID)
if result.Error != nil {
return fmt.Errorf("deleting stream by uuid %s: %w", streamUUID, result.Error)
}
return nil
}
func (r *LiveStreamRepository) BuildGuestAccounts(ctx context.Context, actors []*models.Actor) {
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
cancel()
}()
for _, actor := range actors {
user := buildFederatedId(actor.PreferredUsername, actor.GetActorIri().Host)
tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&auth.Account{
ActorId: actor.ID,
Actor: actor,
User: user,
UUID: uuid.NewString(),
})
}
return
}
func buildFederatedId(id string, domain string) string {
return fmt.Sprintf("%s@%s", id, domain)
}