对接
This commit is contained in:
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
bizlive "kra/internal/biz/live"
|
||||
@@ -14,10 +15,89 @@ import (
|
||||
type Live struct {
|
||||
service *liveservice.StreamService
|
||||
catalog *liveservice.CatalogService
|
||||
follows *liveservice.FollowService
|
||||
}
|
||||
|
||||
func NewLive(service *liveservice.StreamService, catalog *liveservice.CatalogService) *Live {
|
||||
return &Live{service: service, catalog: catalog}
|
||||
func NewLive(service *liveservice.StreamService, catalog *liveservice.CatalogService, follows *liveservice.FollowService) *Live {
|
||||
return &Live{service: service, catalog: catalog, follows: follows}
|
||||
}
|
||||
|
||||
func (h *Live) FollowStatus(c *gin.Context) {
|
||||
id, ok := businessID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
claims := Claims(c)
|
||||
if claims == nil {
|
||||
NoAuth(c, "请登录")
|
||||
return
|
||||
}
|
||||
followed, err := h.follows.Status(c.Request.Context(), claims.ID, id)
|
||||
if err != nil {
|
||||
businessFail(c, err)
|
||||
return
|
||||
}
|
||||
OKWithData(c, gin.H{"followed": followed})
|
||||
}
|
||||
|
||||
func (h *Live) Follow(c *gin.Context) {
|
||||
id, ok := businessID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
claims := Claims(c)
|
||||
if claims == nil {
|
||||
NoAuth(c, "请登录")
|
||||
return
|
||||
}
|
||||
followed, err := h.follows.Follow(c.Request.Context(), claims.ID, id)
|
||||
if err != nil {
|
||||
businessFail(c, err)
|
||||
return
|
||||
}
|
||||
OKWithData(c, gin.H{"followed": followed})
|
||||
}
|
||||
|
||||
func (h *Live) Unfollow(c *gin.Context) {
|
||||
id, ok := businessID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
claims := Claims(c)
|
||||
if claims == nil {
|
||||
NoAuth(c, "请登录")
|
||||
return
|
||||
}
|
||||
if err := h.follows.Unfollow(c.Request.Context(), claims.ID, id); err != nil {
|
||||
businessFail(c, err)
|
||||
return
|
||||
}
|
||||
OKWithData(c, gin.H{"followed": false})
|
||||
}
|
||||
|
||||
func (h *Live) Like(c *gin.Context) {
|
||||
id, ok := businessID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
// Do not create runtime stream state for arbitrary room IDs. The stream
|
||||
// repository is process-local, so validating the catalog first also keeps
|
||||
// bogus likes from accumulating forever.
|
||||
room, err := h.catalog.GetRoom(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
businessFail(c, err)
|
||||
return
|
||||
}
|
||||
if !room.Enabled || room.Status != bizlive.RoomStatusLive {
|
||||
businessFail(c, bizlive.ErrRoomNotLive)
|
||||
return
|
||||
}
|
||||
count, err := h.service.Like(c.Request.Context(), fmt.Sprintf("room-%d", id))
|
||||
if err != nil {
|
||||
businessFail(c, err)
|
||||
return
|
||||
}
|
||||
OKWithData(c, gin.H{"likeCount": count})
|
||||
}
|
||||
|
||||
func (h *Live) List(c *gin.Context) {
|
||||
@@ -114,17 +194,27 @@ func (h *Live) PublicRooms(c *gin.Context) {
|
||||
in.PageSize = 20
|
||||
}
|
||||
if in.OrderBy == "" {
|
||||
in.OrderBy = "sort_asc"
|
||||
in.OrderBy = "viewer_count_desc"
|
||||
}
|
||||
enabled := true
|
||||
in.Enabled = &enabled
|
||||
items, total, e := h.catalog.ListRooms(c.Request.Context(), in)
|
||||
items, total, e := h.catalog.ListPublicRooms(c.Request.Context(), in)
|
||||
if e != nil {
|
||||
businessFail(c, e)
|
||||
return
|
||||
}
|
||||
OKWithData(c, gin.H{"list": items, "total": total, "page": in.Page, "pageSize": in.PageSize})
|
||||
}
|
||||
func (h *Live) GetPublicRoom(c *gin.Context) {
|
||||
id, ok := businessID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
v, e := h.catalog.GetPublicRoom(c.Request.Context(), id)
|
||||
if e != nil {
|
||||
businessFail(c, e)
|
||||
return
|
||||
}
|
||||
OKWithData(c, v)
|
||||
}
|
||||
func (h *Live) GetRoom(c *gin.Context) {
|
||||
id, ok := businessID(c)
|
||||
if !ok {
|
||||
@@ -203,15 +293,6 @@ func (h *Live) SaveRoomSort(c *gin.Context) {
|
||||
OK(c)
|
||||
}
|
||||
|
||||
func (h *Live) LegacyList(c *gin.Context) {
|
||||
items, err := h.service.List(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"ok": false, "error": "直播列表获取失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true, "streams": items})
|
||||
}
|
||||
|
||||
func (h *Live) Find(c *gin.Context) {
|
||||
item, err := h.service.Get(c.Request.Context(), c.Param("streamId"))
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user