59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"spark-server/internal/module/friend/entity"
|
|
"spark-server/internal/pkg/errs"
|
|
)
|
|
|
|
// LookupInput finds a user to add. Exactly one identifier.
|
|
type LookupInput struct {
|
|
UID int64
|
|
Email string
|
|
SparkID string
|
|
QRCode string
|
|
}
|
|
|
|
func (s *Service) Lookup(ctx context.Context, userID int64, in LookupInput) (*FriendView, error) {
|
|
if !s.ready() || userID <= 0 {
|
|
return nil, errs.ErrUnauthorized
|
|
}
|
|
target, _, err := s.resolveTarget(ctx, &ApplyInput{
|
|
UID: in.UID, Email: in.Email, SparkID: in.SparkID, QRCode: in.QRCode,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
blockedBy, err := s.repo.IsBlocked(ctx, target.ID, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if blockedBy {
|
|
return nil, errs.ErrNotFound
|
|
}
|
|
rel, err := s.repo.GetRelation(ctx, userID, target.ID)
|
|
if err != nil && !errors.Is(err, errs.ErrNotFound) {
|
|
return nil, err
|
|
}
|
|
if errors.Is(err, errs.ErrNotFound) {
|
|
rel = nil
|
|
}
|
|
friend, err := s.AreFriends(ctx, userID, target.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
blockedMe := rel != nil && rel.Blocked
|
|
view := viewOf(target, rel)
|
|
view.Relation = entity.ViewRelation(friend, blockedMe)
|
|
if !friend {
|
|
view.CreatedAt = 0
|
|
view.Pinned = false
|
|
if rel == nil {
|
|
view.Remark = ""
|
|
}
|
|
}
|
|
return view, nil
|
|
}
|