✨ Restricted notify ability
This commit is contained in:
parent
1b68faf8ba
commit
35b28d1003
@ -21,6 +21,14 @@ type Channel struct {
|
|||||||
RealmID uint `json:"realm_id"`
|
RealmID uint `json:"realm_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NotifyLevel = int8
|
||||||
|
|
||||||
|
const (
|
||||||
|
NotifyLevelAll = NotifyLevel(iota)
|
||||||
|
NotifyLevelMentioned
|
||||||
|
NotifyLevelNone
|
||||||
|
)
|
||||||
|
|
||||||
type ChannelMember struct {
|
type ChannelMember struct {
|
||||||
BaseModel
|
BaseModel
|
||||||
|
|
||||||
@ -28,6 +36,7 @@ type ChannelMember struct {
|
|||||||
AccountID uint `json:"account_id"`
|
AccountID uint `json:"account_id"`
|
||||||
Channel Channel `json:"channel"`
|
Channel Channel `json:"channel"`
|
||||||
Account Account `json:"account"`
|
Account Account `json:"account"`
|
||||||
|
Notify NotifyLevel `json:"notify"`
|
||||||
|
|
||||||
Messages []Message `json:"messages" gorm:"foreignKey:SenderID"`
|
Messages []Message `json:"messages" gorm:"foreignKey:SenderID"`
|
||||||
}
|
}
|
||||||
|
@ -85,6 +85,35 @@ func kickChannel(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func editChannelMembership(c *fiber.Ctx) error {
|
||||||
|
user := c.Locals("principal").(models.Account)
|
||||||
|
channelId, _ := c.ParamsInt("channelId", 0)
|
||||||
|
|
||||||
|
var data struct {
|
||||||
|
NotifyLevel int8 `json:"notify_level"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := BindAndValidate(c, &data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var membership models.ChannelMember
|
||||||
|
if err := database.C.Where(&models.ChannelMember{
|
||||||
|
ChannelID: uint(channelId),
|
||||||
|
AccountID: user.ID,
|
||||||
|
}).First(&membership).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
membership.Notify = data.NotifyLevel
|
||||||
|
|
||||||
|
if membership, err := services.EditChannelMember(membership); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||||
|
} else {
|
||||||
|
return c.JSON(membership)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func leaveChannel(c *fiber.Ctx) error {
|
func leaveChannel(c *fiber.Ctx) error {
|
||||||
user := c.Locals("principal").(models.Account)
|
user := c.Locals("principal").(models.Account)
|
||||||
channelId, _ := c.ParamsInt("channelId", 0)
|
channelId, _ := c.ParamsInt("channelId", 0)
|
||||||
|
@ -86,6 +86,7 @@ func NewServer() {
|
|||||||
channels.Delete("/:channelId", authMiddleware, deleteChannel)
|
channels.Delete("/:channelId", authMiddleware, deleteChannel)
|
||||||
|
|
||||||
channels.Get("/:channelId/members", listChannelMembers)
|
channels.Get("/:channelId/members", listChannelMembers)
|
||||||
|
channels.Put("/:channelId/members", authMiddleware, editChannelMembership)
|
||||||
channels.Post("/:channelId/invite", authMiddleware, inviteChannel)
|
channels.Post("/:channelId/invite", authMiddleware, inviteChannel)
|
||||||
channels.Post("/:channelId/kick", authMiddleware, kickChannel)
|
channels.Post("/:channelId/kick", authMiddleware, kickChannel)
|
||||||
channels.Post("/:channelId/leave", authMiddleware, leaveChannel)
|
channels.Post("/:channelId/leave", authMiddleware, leaveChannel)
|
||||||
|
64
pkg/services/channel_members.go
Normal file
64
pkg/services/channel_members.go
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.solsynth.dev/hydrogen/messaging/pkg/database"
|
||||||
|
"git.solsynth.dev/hydrogen/messaging/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ListChannelMember(channelId uint) ([]models.ChannelMember, error) {
|
||||||
|
var members []models.ChannelMember
|
||||||
|
|
||||||
|
if err := database.C.
|
||||||
|
Where(&models.ChannelMember{ChannelID: channelId}).
|
||||||
|
Preload("Account").
|
||||||
|
Find(&members).Error; err != nil {
|
||||||
|
return members, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return members, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func InviteChannelMember(user models.Account, target models.Channel) error {
|
||||||
|
if _, err := GetAccountFriend(user.ID, target.AccountID, 1); err != nil {
|
||||||
|
return fmt.Errorf("you only can invite your friends to your channel")
|
||||||
|
}
|
||||||
|
|
||||||
|
member := models.ChannelMember{
|
||||||
|
ChannelID: target.ID,
|
||||||
|
AccountID: user.ID,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := database.C.Save(&member).Error
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddChannelMember(user models.Account, target models.Channel) error {
|
||||||
|
member := models.ChannelMember{
|
||||||
|
ChannelID: target.ID,
|
||||||
|
AccountID: user.ID,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := database.C.Save(&member).Error
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditChannelMember(membership models.ChannelMember) (models.ChannelMember, error) {
|
||||||
|
if err := database.C.Save(&membership).Error; err != nil {
|
||||||
|
return membership, err
|
||||||
|
}
|
||||||
|
return membership, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveChannelMember(user models.Account, target models.Channel) error {
|
||||||
|
var member models.ChannelMember
|
||||||
|
|
||||||
|
if err := database.C.Where(&models.ChannelMember{
|
||||||
|
ChannelID: target.ID,
|
||||||
|
AccountID: user.ID,
|
||||||
|
}).First(&member).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return database.C.Delete(&member).Error
|
||||||
|
}
|
@ -119,56 +119,6 @@ func NewChannel(user models.Account, alias, name, description string) (models.Ch
|
|||||||
return channel, err
|
return channel, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListChannelMember(channelId uint) ([]models.ChannelMember, error) {
|
|
||||||
var members []models.ChannelMember
|
|
||||||
|
|
||||||
if err := database.C.
|
|
||||||
Where(&models.ChannelMember{ChannelID: channelId}).
|
|
||||||
Preload("Account").
|
|
||||||
Find(&members).Error; err != nil {
|
|
||||||
return members, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return members, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func InviteChannelMember(user models.Account, target models.Channel) error {
|
|
||||||
if _, err := GetAccountFriend(user.ID, target.AccountID, 1); err != nil {
|
|
||||||
return fmt.Errorf("you only can invite your friends to your channel")
|
|
||||||
}
|
|
||||||
|
|
||||||
member := models.ChannelMember{
|
|
||||||
ChannelID: target.ID,
|
|
||||||
AccountID: user.ID,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := database.C.Save(&member).Error
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddChannelMember(user models.Account, target models.Channel) error {
|
|
||||||
member := models.ChannelMember{
|
|
||||||
ChannelID: target.ID,
|
|
||||||
AccountID: user.ID,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := database.C.Save(&member).Error
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func RemoveChannelMember(user models.Account, target models.Channel) error {
|
|
||||||
var member models.ChannelMember
|
|
||||||
|
|
||||||
if err := database.C.Where(&models.ChannelMember{
|
|
||||||
ChannelID: target.ID,
|
|
||||||
AccountID: user.ID,
|
|
||||||
}).First(&member).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return database.C.Delete(&member).Error
|
|
||||||
}
|
|
||||||
|
|
||||||
func EditChannel(channel models.Channel, alias, name, description string) (models.Channel, error) {
|
func EditChannel(channel models.Channel, alias, name, description string) (models.Channel, error) {
|
||||||
channel.Alias = alias
|
channel.Alias = alias
|
||||||
channel.Name = name
|
channel.Name = name
|
||||||
|
@ -85,6 +85,8 @@ func NewMessage(message models.Message) (models.Message, error) {
|
|||||||
for _, member := range members {
|
for _, member := range members {
|
||||||
message, _ = GetMessage(message.Channel, message.ID)
|
message, _ = GetMessage(message.Channel, message.ID)
|
||||||
if member.ID != message.Sender.ID {
|
if member.ID != message.Sender.ID {
|
||||||
|
// TODO Check the mentioned status
|
||||||
|
if member.Notify == models.NotifyLevelAll {
|
||||||
err = NotifyAccount(member.Account,
|
err = NotifyAccount(member.Account,
|
||||||
fmt.Sprintf("New Message #%s", message.Channel.Alias),
|
fmt.Sprintf("New Message #%s", message.Channel.Alias),
|
||||||
fmt.Sprintf("%s: %s", message.Sender.Account.Name, message.Content),
|
fmt.Sprintf("%s: %s", message.Sender.Account.Name, message.Content),
|
||||||
@ -94,6 +96,7 @@ func NewMessage(message models.Message) (models.Message, error) {
|
|||||||
log.Warn().Err(err).Msg("An error occurred when trying notify user.")
|
log.Warn().Err(err).Msg("An error occurred when trying notify user.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
PushCommand(member.AccountID, models.UnifiedCommand{
|
PushCommand(member.AccountID, models.UnifiedCommand{
|
||||||
Action: "messages.new",
|
Action: "messages.new",
|
||||||
Payload: message,
|
Payload: message,
|
||||||
|
Loading…
Reference in New Issue
Block a user