From ed9487a709e890865ef7b9303ac249e7d49616ba Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 28 Jun 2024 16:50:08 +0800 Subject: [PATCH] :sparkles: Better notify message, block same user DM channel --- .../server/api/direct_channels_api.go | 4 ++++ pkg/internal/services/direct_channels.go | 24 +++++++++++++++++++ pkg/internal/services/events.go | 11 ++++++++- settings.toml | 2 +- 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 pkg/internal/services/direct_channels.go diff --git a/pkg/internal/server/api/direct_channels_api.go b/pkg/internal/server/api/direct_channels_api.go index 436ea20..a9499c9 100644 --- a/pkg/internal/server/api/direct_channels_api.go +++ b/pkg/internal/server/api/direct_channels_api.go @@ -49,6 +49,10 @@ func createDirectChannel(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find related user: %v", err)) } + if ch, err := services.GetDirectChannelByUser(user, relatedUser); err == nil { + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you already have a direct with that user #%d", ch.ID)) + } + channel := models.Channel{ Alias: data.Alias, Name: data.Name, diff --git a/pkg/internal/services/direct_channels.go b/pkg/internal/services/direct_channels.go new file mode 100644 index 0000000..f5ad30e --- /dev/null +++ b/pkg/internal/services/direct_channels.go @@ -0,0 +1,24 @@ +package services + +import ( + "fmt" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" + "github.com/spf13/viper" +) + +func GetDirectChannelByUser(user models.Account, other models.Account) (models.Channel, error) { + memberTable := fmt.Sprintf("%schannel_members", viper.GetString("database.prefix")) + channelTable := fmt.Sprintf("%schannels", viper.GetString("database.prefix")) + + var channel models.Channel + if err := database.C.Preload("Members"). + Where("type = ?", models.ChannelTypeDirect). + Joins(fmt.Sprintf("JOIN %s cm1 ON cm1.channel_id = %s.id AND cm1.account_id = ?", memberTable, channelTable), user.ID). + Joins(fmt.Sprintf("JOIN %s cm2 ON cm2.channel_id = %s.id AND cm2.account_id = ?", memberTable, channelTable), other.ID). + First(&channel).Error; err != nil { + return channel, err + } else { + return channel, nil + } +} diff --git a/pkg/internal/services/events.go b/pkg/internal/services/events.go index 650cbd6..d5b6d9c 100644 --- a/pkg/internal/services/events.go +++ b/pkg/internal/services/events.go @@ -125,9 +125,18 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) { displayText = fmt.Sprintf("%d attachment(s)", len(body.Attachments)) } + var channelDisplay string + if event.Channel.Type == models.ChannelTypeDirect { + channelDisplay = "DM" + } + + if len(channelDisplay) == 0 { + channelDisplay = event.Channel.Alias + } + err := NotifyAccountMessager(member.Account, "incomingMessage", - fmt.Sprintf("%s in #%s", event.Sender.Account.Nick, event.Channel.Alias), + fmt.Sprintf("%s in #%s", event.Sender.Account.Nick, channelDisplay), fmt.Sprintf("%s", displayText), true, false, diff --git a/settings.toml b/settings.toml index 6b34762..8836028 100644 --- a/settings.toml +++ b/settings.toml @@ -11,7 +11,7 @@ secret = "LtTjzAGFLshwXhN4ZD4nG5KlMv1MWcsvfv03TSZYnT1VhiAnLIZFTnHUwR0XhGgi" addr = "127.0.0.1:8500" [debug] -database = false +database = true print_routes = false [calling]