Invite friends check

This commit is contained in:
2024-04-06 14:36:36 +08:00
parent b2719ae302
commit a96910edd2
7 changed files with 60 additions and 3 deletions

View File

@ -8,6 +8,7 @@ import (
"google.golang.org/grpc"
)
var Friendships idpb.FriendshipsClient
var Notify idpb.NotifyClient
var Auth idpb.AuthClient
@ -16,6 +17,7 @@ func ConnectPassport() error {
if conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials())); err != nil {
return err
} else {
Friendships = idpb.NewFriendshipsClient(conn)
Notify = idpb.NewNotifyClient(conn)
Auth = idpb.NewAuthClient(conn)
}

View File

@ -44,7 +44,7 @@ func inviteChannel(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
if err := services.AddChannelMember(account, channel); err != nil {
if err := services.InviteChannelMember(account, channel); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
@ -84,3 +84,31 @@ func kickChannel(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
}
}
func leaveChannel(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
channelId, _ := c.ParamsInt("channelId", 0)
var data struct {
AccountName string `json:"account_name" validate:"required"`
}
if err := BindAndValidate(c, &data); err != nil {
return err
}
var channel models.Channel
if err := database.C.Where(&models.Channel{
BaseModel: models.BaseModel{ID: uint(channelId)},
}).First(&channel).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if user.ID == channel.AccountID {
return fiber.NewError(fiber.StatusBadRequest, "you cannot leave your own channel")
}
if err := services.RemoveChannelMember(user, channel); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
}
}

View File

@ -88,6 +88,7 @@ func NewServer() {
channels.Get("/:channelId/members", listChannelMembers)
channels.Post("/:channelId/invite", authMiddleware, inviteChannel)
channels.Post("/:channelId/kick", authMiddleware, kickChannel)
channels.Post("/:channelId/leave", authMiddleware, leaveChannel)
channels.Get("/:channel/messages", listMessage)
channels.Post("/:channel/messages", authMiddleware, newTextMessage)

View File

@ -10,6 +10,17 @@ import (
"github.com/spf13/viper"
)
func GetAccountFriend(userId, relatedId uint, status int) (*proto.FriendshipResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
return grpc.Friendships.GetFriendship(ctx, &proto.FriendshipTwoSideLookupRequest{
AccountId: uint64(userId),
RelatedId: uint64(relatedId),
Status: uint32(status),
})
}
func NotifyAccount(user models.Account, subject, content string, realtime bool, links ...*proto.NotifyLink) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

View File

@ -132,6 +132,20 @@ func ListChannelMember(channelId uint) ([]models.ChannelMember, error) {
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,
@ -139,7 +153,6 @@ func AddChannelMember(user models.Account, target models.Channel) error {
}
err := database.C.Save(&member).Error
return err
}