203 lines
6.0 KiB
Go
203 lines
6.0 KiB
Go
package http
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
roomv1 "hyapp/api/proto/room/v1"
|
|
"hyapp/pkg/roomid"
|
|
)
|
|
|
|
// createRoom 把创建房间 HTTP JSON 请求转换为 room-service CreateRoom 命令。
|
|
func (h *Handler) createRoom(writer http.ResponseWriter, request *http.Request) {
|
|
var body struct {
|
|
RoomID string `json:"room_id"`
|
|
CommandID string `json:"command_id"`
|
|
SeatCount int32 `json:"seat_count"`
|
|
Mode string `json:"mode"`
|
|
}
|
|
|
|
if !decode(writer, request, &body) {
|
|
return
|
|
}
|
|
if !roomid.ValidStringID(body.RoomID) {
|
|
// CreateRoom rejects IDs that would later fail Tencent IM GroupID or RTC strRoomId.
|
|
writeError(writer, request, http.StatusBadRequest, codeInvalidArgument, "invalid argument")
|
|
return
|
|
}
|
|
body.Mode = strings.TrimSpace(body.Mode)
|
|
if body.SeatCount <= 0 || body.Mode == "" {
|
|
// gateway mirrors room-service's hard requirements so client errors stay INVALID_ARGUMENT.
|
|
writeError(writer, request, http.StatusBadRequest, codeInvalidArgument, "invalid argument")
|
|
return
|
|
}
|
|
|
|
resp, err := h.roomClient.CreateRoom(request.Context(), &roomv1.CreateRoomRequest{
|
|
Meta: meta(request, body.RoomID, body.CommandID),
|
|
SeatCount: body.SeatCount,
|
|
Mode: body.Mode,
|
|
})
|
|
write(writer, request, resp, err)
|
|
}
|
|
|
|
// joinRoom 把进房 HTTP JSON 请求转换为 room-service JoinRoom 命令。
|
|
func (h *Handler) joinRoom(writer http.ResponseWriter, request *http.Request) {
|
|
var body struct {
|
|
RoomID string `json:"room_id"`
|
|
CommandID string `json:"command_id"`
|
|
Role string `json:"role"`
|
|
}
|
|
|
|
if !decode(writer, request, &body) {
|
|
return
|
|
}
|
|
|
|
resp, err := h.roomClient.JoinRoom(request.Context(), &roomv1.JoinRoomRequest{
|
|
Meta: meta(request, body.RoomID, body.CommandID),
|
|
Role: body.Role,
|
|
})
|
|
write(writer, request, resp, err)
|
|
}
|
|
|
|
// leaveRoom 把退房 HTTP JSON 请求转换为 room-service LeaveRoom 命令。
|
|
func (h *Handler) leaveRoom(writer http.ResponseWriter, request *http.Request) {
|
|
var body struct {
|
|
RoomID string `json:"room_id"`
|
|
CommandID string `json:"command_id"`
|
|
}
|
|
|
|
if !decode(writer, request, &body) {
|
|
return
|
|
}
|
|
|
|
resp, err := h.roomClient.LeaveRoom(request.Context(), &roomv1.LeaveRoomRequest{
|
|
Meta: meta(request, body.RoomID, body.CommandID),
|
|
})
|
|
write(writer, request, resp, err)
|
|
}
|
|
|
|
// micUp 把上麦 HTTP JSON 请求转换为 room-service MicUp 命令。
|
|
func (h *Handler) micUp(writer http.ResponseWriter, request *http.Request) {
|
|
var body struct {
|
|
RoomID string `json:"room_id"`
|
|
CommandID string `json:"command_id"`
|
|
SeatNo int32 `json:"seat_no"`
|
|
}
|
|
|
|
if !decode(writer, request, &body) {
|
|
return
|
|
}
|
|
|
|
resp, err := h.roomClient.MicUp(request.Context(), &roomv1.MicUpRequest{
|
|
Meta: meta(request, body.RoomID, body.CommandID),
|
|
SeatNo: body.SeatNo,
|
|
})
|
|
write(writer, request, resp, err)
|
|
}
|
|
|
|
// micDown 把下麦 HTTP JSON 请求转换为 room-service MicDown 命令。
|
|
func (h *Handler) micDown(writer http.ResponseWriter, request *http.Request) {
|
|
var body struct {
|
|
RoomID string `json:"room_id"`
|
|
CommandID string `json:"command_id"`
|
|
TargetUserID int64 `json:"target_user_id"`
|
|
}
|
|
|
|
if !decode(writer, request, &body) {
|
|
return
|
|
}
|
|
|
|
resp, err := h.roomClient.MicDown(request.Context(), &roomv1.MicDownRequest{
|
|
Meta: meta(request, body.RoomID, body.CommandID),
|
|
TargetUserId: body.TargetUserID,
|
|
})
|
|
write(writer, request, resp, err)
|
|
}
|
|
|
|
// changeMicSeat 把换麦位 HTTP JSON 请求转换为 room-service ChangeMicSeat 命令。
|
|
func (h *Handler) changeMicSeat(writer http.ResponseWriter, request *http.Request) {
|
|
var body struct {
|
|
RoomID string `json:"room_id"`
|
|
CommandID string `json:"command_id"`
|
|
TargetUserID int64 `json:"target_user_id"`
|
|
SeatNo int32 `json:"seat_no"`
|
|
}
|
|
|
|
if !decode(writer, request, &body) {
|
|
return
|
|
}
|
|
|
|
resp, err := h.roomClient.ChangeMicSeat(request.Context(), &roomv1.ChangeMicSeatRequest{
|
|
Meta: meta(request, body.RoomID, body.CommandID),
|
|
TargetUserId: body.TargetUserID,
|
|
SeatNo: body.SeatNo,
|
|
})
|
|
write(writer, request, resp, err)
|
|
}
|
|
|
|
// muteUser 把禁言 HTTP JSON 请求转换为 room-service MuteUser 命令。
|
|
func (h *Handler) muteUser(writer http.ResponseWriter, request *http.Request) {
|
|
var body struct {
|
|
RoomID string `json:"room_id"`
|
|
CommandID string `json:"command_id"`
|
|
TargetUserID int64 `json:"target_user_id"`
|
|
Muted bool `json:"muted"`
|
|
}
|
|
|
|
if !decode(writer, request, &body) {
|
|
return
|
|
}
|
|
|
|
resp, err := h.roomClient.MuteUser(request.Context(), &roomv1.MuteUserRequest{
|
|
Meta: meta(request, body.RoomID, body.CommandID),
|
|
TargetUserId: body.TargetUserID,
|
|
Muted: body.Muted,
|
|
})
|
|
write(writer, request, resp, err)
|
|
}
|
|
|
|
// kickUser 把踢人 HTTP JSON 请求转换为 room-service KickUser 命令。
|
|
func (h *Handler) kickUser(writer http.ResponseWriter, request *http.Request) {
|
|
var body struct {
|
|
RoomID string `json:"room_id"`
|
|
CommandID string `json:"command_id"`
|
|
TargetUserID int64 `json:"target_user_id"`
|
|
}
|
|
|
|
if !decode(writer, request, &body) {
|
|
return
|
|
}
|
|
|
|
resp, err := h.roomClient.KickUser(request.Context(), &roomv1.KickUserRequest{
|
|
Meta: meta(request, body.RoomID, body.CommandID),
|
|
TargetUserId: body.TargetUserID,
|
|
})
|
|
write(writer, request, resp, err)
|
|
}
|
|
|
|
// sendGift 把送礼 HTTP JSON 请求转换为 room-service SendGift 命令。
|
|
// 扣费、幂等、房间表现和 outbox 仍由 room-service 的命令链路统一处理。
|
|
func (h *Handler) sendGift(writer http.ResponseWriter, request *http.Request) {
|
|
var body struct {
|
|
RoomID string `json:"room_id"`
|
|
CommandID string `json:"command_id"`
|
|
TargetUserID int64 `json:"target_user_id"`
|
|
GiftID string `json:"gift_id"`
|
|
GiftCount int32 `json:"gift_count"`
|
|
GiftUnitValue int64 `json:"gift_unit_value"`
|
|
}
|
|
|
|
if !decode(writer, request, &body) {
|
|
return
|
|
}
|
|
|
|
resp, err := h.roomClient.SendGift(request.Context(), &roomv1.SendGiftRequest{
|
|
Meta: meta(request, body.RoomID, body.CommandID),
|
|
TargetUserId: body.TargetUserID,
|
|
GiftId: body.GiftID,
|
|
GiftCount: body.GiftCount,
|
|
GiftUnitValue: body.GiftUnitValue,
|
|
})
|
|
write(writer, request, resp, err)
|
|
}
|