Better notify message, block same user DM channel

This commit is contained in:
LittleSheep 2024-06-28 16:50:08 +08:00
parent 1308d4ad4c
commit ed9487a709
4 changed files with 39 additions and 2 deletions

View File

@ -49,6 +49,10 @@ func createDirectChannel(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find related user: %v", err)) 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{ channel := models.Channel{
Alias: data.Alias, Alias: data.Alias,
Name: data.Name, Name: data.Name,

View File

@ -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
}
}

View File

@ -125,9 +125,18 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) {
displayText = fmt.Sprintf("%d attachment(s)", len(body.Attachments)) 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, err := NotifyAccountMessager(member.Account,
"incomingMessage", "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), fmt.Sprintf("%s", displayText),
true, true,
false, false,

View File

@ -11,7 +11,7 @@ secret = "LtTjzAGFLshwXhN4ZD4nG5KlMv1MWcsvfv03TSZYnT1VhiAnLIZFTnHUwR0XhGgi"
addr = "127.0.0.1:8500" addr = "127.0.0.1:8500"
[debug] [debug]
database = false database = true
print_routes = false print_routes = false
[calling] [calling]