From 43917bd789baadcb2cd8f7d510c6e26690252c4d Mon Sep 17 00:00:00 2001 From: Reinaldo Neto <47038980+reinaldonetof@users.noreply.github.com> Date: Fri, 28 Apr 2023 12:16:14 -0300 Subject: [PATCH] fix: update the number of members from a room (#4955) * [FIX] Update the number of members from a room * update the subscription users count inside room actions view * remove console.log * added e2e test --------- Co-authored-by: Gleidson Daniel Silva --- app/definitions/ISubscription.ts | 1 + app/lib/database/model/Subscription.js | 2 ++ app/lib/database/model/migrations.js | 9 ++++++ app/lib/database/schema/app.js | 5 ++-- .../helpers/mergeSubscriptionsRooms.ts | 3 ++ app/views/RoomActionsView/index.tsx | 28 ++++++++++++++++--- e2e/tests/room/03-roomactions.spec.ts | 4 +++ 7 files changed, 46 insertions(+), 6 deletions(-) diff --git a/app/definitions/ISubscription.ts b/app/definitions/ISubscription.ts index 904e13d02..10dce8f19 100644 --- a/app/definitions/ISubscription.ts +++ b/app/definitions/ISubscription.ts @@ -102,6 +102,7 @@ export interface ISubscription { onHold?: boolean; source?: IOmnichannelSource; hideMentionStatus?: boolean; + usersCount?: number; // https://nozbe.github.io/WatermelonDB/Relation.html#relation-api messages: RelationModified; threads: RelationModified; diff --git a/app/lib/database/model/Subscription.js b/app/lib/database/model/Subscription.js index bbc732119..fc873bec6 100644 --- a/app/lib/database/model/Subscription.js +++ b/app/lib/database/model/Subscription.js @@ -137,5 +137,7 @@ export default class Subscription extends Model { @field('on_hold') onHold; + @field('users_count') usersCount; + @json('source', sanitizer) source; } diff --git a/app/lib/database/model/migrations.js b/app/lib/database/model/migrations.js index 678fb0e42..8f23b7f2c 100644 --- a/app/lib/database/model/migrations.js +++ b/app/lib/database/model/migrations.js @@ -257,6 +257,15 @@ export default schemaMigrations({ columns: [{ name: 'e2e_suggested_key', type: 'string', isOptional: true }] }) ] + }, + { + toVersion: 21, + steps: [ + addColumns({ + table: 'subscriptions', + columns: [{ name: 'users_count', type: 'string', isOptional: true }] + }) + ] } ] }); diff --git a/app/lib/database/schema/app.js b/app/lib/database/schema/app.js index 754c19afa..90ef03aa2 100644 --- a/app/lib/database/schema/app.js +++ b/app/lib/database/schema/app.js @@ -1,7 +1,7 @@ import { appSchema, tableSchema } from '@nozbe/watermelondb'; export default appSchema({ - version: 20, + version: 21, tables: [ tableSchema({ name: 'subscriptions', @@ -63,7 +63,8 @@ export default appSchema({ { name: 'team_main', type: 'boolean', isOptional: true }, // Use `Q.notEq(true)` to get false or null { name: 'on_hold', type: 'boolean', isOptional: true }, { name: 'source', type: 'string', isOptional: true }, - { name: 'hide_mention_status', type: 'boolean', isOptional: true } + { name: 'hide_mention_status', type: 'boolean', isOptional: true }, + { name: 'users_count', type: 'number', isOptional: true } ] }), tableSchema({ diff --git a/app/lib/methods/helpers/mergeSubscriptionsRooms.ts b/app/lib/methods/helpers/mergeSubscriptionsRooms.ts index 6763298f2..90b5445b2 100644 --- a/app/lib/methods/helpers/mergeSubscriptionsRooms.ts +++ b/app/lib/methods/helpers/mergeSubscriptionsRooms.ts @@ -85,6 +85,9 @@ export const merge = ( if (room && 'source' in room) { mergedSubscription.source = room?.source; } + if (room && 'usersCount' in room) { + mergedSubscription.usersCount = room.usersCount; + } } if (!mergedSubscription.name) { diff --git a/app/views/RoomActionsView/index.tsx b/app/views/RoomActionsView/index.tsx index 79128f462..5f981f7c5 100644 --- a/app/views/RoomActionsView/index.tsx +++ b/app/views/RoomActionsView/index.tsx @@ -86,7 +86,7 @@ interface IRoomActionsViewProps extends IActionSheetProvider, IBaseScreen; joined: boolean; canViewMembers: boolean; @@ -153,10 +153,12 @@ class RoomActionsView extends React.Component { if (this.mounted) { - this.setState({ room: changes }); + this.setState({ room: changes, membersCount: changes.usersCount }); } else { // @ts-ignore this.state.room = changes; + // @ts-ignore + this.state.membersCount = changes.usersCount; } }); } @@ -191,7 +193,8 @@ class RoomActionsView extends React.Component { + const { room } = this.state; + if (members === room.usersCount) return; + try { + const db = database.active; + await db.write(async () => { + await room.update( + protectedFunction((r: TSubscriptionModel) => { + r.usersCount = members; + }) + ); + }); + } catch { + // + } + }; + onPressTouchable: IOnPressTouch = (item: { route?: keyof ChatsStackParamList; params?: ChatsStackParamList[keyof ChatsStackParamList]; @@ -1052,7 +1072,7 @@ class RoomActionsView extends React.Component 0 ? `${membersCount} ${I18n.t('members')}` : undefined} + subtitle={membersCount && membersCount > 0 ? `${membersCount} ${I18n.t('members')}` : undefined} onPress={() => this.onPressTouchable({ route: 'RoomMembersView', params: { rid, room, joined: this.joined } })} testID='room-actions-members' left={() => } diff --git a/e2e/tests/room/03-roomactions.spec.ts b/e2e/tests/room/03-roomactions.spec.ts index 8c76ace0e..d1b5ac0d1 100644 --- a/e2e/tests/room/03-roomactions.spec.ts +++ b/e2e/tests/room/03-roomactions.spec.ts @@ -103,6 +103,7 @@ describe('Room actions screen', () => { it('should have members', async () => { await expect(element(by.id('room-actions-members'))).toExist(); + await expect(element(by[textMatcher]('1 members'))).toExist(); }); it('should have files', async () => { @@ -338,6 +339,9 @@ describe('Room actions screen', () => { await element(by.id('selected-users-view-submit')).tap(); await sleep(300); await backToActions(); + await waitFor(element(by[textMatcher]('3 members'))) + .toExist() + .withTimeout(5000); }); describe('Room Members', () => {