MCPcopy Create free account
hub / github.com/daodst/chat / RetrieveUserProfile

Function RetrieveUserProfile

appservice/api/query.go:83–134  ·  view source on GitHub ↗

RetrieveUserProfile is a wrapper that queries both the local database and application services for a given user's profile TODO: Remove this, it's called from federationapi and clientapi but is a pure function

(
	ctx context.Context,
	userID string,
	asAPI AppServiceInternalAPI,
	profileAPI userapi.ClientUserAPI,
)

Source from the content-addressed store, hash-verified

81// application services for a given user's profile
82// TODO: Remove this, it's called from federationapi and clientapi but is a pure function
83func RetrieveUserProfile(
84 ctx context.Context,
85 userID string,
86 asAPI AppServiceInternalAPI,
87 profileAPI userapi.ClientUserAPI,
88) (*authtypes.Profile, error) {
89 localpart, _, err := gomatrixserverlib.SplitID('@', userID)
90 if err != nil {
91 return nil, err
92 }
93
94 // Try to query the user from the local database
95 res := &userapi.QueryProfileResponse{}
96 err = profileAPI.QueryProfile(ctx, &userapi.QueryProfileRequest{UserID: userID}, res)
97 if err != nil {
98 return nil, err
99 }
100 profile := &authtypes.Profile{
101 Localpart: localpart,
102 DisplayName: res.DisplayName,
103 AvatarURL: res.AvatarURL,
104 TelNumbers: res.TelNumbers,
105 }
106 if res.UserExists {
107 return profile, nil
108 }
109
110 // Query the appservice component for the existence of an AS user
111 userReq := UserIDExistsRequest{UserID: userID}
112 var userResp UserIDExistsResponse
113 if err = asAPI.UserIDExists(ctx, &userReq, &userResp); err != nil {
114 return nil, err
115 }
116
117 // If no user exists, return
118 if !userResp.UserIDExists {
119 return nil, errors.New("no known profile for given user ID")
120 }
121
122 // Try to query the user from the local database again
123 err = profileAPI.QueryProfile(ctx, &userapi.QueryProfileRequest{UserID: userID}, res)
124 if err != nil {
125 return nil, err
126 }
127
128 // profile should not be nil at this point
129 return &authtypes.Profile{
130 Localpart: localpart,
131 DisplayName: res.DisplayName,
132 AvatarURL: res.AvatarURL,
133 }, nil
134}

Callers

nothing calls this directly

Calls 2

QueryProfileMethod · 0.65
UserIDExistsMethod · 0.65

Tested by

no test coverage detected