2024-06-27 14:38:18 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-11-02 05:24:37 +00:00
|
|
|
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
|
2024-11-02 05:23:27 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
2024-11-02 05:40:37 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
2024-11-02 05:23:27 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
|
|
|
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
|
2024-06-28 15:21:29 +00:00
|
|
|
"strings"
|
|
|
|
|
2024-11-02 05:24:37 +00:00
|
|
|
"git.solsynth.dev/hypernet/messaging/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hypernet/messaging/pkg/internal/models"
|
2024-06-27 14:38:18 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/samber/lo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CountEvent(channel models.Channel) int64 {
|
|
|
|
var count int64
|
|
|
|
if err := database.C.Where(models.Event{
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
}).Model(&models.Event{}).Count(&count).Error; err != nil {
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListEvent(channel models.Channel, take int, offset int) ([]models.Event, error) {
|
|
|
|
if take > 100 {
|
|
|
|
take = 100
|
|
|
|
}
|
|
|
|
|
|
|
|
var events []models.Event
|
|
|
|
if err := database.C.
|
|
|
|
Where(models.Event{
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
}).Limit(take).Offset(offset).
|
|
|
|
Order("created_at DESC").
|
|
|
|
Preload("Sender").
|
|
|
|
Find(&events).Error; err != nil {
|
|
|
|
return events, err
|
|
|
|
} else {
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetEvent(channel models.Channel, id uint) (models.Event, error) {
|
|
|
|
var event models.Event
|
|
|
|
if err := database.C.
|
2024-11-02 05:40:37 +00:00
|
|
|
Where("id = ? AND channel_id = ?", id, channel.ID).
|
2024-06-27 14:38:18 +00:00
|
|
|
Preload("Sender").
|
|
|
|
First(&event).Error; err != nil {
|
|
|
|
return event, err
|
|
|
|
} else {
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetEventWithSender(channel models.Channel, member models.ChannelMember, id uint) (models.Event, error) {
|
|
|
|
var event models.Event
|
|
|
|
if err := database.C.Where(models.Event{
|
2024-11-02 05:40:37 +00:00
|
|
|
BaseModel: cruda.BaseModel{ID: id},
|
2024-06-27 14:38:18 +00:00
|
|
|
ChannelID: channel.ID,
|
|
|
|
SenderID: member.ID,
|
|
|
|
}).First(&event).Error; err != nil {
|
|
|
|
return event, err
|
|
|
|
} else {
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEvent(event models.Event) (models.Event, error) {
|
|
|
|
var members []models.ChannelMember
|
|
|
|
if err := database.C.Save(&event).Error; err != nil {
|
|
|
|
return event, err
|
|
|
|
} else if err = database.C.Where(models.ChannelMember{
|
|
|
|
ChannelID: event.ChannelID,
|
|
|
|
}).Preload("Account").Find(&members).Error; err != nil {
|
|
|
|
// Couldn't get channel members, skip notifying
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
event, _ = GetEvent(event.Channel, event.ID)
|
2024-07-17 06:10:04 +00:00
|
|
|
idxList := lo.Map(members, func(item models.ChannelMember, index int) uint64 {
|
2024-11-02 05:23:27 +00:00
|
|
|
return uint64(item.AccountID)
|
2024-07-17 06:10:04 +00:00
|
|
|
})
|
2024-11-02 05:23:27 +00:00
|
|
|
PushCommandBatch(idxList, nex.WebSocketPackage{
|
2024-07-17 06:10:04 +00:00
|
|
|
Action: "events.new",
|
|
|
|
Payload: event,
|
|
|
|
})
|
2024-06-27 14:38:18 +00:00
|
|
|
|
|
|
|
if strings.HasPrefix(event.Type, "messages") {
|
2024-08-01 04:03:34 +00:00
|
|
|
event.Channel, _ = GetChannel(event.ChannelID)
|
2024-11-02 05:40:37 +00:00
|
|
|
if event.Channel.RealmID == nil {
|
|
|
|
realm, err := authkit.GetRealm(gap.Nx, *event.Channel.RealmID)
|
|
|
|
if err == nil {
|
|
|
|
event.Channel.Realm = &realm
|
|
|
|
}
|
|
|
|
}
|
2024-06-27 14:38:18 +00:00
|
|
|
NotifyMessageEvent(members, event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NotifyMessageEvent(members []models.ChannelMember, event models.Event) {
|
|
|
|
var body models.EventMessageBody
|
|
|
|
raw, _ := jsoniter.Marshal(event.Body)
|
|
|
|
_ = jsoniter.Unmarshal(raw, &body)
|
|
|
|
|
2024-11-02 05:23:27 +00:00
|
|
|
var pendingUsers []uint64
|
|
|
|
var mentionedUsers []uint64
|
2024-07-17 06:10:04 +00:00
|
|
|
|
2024-06-27 14:38:18 +00:00
|
|
|
for _, member := range members {
|
|
|
|
if member.ID != event.SenderID {
|
|
|
|
switch member.Notify {
|
|
|
|
case models.NotifyLevelNone:
|
|
|
|
continue
|
|
|
|
case models.NotifyLevelMentioned:
|
2024-11-02 05:23:27 +00:00
|
|
|
if len(body.RelatedUsers) != 0 && lo.Contains(body.RelatedUsers, member.AccountID) {
|
|
|
|
mentionedUsers = append(mentionedUsers, uint64(member.AccountID))
|
2024-06-27 14:38:18 +00:00
|
|
|
}
|
2024-08-01 05:02:41 +00:00
|
|
|
continue
|
2024-06-27 14:38:18 +00:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-11-02 05:23:27 +00:00
|
|
|
if lo.Contains(body.RelatedUsers, member.AccountID) {
|
|
|
|
mentionedUsers = append(mentionedUsers, uint64(member.AccountID))
|
2024-08-01 04:33:22 +00:00
|
|
|
} else {
|
2024-11-02 05:23:27 +00:00
|
|
|
pendingUsers = append(pendingUsers, uint64(member.AccountID))
|
2024-08-01 04:33:22 +00:00
|
|
|
}
|
2024-07-17 06:10:04 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-27 14:38:18 +00:00
|
|
|
|
2024-07-17 06:10:04 +00:00
|
|
|
var displayText string
|
2024-11-02 05:23:27 +00:00
|
|
|
var displaySubtitle string
|
2024-08-01 04:03:34 +00:00
|
|
|
switch event.Type {
|
|
|
|
case models.EventMessageNew:
|
|
|
|
if body.Algorithm == "plain" {
|
|
|
|
displayText = body.Text
|
|
|
|
}
|
|
|
|
case models.EventMessageEdit:
|
2024-11-02 05:23:27 +00:00
|
|
|
displaySubtitle = "Edited a message"
|
2024-08-01 04:03:34 +00:00
|
|
|
if body.Algorithm == "plain" {
|
|
|
|
displayText = body.Text
|
|
|
|
}
|
|
|
|
case models.EventMessageDelete:
|
|
|
|
displayText = "Recalled a message"
|
2024-07-17 06:10:04 +00:00
|
|
|
}
|
2024-06-27 14:38:18 +00:00
|
|
|
|
2024-07-17 06:10:04 +00:00
|
|
|
if len(displayText) == 0 {
|
2024-08-01 11:45:06 +00:00
|
|
|
if len(displayText) == 1 {
|
|
|
|
displayText = fmt.Sprintf("%d file", len(body.Attachments))
|
|
|
|
} else {
|
|
|
|
displayText = fmt.Sprintf("%d files", len(body.Attachments))
|
|
|
|
}
|
2024-08-01 04:33:22 +00:00
|
|
|
} else if len(body.Attachments) > 0 {
|
2024-08-01 11:45:06 +00:00
|
|
|
if len(displayText) == 1 {
|
|
|
|
displayText += fmt.Sprintf(" (%d file)", len(body.Attachments))
|
|
|
|
} else {
|
|
|
|
displayText += fmt.Sprintf(" (%d files)", len(body.Attachments))
|
|
|
|
}
|
2024-08-01 04:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(pendingUsers) > 0 {
|
2024-11-02 05:23:27 +00:00
|
|
|
err := authkit.NotifyUserBatch(
|
|
|
|
gap.Nx,
|
2024-08-01 04:33:22 +00:00
|
|
|
pendingUsers,
|
2024-11-02 05:23:27 +00:00
|
|
|
pushkit.Notification{
|
2024-08-01 04:33:22 +00:00
|
|
|
Topic: "messaging.message",
|
2024-11-02 05:23:27 +00:00
|
|
|
Title: fmt.Sprintf("%s (%s)", event.Sender.Nick, event.Channel.DisplayText()),
|
2024-08-01 04:33:22 +00:00
|
|
|
Subtitle: displaySubtitle,
|
|
|
|
Body: displayText,
|
2024-11-02 05:23:27 +00:00
|
|
|
Metadata: map[string]any{
|
|
|
|
"avatar": event.Sender.Avatar,
|
|
|
|
"user_id": event.Sender.AccountID,
|
|
|
|
"user_name": event.Sender.Name,
|
|
|
|
"user_nick": event.Sender.Nick,
|
2024-08-01 04:33:22 +00:00
|
|
|
"channel_id": event.ChannelID,
|
2024-11-02 05:23:27 +00:00
|
|
|
},
|
|
|
|
Priority: 5,
|
2024-08-01 04:33:22 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("An error occurred when trying notify user.")
|
|
|
|
}
|
2024-07-17 06:10:04 +00:00
|
|
|
}
|
2024-06-28 08:50:08 +00:00
|
|
|
|
2024-08-23 15:09:47 +00:00
|
|
|
if len(mentionedUsers) > 0 {
|
2024-11-02 05:23:27 +00:00
|
|
|
if len(displaySubtitle) > 0 {
|
|
|
|
displaySubtitle += ", and mentioned you"
|
2024-08-01 04:33:22 +00:00
|
|
|
} else {
|
2024-11-02 05:23:27 +00:00
|
|
|
displaySubtitle = "Mentioned you"
|
2024-08-01 04:33:22 +00:00
|
|
|
}
|
|
|
|
|
2024-11-02 05:23:27 +00:00
|
|
|
err := authkit.NotifyUserBatch(
|
|
|
|
gap.Nx,
|
2024-08-23 15:09:47 +00:00
|
|
|
mentionedUsers,
|
2024-11-02 05:23:27 +00:00
|
|
|
pushkit.Notification{
|
2024-08-01 04:33:22 +00:00
|
|
|
Topic: "messaging.message",
|
2024-11-02 05:23:27 +00:00
|
|
|
Title: fmt.Sprintf("%s (%s)", event.Sender.Nick, event.Channel.DisplayText()),
|
2024-08-01 04:33:22 +00:00
|
|
|
Subtitle: displaySubtitle,
|
|
|
|
Body: displayText,
|
2024-11-02 05:23:27 +00:00
|
|
|
Metadata: map[string]any{
|
|
|
|
"avatar": event.Sender.Avatar,
|
|
|
|
"user_id": event.Sender.AccountID,
|
|
|
|
"user_name": event.Sender.Name,
|
|
|
|
"user_nick": event.Sender.Nick,
|
2024-08-01 04:33:22 +00:00
|
|
|
"channel_id": event.ChannelID,
|
2024-11-02 05:23:27 +00:00
|
|
|
},
|
|
|
|
Priority: 5,
|
2024-08-01 04:33:22 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("An error occurred when trying notify user.")
|
|
|
|
}
|
2024-06-27 14:38:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func EditEvent(event models.Event) (models.Event, error) {
|
|
|
|
if err := database.C.Save(&event).Error; err != nil {
|
|
|
|
return event, err
|
|
|
|
}
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteEvent(event models.Event) (models.Event, error) {
|
|
|
|
if err := database.C.Delete(&event).Error; err != nil {
|
|
|
|
return event, err
|
|
|
|
}
|
|
|
|
return event, nil
|
|
|
|
}
|