Messaging/pkg/services/messages.go

186 lines
4.6 KiB
Go
Raw Normal View History

2024-03-30 09:10:36 +00:00
package services
import (
2024-04-06 07:20:47 +00:00
"fmt"
2024-05-26 15:01:20 +00:00
2024-03-30 09:10:36 +00:00
"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-05-26 15:01:20 +00:00
"github.com/samber/lo"
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-31 13:19:51 +00:00
Preload("ReplyTo").
2024-03-31 15:15:44 +00:00
Preload("ReplyTo.Sender").
2024-03-31 15:26:13 +00:00
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").
2024-03-31 15:26:13 +00:00
Preload("ReplyTo.Sender.Account").
2024-03-30 17:27:41 +00:00
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-04-06 15:04:53 +00:00
channel := message.Channel
message, _ = GetMessage(message.Channel, message.ID)
doesNotify := true
2024-03-30 09:10:36 +00:00
for _, member := range members {
2024-04-04 15:27:59 +00:00
if member.ID != message.Sender.ID {
2024-05-26 15:01:20 +00:00
switch member.Notify {
case models.NotifyLevelNone:
doesNotify = false
2024-05-26 15:01:20 +00:00
case models.NotifyLevelMentioned:
if member.ID == message.ReplyTo.SenderID {
break
}
if val, ok := message.Content["mentioned_users"]; ok {
2024-05-26 15:01:20 +00:00
if usernames, ok := val.([]string); ok {
if lo.Contains(usernames, member.Account.Name) {
break
}
}
2024-04-06 08:08:33 +00:00
}
doesNotify = false
default:
break
2024-05-26 15:01:20 +00:00
}
}
2024-05-26 15:01:20 +00:00
if doesNotify {
2024-05-26 15:01:20 +00:00
var displayText string
if message.Content["algorithm"] == "plain" {
displayText, _ = message.Content["value"].(string)
} else {
displayText = "*encrypted message*"
}
if len(displayText) == 0 {
displayText = fmt.Sprintf("%d attachment(s)", len(message.Attachments))
}
err = NotifyAccountMessager(member.Account,
2024-06-08 08:34:14 +00:00
"incomingMessage",
fmt.Sprintf("%s in #%s", message.Sender.Account.Nick, channel.Alias),
fmt.Sprintf("%s", displayText),
2024-05-26 15:01:20 +00:00
true,
false,
2024-05-26 15:01:20 +00:00
)
if err != nil {
log.Warn().Err(err).Msg("An error occurred when trying notify user.")
2024-04-04 15:27:59 +00:00
}
2024-03-31 14:49:08 +00:00
}
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
}