Skip to content
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 9 commits into from
Jul 28, 2021
46 changes: 34 additions & 12 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { JWTUserToken, DevToken, CheckSignature } from './signing';
import { TokenManager } from './token_manager';
import {
isFunction,
isOwnUserBaseProperty,
addFileToFormData,
chatCodes,
normalizeQuerySort,
Expand Down Expand Up @@ -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 };
}

Expand Down Expand Up @@ -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];
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense :)

}

if (this._user?.[key]) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2021-07-28 at 18 14 34

This comment might answer this one!!

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);
}
Expand Down
10 changes: 8 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,11 +567,11 @@ export type MuteUserResponse<
own_user?: OwnUserResponse<ChannelType, CommandType, UserType>;
};

export type OwnUserResponse<
export type OwnUserBase<
ChannelType extends UnknownType = UnknownType,
CommandType extends string = LiteralStringForUnion,
UserType extends UnknownType = UnknownType
> = UserResponse<UserType> & {
> = {
channel_mutes: ChannelMute<ChannelType, CommandType, UserType>[];
devices: Device<UserType>[];
mutes: Mute<UserType>[];
Expand All @@ -582,6 +582,12 @@ export type OwnUserResponse<
roles?: string[];
};

export type OwnUserResponse<
ChannelType extends UnknownType = UnknownType,
CommandType extends string = LiteralStringForUnion,
UserType extends UnknownType = UnknownType
> = UserResponse<UserType> & OwnUserBase<ChannelType, CommandType, UserType>;

export type PartialUpdateChannelAPIResponse<
ChannelType = UnknownType,
CommandType extends string = LiteralStringForUnion,
Expand Down
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import FormData from 'form-data';
import {
AscDesc,
LiteralStringForUnion,
OwnUserBase,
OwnUserResponse,
UnknownType,
UserResponse,
Expand Down Expand Up @@ -74,6 +75,23 @@ export function isOwnUser<
);
}

export function isOwnUserBaseProperty(property: string) {
const ownUserBaseProperties: {
[Property in keyof Required<OwnUserBase>]: boolean;
} = {
channel_mutes: true,
devices: true,
mutes: true,
total_unread_count: true,
unread_channels: true,
unread_count: true,
invisible: true,
roles: true,
};

return ownUserBaseProperties[property as keyof OwnUserBase];
}

export function addFileToFormData(
uri: string | NodeJS.ReadableStream | Buffer | File,
name?: string,
Expand Down