POST watching

This commit is contained in:
daru
2022-04-15 12:13:23 +02:00
parent 25259653e7
commit be195bed30
6 changed files with 257 additions and 143 deletions

53
ober.go
View File

@@ -1,7 +1,6 @@
package main
import (
"crypto/sha512"
"encoding/json"
"fmt"
"log"
@@ -45,14 +44,11 @@ func Season(ctx *fasthttp.RequestCtx) {
return
}
_, err = ctx.Write(data)
err = writeResponseBody(ctx, data)
if err != nil {
addErrorToCtx(ctx, err)
return
}
ctx.SetContentType("application/json; charset=utf-8")
ctx.SetStatusCode(fasthttp.StatusOK)
}
func WatchGet(ctx *fasthttp.RequestCtx) {
@@ -126,7 +122,7 @@ func Register(ctx *fasthttp.RequestCtx) {
return
}
calcSauce := fmt.Sprintf("%x", sha512.Sum512([]byte(registerSecret+strconv.FormatInt(register.MalID, 10)+register.Username)))
calcSauce := Sauce(register.MalID, register.Username)
if calcSauce != strings.ToLower(register.Sauce) {
ctx.WriteString("Möge die Sauce mit dir sein")
ctx.SetStatusCode(fasthttp.StatusBadRequest)
@@ -174,7 +170,8 @@ func WatchPost(ctx *fasthttp.RequestCtx) {
return
}
username := fmt.Sprintf("%s", ctx.UserValue("user"))
if !GheddoAuth(username, string(auth)) {
legit, userId := GheddoAuth(username, string(auth))
if !legit {
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
return
}
@@ -188,10 +185,21 @@ func WatchPost(ctx *fasthttp.RequestCtx) {
return
}
// iterate sent animes
for _, anime := range animes {
// TODO check if anime is in season
// check if anime is in season
_, err = SearchSeason(anime.Anime)
if err == nil {
// anime exitsts => save
animeData, err := AddUserToAnime(username, userId, anime.Anime)
if err != nil {
addErrorToCtx(ctx, err)
return
}
anime.Users = animeData.Users
}
// iterate sent animes
// TODO
detail, _, err := GetAnimeDetailData(anime.Anime)
if err != nil {
ctx.WriteString(err.Error())
@@ -199,15 +207,36 @@ func WatchPost(ctx *fasthttp.RequestCtx) {
return
}
fmt.Printf("%+v\n", detail)
data, err := json.Marshal(animes)
if err != nil {
addErrorToCtx(ctx, err)
return
}
err = writeResponseBody(ctx, data)
if err != nil {
addErrorToCtx(ctx, err)
return
}
}
}
func GheddoAuth(username, auth string) bool {
func GheddoAuth(username, auth string) (bool, int64) {
user, err := ReadUser(username)
if err != nil {
return false
return false, 0
}
return user.Secret == auth
return user.Secret == auth, user.MalID
}
func writeResponseBody(ctx *fasthttp.RequestCtx, bytes []byte) error {
_, err := ctx.Write(bytes)
if err != nil {
return err
}
ctx.SetContentType("application/json; charset=utf-8")
ctx.SetStatusCode(fasthttp.StatusOK)
return err
}
func addErrorToCtx(ctx *fasthttp.RequestCtx, err error) {