mirror of
https://github.com/ultrasn0w/huso.git
synced 2025-12-14 10:39:53 +01:00
DELETE watching
This commit is contained in:
2
huso.go
2
huso.go
@@ -57,7 +57,7 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
defer cache.Close()
|
defer cache.Close()
|
||||||
animeCache, err = bigcache.NewBigCache(bigcache.DefaultConfig(time.Hour))
|
animeCache, err = bigcache.NewBigCache(bigcache.DefaultConfig(6 * time.Hour))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
45
nuss.go
45
nuss.go
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/xujiajun/nutsdb"
|
"github.com/xujiajun/nutsdb"
|
||||||
)
|
)
|
||||||
@@ -75,6 +76,50 @@ func AddUserToAnime(username string, userId, animeId int64) (*Anime, error) {
|
|||||||
return &anime, err
|
return &anime, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteUserFromAnime(username string, userId, animeId int64) (*Anime, error) {
|
||||||
|
var anime Anime
|
||||||
|
err := db.Update(
|
||||||
|
func(tx *nutsdb.Tx) error {
|
||||||
|
keyBytes := Int64ToByte(animeId)
|
||||||
|
e, err := tx.Get(bucketAnime, keyBytes)
|
||||||
|
var users []WatchUser
|
||||||
|
if err != nil {
|
||||||
|
users = make([]WatchUser, 0)
|
||||||
|
} else {
|
||||||
|
// parse user list
|
||||||
|
users, err = parseUserList(e.Value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
// check if user already part
|
||||||
|
for i, u := range users {
|
||||||
|
// early terminate
|
||||||
|
if u.MalID == userId {
|
||||||
|
// delete user from list
|
||||||
|
users[i] = users[len(users)-1]
|
||||||
|
users = users[:len(users)-1]
|
||||||
|
// check if anime needs recycling
|
||||||
|
anime = Anime{
|
||||||
|
Anime: animeId,
|
||||||
|
Users: users,
|
||||||
|
}
|
||||||
|
if len(users) == 0 {
|
||||||
|
return tx.Delete(bucketAnime, keyBytes)
|
||||||
|
}
|
||||||
|
newData, err := json.Marshal(users)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tx.Put(bucketAnime, keyBytes, newData, nutsdb.Persistent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("%s %d schaut nicht %d", username, userId, animeId)
|
||||||
|
})
|
||||||
|
return &anime, err
|
||||||
|
}
|
||||||
|
|
||||||
func ReadAnimes() ([]Anime, error) {
|
func ReadAnimes() ([]Anime, error) {
|
||||||
var animes []Anime
|
var animes []Anime
|
||||||
err := db.View(
|
err := db.View(
|
||||||
|
|||||||
67
ober.go
67
ober.go
@@ -163,6 +163,14 @@ func Register(ctx *fasthttp.RequestCtx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func WatchPost(ctx *fasthttp.RequestCtx) {
|
func WatchPost(ctx *fasthttp.RequestCtx) {
|
||||||
|
processUpdateReq(ctx, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WatchDelete(ctx *fasthttp.RequestCtx) {
|
||||||
|
processUpdateReq(ctx, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
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" {
|
||||||
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||||
@@ -188,53 +196,42 @@ func WatchPost(ctx *fasthttp.RequestCtx) {
|
|||||||
for i, anime := range animes {
|
for i, anime := range animes {
|
||||||
// check if anime is in season
|
// check if anime is in season
|
||||||
_, err = SearchSeason(anime.Anime)
|
_, err = SearchSeason(anime.Anime)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
// anime exitsts => save
|
// anime not in season => holen sie diese
|
||||||
animeData, err := AddUserToAnime(username, userId, anime.Anime)
|
_, _, err := GetAnimeDetailData(anime.Anime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
addErrorToCtx(ctx, err)
|
ctx.WriteString(err.Error())
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
animes[i].Users = animeData.Users
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
var animeData *Anime
|
||||||
detail, _, err := GetAnimeDetailData(anime.Anime)
|
// update or delete request
|
||||||
if err != nil {
|
if update {
|
||||||
ctx.WriteString(err.Error())
|
// anime exitsts => save
|
||||||
ctx.SetStatusCode(fasthttp.StatusNotFound)
|
animeData, err = AddUserToAnime(username, userId, anime.Anime)
|
||||||
return
|
} else {
|
||||||
|
// anime exitsts => delete
|
||||||
|
animeData, err = DeleteUserFromAnime(username, userId, anime.Anime)
|
||||||
}
|
}
|
||||||
fmt.Printf("%+v\n", detail)
|
|
||||||
|
|
||||||
data, err := json.Marshal(animes)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
addErrorToCtx(ctx, err)
|
addErrorToCtx(ctx, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = writeResponseBody(ctx, data)
|
animes[i].Users = animeData.Users
|
||||||
if err != nil {
|
|
||||||
addErrorToCtx(ctx, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func WatchDelete(ctx *fasthttp.RequestCtx) {
|
|
||||||
auth := ctx.Request.Header.Peek("X-HUSO-AUTH")
|
|
||||||
if ctx.UserValue("user") == nil || auth == nil || string(auth) == "" || string(ctx.Request.Header.ContentType()) != "application/json" {
|
|
||||||
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
username := fmt.Sprintf("%s", ctx.UserValue("user"))
|
|
||||||
legit, userId := GheddoAuth(username, string(auth))
|
|
||||||
if !legit {
|
|
||||||
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
data, err := json.Marshal(animes)
|
||||||
fmt.Printf("%+v\n", userId)
|
if err != nil {
|
||||||
|
addErrorToCtx(ctx, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = writeResponseBody(ctx, data)
|
||||||
|
if err != nil {
|
||||||
|
addErrorToCtx(ctx, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GheddoAuth(username, auth string) (bool, int64) {
|
func GheddoAuth(username, auth string) (bool, int64) {
|
||||||
|
|||||||
Reference in New Issue
Block a user