2024-03-30 09:10:36 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/database"
|
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/models"
|
2024-03-31 14:49:08 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-03-30 09:10:36 +00:00
|
|
|
)
|
|
|
|
|
2024-03-30 10:21:17 +00:00
|
|
|
func CountMessage(channel models.Channel) int64 {
|
|
|
|
var count int64
|
|
|
|
if err := database.C.Where(models.Message{
|
2024-03-30 09:10:36 +00:00
|
|
|
ChannelID: channel.ID,
|
2024-03-30 10:21:17 +00:00
|
|
|
}).Model(&models.Message{}).Count(&count).Error; err != nil {
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListMessage(channel models.Channel, take int, offset int) ([]models.Message, error) {
|
|
|
|
if take > 100 {
|
|
|
|
take = 100
|
|
|
|
}
|
|
|
|
|
|
|
|
var messages []models.Message
|
2024-03-30 10:40:21 +00:00
|
|
|
if err := database.C.
|
|
|
|
Where(models.Message{
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
}).Limit(take).Offset(offset).
|
|
|
|
Order("created_at DESC").
|
2024-03-30 17:27:41 +00:00
|
|
|
Preload("Attachments").
|
2024-03-31 13:19:51 +00:00
|
|
|
Preload("ReplyTo").
|
2024-03-31 15:15:44 +00:00
|
|
|
Preload("ReplyTo.Sender").
|
|
|
|
Preload("ReplyTo.Sender.Account").
|
2024-03-30 10:40:21 +00:00
|
|
|
Preload("Sender").
|
2024-03-30 14:33:20 +00:00
|
|
|
Preload("Sender.Account").
|
2024-03-30 10:40:21 +00:00
|
|
|
Find(&messages).Error; err != nil {
|
2024-03-30 10:21:17 +00:00
|
|
|
return messages, err
|
|
|
|
} else {
|
|
|
|
return messages, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-30 10:40:21 +00:00
|
|
|
func GetMessage(channel models.Channel, id uint) (models.Message, error) {
|
|
|
|
var message models.Message
|
2024-03-30 14:33:20 +00:00
|
|
|
if err := database.C.
|
|
|
|
Where(models.Message{
|
|
|
|
BaseModel: models.BaseModel{ID: id},
|
|
|
|
ChannelID: channel.ID,
|
2024-03-30 17:27:41 +00:00
|
|
|
}).
|
2024-03-31 13:19:51 +00:00
|
|
|
Preload("ReplyTo").
|
2024-03-31 15:15:44 +00:00
|
|
|
Preload("ReplyTo.Sender").
|
|
|
|
Preload("ReplyTo.Sender.Account").
|
2024-03-30 17:27:41 +00:00
|
|
|
Preload("Attachments").
|
|
|
|
Preload("Sender").
|
2024-03-30 14:33:20 +00:00
|
|
|
Preload("Sender.Account").
|
|
|
|
First(&message).Error; err != nil {
|
2024-03-30 10:40:21 +00:00
|
|
|
return message, err
|
|
|
|
} else {
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMessageWithPrincipal(channel models.Channel, member models.ChannelMember, id uint) (models.Message, error) {
|
|
|
|
var message models.Message
|
|
|
|
if err := database.C.Where(models.Message{
|
|
|
|
BaseModel: models.BaseModel{ID: id},
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
SenderID: member.ID,
|
|
|
|
}).First(&message).Error; err != nil {
|
|
|
|
return message, err
|
|
|
|
} else {
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 13:19:51 +00:00
|
|
|
func NewMessage(message models.Message) (models.Message, error) {
|
2024-03-30 09:10:36 +00:00
|
|
|
var members []models.ChannelMember
|
|
|
|
if err := database.C.Save(&message).Error; err != nil {
|
|
|
|
return message, err
|
|
|
|
} else if err = database.C.Where(models.ChannelMember{
|
2024-03-31 13:19:51 +00:00
|
|
|
ChannelID: message.ChannelID,
|
2024-03-31 13:37:13 +00:00
|
|
|
}).Preload("Account").Find(&members).Error; err == nil {
|
2024-03-30 09:10:36 +00:00
|
|
|
for _, member := range members {
|
2024-03-31 15:15:44 +00:00
|
|
|
if member.ID == message.Sender.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-03-31 14:49:08 +00:00
|
|
|
err = NotifyAccount(member.Account, "New message at "+message.Channel.Name, message.Content, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("An error occurred when trying notify user.")
|
|
|
|
}
|
|
|
|
|
2024-03-31 13:19:51 +00:00
|
|
|
message, _ = GetMessage(message.Channel, message.ID)
|
2024-03-30 14:33:20 +00:00
|
|
|
PushCommand(member.AccountID, models.UnifiedCommand{
|
2024-03-30 09:10:36 +00:00
|
|
|
Action: "messages.new",
|
|
|
|
Payload: message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
2024-03-30 10:40:21 +00:00
|
|
|
|
|
|
|
func EditMessage(message models.Message) (models.Message, error) {
|
|
|
|
var members []models.ChannelMember
|
|
|
|
if err := database.C.Save(&message).Error; err != nil {
|
|
|
|
return message, err
|
|
|
|
} else if err = database.C.Where(models.ChannelMember{
|
|
|
|
ChannelID: message.ChannelID,
|
|
|
|
}).Find(&members).Error; err == nil {
|
2024-03-30 14:33:20 +00:00
|
|
|
message, _ = GetMessage(models.Channel{
|
|
|
|
BaseModel: models.BaseModel{ID: message.Channel.ID},
|
|
|
|
}, message.ID)
|
2024-03-30 10:40:21 +00:00
|
|
|
for _, member := range members {
|
2024-03-30 14:33:20 +00:00
|
|
|
PushCommand(member.AccountID, models.UnifiedCommand{
|
2024-03-30 10:40:21 +00:00
|
|
|
Action: "messages.update",
|
|
|
|
Payload: message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteMessage(message models.Message) (models.Message, error) {
|
2024-03-31 12:48:51 +00:00
|
|
|
prev, _ := GetMessage(models.Channel{
|
|
|
|
BaseModel: models.BaseModel{ID: message.Channel.ID},
|
|
|
|
}, message.ID)
|
|
|
|
|
2024-03-30 10:40:21 +00:00
|
|
|
var members []models.ChannelMember
|
|
|
|
if err := database.C.Delete(&message).Error; err != nil {
|
|
|
|
return message, err
|
|
|
|
} else if err = database.C.Where(models.ChannelMember{
|
|
|
|
ChannelID: message.ChannelID,
|
|
|
|
}).Find(&members).Error; err == nil {
|
|
|
|
for _, member := range members {
|
2024-03-30 14:33:20 +00:00
|
|
|
PushCommand(member.AccountID, models.UnifiedCommand{
|
2024-03-30 10:40:21 +00:00
|
|
|
Action: "messages.burnt",
|
2024-03-31 12:48:51 +00:00
|
|
|
Payload: prev,
|
2024-03-30 10:40:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|