2024-03-26 15:05:13 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-10-09 15:56:24 +00:00
|
|
|
"context"
|
2024-03-30 09:10:36 +00:00
|
|
|
"fmt"
|
2024-11-02 05:24:37 +00:00
|
|
|
localCache "git.solsynth.dev/hypernet/messaging/pkg/internal/cache"
|
2024-11-02 05:23:27 +00:00
|
|
|
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
2024-10-09 15:56:24 +00:00
|
|
|
"github.com/eko/gocache/lib/v4/cache"
|
|
|
|
"github.com/eko/gocache/lib/v4/marshaler"
|
|
|
|
"github.com/eko/gocache/lib/v4/store"
|
2024-04-26 16:04:01 +00:00
|
|
|
"regexp"
|
2024-04-25 13:00:45 +00:00
|
|
|
|
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-03-26 15:05:13 +00:00
|
|
|
"github.com/samber/lo"
|
2024-05-28 16:01:46 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"gorm.io/gorm"
|
2024-03-26 15:05:13 +00:00
|
|
|
)
|
|
|
|
|
2024-10-09 15:56:24 +00:00
|
|
|
type channelIdentityCacheEntry struct {
|
|
|
|
Channel models.Channel
|
|
|
|
ChannelMember models.ChannelMember
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetChannelIdentityCacheKey(channel string, user uint, realm ...uint) string {
|
|
|
|
if len(realm) > 0 {
|
|
|
|
return fmt.Sprintf("channel-identity-%s#%d@%d", channel, user, realm)
|
|
|
|
} else {
|
|
|
|
return fmt.Sprintf("channel-identity-%s#%d", channel, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CacheChannelIdentityCache(channel models.Channel, member models.ChannelMember, user uint, realm ...uint) {
|
|
|
|
key := GetChannelIdentityCacheKey(channel.Alias, user, realm...)
|
|
|
|
|
|
|
|
cacheManager := cache.New[any](localCache.S)
|
|
|
|
marshal := marshaler.New(cacheManager)
|
|
|
|
contx := context.Background()
|
|
|
|
|
|
|
|
_ = marshal.Set(
|
|
|
|
contx,
|
|
|
|
key,
|
|
|
|
channelIdentityCacheEntry{channel, member},
|
|
|
|
store.WithTags([]string{"channel-identity", fmt.Sprintf("channel#%d", channel.ID), fmt.Sprintf("user#%d", user)}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-11-02 05:40:37 +00:00
|
|
|
func GetChannelIdentity(alias string, user uint, realm ...authm.Realm) (models.Channel, models.ChannelMember, error) {
|
2024-10-09 15:56:24 +00:00
|
|
|
cacheManager := cache.New[any](localCache.S)
|
|
|
|
marshal := marshaler.New(cacheManager)
|
|
|
|
contx := context.Background()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var channel models.Channel
|
|
|
|
var member models.ChannelMember
|
|
|
|
|
|
|
|
hitCache := false
|
|
|
|
if len(realm) > 0 {
|
|
|
|
if val, err := marshal.Get(contx, GetChannelIdentityCacheKey(alias, user, realm[0].ID), new(channelIdentityCacheEntry)); err == nil {
|
|
|
|
entry := val.(*channelIdentityCacheEntry)
|
|
|
|
channel = entry.Channel
|
|
|
|
member = entry.ChannelMember
|
|
|
|
hitCache = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if val, err := marshal.Get(contx, GetChannelIdentityCacheKey(alias, user), new(channelIdentityCacheEntry)); err == nil {
|
|
|
|
entry := val.(*channelIdentityCacheEntry)
|
|
|
|
channel = entry.Channel
|
|
|
|
member = entry.ChannelMember
|
|
|
|
hitCache = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hitCache {
|
|
|
|
if len(realm) > 0 {
|
|
|
|
channel, member, err = GetAvailableChannelWithAlias(alias, user, realm[0].ID)
|
|
|
|
CacheChannelIdentityCache(channel, member, user, realm[0].ID)
|
|
|
|
} else {
|
|
|
|
channel, member, err = GetAvailableChannelWithAlias(alias, user)
|
|
|
|
CacheChannelIdentityCache(channel, member, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return channel, member, err
|
|
|
|
}
|
|
|
|
|
2024-04-26 16:04:01 +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-11-15 17:48:47 +00:00
|
|
|
tx := database.C.Where("id = ?", id)
|
2024-06-01 02:43:21 +00:00
|
|
|
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-11-15 17:48:47 +00:00
|
|
|
tx := database.C.Where(models.Channel{Alias: alias})
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-09 15:56:24 +00:00
|
|
|
func GetAvailableChannelWithAlias(alias string, user uint, 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
|
2024-05-05 14:56:37 +00:00
|
|
|
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{
|
2024-10-09 15:56:24 +00:00
|
|
|
AccountID: user,
|
2024-03-30 10:21:17 +00:00
|
|
|
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-11-02 05:23:27 +00:00
|
|
|
func GetAvailableChannel(id uint, user authm.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
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:01:46 +00:00
|
|
|
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,
|
|
|
|
)
|
2024-11-02 05:40:37 +00:00
|
|
|
})
|
2024-05-28 16:01:46 +00:00
|
|
|
}
|
|
|
|
|
2024-11-02 05:23:27 +00:00
|
|
|
func ListChannel(user *authm.Account, realmId ...uint) ([]models.Channel, error) {
|
2024-09-17 03:45:44 +00:00
|
|
|
var identities []models.ChannelMember
|
|
|
|
var idRange []uint
|
|
|
|
if user != nil {
|
|
|
|
if err := database.C.Where("account_id = ?", user.ID).Find(&identities).Error; err != nil {
|
|
|
|
return nil, fmt.Errorf("unabkle to get identities: %v", err)
|
|
|
|
}
|
|
|
|
for _, identity := range identities {
|
|
|
|
idRange = append(idRange, identity.ChannelID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 15:05:13 +00:00
|
|
|
var channels []models.Channel
|
2024-11-15 17:48:47 +00:00
|
|
|
tx := database.C
|
2024-09-17 03:45:44 +00:00
|
|
|
tx = tx.Where("id IN ? OR is_public = true", idRange)
|
2024-05-04 17:04:14 +00:00
|
|
|
if len(realmId) > 0 {
|
|
|
|
tx = tx.Where("realm_id = ?", realmId)
|
|
|
|
}
|
2024-05-28 16:01:46 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-11-02 05:23:27 +00:00
|
|
|
func ListChannelWithUser(user authm.Account, realmId ...uint) ([]models.Channel, error) {
|
2024-03-26 15:05:13 +00:00
|
|
|
var channels []models.Channel
|
2024-11-15 17:48:47 +00:00
|
|
|
tx := database.C.Where(&models.Channel{AccountID: user.ID})
|
2024-05-05 14:56:37 +00:00
|
|
|
if len(realmId) > 0 {
|
|
|
|
tx = tx.Where("realm_id = ?", realmId)
|
|
|
|
}
|
2024-05-28 16:01:46 +00:00
|
|
|
|
|
|
|
tx = PreloadDirectChannelMembers(tx)
|
|
|
|
|
2024-05-05 14:56:37 +00:00
|
|
|
if err := tx.Find(&channels).Error; err != nil {
|
2024-03-26 15:05:13 +00:00
|
|
|
return channels, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return channels, nil
|
|
|
|
}
|
|
|
|
|
2024-11-02 05:23:27 +00:00
|
|
|
func ListAvailableChannel(tx *gorm.DB, user authm.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-11-15 17:48:47 +00:00
|
|
|
tx = tx.Where("id IN ?", idx)
|
2024-05-05 14:56:37 +00:00
|
|
|
if len(realmId) > 0 {
|
|
|
|
tx = tx.Where("realm_id = ?", realmId)
|
2024-10-05 14:04:15 +00:00
|
|
|
} else {
|
|
|
|
tx = tx.Where("realm_id IS NULL")
|
2024-05-05 14:56:37 +00:00
|
|
|
}
|
2024-05-28 16:01:46 +00:00
|
|
|
|
|
|
|
tx = PreloadDirectChannelMembers(tx)
|
|
|
|
|
2024-05-05 14:56:37 +00:00
|
|
|
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-09-17 03:45:44 +00:00
|
|
|
func EditChannel(channel models.Channel, alias, name, description string, isPublic, isCommunity 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-09-17 03:45:44 +00:00
|
|
|
channel.IsPublic = isPublic
|
|
|
|
channel.IsCommunity = isCommunity
|
2024-03-26 15:05:13 +00:00
|
|
|
|
|
|
|
err := database.C.Save(&channel).Error
|
|
|
|
|
2024-10-09 15:56:24 +00:00
|
|
|
if err == nil {
|
|
|
|
cacheManager := cache.New[any](localCache.S)
|
|
|
|
marshal := marshaler.New(cacheManager)
|
|
|
|
contx := context.Background()
|
|
|
|
|
|
|
|
_ = marshal.Invalidate(
|
|
|
|
contx,
|
|
|
|
store.WithInvalidateTags([]string{fmt.Sprintf("channel#%d", channel.ID)}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-03-26 15:05:13 +00:00
|
|
|
return channel, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteChannel(channel models.Channel) error {
|
2024-10-09 15:56:24 +00:00
|
|
|
if err := database.C.Delete(&channel).Error; err == nil {
|
|
|
|
database.C.Where("channel_id = ?", channel.ID).Delete(&models.Event{})
|
|
|
|
|
|
|
|
cacheManager := cache.New[any](localCache.S)
|
|
|
|
marshal := marshaler.New(cacheManager)
|
|
|
|
contx := context.Background()
|
|
|
|
|
|
|
|
_ = marshal.Invalidate(
|
|
|
|
contx,
|
|
|
|
store.WithInvalidateTags([]string{fmt.Sprintf("channel#%d", channel.ID)}),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-26 15:05:13 +00:00
|
|
|
}
|