this repo has no description
1package server
2
3import (
4 "context"
5 "fmt"
6 "sync"
7 "time"
8
9 "github.com/labstack/echo/v4"
10 vyletdatabase "github.com/vylet-app/go/database/proto"
11 "github.com/vylet-app/go/generated/handlers"
12 "github.com/vylet-app/go/generated/vylet"
13)
14
15func (s *Server) getProfiles(ctx context.Context, dids []string) (map[string]*vylet.ActorDefs_ProfileView, error) {
16 resp, err := s.client.Profile.GetProfiles(ctx, &vyletdatabase.GetProfilesRequest{
17 Dids: dids,
18 })
19 if err != nil {
20 return nil, fmt.Errorf("error getting profiles: %w", err)
21 }
22 if resp.Error != nil {
23 if *resp.Error != "not found" {
24 return nil, fmt.Errorf("failed to get profiles: %s", *resp.Error)
25 }
26 }
27
28 profiles := make(map[string]*vylet.ActorDefs_ProfileView)
29 var wg sync.WaitGroup
30 var lk sync.Mutex
31 for _, profile := range resp.Profiles {
32 wg.Go(func() {
33 _, handle, err := s.fetchDidHandleFromActor(ctx, profile.Did)
34 if err != nil {
35 s.logger.Error("error getting handle for did", "did", profile.Did, "err", err)
36 return
37 }
38
39 lk.Lock()
40 defer lk.Unlock()
41 profiles[profile.Did] = &vylet.ActorDefs_ProfileView{
42 Did: profile.Did,
43 Handle: handle,
44 Avatar: profile.Avatar,
45 Description: profile.Description,
46 DisplayName: profile.DisplayName,
47 Pronouns: profile.Pronouns,
48 CreatedAt: profile.CreatedAt.AsTime().Format(time.RFC3339Nano),
49 IndexedAt: profile.IndexedAt.AsTime().Format(time.RFC3339Nano),
50 Viewer: &vylet.ActorDefs_ViewerState{},
51 }
52 })
53 }
54 wg.Wait()
55
56 return profiles, nil
57}
58
59func (s *Server) getProfilesBasic(ctx context.Context, dids []string) (map[string]*vylet.ActorDefs_ProfileViewBasic, error) {
60 resp, err := s.client.Profile.GetProfiles(ctx, &vyletdatabase.GetProfilesRequest{
61 Dids: dids,
62 })
63 if err != nil {
64 return nil, fmt.Errorf("error getting profiles: %w", err)
65 }
66 if resp.Error != nil {
67 if *resp.Error != "not found" {
68 return nil, fmt.Errorf("failed to get profiles: %s", *resp.Error)
69 }
70 }
71
72 profiles := make(map[string]*vylet.ActorDefs_ProfileViewBasic)
73 var wg sync.WaitGroup
74 var lk sync.Mutex
75 for _, profile := range resp.Profiles {
76 wg.Go(func() {
77 _, handle, err := s.fetchDidHandleFromActor(ctx, profile.Did)
78 if err != nil {
79 s.logger.Error("error getting handle for did", "did", profile.Did, "err", err)
80 return
81 }
82
83 lk.Lock()
84 defer lk.Unlock()
85 profiles[profile.Did] = &vylet.ActorDefs_ProfileViewBasic{
86 Did: profile.Did,
87 Handle: handle,
88 Avatar: profile.Avatar,
89 DisplayName: profile.DisplayName,
90 Pronouns: profile.Pronouns,
91 CreatedAt: profile.CreatedAt.AsTime().Format(time.RFC3339Nano),
92 IndexedAt: profile.IndexedAt.AsTime().Format(time.RFC3339Nano),
93 Viewer: &vylet.ActorDefs_ViewerState{},
94 }
95 })
96 }
97 wg.Wait()
98
99 return profiles, nil
100}
101
102func (s *Server) ActorGetProfilesRequiresAuth() bool {
103 return false
104}
105
106func (s *Server) HandleActorGetProfiles(e echo.Context, input *handlers.ActorGetProfilesInput) (*vylet.ActorGetProfiles_Output, *echo.HTTPError) {
107 ctx := e.Request().Context()
108 logger := s.logger.With("name", "HandleActorGetProfiles")
109
110 if len(input.Dids) == 0 {
111 return nil, NewValidationError("dids", "at least one DID is required")
112 }
113
114 if len(input.Dids) > 25 {
115 return nil, NewValidationError("dids", "at most 25 DIDs may be supplied")
116 }
117
118 logger = logger.With("dids", input.Dids)
119
120 profiles, err := s.getProfiles(ctx, input.Dids)
121 if err != nil {
122 logger.Error("error getting profiles", "err", err)
123 return nil, ErrInternalServerErr
124 }
125
126 if len(profiles) == 0 {
127 return nil, ErrNotFound
128 }
129
130 orderedProfiles := make([]*vylet.ActorDefs_ProfileView, len(profiles))
131 for _, did := range input.Dids {
132 profile, ok := profiles[did]
133 if !ok {
134 logger.Warn("did not find profile for specified DID", "did", did)
135 continue
136 }
137 orderedProfiles = append(orderedProfiles, profile)
138 }
139
140 return &vylet.ActorGetProfiles_Output{
141 Profiles: orderedProfiles,
142 }, nil
143}