2019-05-01 12:58:32 +00:00
|
|
|
/*
|
2022-03-23 05:16:25 +00:00
|
|
|
Copyright 2019 - 2022 The Matrix.org Foundation C.I.C.
|
2019-05-01 12:58:32 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-12-01 10:52:15 +00:00
|
|
|
import { EventStatus, MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2022-01-17 15:04:37 +00:00
|
|
|
import { EventType, EVENT_VISIBILITY_CHANGE_TYPE, MsgType, RelationType } from "matrix-js-sdk/src/@types/event";
|
2021-12-09 09:10:23 +00:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2023-02-07 21:12:39 +00:00
|
|
|
import { M_POLL_END, M_POLL_START } from "matrix-js-sdk/src/@types/polls";
|
2022-04-19 07:59:23 +00:00
|
|
|
import { M_LOCATION } from "matrix-js-sdk/src/@types/location";
|
2022-06-02 15:43:19 +00:00
|
|
|
import { M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";
|
2022-06-07 10:16:54 +00:00
|
|
|
import { THREAD_RELATION_TYPE } from "matrix-js-sdk/src/models/thread";
|
2021-06-22 16:22:38 +00:00
|
|
|
|
|
|
|
import { MatrixClientPeg } from "../MatrixClientPeg";
|
2019-05-24 12:41:24 +00:00
|
|
|
import shouldHideEvent from "../shouldHideEvent";
|
2022-03-23 05:16:25 +00:00
|
|
|
import { GetRelationsForEvent } from "../components/views/rooms/EventTile";
|
2021-07-13 11:04:37 +00:00
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2022-02-18 15:01:32 +00:00
|
|
|
import defaultDispatcher from "../dispatcher/dispatcher";
|
|
|
|
import { TimelineRenderingType } from "../contexts/RoomContext";
|
|
|
|
import { launchPollEditor } from "../components/views/messages/MPollBody";
|
|
|
|
import { Action } from "../dispatcher/actions";
|
2022-12-12 22:43:29 +00:00
|
|
|
import { ViewRoomPayload } from "../dispatcher/payloads/ViewRoomPayload";
|
2023-01-05 07:40:09 +00:00
|
|
|
import { VoiceBroadcastInfoEventType, VoiceBroadcastInfoState } from "../voice-broadcast/types";
|
2021-06-22 16:22:38 +00:00
|
|
|
|
2019-05-01 12:58:32 +00:00
|
|
|
/**
|
|
|
|
* Returns whether an event should allow actions like reply, reactions, edit, etc.
|
|
|
|
* which effectively checks whether it's a regular message that has been sent and that we
|
|
|
|
* can display.
|
|
|
|
*
|
|
|
|
* @param {MatrixEvent} mxEvent The event to check
|
|
|
|
* @returns {boolean} true if actionable
|
|
|
|
*/
|
2021-06-22 16:22:38 +00:00
|
|
|
export function isContentActionable(mxEvent: MatrixEvent): boolean {
|
2019-05-01 12:58:32 +00:00
|
|
|
const { status: eventStatus } = mxEvent;
|
|
|
|
|
|
|
|
// status is SENT before remote-echo, null after
|
|
|
|
const isSent = !eventStatus || eventStatus === EventStatus.SENT;
|
|
|
|
|
2020-05-23 07:43:15 +00:00
|
|
|
if (isSent && !mxEvent.isRedacted()) {
|
2020-03-27 00:50:41 +00:00
|
|
|
if (mxEvent.getType() === "m.room.message") {
|
|
|
|
const content = mxEvent.getContent();
|
|
|
|
if (content.msgtype && content.msgtype !== "m.bad.encrypted" && content.hasOwnProperty("body")) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-09 12:54:31 +00:00
|
|
|
} else if (
|
|
|
|
mxEvent.getType() === "m.sticker" ||
|
2022-06-02 15:43:19 +00:00
|
|
|
M_POLL_START.matches(mxEvent.getType()) ||
|
2023-02-07 21:12:39 +00:00
|
|
|
M_POLL_END.matches(mxEvent.getType()) ||
|
2023-01-05 07:40:09 +00:00
|
|
|
M_BEACON_INFO.matches(mxEvent.getType()) ||
|
|
|
|
(mxEvent.getType() === VoiceBroadcastInfoEventType &&
|
|
|
|
mxEvent.getContent()?.state === VoiceBroadcastInfoState.Started)
|
2021-12-09 12:54:31 +00:00
|
|
|
) {
|
2019-05-01 12:58:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-15 14:53:49 +00:00
|
|
|
|
2021-06-22 16:22:38 +00:00
|
|
|
export function canEditContent(mxEvent: MatrixEvent): boolean {
|
2022-02-17 09:13:05 +00:00
|
|
|
const isCancellable = mxEvent.getType() === EventType.RoomMessage || M_POLL_START.matches(mxEvent.getType());
|
|
|
|
|
|
|
|
if (
|
|
|
|
!isCancellable ||
|
|
|
|
mxEvent.status === EventStatus.CANCELLED ||
|
2021-12-01 10:52:15 +00:00
|
|
|
mxEvent.isRedacted() ||
|
|
|
|
mxEvent.isRelation(RelationType.Replace) ||
|
|
|
|
mxEvent.getSender() !== MatrixClientPeg.get().getUserId()
|
|
|
|
) {
|
2019-06-14 09:01:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-12-01 10:52:15 +00:00
|
|
|
|
|
|
|
const { msgtype, body } = mxEvent.getOriginalContent();
|
2022-02-17 09:13:05 +00:00
|
|
|
return (
|
|
|
|
M_POLL_START.matches(mxEvent.getType()) ||
|
|
|
|
((msgtype === MsgType.Text || msgtype === MsgType.Emote) && !!body && typeof body === "string")
|
|
|
|
);
|
2019-05-15 14:53:49 +00:00
|
|
|
}
|
2019-05-24 12:41:24 +00:00
|
|
|
|
2021-06-22 16:22:38 +00:00
|
|
|
export function canEditOwnEvent(mxEvent: MatrixEvent): boolean {
|
2019-05-27 14:41:03 +00:00
|
|
|
// for now we only allow editing
|
|
|
|
// your own events. So this just call through
|
|
|
|
// In the future though, moderators will be able to
|
|
|
|
// edit other people's messages as well but we don't
|
|
|
|
// want findEditableEvent to return other people's events
|
|
|
|
// hence this method.
|
|
|
|
return canEditContent(mxEvent);
|
|
|
|
}
|
|
|
|
|
2019-05-27 14:45:26 +00:00
|
|
|
const MAX_JUMP_DISTANCE = 100;
|
2021-10-01 13:35:54 +00:00
|
|
|
export function findEditableEvent({
|
|
|
|
events,
|
|
|
|
isForward,
|
|
|
|
fromEventId,
|
|
|
|
}: {
|
|
|
|
events: MatrixEvent[];
|
|
|
|
isForward: boolean;
|
|
|
|
fromEventId?: string;
|
2022-11-29 14:55:48 +00:00
|
|
|
}): MatrixEvent | undefined {
|
|
|
|
if (!events.length) return;
|
2019-05-27 14:22:55 +00:00
|
|
|
const maxIdx = events.length - 1;
|
|
|
|
const inc = isForward ? 1 : -1;
|
|
|
|
const beginIdx = isForward ? 0 : maxIdx;
|
|
|
|
let endIdx = isForward ? maxIdx : 0;
|
|
|
|
if (!fromEventId) {
|
2019-05-27 14:45:26 +00:00
|
|
|
endIdx = Math.min(Math.max(0, beginIdx + inc * MAX_JUMP_DISTANCE), maxIdx);
|
2019-05-24 12:41:24 +00:00
|
|
|
}
|
2019-05-27 14:22:55 +00:00
|
|
|
let foundFromEventId = !fromEventId;
|
|
|
|
for (let i = beginIdx; i !== endIdx + inc; i += inc) {
|
|
|
|
const e = events[i];
|
|
|
|
// find start event first
|
|
|
|
if (!foundFromEventId && e.getId() === fromEventId) {
|
|
|
|
foundFromEventId = true;
|
2019-05-27 14:45:26 +00:00
|
|
|
// don't look further than MAX_JUMP_DISTANCE events from `fromEventId`
|
2019-05-27 14:22:55 +00:00
|
|
|
// to not iterate potentially 1000nds of events on key up/down
|
2019-05-27 14:45:26 +00:00
|
|
|
endIdx = Math.min(Math.max(0, i + inc * MAX_JUMP_DISTANCE), maxIdx);
|
2019-05-27 14:41:03 +00:00
|
|
|
} else if (foundFromEventId && !shouldHideEvent(e) && canEditOwnEvent(e)) {
|
2019-05-27 14:22:55 +00:00
|
|
|
// otherwise look for editable event
|
|
|
|
return e;
|
2019-05-24 12:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-27 14:22:55 +00:00
|
|
|
|
2022-01-17 15:04:37 +00:00
|
|
|
/**
|
|
|
|
* How we should render a message depending on its moderation state.
|
|
|
|
*/
|
2022-03-23 05:16:25 +00:00
|
|
|
export enum MessageModerationState {
|
2022-01-17 15:04:37 +00:00
|
|
|
/**
|
|
|
|
* The message is visible to all.
|
|
|
|
*/
|
|
|
|
VISIBLE_FOR_ALL = "VISIBLE_FOR_ALL",
|
|
|
|
/**
|
|
|
|
* The message is hidden pending moderation and we're not a user who should
|
|
|
|
* see it nevertheless.
|
|
|
|
*/
|
|
|
|
HIDDEN_TO_CURRENT_USER = "HIDDEN_TO_CURRENT_USER",
|
|
|
|
/**
|
|
|
|
* The message is hidden pending moderation and we're either the author of
|
|
|
|
* the message or a moderator. In either case, we need to see the message
|
|
|
|
* with a marker.
|
|
|
|
*/
|
|
|
|
SEE_THROUGH_FOR_CURRENT_USER = "SEE_THROUGH_FOR_CURRENT_USER",
|
|
|
|
}
|
|
|
|
|
2022-04-14 23:23:22 +00:00
|
|
|
// This is lazily initialized and cached since getMessageModerationState needs it,
|
|
|
|
// and is called on timeline rendering hot-paths
|
|
|
|
let msc3531Enabled: boolean | null = null;
|
|
|
|
const getMsc3531Enabled = (): boolean => {
|
|
|
|
if (msc3531Enabled === null) {
|
|
|
|
msc3531Enabled = SettingsStore.getValue("feature_msc3531_hide_messages_pending_moderation");
|
|
|
|
}
|
2023-02-13 17:01:43 +00:00
|
|
|
return msc3531Enabled!;
|
2022-04-14 23:23:22 +00:00
|
|
|
};
|
|
|
|
|
2022-01-17 15:04:37 +00:00
|
|
|
/**
|
|
|
|
* Determine whether a message should be displayed as hidden pending moderation.
|
|
|
|
*
|
|
|
|
* If MSC3531 is deactivated in settings, all messages are considered visible
|
|
|
|
* to all.
|
|
|
|
*/
|
2022-03-23 05:16:25 +00:00
|
|
|
export function getMessageModerationState(mxEvent: MatrixEvent, client?: MatrixClient): MessageModerationState {
|
|
|
|
client = client ?? MatrixClientPeg.get(); // because param defaults don't do the correct thing
|
|
|
|
|
2022-04-14 23:23:22 +00:00
|
|
|
if (!getMsc3531Enabled()) {
|
2022-01-17 15:04:37 +00:00
|
|
|
return MessageModerationState.VISIBLE_FOR_ALL;
|
|
|
|
}
|
|
|
|
const visibility = mxEvent.messageVisibility();
|
|
|
|
if (visibility.visible) {
|
|
|
|
return MessageModerationState.VISIBLE_FOR_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we know that the message is marked as hidden
|
|
|
|
// pending moderation. However, if we're the author or a moderator,
|
|
|
|
// we still need to display it.
|
|
|
|
|
|
|
|
if (mxEvent.sender?.userId === client.getUserId()) {
|
|
|
|
// We're the author, show the message.
|
|
|
|
return MessageModerationState.SEE_THROUGH_FOR_CURRENT_USER;
|
|
|
|
}
|
|
|
|
|
|
|
|
const room = client.getRoom(mxEvent.getRoomId());
|
|
|
|
if (
|
|
|
|
EVENT_VISIBILITY_CHANGE_TYPE.name &&
|
2023-02-13 17:01:43 +00:00
|
|
|
room?.currentState.maySendStateEvent(EVENT_VISIBILITY_CHANGE_TYPE.name, client.getUserId()!)
|
2022-01-17 15:04:37 +00:00
|
|
|
) {
|
|
|
|
// We're a moderator (as indicated by prefixed event name), show the message.
|
|
|
|
return MessageModerationState.SEE_THROUGH_FOR_CURRENT_USER;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
EVENT_VISIBILITY_CHANGE_TYPE.altName &&
|
2023-02-13 17:01:43 +00:00
|
|
|
room?.currentState.maySendStateEvent(EVENT_VISIBILITY_CHANGE_TYPE.altName, client.getUserId()!)
|
2022-01-17 15:04:37 +00:00
|
|
|
) {
|
|
|
|
// We're a moderator (as indicated by unprefixed event name), show the message.
|
|
|
|
return MessageModerationState.SEE_THROUGH_FOR_CURRENT_USER;
|
|
|
|
}
|
|
|
|
// For everybody else, hide the message.
|
|
|
|
return MessageModerationState.HIDDEN_TO_CURRENT_USER;
|
|
|
|
}
|
|
|
|
|
2021-08-18 07:28:27 +00:00
|
|
|
export function isVoiceMessage(mxEvent: MatrixEvent): boolean {
|
|
|
|
const content = mxEvent.getContent();
|
|
|
|
// MSC2516 is a legacy identifier. See https://github.com/matrix-org/matrix-doc/pull/3245
|
|
|
|
return !!content["org.matrix.msc2516.voice"] || !!content["org.matrix.msc3245.voice"];
|
|
|
|
}
|
2021-10-22 08:30:36 +00:00
|
|
|
|
|
|
|
export async function fetchInitialEvent(
|
|
|
|
client: MatrixClient,
|
|
|
|
roomId: string,
|
2022-05-03 13:25:08 +00:00
|
|
|
eventId: string,
|
|
|
|
): Promise<MatrixEvent | null> {
|
2023-02-13 17:01:43 +00:00
|
|
|
let initialEvent: MatrixEvent | null;
|
2021-10-22 08:30:36 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const eventData = await client.fetchRoomEvent(roomId, eventId);
|
|
|
|
initialEvent = new MatrixEvent(eventData);
|
|
|
|
} catch (e) {
|
2022-01-10 16:44:24 +00:00
|
|
|
logger.warn("Could not find initial event: " + eventId);
|
2021-10-22 08:30:36 +00:00
|
|
|
initialEvent = null;
|
|
|
|
}
|
|
|
|
|
2023-01-30 12:20:11 +00:00
|
|
|
if (client.supportsThreads() && initialEvent?.isRelation(THREAD_RELATION_TYPE.name) && !initialEvent.getThread()) {
|
2023-02-13 17:01:43 +00:00
|
|
|
const threadId = initialEvent.threadRootId!;
|
2022-05-03 13:25:08 +00:00
|
|
|
const room = client.getRoom(roomId);
|
2022-10-28 11:48:15 +00:00
|
|
|
const mapper = client.getEventMapper();
|
2023-02-13 17:01:43 +00:00
|
|
|
const rootEvent = room?.findEventById(threadId) ?? mapper(await client.fetchRoomEvent(roomId, threadId));
|
2021-10-22 08:30:36 +00:00
|
|
|
try {
|
2023-02-13 17:01:43 +00:00
|
|
|
room?.createThread(threadId, rootEvent, [initialEvent], true);
|
2021-10-22 08:30:36 +00:00
|
|
|
} catch (e) {
|
2022-05-03 13:25:08 +00:00
|
|
|
logger.warn("Could not find root event: " + threadId);
|
2021-10-22 08:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return initialEvent;
|
|
|
|
}
|
2022-02-18 15:01:32 +00:00
|
|
|
|
|
|
|
export function editEvent(
|
|
|
|
mxEvent: MatrixEvent,
|
|
|
|
timelineRenderingType: TimelineRenderingType,
|
|
|
|
getRelationsForEvent?: GetRelationsForEvent,
|
|
|
|
): void {
|
|
|
|
if (!canEditContent(mxEvent)) return;
|
|
|
|
|
|
|
|
if (M_POLL_START.matches(mxEvent.getType())) {
|
|
|
|
launchPollEditor(mxEvent, getRelationsForEvent);
|
|
|
|
} else {
|
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: Action.EditEvent,
|
|
|
|
event: mxEvent,
|
|
|
|
timelineRenderingType: timelineRenderingType,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 18:12:40 +00:00
|
|
|
|
2023-03-13 15:07:20 +00:00
|
|
|
export function canCancel(status?: EventStatus | null): boolean {
|
2022-04-15 18:12:40 +00:00
|
|
|
return status === EventStatus.QUEUED || status === EventStatus.NOT_SENT || status === EventStatus.ENCRYPTING;
|
|
|
|
}
|
2022-04-19 07:59:23 +00:00
|
|
|
|
|
|
|
export const isLocationEvent = (event: MatrixEvent): boolean => {
|
|
|
|
const eventType = event.getType();
|
|
|
|
return (
|
|
|
|
M_LOCATION.matches(eventType) ||
|
2023-02-13 17:01:43 +00:00
|
|
|
(eventType === EventType.RoomMessage && M_LOCATION.matches(event.getContent().msgtype!))
|
2022-04-19 07:59:23 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-05-13 11:59:50 +00:00
|
|
|
export function hasThreadSummary(event: MatrixEvent): boolean {
|
2023-02-13 17:01:43 +00:00
|
|
|
return event.isThreadRoot && !!event.getThread()?.length && !!event.getThread()!.replyToEvent;
|
2022-05-13 11:59:50 +00:00
|
|
|
}
|
2022-06-29 07:11:33 +00:00
|
|
|
|
|
|
|
export function canPinEvent(event: MatrixEvent): boolean {
|
|
|
|
return !M_BEACON_INFO.matches(event.getType());
|
|
|
|
}
|
2022-12-12 15:03:56 +00:00
|
|
|
|
|
|
|
export const highlightEvent = (roomId: string, eventId: string): void => {
|
|
|
|
defaultDispatcher.dispatch<ViewRoomPayload>({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
event_id: eventId,
|
|
|
|
highlighted: true,
|
|
|
|
room_id: roomId,
|
|
|
|
metricsTrigger: undefined, // room doesn't change
|
|
|
|
});
|
|
|
|
};
|