💄 Redesigned direct messages channel

This commit is contained in:
LittleSheep 2024-05-28 20:24:40 +08:00
parent b3b1ec4585
commit 9aa5dec54d
2 changed files with 19 additions and 11 deletions

View File

@ -47,7 +47,11 @@ func addChannelMember(c *fiber.Ctx) error {
Alias: alias,
}).First(&channel).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if member, err := services.GetChannelMember(user, channel.ID); err != nil {
} else if channel.Type == models.ChannelTypeDirect {
return fiber.NewError(fiber.StatusBadRequest, "direct message member changes was not allowed")
}
if member, err := services.GetChannelMember(user, channel.ID); err != nil {
return fiber.NewError(fiber.StatusForbidden, err.Error())
} else if member.PowerLevel < 50 {
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of a channel to add member into it")
@ -85,7 +89,11 @@ func removeChannelMember(c *fiber.Ctx) error {
AccountID: user.ID,
}).First(&channel).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if member, err := services.GetChannelMember(user, channel.ID); err != nil {
} else if channel.Type == models.ChannelTypeDirect {
return fiber.NewError(fiber.StatusBadRequest, "direct message member changes was not allowed")
}
if member, err := services.GetChannelMember(user, channel.ID); err != nil {
return fiber.NewError(fiber.StatusForbidden, err.Error())
} else if member.PowerLevel < 50 {
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of a channel to remove member into it")

View File

@ -1,11 +1,12 @@
package server
import (
"fmt"
"git.solsynth.dev/hydrogen/messaging/pkg/database"
"git.solsynth.dev/hydrogen/messaging/pkg/models"
"git.solsynth.dev/hydrogen/messaging/pkg/services"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
)
func createDirectChannel(c *fiber.Ctx) error {
@ -15,7 +16,7 @@ func createDirectChannel(c *fiber.Ctx) error {
Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"`
Name string `json:"name" validate:"required"`
Description string `json:"description"`
Members []uint `json:"members"`
RelatedUser uint `json:"related_user"`
IsEncrypted bool `json:"is_encrypted"`
}
@ -36,9 +37,9 @@ func createDirectChannel(c *fiber.Ctx) error {
}
}
var members []models.Account
if err := database.C.Where("id IN ?", data.Members).Find(&members).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
var relatedUser models.Account
if err := database.C.Where("id = ?", data.RelatedUser).First(&relatedUser).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find related user: %v", err))
}
channel := models.Channel{
@ -48,11 +49,10 @@ func createDirectChannel(c *fiber.Ctx) error {
IsEncrypted: data.IsEncrypted,
AccountID: user.ID,
Type: models.ChannelTypeDirect,
Members: append([]models.ChannelMember{
Members: []models.ChannelMember{
{AccountID: user.ID, PowerLevel: 100},
}, lo.Map(members, func(item models.Account, idx int) models.ChannelMember {
return models.ChannelMember{AccountID: item.ID, PowerLevel: 100}
})...),
{AccountID: relatedUser.ID, PowerLevel: 100},
},
}
if realm != nil {