Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Implement new unreachable state and fix broken string ref (#11748)
Browse files Browse the repository at this point in the history
* Fix string ref issue

* Implement unreachable state

* Fix eslint failure

* Fix i18n

* Fix i18n again

* Write cypress test

* Write jest test

* Write more jest tests

* Update method name

* Use unstable prefix

* Always use prefix

This is never to going to be in the spec so always use the io.element
prefix

* Update tests

* Remove redundant code from cypress test

* Use unstable prefix

* Refactor code

* Remove supressOnHover prop

* Remove sub-text label

* Join lines

* Remove blank line

* Add jsdoc
  • Loading branch information
MidhunSureshR authored Nov 7, 2023
1 parent 6849afd commit 90419bd
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 133 deletions.
64 changes: 64 additions & 0 deletions cypress/e2e/presence/presence.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/// <reference types="cypress" />
import { HomeserverInstance } from "../../plugins/utils/homeserver";

describe("Presence tests", () => {
let homeserver: HomeserverInstance;

beforeEach(() => {
cy.startHomeserver("default").then((data) => {
homeserver = data;
});
});

afterEach(() => {
cy.stopHomeserver(homeserver);
});

it("renders unreachable presence state correctly", () => {
cy.initTestUser(homeserver, "Janet");
cy.getBot(homeserver, { displayName: "Bob" }).then((bob) => {
cy.intercept("GET", "**/sync*", (req) => {
req.continue((res) => {
res.body.presence = {
events: [
{
type: "m.presence",
sender: bob.getUserId(),
content: {
presence: "io.element.unreachable",
currently_active: false,
},
},
],
};
});
});
cy.createRoom({ name: "My Room", invite: [bob.getUserId()] }).then((roomId) => {
cy.viewRoomById(roomId);
});
cy.findByRole("button", { name: "Room info" }).click();
cy.get(".mx_RightPanel").within(() => {
cy.contains("People").click();
});
cy.get(".mx_EntityTile_unreachable")
.should("contain.text", "Bob")
.should("contain.text", "User's server unreachable");
});
});
});
8 changes: 5 additions & 3 deletions res/css/views/rooms/_EntityTile.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ limitations under the License.
background-color: $header-panel-text-primary-color;
}

