this repo has no description
1package server
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "time"
8
9 "github.com/labstack/echo/v4"
10 "github.com/vylet-app/go/database/client"
11 vyletdatabase "github.com/vylet-app/go/database/proto"
12 "github.com/vylet-app/go/generated/handlers"
13 "github.com/vylet-app/go/generated/vylet"
14)
15
16type ActorGetProfileInput struct {
17 Actor string `query:"actor"`
18}
19
20func (s *Server) getProfile(ctx context.Context, actor string) (*vylet.ActorDefs_ProfileView, error) {
21 did, handle, err := s.fetchDidHandleFromActor(ctx, actor)
22 if err != nil {
23 return nil, fmt.Errorf("error fetching did and handle: %w", err)
24 }
25
26 resp, err := s.client.Profile.GetProfile(ctx, &vyletdatabase.GetProfileRequest{
27 Did: did,
28 })
29 if err != nil {
30 return nil, fmt.Errorf("error getting profile: %w", err)
31 }
32 if resp.Error != nil {
33 if client.IsNotFoundError(resp.Error) {
34 return nil, ErrDatabaseNotFound
35 }
36 return nil, fmt.Errorf("error getting profile: %s", *resp.Error)
37 }
38
39 return &vylet.ActorDefs_ProfileView{
40 Did: did,
41 Handle: handle,
42 Avatar: resp.Profile.Avatar,
43 Description: resp.Profile.Description,
44 DisplayName: resp.Profile.DisplayName,
45 Pronouns: resp.Profile.Pronouns,
46 CreatedAt: resp.Profile.CreatedAt.AsTime().Format(time.RFC3339Nano),
47 IndexedAt: resp.Profile.IndexedAt.AsTime().Format(time.RFC3339Nano),
48 Viewer: &vylet.ActorDefs_ViewerState{},
49 }, nil
50}
51
52func (s *Server) getProfileBasic(ctx context.Context, actor string) (*vylet.ActorDefs_ProfileViewBasic, error) {
53 did, handle, err := s.fetchDidHandleFromActor(ctx, actor)
54 if err != nil {
55 return nil, fmt.Errorf("error fetching did and handle: %w", err)
56 }
57
58 resp, err := s.client.Profile.GetProfile(ctx, &vyletdatabase.GetProfileRequest{
59 Did: did,
60 })
61 if err != nil {
62 return nil, fmt.Errorf("error getting profile: %w", err)
63 }
64 if resp.Error != nil {
65 if client.IsNotFoundError(resp.Error) {
66 return nil, ErrDatabaseNotFound
67 }
68 return nil, fmt.Errorf("error getting profile: %s", *resp.Error)
69 }
70
71 return &vylet.ActorDefs_ProfileViewBasic{
72 Did: did,
73 Handle: handle,
74 Avatar: resp.Profile.Avatar,
75 DisplayName: resp.Profile.DisplayName,
76 Pronouns: resp.Profile.Pronouns,
77 CreatedAt: resp.Profile.CreatedAt.AsTime().Format(time.RFC3339Nano),
78 IndexedAt: resp.Profile.IndexedAt.AsTime().Format(time.RFC3339Nano),
79 Viewer: &vylet.ActorDefs_ViewerState{},
80 }, nil
81}
82
83func (s *Server) ActorGetProfileRequiresAuth() bool {
84 return false
85}
86
87func (s *Server) HandleActorGetProfile(e echo.Context, input *handlers.ActorGetProfileInput) (*vylet.ActorDefs_ProfileView, *echo.HTTPError) {
88 ctx := e.Request().Context()
89 logger := s.logger.With("name", "HandleActorGetProfile")
90
91 if input.Actor == "" {
92 return nil, NewValidationError("actor", "actor parameter is required")
93 }
94
95 logger = logger.With("actor", input.Actor)
96
97 profile, err := s.getProfile(ctx, input.Actor)
98 if err != nil {
99 if errors.Is(err, ErrActorNotValid) {
100 return nil, NewValidationError("actor", "actor parameter must be a valid DID or handle")
101 }
102 if errors.Is(err, ErrDatabaseNotFound) {
103 return nil, ErrNotFound
104 }
105 logger.Error("error getting profile", "err", err)
106 return nil, ErrInternalServerErr
107 }
108
109 return profile, nil
110}