Messaging/pkg/internal/services/channel_members.go

131 lines
3.2 KiB
Go
Raw Normal View History

2024-04-06 08:08:33 +00:00
package services
import (
2024-10-09 15:56:24 +00:00
"context"
2024-04-06 08:08:33 +00:00
"fmt"
2024-11-02 05:24:37 +00:00
localCache "git.solsynth.dev/hypernet/messaging/pkg/internal/cache"
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
2024-11-02 05:23:27 +00:00
"git.solsynth.dev/hypernet/passport/pkg/authkit"
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-05-26 15:01:20 +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-04-06 08:08:33 +00:00
)
func ListChannelMember(channelId uint) ([]models.ChannelMember, error) {
var members []models.ChannelMember
if err := database.C.
Where(&models.ChannelMember{ChannelID: channelId}).
Find(&members).Error; err != nil {
return members, err
}
return members, nil
}
2024-11-02 05:23:27 +00:00
func GetChannelMember(user authm.Account, channelId uint) (models.ChannelMember, error) {
2024-05-26 15:01:20 +00:00
var member models.ChannelMember
if err := database.C.
Where(&models.ChannelMember{AccountID: user.ID, ChannelID: channelId}).
Find(&member).Error; err != nil {
return member, err
}
return member, nil
}
2024-11-02 05:23:27 +00:00
func AddChannelMemberWithCheck(user authm.Account, target models.Channel) error {
if err := authkit.EnsureUserPermGranted(gap.Nx, user.ID, target.AccountID, "ChannelAdd", true); err != nil {
return fmt.Errorf("unable to add user into your channel due to access denied: %v", err)
2024-04-06 08:08:33 +00:00
}
member := models.ChannelMember{
ChannelID: target.ID,
AccountID: user.ID,
}
err := database.C.Save(&member).Error
return err
}
2024-11-02 05:23:27 +00:00
func AddChannelMember(user authm.Account, target models.Channel) error {
2024-04-06 08:08:33 +00:00
member := models.ChannelMember{
ChannelID: target.ID,
AccountID: user.ID,
}
err := database.C.Save(&member).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", target.ID),
fmt.Sprintf("user#%d", user.ID),
}),
)
}
2024-04-06 08:08:33 +00:00
return err
}
func EditChannelMember(membership models.ChannelMember) (models.ChannelMember, error) {
if err := database.C.Save(&membership).Error; err != nil {
return membership, err
2024-10-09 15:56:24 +00:00
} else {
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
_ = marshal.Invalidate(
contx,
store.WithInvalidateTags([]string{
fmt.Sprintf("channel#%d", membership.ChannelID),
fmt.Sprintf("user#%d", membership.AccountID),
}),
)
2024-04-06 08:08:33 +00:00
}
2024-10-09 15:56:24 +00:00
2024-04-06 08:08:33 +00:00
return membership, nil
}
2024-11-02 05:23:27 +00:00
func RemoveChannelMember(user authm.Account, target models.Channel) error {
2024-04-06 08:08:33 +00:00
var member models.ChannelMember
if err := database.C.Where(&models.ChannelMember{
ChannelID: target.ID,
AccountID: user.ID,
}).First(&member).Error; err != nil {
return err
}
2024-10-09 15:56:24 +00:00
if err := database.C.Delete(&member).Error; err == nil {
database.C.Where("sender_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", target.ID),
fmt.Sprintf("user#%d", user.ID),
}),
)
return nil
} else {
return err
}
2024-04-06 08:08:33 +00:00
}