Add HTTP /v1 MVC, TCP send path, groups with server-side member-avatar collage, object-storage upload, and friends/favorites around the architecture-v3 layout. Co-authored-by: Cursor <cursoragent@cursor.com>
75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"spark-server/internal/module/group/entity"
|
|
userentity "spark-server/internal/module/user/entity"
|
|
"spark-server/internal/pkg/errs"
|
|
"spark-server/internal/pkg/i18n"
|
|
"spark-server/internal/pkg/reqctx"
|
|
)
|
|
|
|
func (s *Service) activeUser(ctx context.Context, id int64) error {
|
|
u, err := s.users.Get(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if u == nil || u.Status != userentity.StatusActive {
|
|
return errs.ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) loadActive(ctx context.Context, groupID int64) (*entity.Group, error) {
|
|
g, err := s.repo.Get(ctx, groupID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !g.Active() {
|
|
return nil, errs.ErrNotFound.WithMsg(i18n.T(reqctx.Lang(ctx), i18n.KeyGroupDissolved, nil))
|
|
}
|
|
return g, nil
|
|
}
|
|
|
|
func (s *Service) requireMember(ctx context.Context, groupID, userID int64) (*entity.Member, error) {
|
|
mem, err := s.repo.GetMember(ctx, groupID, userID)
|
|
if err != nil {
|
|
if errors.Is(err, errs.ErrNotFound) {
|
|
return nil, errs.ErrForbidden.WithMsg(i18n.T(reqctx.Lang(ctx), i18n.KeyGroupNotMember, nil))
|
|
}
|
|
return nil, err
|
|
}
|
|
return mem, nil
|
|
}
|
|
|
|
func (s *Service) requireManage(ctx context.Context, groupID, userID int64) (*entity.Member, error) {
|
|
mem, err := s.requireMember(ctx, groupID, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !mem.CanManage() {
|
|
return nil, errs.ErrForbidden.WithMsg(i18n.T(reqctx.Lang(ctx), i18n.KeyGroupRoleDenied, nil))
|
|
}
|
|
return mem, nil
|
|
}
|
|
|
|
func (s *Service) requireOwner(ctx context.Context, g *entity.Group, userID int64) error {
|
|
if g == nil || g.OwnerID != userID {
|
|
return errs.ErrForbidden.WithMsg(i18n.T(reqctx.Lang(ctx), i18n.KeyGroupRoleDenied, nil))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) requireFriend(ctx context.Context, a, b int64) error {
|
|
ok, err := s.friends.AreFriends(ctx, a, b)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
return errs.ErrForbidden.WithMsg(i18n.T(reqctx.Lang(ctx), i18n.KeyGroupNotFriend, nil))
|
|
}
|
|
return nil
|
|
}
|