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-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").
|
|
|
|
Preload("Sender").
|
|
|
|
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
|
|
|
|
if err := database.C.Where(models.Message{
|
|
|
|
BaseModel: models.BaseModel{ID: id},
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
}).First(&message).Error; err != nil {
|
|
|
|
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-30 10:21:17 +00:00
|
|
|
func NewTextMessage(content string, sender models.ChannelMember, channel models.Channel, attachments ...models.Attachment) (models.Message, error) {
|
|
|
|
message := models.Message{
|
|
|
|
Content: content,
|
|
|
|
Metadata: nil,
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
SenderID: sender.ID,
|
|
|
|
Attachments: attachments,
|
|
|
|
Type: models.MessageTypeText,
|
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{
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
}).Find(&members).Error; err == nil {
|
|
|
|
for _, member := range members {
|
|
|
|
PushCommand(member.ID, models.UnifiedCommand{
|
|
|
|
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 {
|
|
|
|
for _, member := range members {
|
|
|
|
PushCommand(member.ID, models.UnifiedCommand{
|
|
|
|
Action: "messages.update",
|
|
|
|
Payload: message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteMessage(message models.Message) (models.Message, error) {
|
|
|
|
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 {
|
|
|
|
PushCommand(member.ID, models.UnifiedCommand{
|
|
|
|
Action: "messages.burnt",
|
|
|
|
Payload: message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|