Messaging/pkg/services/connections.go
2024-03-30 18:21:17 +08:00

52 lines
1.3 KiB
Go

package services
import (
"git.solsynth.dev/hydrogen/messaging/pkg/models"
"github.com/gofiber/contrib/websocket"
"github.com/samber/lo"
)
var WsConn = make(map[uint][]*websocket.Conn)
func CheckOnline(user models.Account) bool {
return len(WsConn[user.ID]) > 0
}
func PushCommand(userId uint, task models.UnifiedCommand) {
for _, conn := range WsConn[userId] {
_ = conn.WriteMessage(1, task.Marshal())
}
}
func DealCommand(task models.UnifiedCommand, user models.Account) *models.UnifiedCommand {
switch task.Action {
case "messages.send.text":
var req struct {
ChannelID uint `json:"channel_id"`
Content string `json:"content"`
Attachments []models.Attachment `json:"attachments"`
}
models.FitStruct(task.Payload, &req)
if len(req.Content) == 0 {
return &models.UnifiedCommand{
Action: "error",
Message: "content cannot be empty",
}
}
if channel, member, err := GetAvailableChannel(req.ChannelID, user); err != nil {
return lo.ToPtr(models.UnifiedCommandFromError(err))
} else if _, err = NewTextMessage(req.Content, member, channel); err != nil {
return lo.ToPtr(models.UnifiedCommandFromError(err))
} else {
return nil
}
default:
return &models.UnifiedCommand{
Action: "error",
Message: "command not found",
}
}
}