2024-03-30 09:10:36 +00:00
|
|
|
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 {
|
2024-03-30 10:21:17 +00:00
|
|
|
ChannelID uint `json:"channel_id"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
Attachments []models.Attachment `json:"attachments"`
|
2024-03-30 09:10:36 +00:00
|
|
|
}
|
2024-03-30 10:21:17 +00:00
|
|
|
|
2024-03-30 09:10:36 +00:00
|
|
|
models.FitStruct(task.Payload, &req)
|
2024-03-30 10:21:17 +00:00
|
|
|
if len(req.Content) == 0 {
|
|
|
|
return &models.UnifiedCommand{
|
|
|
|
Action: "error",
|
|
|
|
Message: "content cannot be empty",
|
|
|
|
}
|
|
|
|
}
|
2024-03-30 09:10:36 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|