mirror of
https://github.com/ultrasn0w/huso.git
synced 2025-12-14 10:39:53 +01:00
GET watch extended
This commit is contained in:
2
klotz.go
2
klotz.go
@@ -7,7 +7,7 @@ type AnimeUser struct {
|
|||||||
Users []WatchUser `json:"users"`
|
Users []WatchUser `json:"users"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AnimeUserDetail struct {
|
type AnimeUserExtended struct {
|
||||||
Anime int64 `json:"anime"`
|
Anime int64 `json:"anime"`
|
||||||
Users []WatchUser `json:"users"`
|
Users []WatchUser `json:"users"`
|
||||||
Data Anime `json:"data"`
|
Data Anime `json:"data"`
|
||||||
|
|||||||
145
ober.go
145
ober.go
@@ -19,6 +19,7 @@ func RunWebserv() {
|
|||||||
r.GET("/api/anime/{id}", Headers(AnimeGet))
|
r.GET("/api/anime/{id}", Headers(AnimeGet))
|
||||||
r.GET("/api/user/{user?}", Headers(UserGet))
|
r.GET("/api/user/{user?}", Headers(UserGet))
|
||||||
r.GET("/api/watch/{user?}", Headers(WatchGet))
|
r.GET("/api/watch/{user?}", Headers(WatchGet))
|
||||||
|
r.GET("/api/watchext/{user?}", Headers(WatchExtendedGet))
|
||||||
r.POST("/api/register", Headers(Register))
|
r.POST("/api/register", Headers(Register))
|
||||||
r.POST("/api/watch/{user}", Headers(WatchPost))
|
r.POST("/api/watch/{user}", Headers(WatchPost))
|
||||||
r.DELETE("/api/register", Headers(UnRegister))
|
r.DELETE("/api/register", Headers(UnRegister))
|
||||||
@@ -143,43 +144,48 @@ func UserGet(ctx *fasthttp.RequestCtx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func WatchGet(ctx *fasthttp.RequestCtx) {
|
func WatchGet(ctx *fasthttp.RequestCtx) {
|
||||||
usrVal := ctx.UserValue("user")
|
animeUsers, err := watchGetLogic(ctx)
|
||||||
var userId int64
|
|
||||||
if usrVal != nil {
|
|
||||||
// check user exists
|
|
||||||
user, err := ReadUser(fmt.Sprintf("%s", usrVal))
|
|
||||||
if err != nil {
|
|
||||||
ctx.WriteString("Dich gibts nicht")
|
|
||||||
ctx.SetStatusCode(fasthttp.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
userId = user.MalID
|
|
||||||
}
|
|
||||||
animes, err := ReadAnimeUsers()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// check if no anime were inserted
|
return
|
||||||
if err != nutsdb.ErrBucketEmpty {
|
}
|
||||||
|
|
||||||
|
bytes, err := json.Marshal(animeUsers)
|
||||||
|
if err != nil {
|
||||||
|
addErrorToCtx(ctx, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = ctx.Write(bytes)
|
||||||
|
if err != nil {
|
||||||
|
addErrorToCtx(ctx, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.SetContentType("application/json; charset=utf-8")
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WatchExtendedGet(ctx *fasthttp.RequestCtx) {
|
||||||
|
animeUsers, err := watchGetLogic(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
animeUsersExtended := make([]AnimeUserExtended, 0)
|
||||||
|
// make list advanced
|
||||||
|
for _, a := range animeUsers {
|
||||||
|
data, _, err := GetAnimeDetailData(a.Anime)
|
||||||
|
if err != nil {
|
||||||
addErrorToCtx(ctx, err)
|
addErrorToCtx(ctx, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
animes = make([]AnimeUser, 0)
|
animeUsersExtended = append(animeUsersExtended, AnimeUserExtended{
|
||||||
|
Anime: a.Anime,
|
||||||
|
Users: a.Users,
|
||||||
|
Data: *data,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// apply single user logic
|
bytes, err := json.Marshal(animeUsersExtended)
|
||||||
if usrVal != nil {
|
|
||||||
filteredAnimes := make([]AnimeUser, 0)
|
|
||||||
for _, a := range animes {
|
|
||||||
for _, u := range a.Users {
|
|
||||||
if u.MalID == userId {
|
|
||||||
filteredAnimes = append(filteredAnimes, a)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
animes = filteredAnimes
|
|
||||||
}
|
|
||||||
|
|
||||||
bytes, err := json.Marshal(animes)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
addErrorToCtx(ctx, err)
|
addErrorToCtx(ctx, err)
|
||||||
return
|
return
|
||||||
@@ -305,6 +311,64 @@ func WatchDelete(ctx *fasthttp.RequestCtx) {
|
|||||||
processUpdateReq(ctx, false)
|
processUpdateReq(ctx, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GheddoAuth(username, auth string) (bool, int64) {
|
||||||
|
user, err := ReadUser(username)
|
||||||
|
if err != nil {
|
||||||
|
return false, 0
|
||||||
|
}
|
||||||
|
return user.Secret == auth, user.MalID
|
||||||
|
}
|
||||||
|
|
||||||
|
func Headers(h fasthttp.RequestHandler) fasthttp.RequestHandler {
|
||||||
|
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
|
||||||
|
if *localServer {
|
||||||
|
ctx.Response.Header.Set("Access-Control-Allow-Origin", "*")
|
||||||
|
ctx.Response.Header.Set("Access-Control-Allow-Headers", "*")
|
||||||
|
ctx.Response.Header.Set("Access-Control-Allow-Methods", "*")
|
||||||
|
}
|
||||||
|
h(ctx)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func watchGetLogic(ctx *fasthttp.RequestCtx) ([]AnimeUser, error) {
|
||||||
|
usrVal := ctx.UserValue("user")
|
||||||
|
var userId int64
|
||||||
|
if usrVal != nil {
|
||||||
|
// check user exists
|
||||||
|
user, err := ReadUser(fmt.Sprintf("%s", usrVal))
|
||||||
|
if err != nil {
|
||||||
|
ctx.WriteString("Dich gibts nicht")
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusNotFound)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
userId = user.MalID
|
||||||
|
}
|
||||||
|
animesUsers, err := ReadAnimeUsers()
|
||||||
|
if err != nil {
|
||||||
|
// check if no anime were inserted
|
||||||
|
if err != nutsdb.ErrBucketEmpty {
|
||||||
|
addErrorToCtx(ctx, err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
animesUsers = make([]AnimeUser, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// apply single user logic
|
||||||
|
if usrVal != nil {
|
||||||
|
filteredAnimes := make([]AnimeUser, 0)
|
||||||
|
for _, a := range animesUsers {
|
||||||
|
for _, u := range a.Users {
|
||||||
|
if u.MalID == userId {
|
||||||
|
filteredAnimes = append(filteredAnimes, a)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
animesUsers = filteredAnimes
|
||||||
|
}
|
||||||
|
return animesUsers, err
|
||||||
|
}
|
||||||
|
|
||||||
func processUpdateReq(ctx *fasthttp.RequestCtx, update bool) {
|
func processUpdateReq(ctx *fasthttp.RequestCtx, update bool) {
|
||||||
auth := ctx.Request.Header.Peek("X-HUSO-AUTH")
|
auth := ctx.Request.Header.Peek("X-HUSO-AUTH")
|
||||||
if ctx.UserValue("user") == nil || auth == nil || string(auth) == "" || string(ctx.Request.Header.ContentType()) != "application/json" {
|
if ctx.UserValue("user") == nil || auth == nil || string(auth) == "" || string(ctx.Request.Header.ContentType()) != "application/json" {
|
||||||
@@ -370,25 +434,6 @@ func processUpdateReq(ctx *fasthttp.RequestCtx, update bool) {
|
|||||||
ctx.SetContentType("application/json; charset=utf-8")
|
ctx.SetContentType("application/json; charset=utf-8")
|
||||||
}
|
}
|
||||||
|
|
||||||
func GheddoAuth(username, auth string) (bool, int64) {
|
|
||||||
user, err := ReadUser(username)
|
|
||||||
if err != nil {
|
|
||||||
return false, 0
|
|
||||||
}
|
|
||||||
return user.Secret == auth, user.MalID
|
|
||||||
}
|
|
||||||
|
|
||||||
func Headers(h fasthttp.RequestHandler) fasthttp.RequestHandler {
|
|
||||||
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
|
|
||||||
if *localServer {
|
|
||||||
ctx.Response.Header.Set("Access-Control-Allow-Origin", "*")
|
|
||||||
ctx.Response.Header.Set("Access-Control-Allow-Headers", "*")
|
|
||||||
ctx.Response.Header.Set("Access-Control-Allow-Methods", "*")
|
|
||||||
}
|
|
||||||
h(ctx)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeResponseBody(ctx *fasthttp.RequestCtx, bytes []byte) error {
|
func writeResponseBody(ctx *fasthttp.RequestCtx, bytes []byte) error {
|
||||||
_, err := ctx.Write(bytes)
|
_, err := ctx.Write(bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user