2020-07-01 01:24:46 +03:00
|
|
|
/*
|
|
|
|
Copyright 2020 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.
|
|
|
|
*/
|
|
|
|
|
2020-08-14 12:20:59 +03:00
|
|
|
import React from "react";
|
|
|
|
import classNames from "classnames";
|
2020-07-01 01:24:46 +03:00
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
2020-08-14 12:20:59 +03:00
|
|
|
import { User } from "matrix-js-sdk/src/models/user";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2020-07-01 01:24:46 +03:00
|
|
|
|
|
|
|
import { TagID } from '../../../stores/room-list/models';
|
|
|
|
import RoomAvatar from "./RoomAvatar";
|
2020-07-02 22:28:06 +03:00
|
|
|
import NotificationBadge from '../rooms/NotificationBadge';
|
2020-07-09 04:26:25 +03:00
|
|
|
import { RoomNotificationStateStore } from "../../../stores/notifications/RoomNotificationStateStore";
|
|
|
|
import { NotificationState } from "../../../stores/notifications/NotificationState";
|
2020-08-14 12:20:59 +03:00
|
|
|
import {isPresenceEnabled} from "../../../utils/presence";
|
|
|
|
import {MatrixClientPeg} from "../../../MatrixClientPeg";
|
|
|
|
import {_t} from "../../../languageHandler";
|
|
|
|
import TextWithTooltip from "../elements/TextWithTooltip";
|
|
|
|
import DMRoomMap from "../../../utils/DMRoomMap";
|
2020-07-01 01:24:46 +03:00
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
room: Room;
|
|
|
|
avatarSize: number;
|
|
|
|
tag: TagID;
|
|
|
|
displayBadge?: boolean;
|
|
|
|
forceCount?: boolean;
|
2020-07-14 01:56:25 +03:00
|
|
|
oobData?: object;
|
|
|
|
viewAvatarOnClick?: boolean;
|
2020-07-01 01:24:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
2020-07-09 04:26:25 +03:00
|
|
|
notificationState?: NotificationState;
|
2020-08-14 12:20:59 +03:00
|
|
|
icon: Icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Icon {
|
|
|
|
// Note: the names here are used in CSS class names
|
|
|
|
None = "NONE", // ... except this one
|
|
|
|
Globe = "GLOBE",
|
|
|
|
PresenceOnline = "ONLINE",
|
|
|
|
PresenceAway = "AWAY",
|
|
|
|
PresenceOffline = "OFFLINE",
|
|
|
|
}
|
|
|
|
|
|
|
|
function tooltipText(variant: Icon) {
|
|
|
|
switch (variant) {
|
|
|
|
case Icon.Globe:
|
|
|
|
return _t("This room is public");
|
|
|
|
case Icon.PresenceOnline:
|
|
|
|
return _t("Online");
|
|
|
|
case Icon.PresenceAway:
|
|
|
|
return _t("Away");
|
|
|
|
case Icon.PresenceOffline:
|
|
|
|
return _t("Offline");
|
|
|
|
}
|
2020-07-01 01:24:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class DecoratedRoomAvatar extends React.PureComponent<IProps, IState> {
|
2020-08-14 12:20:59 +03:00
|
|
|
private _dmUser: User;
|
|
|
|
private isUnmounted = false;
|
|
|
|
private isWatchingTimeline = false;
|
2020-07-01 01:24:46 +03:00
|
|
|
|
|
|
|
constructor(props: IProps) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2020-07-22 05:58:59 +03:00
|
|
|
notificationState: RoomNotificationStateStore.instance.getRoomState(this.props.room),
|
2020-08-14 12:20:59 +03:00
|
|
|
icon: this.calculateIcon(),
|
2020-07-01 01:39:25 +03:00
|
|
|
};
|
2020-07-01 01:24:46 +03:00
|
|
|
}
|
|
|
|
|
2020-08-14 12:20:59 +03:00
|
|
|
public componentWillUnmount() {
|
|
|
|
this.isUnmounted = true;
|
|
|
|
if (this.isWatchingTimeline) this.props.room.off('Room.timeline', this.onRoomTimeline);
|
|
|
|
this.dmUser = null; // clear listeners, if any
|
|
|
|
}
|
|
|
|
|
|
|
|
private get isPublicRoom(): boolean {
|
|
|
|
const joinRules = this.props.room.currentState.getStateEvents("m.room.join_rules", "");
|
|
|
|
const joinRule = joinRules && joinRules.getContent().join_rule;
|
|
|
|
return joinRule === 'public';
|
|
|
|
}
|
|
|
|
|
|
|
|
private get dmUser(): User {
|
|
|
|
return this._dmUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
private set dmUser(val: User) {
|
|
|
|
const oldUser = this._dmUser;
|
|
|
|
this._dmUser = val;
|
|
|
|
if (oldUser && oldUser !== this._dmUser) {
|
|
|
|
oldUser.off('User.currentlyActive', this.onPresenceUpdate);
|
|
|
|
oldUser.off('User.presence', this.onPresenceUpdate);
|
|
|
|
}
|
|
|
|
if (this._dmUser && oldUser !== this._dmUser) {
|
|
|
|
this._dmUser.on('User.currentlyActive', this.onPresenceUpdate);
|
|
|
|
this._dmUser.on('User.presence', this.onPresenceUpdate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private onRoomTimeline = (ev: MatrixEvent, room: Room) => {
|
|
|
|
if (this.isUnmounted) return;
|
|
|
|
|
|
|
|
// apparently these can happen?
|
|
|
|
if (!room) return;
|
|
|
|
if (this.props.room.roomId !== room.roomId) return;
|
|
|
|
|
|
|
|
if (ev.getType() === 'm.room.join_rules' || ev.getType() === 'm.room.member') {
|
|
|
|
this.setState({icon: this.calculateIcon()});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private onPresenceUpdate = () => {
|
|
|
|
if (this.isUnmounted) return;
|
|
|
|
|
|
|
|
let newIcon = this.getPresenceIcon();
|
|
|
|
if (newIcon !== this.state.icon) this.setState({icon: newIcon});
|
|
|
|
};
|
|
|
|
|
|
|
|
private getPresenceIcon(): Icon {
|
|
|
|
if (!this.dmUser) return Icon.None;
|
|
|
|
|
|
|
|
let icon = Icon.None;
|
|
|
|
|
|
|
|
const isOnline = this.dmUser.currentlyActive || this.dmUser.presence === 'online';
|
|
|
|
if (isOnline) {
|
|
|
|
icon = Icon.PresenceOnline;
|
|
|
|
} else if (this.dmUser.presence === 'offline') {
|
|
|
|
icon = Icon.PresenceOffline;
|
|
|
|
} else if (this.dmUser.presence === 'unavailable') {
|
|
|
|
icon = Icon.PresenceAway;
|
|
|
|
}
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
private calculateIcon(): Icon {
|
|
|
|
let icon = Icon.None;
|
|
|
|
|
|
|
|
// We look at the DMRoomMap and not the tag here so that we don't exclude DMs in Favourites
|
|
|
|
const otherUserId = DMRoomMap.shared().getUserIdForRoomId(this.props.room.roomId);
|
|
|
|
if (otherUserId && this.props.room.getJoinedMemberCount() === 2) {
|
|
|
|
// Track presence, if available
|
|
|
|
if (isPresenceEnabled()) {
|
|
|
|
if (otherUserId) {
|
|
|
|
this.dmUser = MatrixClientPeg.get().getUser(otherUserId);
|
|
|
|
icon = this.getPresenceIcon();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Track publicity
|
|
|
|
icon = this.isPublicRoom ? Icon.Globe : Icon.None;
|
|
|
|
if (!this.isWatchingTimeline) {
|
|
|
|
this.props.room.on('Room.timeline', this.onRoomTimeline);
|
|
|
|
this.isWatchingTimeline = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
2020-07-01 01:24:46 +03:00
|
|
|
public render(): React.ReactNode {
|
|
|
|
let badge: React.ReactNode;
|
|
|
|
if (this.props.displayBadge) {
|
|
|
|
badge = <NotificationBadge
|
|
|
|
notification={this.state.notificationState}
|
|
|
|
forceCount={this.props.forceCount}
|
|
|
|
roomId={this.props.room.roomId}
|
|
|
|
/>;
|
|
|
|
}
|
|
|
|
|
2020-08-14 12:20:59 +03:00
|
|
|
let icon;
|
|
|
|
if (this.state.icon !== Icon.None) {
|
|
|
|
icon = <TextWithTooltip
|
|
|
|
tooltip={tooltipText(this.state.icon)}
|
|
|
|
class={`mx_DecoratedRoomAvatar_icon mx_DecoratedRoomAvatar_icon_${this.state.icon.toLowerCase()}`}
|
|
|
|
/>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const classes = classNames("mx_DecoratedRoomAvatar", {
|
|
|
|
mx_DecoratedRoomAvatar_cutout: icon,
|
|
|
|
});
|
|
|
|
|
|
|
|
return <div className={classes}>
|
2020-07-14 01:56:25 +03:00
|
|
|
<RoomAvatar
|
|
|
|
room={this.props.room}
|
|
|
|
width={this.props.avatarSize}
|
|
|
|
height={this.props.avatarSize}
|
|
|
|
oobData={this.props.oobData}
|
|
|
|
viewAvatarOnClick={this.props.viewAvatarOnClick}
|
|
|
|
/>
|
2020-08-14 12:20:59 +03:00
|
|
|
{icon}
|
2020-07-01 01:24:46 +03:00
|
|
|
{badge}
|
2020-07-01 01:39:25 +03:00
|
|
|
</div>;
|
2020-07-01 01:24:46 +03:00
|
|
|
}
|
2020-07-02 22:28:06 +03:00
|
|
|
}
|