53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"spark-server/internal/pkg/errs"
|
|
"spark-server/internal/pkg/i18n"
|
|
"spark-server/internal/pkg/reqctx"
|
|
)
|
|
|
|
func (s *Service) Block(ctx context.Context, userID, targetID int64) error {
|
|
return s.setBlocked(ctx, userID, targetID, true)
|
|
}
|
|
|
|
func (s *Service) Unblock(ctx context.Context, userID, targetID int64) error {
|
|
return s.setBlocked(ctx, userID, targetID, false)
|
|
}
|
|
|
|
func (s *Service) setBlocked(ctx context.Context, userID, targetID int64, blocked bool) error {
|
|
if !s.ready() || userID <= 0 {
|
|
return errs.ErrUnauthorized
|
|
}
|
|
if targetID <= 0 {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
if userID == targetID {
|
|
return errs.ErrInvalidArgument.WithMsg(i18n.T(reqctx.Lang(ctx), i18n.KeyFriendSelf, nil))
|
|
}
|
|
if blocked {
|
|
if _, err := s.activeUser(ctx, targetID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return s.repo.SetBlocked(ctx, userID, targetID, blocked)
|
|
}
|
|
|
|
func (s *Service) Delete(ctx context.Context, userID, friendID int64) error {
|
|
if !s.ready() || userID <= 0 {
|
|
return errs.ErrUnauthorized
|
|
}
|
|
if friendID <= 0 {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
if err := s.repo.DeleteFriend(ctx, userID, friendID); err != nil {
|
|
if errors.Is(err, errs.ErrNotFound) {
|
|
return errs.ErrNotFound.WithMsg(i18n.T(reqctx.Lang(ctx), i18n.KeyFriendNotFriend, nil))
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|