支持 square/squarenovel/squaresteps/squarehealth 实例与 category 筛选;音乐增加免登录 grouped/track;订阅套餐按 package_name 区分;补 migration 0030/0031 与 OpenAPI。 Co-authored-by: Cursor <cursoragent@cursor.com>
271 lines
5.5 KiB
Go
271 lines
5.5 KiB
Go
package squarehandler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"spark-server/internal/module/square/entity"
|
|
squarelogic "spark-server/internal/module/square/logic"
|
|
"spark-server/internal/pkg/errs"
|
|
"spark-server/internal/pkg/reqctx"
|
|
"spark-server/internal/transport/http/respond"
|
|
)
|
|
|
|
// Handler is the square HTTP controller for one plaza channel.
|
|
type Handler struct {
|
|
square *squarelogic.Service
|
|
channel entity.Channel
|
|
}
|
|
|
|
func New(square *squarelogic.Service, channel entity.Channel) *Handler {
|
|
if channel == "" {
|
|
channel = entity.ChannelSquare
|
|
}
|
|
return &Handler{square: square, channel: channel}
|
|
}
|
|
|
|
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 queryFloat(c *gin.Context, name string) float64 {
|
|
n, _ := strconv.ParseFloat(c.Query(name), 64)
|
|
return n
|
|
}
|
|
|
|
func (h *Handler) Categories(c *gin.Context) {
|
|
respond.JSON(c, gin.H{"items": h.square.Categories()})
|
|
}
|
|
|
|
func (h *Handler) Feed(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
cat, ok := entity.ParseCategory(c.Query("category"))
|
|
if !ok {
|
|
respond.JSONError(c, errs.ErrInvalidArgument)
|
|
return
|
|
}
|
|
if cat == "" {
|
|
cat = entity.CategoryRecommend
|
|
}
|
|
out, err := h.square.Feed(c.Request.Context(), uid, h.channel, squarelogic.FeedQuery{
|
|
Cursor: queryInt(c, "cursor"), Limit: int(queryInt(c, "limit")),
|
|
Category: cat, Lat: queryFloat(c, "lat"), Lng: queryFloat(c, "lng"),
|
|
})
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) Publish(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var in squarelogic.PublishInput
|
|
if err := respond.BindJSON(c, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
out, err := h.square.Publish(c.Request.Context(), uid, h.channel, &in)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) UserPosts(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
owner, ok := pathInt(c, "uid")
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.square.UserPosts(c.Request.Context(), uid, owner, h.channel, 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.square.Get(c.Request.Context(), uid, id, h.channel)
|
|
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.square.Delete(c.Request.Context(), uid, id, h.channel); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, gin.H{})
|
|
}
|
|
|
|
func (h *Handler) Like(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
id, ok := pathInt(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.square.Like(c.Request.Context(), uid, id, h.channel)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) Unlike(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
id, ok := pathInt(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.square.Unlike(c.Request.Context(), uid, id, h.channel)
|
|
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 squarelogic.CommentInput
|
|
if err := respond.BindJSON(c, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
out, err := h.square.Comment(c.Request.Context(), uid, id, h.channel, &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
|
|
}
|
|
postID, ok := pathInt(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
cid, ok := pathInt(c, "cid")
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.square.DeleteComment(c.Request.Context(), uid, postID, cid, h.channel)
|
|
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.square.ListNotices(c.Request.Context(), uid, h.channel, queryInt(c, "cursor"), int(queryInt(c, "limit")))
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) NoticeUnread(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.square.NoticeUnread(c.Request.Context(), uid, h.channel)
|
|
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 squarelogic.NoticeReadInput
|
|
if err := respond.BindJSON(c, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
if err := h.square.ReadNotices(c.Request.Context(), uid, h.channel, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, gin.H{})
|
|
}
|