This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/domain/session/leftpanel/RoomTileViewModel.js
Bruno Windels bb1c64e1ac add left panel tile view model for invites
and track in-progress state in Invite so it is shared by left panel
and open invite
2021-04-20 19:01:40 +02:00

109 lines
3.2 KiB
JavaScript

/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
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.
*/
import {BaseTileViewModel} from "./BaseTileViewModel.js";
function isSortedAsUnread(vm) {
return vm.isUnread || (vm.isOpen && vm._wasUnreadWhenOpening);
}
export class RoomTileViewModel extends BaseTileViewModel {
constructor(options) {
super(options);
const {room} = options;
this._room = room;
this._wasUnreadWhenOpening = false;
this._url = this.urlCreator.openRoomActionUrl(this._room.id);
}
get kind() {
return "room";
}
get url() {
return this._url;
}
compare(other) {
const parentComparison = super.compare(other);
if (parentComparison !== 0) {
return parentComparison;
}
/*
put unread rooms first
then put rooms with a timestamp first, and sort by name
then sort by name for rooms without a timestamp
*/
const myRoom = this._room;
const theirRoom = other._room;
if (myRoom.isLowPriority !== theirRoom.isLowPriority) {
if (myRoom.isLowPriority) {
return 1;
}
return -1;
}
if (isSortedAsUnread(this) !== isSortedAsUnread(other)) {
if (isSortedAsUnread(this)) {
return -1;
}
return 1;
}
const myTimestamp = myRoom.lastMessageTimestamp;
const theirTimestamp = theirRoom.lastMessageTimestamp;
const myTimestampValid = Number.isSafeInteger(myTimestamp);
const theirTimestampValid = Number.isSafeInteger(theirTimestamp);
// if either does not have a timestamp, put the one with a timestamp first
if (myTimestampValid !== theirTimestampValid) {
if (!theirTimestampValid) {
return -1;
}
return 1;
}
const timeDiff = theirTimestamp - myTimestamp;
if (timeDiff === 0 || !theirTimestampValid || !myTimestampValid) {
// sort alphabetically
const nameCmp = this.name.localeCompare(other.name);
if (nameCmp === 0) {
return this._room.id.localeCompare(other._room.id);
}
return nameCmp;
}
return timeDiff;
}
get isUnread() {
return this._room.isUnread;
}
get name() {
return this._room.name || this.i18n`Empty Room`;
}
get badgeCount() {
return this._room.notificationCount;
}
get isHighlighted() {
return this._room.highlightCount !== 0;
}
get _avatarSource() {
return this._room;
}
}