fix: include username in me endpoint
This commit is contained in:
+18
-3
@@ -3247,8 +3247,14 @@ func serveIMMe(wrt http.ResponseWriter, req *http.Request) {
|
|||||||
writeIMRelationError(wrt, req, err)
|
writeIMRelationError(wrt, req, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
account, err := currentUserAccountPayload(principal.TenantID, principal.Uid)
|
||||||
|
if err != nil {
|
||||||
|
writeIMRelationError(wrt, req, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
writeIMSuccess(wrt, req, map[string]any{
|
writeIMSuccess(wrt, req, map[string]any{
|
||||||
"user_id": principal.Uid.UserId(),
|
"user_id": principal.Uid.UserId(),
|
||||||
|
"username": account["username"],
|
||||||
"profile": userProfilePayload10(profile),
|
"profile": userProfilePayload10(profile),
|
||||||
"profile_version": profile.Version,
|
"profile_version": profile.Version,
|
||||||
"realname": map[string]any{"status": "unknown"},
|
"realname": map[string]any{"status": "unknown"},
|
||||||
@@ -10093,13 +10099,22 @@ func userProfilePayload10(profile *types.UserProfile) map[string]any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func currentUserProfilePayload(tenantID types.TenantID, uid types.Uid, profile *types.UserProfile) (map[string]any, error) {
|
func currentUserProfilePayload(tenantID types.TenantID, uid types.Uid, profile *types.UserProfile) (map[string]any, error) {
|
||||||
user, err := store.Users.Get(tenantID, uid)
|
account, err := currentUserAccountPayload(tenantID, uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
data := userProfilePayload10(profile)
|
data := userProfilePayload10(profile)
|
||||||
data["phone_mask"] = ""
|
data["phone_mask"] = account["phone_mask"]
|
||||||
data["username"] = ""
|
data["username"] = account["username"]
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func currentUserAccountPayload(tenantID types.TenantID, uid types.Uid) (map[string]any, error) {
|
||||||
|
user, err := store.Users.Get(tenantID, uid)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
data := map[string]any{"phone_mask": "", "username": ""}
|
||||||
if user != nil {
|
if user != nil {
|
||||||
data["phone_mask"] = maskIMProfilePhone(user.Phone)
|
data["phone_mask"] = maskIMProfilePhone(user.Phone)
|
||||||
data["username"] = user.Username
|
data["username"] = user.Username
|
||||||
|
|||||||
@@ -3820,6 +3820,33 @@ func TestServeIMMailboxSubmissionMapsPolicyToValidationFailed(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestServeIMMeIncludesUsername(t *testing.T) {
|
||||||
|
ctrl := gomock.NewController(t)
|
||||||
|
defer ctrl.Finish()
|
||||||
|
expectIMBearer(t, ctrl, "token-1", 7, 42)
|
||||||
|
oldUsers := store.Users
|
||||||
|
users := mock_store.NewMockUsersPersistenceInterface(ctrl)
|
||||||
|
users.EXPECT().Get(types.TenantID(7), types.Uid(42)).Return(&types.User{Username: "gcharles7", Phone: "+8613800138000"}, nil)
|
||||||
|
store.Users = users
|
||||||
|
defer func() { store.Users = oldUsers }()
|
||||||
|
oldProfiles := store.Profiles
|
||||||
|
store.Profiles = &testProfilesStore{}
|
||||||
|
defer func() { store.Profiles = oldProfiles }()
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/v1/im/me", nil)
|
||||||
|
req.Header.Set("Authorization", "Bearer token-1")
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
serveIMMe(rec, req)
|
||||||
|
|
||||||
|
if rec.Code != http.StatusOK {
|
||||||
|
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
|
||||||
|
}
|
||||||
|
data := decodeIMHTTPResponse(t, rec).Data.(map[string]any)
|
||||||
|
if data["user_id"] != types.Uid(42).UserId() || data["username"] != "gcharles7" {
|
||||||
|
t.Fatalf("unexpected me payload: %#v body=%s", data, rec.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestServeIMMeProfile(t *testing.T) {
|
func TestServeIMMeProfile(t *testing.T) {
|
||||||
ctrl := gomock.NewController(t)
|
ctrl := gomock.NewController(t)
|
||||||
defer ctrl.Finish()
|
defer ctrl.Finish()
|
||||||
|
|||||||
Reference in New Issue
Block a user