🐛 Bug fixes of message pushing

This commit is contained in:
LittleSheep 2024-03-30 22:33:20 +08:00
parent eacfd7296e
commit 56af8688f6
3 changed files with 23 additions and 13 deletions

View File

@ -18,7 +18,7 @@ type Message struct {
Type MessageType `json:"type"`
Attachments []Attachment `json:"attachments"`
Channel Channel `json:"channel"`
Sender Account `json:"sender"`
Sender ChannelMember `json:"sender"`
ChannelID uint `json:"channel_id"`
SenderID uint `json:"sender_id"`
}

View File

@ -5,13 +5,13 @@ import (
"git.solsynth.dev/hydrogen/messaging/pkg/services"
"github.com/gofiber/contrib/websocket"
jsoniter "github.com/json-iterator/go"
"github.com/samber/lo"
)
func unifiedGateway(c *websocket.Conn) {
user := c.Locals("principal").(models.Account)
// Push connection
connectionIdx := len(services.WsConn)
services.WsConn[user.ID] = append(services.WsConn[user.ID], c)
// Event loop
@ -42,8 +42,7 @@ func unifiedGateway(c *websocket.Conn) {
}
// Pop connection
services.WsConn[user.ID] = append(
services.WsConn[user.ID][:connectionIdx],
services.WsConn[user.ID][connectionIdx+1:]...,
)
services.WsConn[user.ID] = lo.Filter(services.WsConn[user.ID], func(item *websocket.Conn, idx int) bool {
return item != c
})
}

View File

@ -28,6 +28,7 @@ func ListMessage(channel models.Channel, take int, offset int) ([]models.Message
}).Limit(take).Offset(offset).
Order("created_at DESC").
Preload("Sender").
Preload("Sender.Account").
Find(&messages).Error; err != nil {
return messages, err
} else {
@ -37,10 +38,13 @@ func ListMessage(channel models.Channel, take int, offset int) ([]models.Message
func GetMessage(channel models.Channel, id uint) (models.Message, error) {
var message models.Message
if err := database.C.Where(models.Message{
if err := database.C.
Where(models.Message{
BaseModel: models.BaseModel{ID: id},
ChannelID: channel.ID,
}).First(&message).Error; err != nil {
}).Preload("Sender").
Preload("Sender.Account").
First(&message).Error; err != nil {
return message, err
} else {
return message, nil
@ -77,7 +81,8 @@ func NewTextMessage(content string, sender models.ChannelMember, channel models.
ChannelID: channel.ID,
}).Find(&members).Error; err == nil {
for _, member := range members {
PushCommand(member.ID, models.UnifiedCommand{
message, _ = GetMessage(channel, message.ID)
PushCommand(member.AccountID, models.UnifiedCommand{
Action: "messages.new",
Payload: message,
})
@ -94,8 +99,11 @@ func EditMessage(message models.Message) (models.Message, error) {
} else if err = database.C.Where(models.ChannelMember{
ChannelID: message.ChannelID,
}).Find(&members).Error; err == nil {
message, _ = GetMessage(models.Channel{
BaseModel: models.BaseModel{ID: message.Channel.ID},
}, message.ID)
for _, member := range members {
PushCommand(member.ID, models.UnifiedCommand{
PushCommand(member.AccountID, models.UnifiedCommand{
Action: "messages.update",
Payload: message,
})
@ -112,8 +120,11 @@ func DeleteMessage(message models.Message) (models.Message, error) {
} else if err = database.C.Where(models.ChannelMember{
ChannelID: message.ChannelID,
}).Find(&members).Error; err == nil {
message, _ = GetMessage(models.Channel{
BaseModel: models.BaseModel{ID: message.Channel.ID},
}, message.ID)
for _, member := range members {
PushCommand(member.ID, models.UnifiedCommand{
PushCommand(member.AccountID, models.UnifiedCommand{
Action: "messages.burnt",
Payload: message,
})