/
routes.go
241 lines (200 loc) · 6.07 KB
/
routes.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
package routes
import (
"encoding/base64"
"net/http"
"net/url"
"strconv"
"time"
socketio "github.com/googollee/go-socket.io"
"github.com/horahoradev/horahora/front_api/config"
partyproto "github.com/horahoradev/horahora/partyservice/protocol"
schedulerproto "github.com/horahoradev/horahora/scheduler/protocol"
userproto "github.com/horahoradev/horahora/user_service/protocol"
videoproto "github.com/horahoradev/horahora/video_service/protocol"
"github.com/labstack/echo/v4"
)
type RouteHandler struct {
v videoproto.VideoServiceClient
u userproto.UserServiceClient
s schedulerproto.SchedulerClient
p partyproto.PartyserviceClient
srv *socketio.Server
}
func NewRouteHandler(v videoproto.VideoServiceClient, u userproto.UserServiceClient, s schedulerproto.SchedulerClient, p partyproto.PartyserviceClient, srv *socketio.Server) *RouteHandler {
return &RouteHandler{
v: v,
u: u,
s: s,
p: p,
srv: srv,
}
}
func SetupRoutes(e *echo.Echo, cfg *config.Config, srv *socketio.Server) {
r := NewRouteHandler(cfg.VideoClient, cfg.UserClient, cfg.SchedulerClient, cfg.PartyClient, srv)
e.GET("/api/home", r.getHome)
e.GET("/api/users/:id", r.getUser)
e.GET("/api/currentuserprofile/", r.getCurrentUserProfile)
e.GET("/api/auditevents/:id", r.handleGetAudits)
e.GET("/api/downloadsinprogress", r.handleGetDownloadsInProgress)
e.GET("/api/videos/:id", r.getVideo)
e.POST("/api/rate/:id", r.handleRating)
e.POST("/api/approve/:id", r.handleApproval)
e.POST("/api/login", r.handleLogin)
e.POST("/api/revoltlogin", r.handleRevoltLogin)
e.POST("/api/register", r.handleRegister)
e.POST("/api/logout", r.handleLogout)
e.GET("/api/archiverequests", r.getArchiveRequests)
e.GET("/api/archiveevents/:id", r.getArchiveEvents)
e.POST("/api/archiverequests", r.handleArchiveRequest)
e.POST("/api/delete-archiverequest", r.handleDeleteArchivalRequest)
e.POST("/api/retry-archiverequest", r.handleRetryArchivalRequest)
e.GET("/api/comments/:id", r.getComments)
e.POST("/api/comments/", r.handleComment)
e.POST("/api/comment_upvotes/", r.handleUpvote)
e.POST("/api/upload", r.upload)
e.POST("/api/ban/:id", r.handleBan)
e.POST("/api/delete/:id", r.handleDelete)
e.POST("/api/setrank/:userid/:rank", r.handleSetRank)
e.POST("/api/password-reset", r.handlePasswordReset)
// Watch party stuff goes here
e.POST("/api/newwatchparty/:id", r.handleNewWatchParty)
e.POST("/api/joinwatchparty/:id", r.handleJoinWatchParty)
e.POST("/api/heartbeat", r.handleHeartbeat)
e.GET("/api/partystate/:id", r.handleGetPartyState)
e.POST("/api/addvideo/:id", r.handleAddVideo)
e.POST("/api/nextvideo/:id", r.handleNextVideo)
e.GET("/api/unapprovedvideos", r.getUnapprovedVideos)
e.POST("/api/approvevideosvideo/:id", r.handleSchedulerVideoApproavl)
e.POST("/api/unapprovevideosvideo/:id", r.handleSchedulerVideoUnapproval)
e.GET("/api/getinferencecategories", r.getInferenceCategories)
e.POST("/api/addinferencecategory", r.addInferenceCategory)
}
type Video struct {
Title string
VideoID int64
Views uint64
AuthorID int64
AuthorName string
ThumbnailLoc string
Rating float64
}
type Comment struct {
ProfilePicture string
Username string
Comment string
}
type VideoInProgress struct {
Website string
VideoID string
DlStatus string
}
type UnapprovedVideo struct {
URL string
VideoID string
Category string
}
type VideoDetail struct {
Title string
MPDLoc string
Views uint64
Rating float64
VideoID int64
AuthorID int64
Username string
UserDescription string
VideoDescription string
UserSubscribers uint64
ProfilePicture string
UploadDate string // should be a datetime
Tags []string
RecommendedVideos []Video
L *LoggedInUserData
}
type ProfileData struct {
PaginationData PaginationData
UserID int64
Username string
ProfilePictureURL string
Videos []Video
Banned bool
L *LoggedInUserData
}
type AuditEvent struct {
ID int64
UserID int64
Message string
Timestamp string
}
type AuditData struct {
Length int
Events []AuditEvent
}
type PaginationData struct {
NumberOfItems int
CurrentPage int
}
type ArchiveRequestsPageData struct {
ArchivalRequests []ArchivalRequest
}
type ArchiveEventsData struct {
ArchivalEvents []*schedulerproto.ArchivalEvent
}
type ArchivalRequest struct {
UserID int64
Url string
ArchivedVideos uint64
CurrentTotalVideos uint64
LastSynced string
BackoffFactor uint32
DownloadID uint64
UndownloadableVideos uint64
}
func setCookie(c echo.Context, jwt string) error {
cookie := new(http.Cookie)
cookie.Name = "jwt"
cookie.Value = base64.StdEncoding.EncodeToString([]byte(jwt)) //
cookie.Expires = time.Now().Add(24 * time.Hour)
cookie.SameSite = http.SameSiteStrictMode
//cookie.Secure = true // set this later
cookie.HttpOnly = false
c.SetCookie(cookie)
return c.JSON(http.StatusOK, nil)
}
type CommentData struct {
ID int64 `json:"id"`
CreationDate string `json:"created"`
Content string `json:"content"`
Username string `json:"fullname"`
ProfileImage string `json:"profile_picture_url"`
VoteScore int64 `json:"upvote_count"`
CurrUserHasUpvoted bool `json:"user_has_upvoted"`
ParentID int64 `json:"parent,omitempty"`
}
const (
videoKey = "file[0]"
thumbnailKey = "file[1]"
MINIMUM_NUMBER_OF_TAGS = 5
fileUploadChunkSize = 1024 * 1024
)
func getAsInt64(data url.Values, key string) (int64, error) {
val, err := url.QueryUnescape(data.Get(key))
if err != nil {
return 0, err
}
valInt, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return 0, err
}
return valInt, nil
}
func getAsBool(data url.Values, key string) (bool, error) {
val, err := url.QueryUnescape(data.Get(key))
if err != nil {
return false, err
}
valBool, err := strconv.ParseBool(val)
if err != nil {
return false, err
}
return valBool, nil
}