fix: avoid profile default hot path writes
This commit is contained in:
@@ -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)"}
|
||||
|
||||
Reference in New Issue
Block a user