spark-server/internal/transport/tcp/protocol/payload.go
zhr ae9a71034d feat: implement Spark IM V3 modular monolith
Add HTTP /v1 MVC, TCP send path, groups with server-side member-avatar collage, object-storage upload, and friends/favorites around the architecture-v3 layout.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-17 19:52:11 +08:00

70 lines
1.3 KiB
Go

package protocol
import (
"encoding/json"
"spark-server/internal/pkg/errs"
"spark-server/internal/pkg/reply"
)
type AuthRequest struct {
Token string `json:"token"`
DeviceID uint64 `json:"device_id"`
AppVersion string `json:"app_version"`
Language string `json:"language"`
}
type AuthResponse struct {
UserID int64 `json:"user_id"`
DeviceID uint64 `json:"device_id"`
}
type MsgIDBody struct {
MsgID int64 `json:"msg_id"`
}
type KickBody struct {
Reason string `json:"reason"`
}
type ErrorBody struct {
Code string
Message string
}
func EncodeAuthRequest(a AuthRequest) []byte {
b, _ := json.Marshal(a)
return b
}
func DecodeAuthRequest(body []byte) (AuthRequest, error) {
var req AuthRequest
if err := json.Unmarshal(body, &req); err != nil {
return AuthRequest{}, errs.ErrInvalidFrame
}
if req.Token == "" {
return AuthRequest{}, errs.ErrInvalidArgument
}
return req, nil
}
func EncodeError(e ErrorBody) []byte {
return reply.MustMarshal(reply.Fail(e.Code, e.Message))
}
func DecodeError(body []byte) (ErrorBody, error) {
env, err := reply.Unmarshal(body)
if err != nil {
return ErrorBody{}, err
}
return ErrorBody{Code: env.Code, Message: env.Msg}, nil
}
func EncodeMsgID(id int64) []byte {
return EncodeOK(MsgIDBody{MsgID: id})
}
func EncodeKick(reason string) []byte {
return EncodeOK(KickBody{Reason: reason})
}