Chat feature

This commit is contained in:
daru
2022-05-14 18:31:58 +02:00
parent 22179e38c8
commit e660b52bd4
4 changed files with 113 additions and 7 deletions

73
ober.go
View File

@@ -20,10 +20,12 @@ func RunWebserv() {
r.GET("/api/anime/{id}", Headers(AnimeGet))
r.GET("/api/animesearch", Headers(AnimeSearchGet))
r.GET("/api/appointment", Headers(AppointmentGet))
r.GET("/api/chat/{id}", Headers(ChatGet))
r.GET("/api/log", Headers(LogGet))
r.GET("/api/user/{user?}", Headers(UserGet))
r.GET("/api/watch/{user?}", Headers(WatchGet))
r.GET("/api/watchext/{user?}", Headers(WatchExtendedGet))
r.POST("/api/chat/{id}/{user}", Headers(ChatPost))
r.POST("/api/register", Headers(Register))
r.POST("/api/watch/{user}", Headers(WatchPost))
r.DELETE("/api/register", Headers(UnRegister))
@@ -82,7 +84,7 @@ func AnimeGet(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
// check user exists
// check anime exists
malId, err := strconv.ParseInt(fmt.Sprintf("%s", idVal), 10, 64)
if err != nil {
ctx.WriteString(err.Error())
@@ -141,11 +143,41 @@ func AppointmentGet(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusNotImplemented)
}
func ChatGet(ctx *fasthttp.RequestCtx) {
idVal := ctx.UserValue("id")
if idVal == nil {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
animeId, err := strconv.ParseInt(fmt.Sprintf("%s", idVal), 10, 64)
if err != nil {
ctx.WriteString(err.Error())
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
text, err := ReadChat(animeId)
if err != nil {
if strings.Contains(err.Error(), "not found") {
ctx.WriteString(err.Error())
ctx.SetStatusCode(fasthttp.StatusNotFound)
return
}
addErrorToCtx(ctx, err)
return
}
_, err = ctx.WriteString(text)
if err != nil {
addErrorToCtx(ctx, err)
return
}
ctx.SetContentType("text/plain; charset=utf-8")
ctx.SetStatusCode(fasthttp.StatusOK)
}
func LogGet(ctx *fasthttp.RequestCtx) {
_, err := ctx.WriteString(logOut.String())
if err != nil {
addErrorToCtx(ctx, err)
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
return
}
ctx.SetContentType("text/plain; charset=utf-8")
@@ -317,6 +349,43 @@ func Register(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusOK)
}
func ChatPost(ctx *fasthttp.RequestCtx) {
auth := ctx.Request.Header.Peek("X-HUSO-AUTH")
if ctx.UserValue("id") == nil || ctx.UserValue("user") == nil || auth == nil || string(auth) == "" || !strings.Contains(string(ctx.Request.Header.ContentType()), "text/plain") {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
username := fmt.Sprintf("%s", ctx.UserValue("user"))
legit, _ := GheddoAuth(username, string(auth))
if !legit {
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
return
}
animeId, err := strconv.ParseInt(fmt.Sprintf("%s", ctx.UserValue("id")), 10, 64)
if err != nil {
ctx.WriteString(err.Error())
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
sent := strings.TrimSpace(string(ctx.PostBody()))
if sent == "" {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
text, err := SaveChat(animeId, username, sent)
if err != nil {
addErrorToCtx(ctx, err)
return
}
_, err = ctx.WriteString(text)
if err != nil {
addErrorToCtx(ctx, err)
return
}
ctx.SetContentType("text/plain; charset=utf-8")
ctx.SetStatusCode(fasthttp.StatusOK)
}
func WatchPost(ctx *fasthttp.RequestCtx) {
processUpdateReq(ctx, true)
}