/
context.go
232 lines (188 loc) · 5.71 KB
/
context.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
// Copyright (C) 2022 The Takeout Authors.
//
// This file is part of Takeout.
//
// Takeout is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// Takeout is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
// more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Takeout. If not, see <https://www.gnu.org/licenses/>.
package server
import (
"context"
"fmt"
"html/template"
"net/http"
"github.com/defsub/takeout/activity"
"github.com/defsub/takeout/auth"
"github.com/defsub/takeout/config"
"github.com/defsub/takeout/lib/client"
"github.com/defsub/takeout/music"
"github.com/defsub/takeout/podcast"
"github.com/defsub/takeout/progress"
"github.com/defsub/takeout/video"
)
type contextKey string
var (
contextKeyContext = contextKey("context")
)
func withContext(r *http.Request, ctx Context) *http.Request {
return r.WithContext(context.WithValue(r.Context(), contextKeyContext, ctx))
}
func contextValue(r *http.Request) Context {
return r.Context().Value(contextKeyContext).(Context)
}
type Context interface {
Activity() *activity.Activity
Auth() *auth.Auth
Config() *config.Config
Music() *music.Music
Podcast() *podcast.Podcast
Progress() *progress.Progress
Template() *template.Template
User() *auth.User
Session() *auth.Session
Video() *video.Video
ImageClient() *client.Client
LocateTrack(music.Track) string
LocateMovie(video.Movie) string
LocateEpisode(podcast.Episode) string
FindArtist(string) (music.Artist, error)
FindRelease(string) (music.Release, error)
FindTrack(string) (music.Track, error)
FindStation(string) (music.Station, error)
FindMovie(string) (video.Movie, error)
FindSeries(string) (podcast.Series, error)
FindEpisode(string) (podcast.Episode, error)
TrackImage(music.Track) string
ArtistImage(music.Artist) string
ArtistBackground(music.Artist) string
MovieImage(video.Movie) string
EpisodeImage(podcast.Episode) string
}
type RequestContext struct {
activity *activity.Activity
auth *auth.Auth
config *config.Config
user *auth.User
media *Media
progress *progress.Progress
session *auth.Session
template *template.Template
imageClient *client.Client
}
func makeContext(ctx Context, u *auth.User, c *config.Config, m *Media) RequestContext {
return RequestContext{
activity: ctx.Activity(),
config: c,
media: m,
progress: ctx.Progress(),
template: ctx.Template(),
user: u,
}
}
func makeAuthOnlyContext(ctx Context, session *auth.Session) RequestContext {
return RequestContext{
auth: ctx.Auth(),
session: session,
}
}
func makeImageContext(ctx Context, client *client.Client) RequestContext {
return RequestContext{
imageClient: client,
}
}
func (ctx RequestContext) Activity() *activity.Activity {
return ctx.activity
}
func (ctx RequestContext) Auth() *auth.Auth {
return ctx.auth
}
func (ctx RequestContext) Config() *config.Config {
return ctx.config
}
func (ctx RequestContext) Music() *music.Music {
return ctx.media.music
}
func (ctx RequestContext) Podcast() *podcast.Podcast {
return ctx.media.podcast
}
func (ctx RequestContext) Progress() *progress.Progress {
return ctx.progress
}
func (ctx RequestContext) Template() *template.Template {
return ctx.template
}
func (ctx RequestContext) User() *auth.User {
return ctx.user
}
func (ctx RequestContext) Session() *auth.Session {
return ctx.session
}
func (ctx RequestContext) Video() *video.Video {
return ctx.media.video
}
func (RequestContext) LocateTrack(t music.Track) string {
return locateTrack(t)
}
func (RequestContext) LocateMovie(v video.Movie) string {
return locateMovie(v)
}
func (RequestContext) LocateEpisode(e podcast.Episode) string {
return locateEpisode(e)
}
func (ctx RequestContext) FindArtist(id string) (music.Artist, error) {
return ctx.Music().FindArtist(id)
}
func (ctx RequestContext) FindRelease(id string) (music.Release, error) {
return ctx.Music().FindRelease(id)
}
func (ctx RequestContext) FindTrack(id string) (music.Track, error) {
return ctx.Music().FindTrack(id)
}
func (ctx RequestContext) FindStation(id string) (music.Station, error) {
return ctx.Music().FindStation(id)
}
func (ctx RequestContext) FindMovie(id string) (video.Movie, error) {
return ctx.Video().FindMovie(id)
}
func (ctx RequestContext) FindSeries(id string) (podcast.Series, error) {
return ctx.Podcast().FindSeries(id)
}
func (ctx RequestContext) FindEpisode(id string) (podcast.Episode, error) {
return ctx.Podcast().FindEpisode(id)
}
func (ctx RequestContext) TrackImage(t music.Track) string {
return ctx.Music().TrackImage(t).String()
}
func (ctx RequestContext) ArtistImage(a music.Artist) string {
return ctx.Music().ArtistImage(&a)
}
func (ctx RequestContext) ArtistBackground(a music.Artist) string {
return ctx.Music().ArtistBackground(&a)
}
func (ctx RequestContext) MovieImage(m video.Movie) string {
return ctx.Video().MoviePoster(m)
}
func (ctx RequestContext) EpisodeImage(e podcast.Episode) string {
return ctx.Podcast().EpisodeImage(e)
}
func (ctx RequestContext) ImageClient() *client.Client {
return ctx.imageClient
}
func locateTrack(t music.Track) string {
return fmt.Sprintf("/api/tracks/%s/location", t.UUID)
}
func locateMovie(v video.Movie) string {
return fmt.Sprintf("/api/movies/%s/location", v.UUID)
}
func locateEpisode(e podcast.Episode) string {
return fmt.Sprintf("/api/episodes/%d/location", e.ID)
}