2022-03-24 21:54:06 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 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 { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
import { EventType, MsgType } from "matrix-js-sdk/src/@types/event";
|
|
|
|
import { M_POLL_START } from "matrix-events-sdk";
|
2022-04-08 09:05:26 +00:00
|
|
|
import { M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";
|
2022-12-29 10:03:24 +00:00
|
|
|
import { IContent } from "matrix-js-sdk/src/matrix";
|
2022-03-24 21:54:06 +00:00
|
|
|
|
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
|
|
|
import { haveRendererForEvent, JitsiEventFactory, JSONEventFactory, pickFactory } from "../events/EventTileFactory";
|
|
|
|
import { MatrixClientPeg } from "../MatrixClientPeg";
|
2022-05-10 15:39:28 +00:00
|
|
|
import { getMessageModerationState, isLocationEvent, MessageModerationState } from "./EventUtils";
|
2022-09-30 19:26:08 +00:00
|
|
|
import { ElementCall } from "../models/Call";
|
2022-12-29 10:03:24 +00:00
|
|
|
import { VoiceBroadcastInfoEventType, VoiceBroadcastInfoState } from "../voice-broadcast";
|
|
|
|
|
|
|
|
const calcIsInfoMessage = (
|
|
|
|
eventType: EventType | string,
|
|
|
|
content: IContent,
|
|
|
|
isBubbleMessage: boolean,
|
|
|
|
isLeftAlignedBubbleMessage: boolean,
|
|
|
|
): boolean => {
|
|
|
|
return (
|
|
|
|
!isBubbleMessage &&
|
|
|
|
!isLeftAlignedBubbleMessage &&
|
|
|
|
eventType !== EventType.RoomMessage &&
|
|
|
|
eventType !== EventType.RoomMessageEncrypted &&
|
|
|
|
eventType !== EventType.Sticker &&
|
|
|
|
eventType !== EventType.RoomCreate &&
|
|
|
|
!M_POLL_START.matches(eventType) &&
|
|
|
|
!M_BEACON_INFO.matches(eventType) &&
|
|
|
|
!(eventType === VoiceBroadcastInfoEventType && content?.state === VoiceBroadcastInfoState.Started)
|
|
|
|
);
|
|
|
|
};
|
2022-03-24 21:54:06 +00:00
|
|
|
|
2022-04-14 23:23:22 +00:00
|
|
|
export function getEventDisplayInfo(
|
|
|
|
mxEvent: MatrixEvent,
|
|
|
|
showHiddenEvents: boolean,
|
|
|
|
hideEvent?: boolean,
|
|
|
|
): {
|
2022-03-24 21:54:06 +00:00
|
|
|
isInfoMessage: boolean;
|
|
|
|
hasRenderer: boolean;
|
|
|
|
isBubbleMessage: boolean;
|
|
|
|
isLeftAlignedBubbleMessage: boolean;
|
|
|
|
noBubbleEvent: boolean;
|
|
|
|
isSeeingThroughMessageHiddenForModeration: boolean;
|
|
|
|
} {
|
|
|
|
const content = mxEvent.getContent();
|
|
|
|
const msgtype = content.msgtype;
|
|
|
|
const eventType = mxEvent.getType();
|
|
|
|
|
|
|
|
let isSeeingThroughMessageHiddenForModeration = false;
|
|
|
|
if (SettingsStore.getValue("feature_msc3531_hide_messages_pending_moderation")) {
|
|
|
|
switch (getMessageModerationState(mxEvent)) {
|
|
|
|
case MessageModerationState.VISIBLE_FOR_ALL:
|
|
|
|
case MessageModerationState.HIDDEN_TO_CURRENT_USER:
|
|
|
|
// Nothing specific to do here
|
|
|
|
break;
|
|
|
|
case MessageModerationState.SEE_THROUGH_FOR_CURRENT_USER:
|
|
|
|
// Show message with a marker.
|
|
|
|
isSeeingThroughMessageHiddenForModeration = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Thread a MatrixClient through to here
|
2022-04-14 23:23:22 +00:00
|
|
|
let factory = pickFactory(mxEvent, MatrixClientPeg.get(), showHiddenEvents);
|
2022-03-24 21:54:06 +00:00
|
|
|
|
|
|
|
// Info messages are basically information about commands processed on a room
|
|
|
|
let isBubbleMessage =
|
|
|
|
eventType.startsWith("m.key.verification") ||
|
|
|
|
(eventType === EventType.RoomMessage && msgtype?.startsWith("m.key.verification")) ||
|
|
|
|
eventType === EventType.RoomCreate ||
|
|
|
|
eventType === EventType.RoomEncryption ||
|
|
|
|
factory === JitsiEventFactory;
|
2022-09-30 19:26:08 +00:00
|
|
|
const isLeftAlignedBubbleMessage =
|
|
|
|
!isBubbleMessage && (eventType === EventType.CallInvite || ElementCall.CALL_EVENT_TYPE.matches(eventType));
|
2022-12-29 10:03:24 +00:00
|
|
|
let isInfoMessage = calcIsInfoMessage(eventType, content, isBubbleMessage, isLeftAlignedBubbleMessage);
|
2022-03-24 21:54:06 +00:00
|
|
|
// Some non-info messages want to be rendered in the appropriate bubble column but without the bubble background
|
|
|
|
const noBubbleEvent =
|
|
|
|
(eventType === EventType.RoomMessage && msgtype === MsgType.Emote) ||
|
|
|
|
M_POLL_START.matches(eventType) ||
|
2022-04-12 08:13:55 +00:00
|
|
|
M_BEACON_INFO.matches(eventType) ||
|
2023-01-05 11:33:28 +00:00
|
|
|
isLocationEvent(mxEvent) ||
|
|
|
|
eventType === VoiceBroadcastInfoEventType;
|
2022-03-24 21:54:06 +00:00
|
|
|
|
|
|
|
// If we're showing hidden events in the timeline, we should use the
|
|
|
|
// source tile when there's no regular tile for an event and also for
|
|
|
|
// replace relations (which otherwise would display as a confusing
|
|
|
|
// duplicate of the thing they are replacing).
|
2022-04-14 23:23:22 +00:00
|
|
|
if (hideEvent || !haveRendererForEvent(mxEvent, showHiddenEvents)) {
|
2022-03-24 21:54:06 +00:00
|
|
|
// forcefully ask for a factory for a hidden event (hidden event
|
|
|
|
// setting is checked internally)
|
|
|
|
// TODO: Thread a MatrixClient through to here
|
2022-04-14 23:23:22 +00:00
|
|
|
factory = pickFactory(mxEvent, MatrixClientPeg.get(), showHiddenEvents, true);
|
2022-03-24 21:54:06 +00:00
|
|
|
if (factory === JSONEventFactory) {
|
|
|
|
isBubbleMessage = false;
|
|
|
|
// Reuse info message avatar and sender profile styling
|
|
|
|
isInfoMessage = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
hasRenderer: !!factory,
|
|
|
|
isInfoMessage,
|
|
|
|
isBubbleMessage,
|
|
|
|
isLeftAlignedBubbleMessage,
|
|
|
|
noBubbleEvent,
|
|
|
|
isSeeingThroughMessageHiddenForModeration,
|
|
|
|
};
|
|
|
|
}
|