-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CRNS-317: handling deleted properties of user #727
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9fa4a5a
handling deleted properties of user
vishalnarkhede 337a3dc
fixing code comment
vishalnarkhede 62e289a
moving own user properties to utils
vishalnarkhede 042de75
Removed unused generics
vishalnarkhede 95a5f3b
improving the type structure
vishalnarkhede 54f3eb5
Merge branch 'master' into vishal/user-deleted-property-fix
vishalnarkhede ae31f82
updating user object using shallow copy
vishalnarkhede 0b920cf
Merge branch 'master' into vishal/user-deleted-property-fix
vishalnarkhede a859398
Merge branch 'vishal/user-deleted-property-fix' of github.com:GetStre…
vishalnarkhede File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import { JWTUserToken, DevToken, CheckSignature } from './signing'; | |
import { TokenManager } from './token_manager'; | ||
import { | ||
isFunction, | ||
isOwnUserBaseProperty, | ||
addFileToFormData, | ||
chatCodes, | ||
normalizeQuerySort, | ||
|
@@ -545,9 +546,12 @@ export class StreamChat< | |
_setUser( | ||
user: OwnUserResponse<ChannelType, CommandType, UserType> | UserResponse<UserType>, | ||
) { | ||
// this one is used by the frontend | ||
/** | ||
* This one is used by the frontend. This is a copy of the current user object stored on backend. | ||
* It contains reserved properties and own user properties which are not present in `this._user`. | ||
*/ | ||
this.user = user; | ||
// this one is actually used for requests... | ||
// this one is actually used for requests. This is a copy of current user provided to `connectUser` function. | ||
this._user = { ...user }; | ||
} | ||
|
||
|
@@ -1339,17 +1343,35 @@ export class StreamChat< | |
} | ||
|
||
/** update the client.state with any changes to users */ | ||
if (event.type === 'user.presence.changed' || event.type === 'user.updated') { | ||
if (event.user?.id === this.userID) { | ||
this.user = this.user && { ...this.user, ...event.user }; | ||
/** Updating only available properties in _user object. */ | ||
Object.keys(event.user).forEach((key) => { | ||
if (this._user && key in this._user) { | ||
/** @ts-expect-error */ | ||
this._user[key] = event.user[key]; | ||
} | ||
}); | ||
if ( | ||
(event.type === 'user.presence.changed' || event.type === 'user.updated') && | ||
event.user.id === this.userID | ||
) { | ||
// Remove deleted properties from user objects. | ||
for (const key in this.user) { | ||
if (key in event.user || isOwnUserBaseProperty(key)) { | ||
continue; | ||
} | ||
|
||
if (this.user?.[key]) { | ||
delete this.user[key]; | ||
} | ||
|
||
if (this._user?.[key]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whats the difference between user and _user here? just a question, not really a review feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
delete this._user[key]; | ||
} | ||
} | ||
|
||
this.user = this.user && { ...this.user, ...event.user }; | ||
|
||
/** Updating only available properties in _user object. */ | ||
for (const key in event.user) { | ||
if (this._user && key in this._user) { | ||
/** @ts-expect-error */ | ||
this._user[key] = event.user[key]; | ||
} | ||
} | ||
|
||
this.state.updateUser(event.user); | ||
this._updateMemberWatcherReferences(event.user); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey maybe create a shallow copy of user and delete properties on it instead of deleting this.user[key] directly? this way we can only set this.user in the end of the function so we will only mutate it once instead of manually deleting the keys during the function execution.
this is useful to avoid half-updated states in case this function fails in the middle of its execution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense :)