spark-server/internal/transport/http/handler/moment/handler.go
2026-09-18 17:51:42 +08:00

330 lines
6.4 KiB
Go

package momenthandler
import (
"strconv"
"github.com/gin-gonic/gin"
momentlogic "spark-server/internal/module/moment/logic"
"spark-server/internal/pkg/errs"
"spark-server/internal/pkg/reqctx"
"spark-server/internal/transport/http/respond"
)
// Handler is the Moments HTTP controller.
type Handler struct {
moments *momentlogic.Service
}
func New(moments *momentlogic.Service) *Handler {
return &Handler{moments: moments}
}
func currentUID(c *gin.Context) (int64, bool) {
rc := reqctx.From(c.Request.Context())
if rc == nil || rc.UserID <= 0 {
respond.JSONError(c, errs.ErrUnauthorized)
return 0, false
}
return rc.UserID, true
}
func pathInt(c *gin.Context, name string) (int64, bool) {
id, err := strconv.ParseInt(c.Param(name), 10, 64)
if err != nil || id <= 0 {
respond.JSONError(c, errs.ErrInvalidArgument)
return 0, false
}
return id, true
}
func queryInt(c *gin.Context, name string) int64 {
n, _ := strconv.ParseInt(c.Query(name), 10, 64)
return n
}
func (h *Handler) Publish(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
var in momentlogic.PublishInput
if err := respond.BindJSON(c, &in); err != nil {
respond.JSONError(c, err)
return
}
out, err := h.moments.Publish(c.Request.Context(), uid, &in)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) Feed(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
out, err := h.moments.Feed(c.Request.Context(), uid, queryInt(c, "cursor"), int(queryInt(c, "limit")))
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) Album(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
owner, ok := pathInt(c, "uid")
if !ok {
return
}
out, err := h.moments.Album(c.Request.Context(), uid, owner, queryInt(c, "cursor"), int(queryInt(c, "limit")))
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) Get(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
id, ok := pathInt(c, "id")
if !ok {
return
}
out, err := h.moments.Get(c.Request.Context(), uid, id)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) Delete(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
id, ok := pathInt(c, "id")
if !ok {
return
}
if err := h.moments.Delete(c.Request.Context(), uid, id); err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, gin.H{})
}
func (h *Handler) Like(c *gin.Context) {
h.mutatePost(c, func(uid, id int64) (*momentlogic.View, error) {
return h.moments.Like(c.Request.Context(), uid, id)
})
}
func (h *Handler) Unlike(c *gin.Context) {
h.mutatePost(c, func(uid, id int64) (*momentlogic.View, error) {
return h.moments.Unlike(c.Request.Context(), uid, id)
})
}
func (h *Handler) mutatePost(c *gin.Context, fn func(uid, id int64) (*momentlogic.View, error)) {
uid, ok := currentUID(c)
if !ok {
return
}
id, ok := pathInt(c, "id")
if !ok {
return
}
out, err := fn(uid, id)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) CreateComment(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
id, ok := pathInt(c, "id")
if !ok {
return
}
var in momentlogic.CommentInput
if err := respond.BindJSON(c, &in); err != nil {
respond.JSONError(c, err)
return
}
out, err := h.moments.Comment(c.Request.Context(), uid, id, &in)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) DeleteComment(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
id, ok := pathInt(c, "id")
if !ok {
return
}
cid, ok := pathInt(c, "cid")
if !ok {
return
}
out, err := h.moments.DeleteComment(c.Request.Context(), uid, id, cid)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) ListNotices(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
out, err := h.moments.ListNotices(c.Request.Context(), uid, queryInt(c, "cursor"), int(queryInt(c, "limit")))
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) ReadNotices(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
var in momentlogic.NoticeReadInput
if c.Request.ContentLength > 0 {
if err := respond.BindJSON(c, &in); err != nil {
respond.JSONError(c, err)
return
}
}
if err := h.moments.ReadNotices(c.Request.Context(), uid, &in); err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, gin.H{})
}
func (h *Handler) Unread(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
out, err := h.moments.UnreadFeed(c.Request.Context(), uid)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) Read(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
var in momentlogic.ReadInput
if err := respond.BindJSON(c, &in); err != nil {
respond.JSONError(c, err)
return
}
if err := h.moments.MarkFeedRead(c.Request.Context(), uid, in.PostID); err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, gin.H{})
}
func (h *Handler) GetSettings(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
out, err := h.moments.GetSettings(c.Request.Context(), uid)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) PatchSettings(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
var in momentlogic.PatchSettingsInput
if err := respond.BindJSON(c, &in); err != nil {
respond.JSONError(c, err)
return
}
out, err := h.moments.PatchSettings(c.Request.Context(), uid, &in)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) PutHide(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
peer, ok := pathInt(c, "uid")
if !ok {
return
}
var in momentlogic.HideInput
if err := respond.BindJSON(c, &in); err != nil {
respond.JSONError(c, err)
return
}
out, err := h.moments.PutHide(c.Request.Context(), uid, peer, &in)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) DeleteHide(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
peer, ok := pathInt(c, "uid")
if !ok {
return
}
out, err := h.moments.DeleteHide(c.Request.Context(), uid, peer)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}