diff --git a/server/db/mysql/tenant_adapter.go b/server/db/mysql/tenant_adapter.go index 45d4333..be58b87 100644 --- a/server/db/mysql/tenant_adapter.go +++ b/server/db/mysql/tenant_adapter.go @@ -6087,16 +6087,30 @@ func (a *adapter) TenantUserProfileGet(id t.TenantID, uid t.Uid) (*t.UserProfile if cancel != nil { defer cancel() } - profileID, err := nextSnowflakeID() - if err != nil { + profile, err := a.userProfileByUID(ctx, uid) + if err == nil { + return profile, nil + } + if err != sql.ErrNoRows { return nil, err } - if _, err := a.db.ExecContext(ctx, `INSERT IGNORE INTO im_user_profiles(id,user_id,public_account) VALUES(?,?,?)`, profileID, decodeUID(uid), uid.UserId()); err != nil { + if err := a.ensureUserProfile(ctx, uid); err != nil { return nil, err } return a.userProfileByUID(ctx, uid) } +func (a *adapter) ensureUserProfile(ctx context.Context, uid t.Uid) error { + profileID, err := nextSnowflakeID() + if err != nil { + return err + } + if _, err := a.db.ExecContext(ctx, `INSERT IGNORE INTO im_user_profiles(id,user_id,public_account) VALUES(?,?,?)`, profileID, decodeUID(uid), uid.UserId()); err != nil { + return err + } + return nil +} + func (a *adapter) TenantUserProfileUpdate(id t.TenantID, uid t.Uid, patch t.UserProfilePatch) (*t.UserProfile, error) { if !a.tenantScopedDB { tenantAdp, err := a.routedTenantAdapter(id) @@ -6114,11 +6128,7 @@ func (a *adapter) TenantUserProfileUpdate(id t.TenantID, uid t.Uid, patch t.User if cancel != nil { defer cancel() } - profileID, err := nextSnowflakeID() - if err != nil { - return nil, err - } - if _, err := a.db.ExecContext(ctx, `INSERT IGNORE INTO im_user_profiles(id,user_id,public_account) VALUES(?,?,?)`, profileID, decodeUID(uid), uid.UserId()); err != nil { + if err := a.ensureUserProfile(ctx, uid); err != nil { return nil, err } sets := []string{"version=version+1", "updated_at=CURRENT_TIMESTAMP(3)"} @@ -6185,16 +6195,30 @@ func (a *adapter) TenantUserPrivacyGet(id t.TenantID, uid t.Uid) (*t.UserPrivacy if cancel != nil { defer cancel() } - privacyID, err := nextSnowflakeID() - if err != nil { + privacy, err := a.userPrivacyByUID(ctx, uid) + if err == nil { + return privacy, nil + } + if err != sql.ErrNoRows { return nil, err } - if _, err := a.db.ExecContext(ctx, `INSERT IGNORE INTO im_user_privacy_settings(id,user_id,add_me_methods) VALUES(?,?,'{"account":true,"phone":true,"qr":true,"group":true}')`, privacyID, decodeUID(uid)); err != nil { + if err := a.ensureUserPrivacy(ctx, uid); err != nil { return nil, err } return a.userPrivacyByUID(ctx, uid) } +func (a *adapter) ensureUserPrivacy(ctx context.Context, uid t.Uid) error { + privacyID, err := nextSnowflakeID() + if err != nil { + return err + } + if _, err := a.db.ExecContext(ctx, `INSERT IGNORE INTO im_user_privacy_settings(id,user_id,add_me_methods) VALUES(?,?,'{"account":true,"phone":true,"qr":true,"group":true}')`, privacyID, decodeUID(uid)); err != nil { + return err + } + return nil +} + func (a *adapter) TenantUserPrivacyUpdate(id t.TenantID, uid t.Uid, patch t.UserPrivacyPatch) (*t.UserPrivacySettings, error) { if !a.tenantScopedDB { tenantAdp, err := a.routedTenantAdapter(id) @@ -6212,11 +6236,7 @@ func (a *adapter) TenantUserPrivacyUpdate(id t.TenantID, uid t.Uid, patch t.User if cancel != nil { defer cancel() } - privacyID, err := nextSnowflakeID() - if err != nil { - return nil, err - } - if _, err := a.db.ExecContext(ctx, `INSERT IGNORE INTO im_user_privacy_settings(id,user_id,add_me_methods) VALUES(?,?,'{"account":true,"phone":true,"qr":true,"group":true}')`, privacyID, decodeUID(uid)); err != nil { + if err := a.ensureUserPrivacy(ctx, uid); err != nil { return nil, err } sets := []string{"version=version+1", "updated_at=CURRENT_TIMESTAMP(3)"} diff --git a/server/im_http.go b/server/im_http.go index 5042e93..84c3746 100644 --- a/server/im_http.go +++ b/server/im_http.go @@ -6575,6 +6575,8 @@ func serveIMUserProfile(wrt http.ResponseWriter, req *http.Request, identifier s } user, err := store.Users.Get(principal.TenantID, userID) if err != nil { + logs.Warn.Printf("im user profile: load user failed request_id=%s viewer=%s target=%s: %v", + req.Header.Get("X-Request-ID"), principal.Uid.UserId(), userID.UserId(), err) writeIMError(wrt, req, http.StatusInternalServerError, 90000, imReasonInternal, "internal error", nil) return } @@ -6584,11 +6586,15 @@ func serveIMUserProfile(wrt http.ResponseWriter, req *http.Request, identifier s } ctx, err := store.Relations.GetRelationshipContext(principal.TenantID, principal.Uid, userID, principal.Uid.P2PName(userID)) if err != nil { + logs.Warn.Printf("im user profile: relationship context failed request_id=%s viewer=%s target=%s: %v", + req.Header.Get("X-Request-ID"), principal.Uid.UserId(), userID.UserId(), err) writeIMRelationError(wrt, req, err) return } privacy, err := store.Profiles.GetPrivacy(principal.TenantID, userID) if err != nil { + logs.Warn.Printf("im user profile: privacy load failed request_id=%s viewer=%s target=%s: %v", + req.Header.Get("X-Request-ID"), principal.Uid.UserId(), userID.UserId(), err) writeIMRelationError(wrt, req, err) return }