Messaging/pkg/services/channels.go

178 lines
4.5 KiB
Go
Raw Normal View History

2024-03-26 15:05:13 +00:00
package services
import (
2024-03-30 09:10:36 +00:00
"fmt"
"regexp"
2024-03-26 15:05:13 +00:00
"git.solsynth.dev/hydrogen/messaging/pkg/database"
"git.solsynth.dev/hydrogen/messaging/pkg/models"
"github.com/samber/lo"
"github.com/spf13/viper"
"gorm.io/gorm"
2024-03-26 15:05:13 +00:00
)
func GetChannelAliasAvailability(alias string) error {
if !regexp.MustCompile("^[a-z0-9-]+$").MatchString(alias) {
return fmt.Errorf("channel alias should only contains lowercase letters, numbers, and hyphens")
}
return nil
}
2024-03-30 10:21:17 +00:00
func GetChannel(id uint) (models.Channel, error) {
var channel models.Channel
2024-06-01 02:43:21 +00:00
tx := database.C.Where(models.Channel{
2024-03-30 10:21:17 +00:00
BaseModel: models.BaseModel{ID: id},
2024-06-01 02:43:21 +00:00
}).Preload("Account").Preload("Realm")
tx = PreloadDirectChannelMembers(tx)
if err := tx.First(&channel).Error; err != nil {
2024-03-30 10:21:17 +00:00
return channel, err
}
return channel, nil
}
2024-05-04 17:04:14 +00:00
func GetChannelWithAlias(alias string, realmId ...uint) (models.Channel, error) {
2024-03-30 10:21:17 +00:00
var channel models.Channel
2024-06-01 02:43:21 +00:00
tx := database.C.Where(models.Channel{Alias: alias}).Preload("Account").Preload("Realm")
2024-05-04 17:04:14 +00:00
if len(realmId) > 0 {
tx = tx.Where("realm_id = ?", realmId)
} else {
tx = tx.Where("realm_id IS NULL")
}
2024-06-01 02:43:21 +00:00
tx = PreloadDirectChannelMembers(tx)
2024-05-04 17:04:14 +00:00
if err := tx.First(&channel).Error; err != nil {
2024-03-30 10:21:17 +00:00
return channel, err
}
return channel, nil
}
func GetAvailableChannelWithAlias(alias string, user models.Account, realmId ...uint) (models.Channel, models.ChannelMember, error) {
2024-03-30 10:21:17 +00:00
var err error
var member models.ChannelMember
var channel models.Channel
if channel, err = GetChannelWithAlias(alias, realmId...); err != nil {
2024-03-30 10:21:17 +00:00
return channel, member, err
}
if err := database.C.Where(models.ChannelMember{
AccountID: user.ID,
ChannelID: channel.ID,
}).First(&member).Error; err != nil {
return channel, member, fmt.Errorf("channel principal not found: %v", err.Error())
}
return channel, member, nil
}
2024-03-30 09:10:36 +00:00
func GetAvailableChannel(id uint, user models.Account) (models.Channel, models.ChannelMember, error) {
2024-03-30 10:21:17 +00:00
var err error
2024-03-30 09:10:36 +00:00
var member models.ChannelMember
var channel models.Channel
2024-03-30 10:21:17 +00:00
if channel, err = GetChannel(id); err != nil {
2024-03-30 09:10:36 +00:00
return channel, member, err
}
2024-06-01 02:43:21 +00:00
tx := database.C.Where(models.ChannelMember{
2024-03-30 09:10:36 +00:00
AccountID: user.ID,
ChannelID: channel.ID,
2024-06-01 02:43:21 +00:00
})
if err := tx.First(&member).Error; err != nil {
2024-03-30 09:10:36 +00:00
return channel, member, fmt.Errorf("channel principal not found: %v", err.Error())
}
return channel, member, nil
}
func PreloadDirectChannelMembers(tx *gorm.DB) *gorm.DB {
return tx.Preload("Members", func(db *gorm.DB) *gorm.DB {
return db.Joins(
fmt.Sprintf(
"JOIN %schannels AS c ON c.type = ?",
viper.GetString("database.prefix"),
),
models.ChannelTypeDirect,
)
}).Preload("Members.Account")
}
2024-05-04 17:04:14 +00:00
func ListChannel(realmId ...uint) ([]models.Channel, error) {
2024-03-26 15:05:13 +00:00
var channels []models.Channel
2024-05-26 15:01:20 +00:00
tx := database.C.Preload("Account").Preload("Realm")
2024-05-04 17:04:14 +00:00
if len(realmId) > 0 {
tx = tx.Where("realm_id = ?", realmId)
}
tx = PreloadDirectChannelMembers(tx)
2024-05-04 17:04:14 +00:00
if err := tx.Find(&channels).Error; err != nil {
2024-03-26 15:05:13 +00:00
return channels, err
}
return channels, nil
}
func ListChannelWithUser(user models.Account, realmId ...uint) ([]models.Channel, error) {
2024-03-26 15:05:13 +00:00
var channels []models.Channel
2024-05-26 15:01:20 +00:00
tx := database.C.Where(&models.Channel{AccountID: user.ID}).Preload("Realm")
if len(realmId) > 0 {
tx = tx.Where("realm_id = ?", realmId)
}
tx = PreloadDirectChannelMembers(tx)
if err := tx.Find(&channels).Error; err != nil {
2024-03-26 15:05:13 +00:00
return channels, err
}
return channels, nil
}
func ListAvailableChannel(user models.Account, realmId ...uint) ([]models.Channel, error) {
2024-03-26 15:05:13 +00:00
var channels []models.Channel
var members []models.ChannelMember
if err := database.C.Where(&models.ChannelMember{
AccountID: user.ID,
}).Find(&members).Error; err != nil {
return channels, err
}
idx := lo.Map(members, func(item models.ChannelMember, index int) uint {
return item.ChannelID
})
2024-05-26 15:01:20 +00:00
tx := database.C.Preload("Realm").Where("id IN ?", idx)
if len(realmId) > 0 {
tx = tx.Where("realm_id = ?", realmId)
}
tx = PreloadDirectChannelMembers(tx)
if err := tx.Find(&channels).Error; err != nil {
2024-03-26 15:05:13 +00:00
return channels, err
}
return channels, nil
}
2024-05-26 15:01:20 +00:00
func NewChannel(channel models.Channel) (models.Channel, error) {
2024-03-26 15:05:13 +00:00
err := database.C.Save(&channel).Error
return channel, err
}
2024-05-12 14:00:22 +00:00
func EditChannel(channel models.Channel, alias, name, description string, isEncrypted bool) (models.Channel, error) {
2024-03-30 09:10:36 +00:00
channel.Alias = alias
2024-03-26 15:05:13 +00:00
channel.Name = name
channel.Description = description
2024-05-12 14:00:22 +00:00
if !channel.IsEncrypted {
channel.IsEncrypted = isEncrypted
}
2024-03-26 15:05:13 +00:00
err := database.C.Save(&channel).Error
return channel, err
}
func DeleteChannel(channel models.Channel) error {
return database.C.Delete(&channel).Error
}