Skip to content

Commit

Permalink
fix: Mark as unread not working (#32939)
Browse files Browse the repository at this point in the history
Co-authored-by: Douglas Fabris <[email protected]>
  • Loading branch information
2 people authored and KevLehman committed Sep 20, 2024
1 parent da060b1 commit 51c8c73
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-balloons-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fixed issue where when you marked a room as unread and you were part of it, sometimes it would mark it as read right after
4 changes: 2 additions & 2 deletions apps/meteor/app/message-mark-as-unread/client/actionButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ Meteor.startup(() => {
const { message = messageArgs(this).msg } = props;

try {
await sdk.call('unreadMessages', message);
const subscription = ChatSubscription.findOne({
rid: message.rid,
});

if (subscription == null) {
return;
}
router.navigate('/home');
await LegacyRoomManager.close(subscription.t + subscription.name);
return router.navigate('/home');
await sdk.call('unreadMessages', message);
} catch (error) {
dispatchToastMessage({ type: 'error', message: error });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function SideBarItemTemplateWithData({
const badges = (
<Margins inlineStart={8}>
{showBadge && isUnread && (
<Badge {...({ style: { display: 'inline-flex', flexShrink: 0 } } as any)} variant={variant} title={badgeTitle}>
<Badge role='status' {...({ style: { display: 'inline-flex', flexShrink: 0 } } as any)} variant={variant} title={badgeTitle}>
{unread + tunread?.length}
</Badge>
)}
Expand All @@ -180,6 +180,7 @@ function SideBarItemTemplateWithData({
is='a'
id={id}
data-qa='sidebar-item'
data-unread={highlighted}
unread={highlighted}
selected={selected}
href={href}
Expand All @@ -205,7 +206,7 @@ function SideBarItemTemplateWithData({
threadUnread={threadUnread}
rid={rid}
unread={!!unread}
roomOpen={false}
roomOpen={selected}
type={type}
cl={cl}
name={title}
Expand Down
10 changes: 9 additions & 1 deletion apps/meteor/client/sidebar/RoomMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
useTranslation,
useEndpoint,
} from '@rocket.chat/ui-contexts';
import { useQueryClient } from '@tanstack/react-query';
import type { ReactElement } from 'react';
import React, { memo, useMemo } from 'react';

Expand Down Expand Up @@ -100,6 +101,8 @@ const RoomMenu = ({
const isOmnichannelRoom = type === 'l';
const prioritiesMenu = useOmnichannelPrioritiesMenu(rid);

const queryClient = useQueryClient();

const canLeave = ((): boolean => {
if (type === 'c' && !canLeaveChannel) {
return false;
Expand Down Expand Up @@ -173,17 +176,22 @@ const RoomMenu = ({

const handleToggleRead = useMutableCallback(async () => {
try {
queryClient.invalidateQueries(['sidebar/search/spotlight']);

if (isUnread) {
await readMessages({ rid, readThreads: true });
return;
}
await unreadMessages(undefined, rid);

if (subscription == null) {
return;
}

LegacyRoomManager.close(subscription.t + subscription.name);

router.navigate('/home');

await unreadMessages(undefined, rid);
} catch (error) {
dispatchToastMessage({ type: 'error', message: error });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ const SidebarItemTemplateWithData = ({
is='a'
id={id}
data-qa='sidebar-item'
data-unread={highlighted}
unread={highlighted}
selected={selected}
href={href}
Expand All @@ -205,7 +206,7 @@ const SidebarItemTemplateWithData = ({
threadUnread={threadUnread}
rid={rid}
unread={!!unread}
roomOpen={false}
roomOpen={selected}
type={type}
cl={cl}
name={title}
Expand Down
10 changes: 9 additions & 1 deletion apps/meteor/client/sidebarv2/RoomMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
useTranslation,
useEndpoint,
} from '@rocket.chat/ui-contexts';
import { useQueryClient } from '@tanstack/react-query';
import type { ReactElement } from 'react';
import React, { memo, useMemo } from 'react';

Expand Down Expand Up @@ -100,6 +101,8 @@ const RoomMenu = ({
const isOmnichannelRoom = type === 'l';
const prioritiesMenu = useOmnichannelPrioritiesMenu(rid);

const queryClient = useQueryClient();

const canLeave = ((): boolean => {
if (type === 'c' && !canLeaveChannel) {
return false;
Expand Down Expand Up @@ -173,17 +176,22 @@ const RoomMenu = ({

const handleToggleRead = useEffectEvent(async () => {
try {
queryClient.invalidateQueries(['sidebar/search/spotlight']);

if (isUnread) {
await readMessages({ rid, readThreads: true });
return;
}
await unreadMessages(undefined, rid);

if (subscription == null) {
return;
}

LegacyRoomManager.close(subscription.t + subscription.name);

router.navigate('/home');

await unreadMessages(undefined, rid);
} catch (error) {
dispatchToastMessage({ type: 'error', message: error });
}
Expand Down
71 changes: 71 additions & 0 deletions apps/meteor/tests/e2e/mark-unread.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { createAuxContext } from './fixtures/createAuxContext';
import { Users } from './fixtures/userStates';
import { HomeChannel } from './page-objects';
import { createTargetChannel } from './utils';
import { test, expect } from './utils/test';

test.use({ storageState: Users.admin.state });

test.describe.serial('mark-unread', () => {
let poHomeChannel: HomeChannel;
let targetChannel: string;

test.beforeEach(async ({ page, api }) => {
poHomeChannel = new HomeChannel(page);
targetChannel = await createTargetChannel(api, { members: ['user2'] });

await page.emulateMedia({ reducedMotion: 'reduce' });
await page.goto('/home');
});

test.afterEach(async ({ api }) => {
await api.post('/channels.delete', { roomName: targetChannel });
});

test.describe('Mark Unread - Sidebar Action', () => {
test('should not mark empty room as unread', async () => {
await poHomeChannel.sidenav.selectMarkAsUnread(targetChannel);

await expect(poHomeChannel.sidenav.getRoomBadge(targetChannel)).not.toBeVisible();
});

test('should mark a populated room as unread', async () => {
await poHomeChannel.sidenav.openChat(targetChannel);
await poHomeChannel.content.sendMessage('this is a message for reply');
await poHomeChannel.sidenav.selectMarkAsUnread(targetChannel);

await expect(poHomeChannel.sidenav.getRoomBadge(targetChannel)).toBeVisible();
});

test('should mark a populated room as unread - search', async () => {
await poHomeChannel.sidenav.openChat(targetChannel);
await poHomeChannel.content.sendMessage('this is a message for reply');
await poHomeChannel.sidenav.selectMarkAsUnread(targetChannel);
await poHomeChannel.sidenav.searchRoom(targetChannel);

await expect(poHomeChannel.sidenav.getSearchChannelBadge(targetChannel)).toBeVisible();
});
});

test.describe('Mark Unread - Message Action', () => {
let poHomeChannelUser2: HomeChannel;

test('should mark a populated room as unread', async ({ browser }) => {
const { page: user2Page } = await createAuxContext(browser, Users.user2);
poHomeChannelUser2 = new HomeChannel(user2Page);

await poHomeChannelUser2.sidenav.openChat(targetChannel);
await poHomeChannelUser2.content.sendMessage('this is a message for reply');
await user2Page.close();

await poHomeChannel.sidenav.openChat(targetChannel);

// wait for the sidebar item to be read
await poHomeChannel.sidenav.getSidebarItemByName(targetChannel, true).waitFor();
await poHomeChannel.content.openLastMessageMenu();
await poHomeChannel.markUnread.click();

await expect(poHomeChannel.sidenav.getRoomBadge(targetChannel)).toBeVisible();
});
});
});
22 changes: 20 additions & 2 deletions apps/meteor/tests/e2e/page-objects/fragments/home-sidenav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,18 @@ export class HomeSidenav {
return this.page.locator('role=menuitemcheckbox[name="Profile"]');
}

getSidebarItemByName(name: string): Locator {
return this.page.locator(`[data-qa="sidebar-item"][aria-label="${name}"]`);
// TODO: refactor getSidebarItemByName to not use data-qa
getSidebarItemByName(name: string, isRead?: boolean): Locator {
return this.page.locator(
['[data-qa="sidebar-item"]', `[aria-label="${name}"]`, isRead && '[data-unread="false"]'].filter(Boolean).join(''),
);
}

async selectMarkAsUnread(name: string) {
const sidebarItem = this.getSidebarItemByName(name);
await sidebarItem.focus();
await sidebarItem.locator('.rcx-sidebar-item__menu').click();
await this.page.getByRole('option', { name: 'Mark Unread' }).click();
}

async selectPriority(name: string, priority: string) {
Expand Down Expand Up @@ -170,4 +180,12 @@ export class HomeSidenav {
await this.checkboxEncryption.click();
await this.btnCreate.click();
}

getRoomBadge(roomName: string): Locator {
return this.getSidebarItemByName(roomName).getByRole('status', { exact: true });
}

getSearchChannelBadge(name: string): Locator {
return this.page.locator(`[data-qa="sidebar-item"][aria-label="${name}"]`).first().getByRole('status', { exact: true });
}
}
4 changes: 4 additions & 0 deletions apps/meteor/tests/e2e/page-objects/home-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ export class HomeChannel {
get roomHeaderToolbar(): Locator {
return this.page.locator('[role=toolbar][aria-label="Primary Room actions"]');
}

get markUnread(): Locator {
return this.page.locator('role=menuitem[name="Mark Unread"]');
}
}

0 comments on commit 51c8c73

Please sign in to comment.