.mx_EntityTile .mx_PresenceLabel {
.mx_EntityTile:not(.mx_EntityTile_unreachable) .mx_PresenceLabel {
display: none;
}

.mx_EntityTile:not(.mx_EntityTile_noHover):hover .mx_PresenceLabel {
.mx_EntityTile:hover .mx_PresenceLabel {
display: block;
}

Expand Down Expand Up @@ -106,7 +106,9 @@ limitations under the License.
}

.mx_EntityTile_unknown .mx_EntityTile_avatar,
.mx_EntityTile_unknown .mx_EntityTile_name {
.mx_EntityTile_unknown .mx_EntityTile_name,
.mx_EntityTile_unreachable .mx_EntityTile_avatar,
.mx_EntityTile_unreachable .mx_EntityTile_name {
opacity: 0.25;
}

Expand Down
3 changes: 1 addition & 2 deletions src/components/views/dialogs/ForwardDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,7 @@ const ForwardDialog: React.FC<IProps> = ({ matrixClient: cli, event, permalinkCr
<BaseAvatar url={require("../../../../res/img/ellipsis.svg").default} name="..." size="36px" />
}
name={text}
presenceState="online"
suppressOnHover={true}
showPresence={false}
onClick={() => setTruncateAt(totalCount)}
/>
);
Expand Down
74 changes: 30 additions & 44 deletions src/components/views/rooms/EntityTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ const PowerLabel: Record<PowerStatus, TranslationKey> = {
[PowerStatus.Moderator]: _td("power_level|mod"),
};

export type PresenceState = "offline" | "online" | "unavailable";
export type PresenceState = "offline" | "online" | "unavailable" | "io.element.unreachable";

const PRESENCE_CLASS: Record<PresenceState, string> = {
offline: "mx_EntityTile_offline",
online: "mx_EntityTile_online",
unavailable: "mx_EntityTile_unavailable",
"offline": "mx_EntityTile_offline",
"online": "mx_EntityTile_online",
"unavailable": "mx_EntityTile_unavailable",
"io.element.unreachable": "mx_EntityTile_unreachable",
};

function presenceClassForMember(presenceState?: PresenceState, lastActiveAgo?: number, showPresence?: boolean): string {
Expand Down Expand Up @@ -75,7 +76,6 @@ interface IProps {
presenceCurrentlyActive?: boolean;
showInviteButton: boolean;
onClick(): void;
suppressOnHover: boolean;
showPresence: boolean;
subtextLabel?: string;
e2eStatus?: E2EState;
Expand All @@ -93,7 +93,6 @@ export default class EntityTile extends React.PureComponent<IProps, IState> {
presenceLastActiveAgo: 0,
presenceLastTs: 0,
showInviteButton: false,
suppressOnHover: false,
showPresence: true,
};

Expand All @@ -105,10 +104,27 @@ export default class EntityTile extends React.PureComponent<IProps, IState> {
};
}

/**
* Creates the PresenceLabel component if needed
* @returns The PresenceLabel component if we need to render it, undefined otherwise
*/
private getPresenceLabel(): JSX.Element | undefined {
if (!this.props.showPresence) return;
const activeAgo = this.props.presenceLastActiveAgo
? Date.now() - (this.props.presenceLastTs - this.props.presenceLastActiveAgo)
: -1;
return (
<PresenceLabel
activeAgo={activeAgo}
currentlyActive={this.props.presenceCurrentlyActive}
presenceState={this.props.presenceState}
/>
);
}

public render(): React.ReactNode {
const mainClassNames: Record<string, boolean> = {
mx_EntityTile: true,
mx_EntityTile_noHover: !!this.props.suppressOnHover,
};
if (this.props.className) mainClassNames[this.props.className] = true;

Expand All @@ -119,43 +135,13 @@ export default class EntityTile extends React.PureComponent<IProps, IState> {
);
mainClassNames[presenceClass] = true;

let nameEl;
const name = this.props.nameJSX || this.props.name;

if (!this.props.suppressOnHover) {
const activeAgo = this.props.presenceLastActiveAgo
? Date.now() - (this.props.presenceLastTs - this.props.presenceLastActiveAgo)
: -1;

let presenceLabel: JSX.Element | undefined;
if (this.props.showPresence) {
presenceLabel = (
<PresenceLabel
activeAgo={activeAgo}
currentlyActive={this.props.presenceCurrentlyActive}
presenceState={this.props.presenceState}
/>
);
}
if (this.props.subtextLabel) {
presenceLabel = <span className="mx_EntityTile_subtext">{this.props.subtextLabel}</span>;
}
nameEl = (
<div className="mx_EntityTile_details">
<div className="mx_EntityTile_name">{name}</div>
{presenceLabel}
</div>
);
} else if (this.props.subtextLabel) {
nameEl = (
<div className="mx_EntityTile_details">
<div className="mx_EntityTile_name">{name}</div>
<span className="mx_EntityTile_subtext">{this.props.subtextLabel}</span>
</div>
);
} else {
nameEl = <div className="mx_EntityTile_name">{name}</div>;
}
const nameAndPresence = (
<div className="mx_EntityTile_details">
<div className="mx_EntityTile_name">{name}</div>
{this.getPresenceLabel()}
</div>
);

let inviteButton;
if (this.props.showInviteButton) {
Expand Down Expand Up @@ -198,7 +184,7 @@ export default class EntityTile extends React.PureComponent<IProps, IState> {
{av}
{e2eIcon}
</div>
{nameEl}
{nameAndPresence}
{powerLabel}
{inviteButton}
</AccessibleButton>
Expand Down
20 changes: 15 additions & 5 deletions src/components/views/rooms/MemberList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default class MemberList extends React.Component<IProps, IState> {

public static contextType = SDKContext;
public context!: React.ContextType<typeof SDKContext>;
private tiles: Map<string, MemberTile> = new Map();

public constructor(props: IProps, context: React.ContextType<typeof SDKContext>) {
super(props);
Expand Down Expand Up @@ -154,7 +155,7 @@ export default class MemberList extends React.Component<IProps, IState> {
// Attach a SINGLE listener for global presence changes then locate the
// member tile and re-render it. This is more efficient than every tile
// ever attaching their own listener.
const tile = this.refs[user.userId];
const tile = this.tiles.get(user.userId);
if (tile) {
this.updateList(); // reorder the membership list
}
Expand Down Expand Up @@ -245,8 +246,7 @@ export default class MemberList extends React.Component<IProps, IState> {
<BaseAvatar url={require("../../../../res/img/ellipsis.svg").default} name="..." size="36px" />
}
name={text}
presenceState="online"
suppressOnHover={true}
showPresence={false}
onClick={onClick}
/>
);
Expand Down Expand Up @@ -307,14 +307,24 @@ export default class MemberList extends React.Component<IProps, IState> {
return members.map((m) => {
if (m instanceof RoomMember) {
// Is a Matrix invite
return <MemberTile key={m.userId} member={m} ref={m.userId} showPresence={this.showPresence} />;
return (
<MemberTile
key={m.userId}
member={m}
ref={(tile) => {
if (tile) this.tiles.set(m.userId, tile);
else this.tiles.delete(m.userId);
}}
showPresence={this.showPresence}
/>
);
} else {
// Is a 3pid invite
return (
<EntityTile
key={m.getStateKey()}
name={m.getContent().display_name}
suppressOnHover={true}
showPresence={false}
onClick={() => this.onPending3pidInviteClick(m)}
/>
);
Expand Down
2 changes: 2 additions & 0 deletions src/components/views/rooms/PresenceLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export default class PresenceLabel extends React.Component<IProps> {
// the 'active ago' ends up being 0.
if (presence && BUSY_PRESENCE_NAME.matches(presence)) return _t("presence|busy");

if (presence === "io.element.unreachable") return _t("presence|unreachable");

if (!currentlyActive && activeAgo !== undefined && activeAgo > 0) {
const duration = formatDuration(activeAgo);
if (presence === "online") return _t("presence|online_for", { duration: duration });
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -1740,7 +1740,8 @@
"online": "Online",
"online_for": "Online for %(duration)s",
"unknown": "Unknown",
"unknown_for": "Unknown for %(duration)s"
"unknown_for": "Unknown for %(duration)s",
"unreachable": "User's server unreachable"
},
"quick_settings": {
"all_settings": "All settings",
Expand Down
Loading

0 comments on commit 90419bd

Please sign in to comment.