2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
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-04-21 00:56:39 +05:30
|
|
|
import {SimpleTile} from "./SimpleTile.js";
|
2021-06-03 22:43:13 +05:30
|
|
|
import {ReactionsViewModel} from "../ReactionsViewModel.js";
|
2021-04-28 17:40:54 +05:30
|
|
|
import {getIdentifierColorNumber, avatarInitials, getAvatarHttpUrl} from "../../../../avatar.js";
|
2019-03-09 05:10:03 +05:30
|
|
|
|
2021-05-17 15:45:13 +05:30
|
|
|
export class BaseMessageTile extends SimpleTile {
|
2019-03-09 05:10:03 +05:30
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2020-08-19 13:57:27 +05:30
|
|
|
this._date = this._entry.timestamp ? new Date(this._entry.timestamp) : null;
|
2019-06-16 19:09:24 +05:30
|
|
|
this._isContinuation = false;
|
2021-06-03 22:43:13 +05:30
|
|
|
this._reactions = null;
|
2021-06-04 19:04:44 +05:30
|
|
|
if (this._entry.annotations || this._entry.pendingAnnotations) {
|
2021-06-03 22:43:13 +05:30
|
|
|
this._updateReactions();
|
|
|
|
}
|
2019-03-09 05:10:03 +05:30
|
|
|
}
|
|
|
|
|
2020-10-28 22:07:45 +05:30
|
|
|
get _mediaRepository() {
|
|
|
|
return this._room.mediaRepository;
|
|
|
|
}
|
|
|
|
|
2020-09-11 15:13:40 +05:30
|
|
|
get displayName() {
|
|
|
|
return this._entry.displayName || this.sender;
|
|
|
|
}
|
|
|
|
|
2019-03-09 05:10:03 +05:30
|
|
|
get sender() {
|
2020-09-11 15:13:40 +05:30
|
|
|
return this._entry.sender;
|
2019-03-09 05:10:03 +05:30
|
|
|
}
|
|
|
|
|
2020-08-20 21:02:18 +05:30
|
|
|
// Avatar view model contract
|
2020-08-20 19:33:52 +05:30
|
|
|
get avatarColorNumber() {
|
2020-08-13 16:11:00 +05:30
|
|
|
return getIdentifierColorNumber(this._entry.sender);
|
|
|
|
}
|
|
|
|
|
2021-04-28 17:40:54 +05:30
|
|
|
avatarUrl(size) {
|
|
|
|
return getAvatarHttpUrl(this._entry.avatarUrl, size, this.platform, this._mediaRepository);
|
2020-08-20 19:33:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get avatarLetter() {
|
|
|
|
return avatarInitials(this.sender);
|
|
|
|
}
|
|
|
|
|
2020-08-20 21:02:18 +05:30
|
|
|
get avatarTitle() {
|
2020-09-11 15:13:40 +05:30
|
|
|
return this.displayName;
|
2020-08-20 21:02:18 +05:30
|
|
|
}
|
|
|
|
|
2019-03-09 05:10:03 +05:30
|
|
|
get date() {
|
2020-08-19 13:57:27 +05:30
|
|
|
return this._date && this._date.toLocaleDateString({}, {month: "numeric", day: "numeric"});
|
2019-03-09 05:10:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get time() {
|
2020-08-19 13:57:27 +05:30
|
|
|
return this._date && this._date.toLocaleTimeString({}, {hour: "numeric", minute: "2-digit"});
|
2019-03-09 05:10:03 +05:30
|
|
|
}
|
|
|
|
|
2019-06-16 14:23:23 +05:30
|
|
|
get isOwn() {
|
2021-05-31 18:55:01 +05:30
|
|
|
return this._entry.sender === this._ownMember.userId;
|
2019-06-16 14:23:23 +05:30
|
|
|
}
|
|
|
|
|
2019-06-16 19:09:24 +05:30
|
|
|
get isContinuation() {
|
|
|
|
return this._isContinuation;
|
|
|
|
}
|
|
|
|
|
2020-09-08 14:20:39 +05:30
|
|
|
get isUnverified() {
|
|
|
|
return this._entry.isUnverified;
|
|
|
|
}
|
|
|
|
|
2019-03-09 05:10:03 +05:30
|
|
|
_getContent() {
|
2019-07-29 13:57:12 +05:30
|
|
|
return this._entry.content;
|
2019-03-09 05:10:03 +05:30
|
|
|
}
|
2019-06-16 19:09:24 +05:30
|
|
|
|
|
|
|
updatePreviousSibling(prev) {
|
|
|
|
super.updatePreviousSibling(prev);
|
2020-08-19 13:58:09 +05:30
|
|
|
let isContinuation = false;
|
2021-05-17 15:45:13 +05:30
|
|
|
if (prev && prev instanceof BaseMessageTile && prev.sender === this.sender) {
|
2020-08-19 13:58:09 +05:30
|
|
|
// timestamp is null for pending events
|
2021-05-31 15:28:01 +05:30
|
|
|
const myTimestamp = this._entry.timestamp;
|
|
|
|
const otherTimestamp = prev._entry.timestamp;
|
2020-08-19 13:58:09 +05:30
|
|
|
// other message was sent less than 5min ago
|
|
|
|
isContinuation = (myTimestamp - otherTimestamp) < (5 * 60 * 1000);
|
|
|
|
}
|
2019-06-16 19:09:24 +05:30
|
|
|
if (isContinuation !== this._isContinuation) {
|
|
|
|
this._isContinuation = isContinuation;
|
2020-08-17 18:41:39 +05:30
|
|
|
this.emitChange("isContinuation");
|
2019-06-16 19:09:24 +05:30
|
|
|
}
|
|
|
|
}
|
2021-05-20 18:23:17 +05:30
|
|
|
|
2021-06-03 22:43:13 +05:30
|
|
|
updateEntry(entry, param) {
|
|
|
|
const action = super.updateEntry(entry, param);
|
|
|
|
if (action.shouldUpdate) {
|
|
|
|
this._updateReactions();
|
|
|
|
}
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2021-05-26 16:38:54 +05:30
|
|
|
redact(reason, log) {
|
|
|
|
return this._room.sendRedaction(this._entry.id, reason, log);
|
2021-05-20 18:23:17 +05:30
|
|
|
}
|
2021-05-31 17:22:03 +05:30
|
|
|
|
|
|
|
get canRedact() {
|
|
|
|
return this._powerLevels.canRedactFromSender(this._entry.sender);
|
|
|
|
}
|
2021-06-03 22:43:13 +05:30
|
|
|
|
|
|
|
get reactions() {
|
2021-06-24 18:19:43 +05:30
|
|
|
if (this.shape !== "redacted") {
|
|
|
|
return this._reactions;
|
|
|
|
}
|
2021-06-24 18:54:53 +05:30
|
|
|
return null;
|
2021-06-03 22:43:13 +05:30
|
|
|
}
|
|
|
|
|
2021-06-03 23:27:48 +05:30
|
|
|
get canReact() {
|
2021-06-17 13:11:25 +05:30
|
|
|
return this._powerLevels.canSendType("m.reaction");
|
2021-06-03 23:27:48 +05:30
|
|
|
}
|
|
|
|
|
2021-06-08 16:50:55 +05:30
|
|
|
react(key, log = null) {
|
2021-06-24 17:05:59 +05:30
|
|
|
return this.logger.wrapOrRun(log, "react", async log => {
|
2021-06-24 17:54:22 +05:30
|
|
|
if (!this.canReact) {
|
|
|
|
log.set("powerlevel_lacking", true);
|
|
|
|
return;
|
|
|
|
}
|
2021-06-24 15:56:38 +05:30
|
|
|
if (this._entry.haveAnnotation(key)) {
|
2021-06-23 19:08:12 +05:30
|
|
|
log.set("already_reacted", true);
|
|
|
|
return;
|
2021-06-08 16:50:55 +05:30
|
|
|
}
|
2021-06-24 17:05:59 +05:30
|
|
|
const redaction = this._entry.pendingAnnotations?.get(key)?.redactionEntry;
|
2021-06-23 21:17:47 +05:30
|
|
|
if (redaction && !redaction.pendingEvent.hasStartedSending) {
|
|
|
|
log.set("abort_redaction", true);
|
|
|
|
await redaction.pendingEvent.abort();
|
|
|
|
} else {
|
|
|
|
await this._room.sendEvent("m.reaction", this._entry.annotate(key), null, log);
|
|
|
|
}
|
2021-06-24 17:05:59 +05:30
|
|
|
});
|
2021-06-23 19:08:12 +05:30
|
|
|
}
|
|
|
|
|
2021-06-08 20:26:17 +05:30
|
|
|
redactReaction(key, log = null) {
|
2021-06-24 17:05:59 +05:30
|
|
|
return this.logger.wrapOrRun(log, "redactReaction", async log => {
|
2021-06-24 17:54:22 +05:30
|
|
|
if (!this._powerLevels.canRedactFromSender(this._ownMember.userId)) {
|
|
|
|
log.set("powerlevel_lacking", true);
|
|
|
|
return;
|
|
|
|
}
|
2021-06-24 15:56:38 +05:30
|
|
|
if (!this._entry.haveAnnotation(key)) {
|
2021-06-23 19:08:12 +05:30
|
|
|
log.set("not_yet_reacted", true);
|
|
|
|
return;
|
|
|
|
}
|
2021-06-24 17:05:59 +05:30
|
|
|
let entry = this._entry.pendingAnnotations?.get(key)?.annotationEntry;
|
|
|
|
if (!entry) {
|
|
|
|
entry = await this._timeline.getOwnAnnotationEntry(this._entry.id, key);
|
|
|
|
}
|
|
|
|
if (entry) {
|
2021-06-08 20:26:17 +05:30
|
|
|
await this._room.sendRedaction(entry.id, null, log);
|
2021-06-24 17:05:59 +05:30
|
|
|
} else {
|
|
|
|
log.set("no_reaction", true);
|
2021-06-08 16:50:55 +05:30
|
|
|
}
|
2021-06-24 17:05:59 +05:30
|
|
|
});
|
2021-06-03 22:46:53 +05:30
|
|
|
}
|
|
|
|
|
2021-06-24 15:56:38 +05:30
|
|
|
toggleReaction(key, log = null) {
|
|
|
|
return this.logger.wrapOrRun(log, "toggleReaction", async log => {
|
|
|
|
if (this._entry.haveAnnotation(key)) {
|
2021-06-24 17:05:59 +05:30
|
|
|
await this.redactReaction(key, log);
|
2021-06-24 15:56:38 +05:30
|
|
|
} else {
|
2021-06-24 17:05:59 +05:30
|
|
|
await this.react(key, log);
|
2021-06-24 15:56:38 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-03 22:43:13 +05:30
|
|
|
_updateReactions() {
|
2021-06-04 19:04:44 +05:30
|
|
|
const {annotations, pendingAnnotations} = this._entry;
|
|
|
|
if (!annotations && !pendingAnnotations) {
|
2021-06-03 22:43:13 +05:30
|
|
|
if (this._reactions) {
|
|
|
|
this._reactions = null;
|
|
|
|
}
|
2021-06-04 00:30:57 +05:30
|
|
|
} else {
|
|
|
|
if (!this._reactions) {
|
|
|
|
this._reactions = new ReactionsViewModel(this);
|
|
|
|
}
|
2021-06-04 19:04:44 +05:30
|
|
|
this._reactions.update(annotations, pendingAnnotations);
|
2021-06-03 22:43:13 +05:30
|
|
|
}
|
|
|
|
}
|
2019-03-09 05:10:03 +05:30
|
|
|
}
|