Merge remote-tracking branch 'origin/develop' into hs/blocked-err

This commit is contained in:
Will Hunt 2021-03-05 18:50:00 +00:00
commit d07069238f
269 changed files with 18170 additions and 6833 deletions

View file

@ -1,29 +0,0 @@
/*
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 * as projectNameGenerator from "project-name-generator";
/**
* Generates a human readable identifier. This should not be used for anything
* which needs secure/cryptographic random: just a level uniquness that is offered
* by something like Date.now().
* @returns {string} The randomly generated ID
*/
export function generateHumanReadableId(): string {
return projectNameGenerator({words: 3}).raw.map(w => {
return w[0].toUpperCase() + w.substring(1).toLowerCase();
}).join('');
}

View file

@ -16,10 +16,15 @@ limitations under the License.
import {MatrixClientPeg} from '../MatrixClientPeg';
const CALL_BEHAVIOUR_WK_KEY = "io.element.call_behaviour";
const E2EE_WK_KEY = "io.element.e2ee";
const E2EE_WK_KEY_DEPRECATED = "im.vector.riot.e2ee";
/* eslint-disable camelcase */
export interface ICallBehaviourWellKnown {
widget_build_url?: string;
}
export interface IE2EEWellKnown {
default?: boolean;
secure_backup_required?: boolean;
@ -27,6 +32,11 @@ export interface IE2EEWellKnown {
}
/* eslint-enable camelcase */
export function getCallBehaviourWellKnown(): ICallBehaviourWellKnown {
const clientWellKnown = MatrixClientPeg.get().getClientWellKnown();
return clientWellKnown?.[CALL_BEHAVIOUR_WK_KEY];
}
export function getE2EEWellKnown(): IE2EEWellKnown {
const clientWellKnown = MatrixClientPeg.get().getClientWellKnown();
if (clientWellKnown && clientWellKnown[E2EE_WK_KEY]) {

View file

@ -27,7 +27,7 @@ import {Room} from "matrix-js-sdk/src/models/room";
import {WidgetType} from "../widgets/WidgetType";
import {objectClone} from "./objects";
import {_t} from "../languageHandler";
import {Capability, IWidgetData, MatrixCapabilities} from "matrix-widget-api";
import {Capability, IWidget, IWidgetData, MatrixCapabilities} from "matrix-widget-api";
import {IApp} from "../stores/WidgetStore";
// How long we wait for the state event echo to come back from the server
@ -297,6 +297,16 @@ export default class WidgetUtils {
content = {};
}
return WidgetUtils.setRoomWidgetContent(roomId, widgetId, content);
}
static setRoomWidgetContent(
roomId: string,
widgetId: string,
content: IWidget,
) {
const addingWidget = !!content.url;
WidgetEchoStore.setRoomWidgetEcho(roomId, widgetId, content);
const client = MatrixClientPeg.get();
@ -467,6 +477,7 @@ export default class WidgetUtils {
'userId=$matrix_user_id',
'roomId=$matrix_room_id',
'theme=$theme',
'roomName=$roomName',
];
if (opts.auth) {
queryStringParts.push(`auth=${opts.auth}`);

View file

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { arrayDiff, arrayHasDiff, arrayMerge, arrayUnion } from "./arrays";
import { arrayDiff, arrayMerge, arrayUnion } from "./arrays";
type ObjectExcluding<O extends {}, P extends (keyof O)[]> = {[k in Exclude<keyof O, P[number]>]: O[k]};
@ -86,11 +86,14 @@ export function objectShallowClone<O extends {}>(a: O, propertyCloner?: (k: keyo
* @returns True if there's a difference between the objects, false otherwise
*/
export function objectHasDiff<O extends {}>(a: O, b: O): boolean {
if (a === b) return false;
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
if (arrayHasDiff(aKeys, bKeys)) return true;
if (aKeys.length !== bKeys.length) return true;
const possibleChanges = arrayUnion(aKeys, bKeys);
// if the amalgamation of both sets of keys has the a different length to the inputs then there must be a change
if (possibleChanges.length !== aKeys.length) return true;
return possibleChanges.some(k => a[k] !== b[k]);
}

View file

@ -1,5 +1,5 @@
/*
Copyright 2019 The Matrix.org Foundation C.I.C.
Copyright 2019, 2021 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.
@ -75,11 +75,28 @@ export default class ElementPermalinkConstructor extends PermalinkConstructor {
throw new Error("Does not appear to be a permalink");
}
const parts = fullUrl.substring(`${this._elementUrl}/#/`.length).split("/");
const parts = fullUrl.substring(`${this._elementUrl}/#/`.length);
return ElementPermalinkConstructor.parseAppRoute(parts);
}
/**
* Parses an app route (`(user|room|group)/identifer`) to a Matrix entity
* (room, user, group).
* @param {string} route The app route
* @returns {PermalinkParts}
*/
static parseAppRoute(route: string): PermalinkParts {
const parts = route.split("/");
if (parts.length < 2) { // we're expecting an entity and an ID of some kind at least
throw new Error("URL is missing parts");
}
// Split optional query out of last part
const [lastPartMaybeWithQuery] = parts.splice(-1, 1);
const [lastPart, query = ""] = lastPartMaybeWithQuery.split("?");
parts.push(lastPart);
const entityType = parts[0];
const entity = parts[1];
if (entityType === 'user') {
@ -89,20 +106,9 @@ export default class ElementPermalinkConstructor extends PermalinkConstructor {
// Probably a group, no further parsing needed.
return PermalinkParts.forGroup(entity);
} else if (entityType === 'room') {
if (parts.length === 2) {
return PermalinkParts.forRoom(entity, []);
}
// rejoin the rest because v3 events can have slashes (annoyingly)
const eventIdAndQuery = parts.length > 2 ? parts.slice(2).join('/') : "";
const secondaryParts = eventIdAndQuery.split("?");
const eventId = secondaryParts[0];
const query = secondaryParts.length > 1 ? secondaryParts[1] : "";
// TODO: Verify Element works with via args
const via = query.split("via=").filter(p => !!p);
// Rejoin the rest because v3 events can have slashes (annoyingly)
const eventId = parts.length > 2 ? parts.slice(2).join('/') : "";
const via = query.split(/&?via=/).filter(p => !!p);
return PermalinkParts.forEvent(entity, eventId, via);
} else {
throw new Error("Unknown entity type in permalink");

View file

@ -1,5 +1,5 @@
/*
Copyright 2019 The Matrix.org Foundation C.I.C.
Copyright 2019, 2021 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.
@ -80,4 +80,12 @@ export class PermalinkParts {
static forEvent(roomId: string, eventId: string, viaServers: string[]): PermalinkParts {
return new PermalinkParts(roomId, eventId, null, null, viaServers || []);
}
get primaryEntityId(): string {
return this.roomIdOrAlias || this.userId || this.groupId;
}
get sigil(): string {
return this.primaryEntityId[0];
}
}

View file

@ -1,5 +1,5 @@
/*
Copyright 2019 New Vector Ltd
Copyright 2019, 2021 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.
@ -14,9 +14,11 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import {MatrixClientPeg} from "../../MatrixClientPeg";
import isIp from "is-ip";
import * as utils from 'matrix-js-sdk/src/utils';
import * as utils from "matrix-js-sdk/src/utils";
import {Room} from "matrix-js-sdk/src/models/room";
import {MatrixClientPeg} from "../../MatrixClientPeg";
import SpecPermalinkConstructor, {baseUrl as matrixtoBaseUrl} from "./SpecPermalinkConstructor";
import PermalinkConstructor, {PermalinkParts} from "./PermalinkConstructor";
import ElementPermalinkConstructor from "./ElementPermalinkConstructor";
@ -121,6 +123,10 @@ export class RoomPermalinkCreator {
this._started = false;
}
get serverCandidates() {
return this._serverCandidates;
}
isStarted() {
return this._started;
}
@ -405,6 +411,23 @@ export function parsePermalink(fullUrl: string): PermalinkParts {
return null; // not a permalink we can handle
}
/**
* Parses an app local link (`#/(user|room|group)/identifer`) to a Matrix entity
* (room, user, group). Such links are produced by `HtmlUtils` when encountering
* links, which calls `tryTransformPermalinkToLocalHref` in this module.
* @param {string} localLink The app local link
* @returns {PermalinkParts}
*/
export function parseAppLocalLink(localLink: string): PermalinkParts {
try {
const segments = localLink.replace("#/", "");
return ElementPermalinkConstructor.parseAppRoute(segments);
} catch (e) {
// Ignore failures
}
return null;
}
function getServerName(userId) {
return userId.split(":").splice(1).join(":");
}
@ -434,3 +457,9 @@ function isHostnameIpAddress(hostname) {
return isIp(hostname);
}
export const calculateRoomVia = (room: Room) => {
const permalinkCreator = new RoomPermalinkCreator(room);
permalinkCreator.load();
return permalinkCreator.serverCandidates;
};

View file

@ -1,5 +1,5 @@
/*
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
Copyright 2019, 2020, 2021 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.
@ -19,7 +19,8 @@ import ReactDOM from 'react-dom';
import {MatrixClientPeg} from '../MatrixClientPeg';
import SettingsStore from "../settings/SettingsStore";
import {PushProcessor} from 'matrix-js-sdk/src/pushprocessor';
import * as sdk from '../index';
import Pill from "../components/views/elements/Pill";
import { parseAppLocalLink } from "./permalinks/Permalinks";
/**
* Recurses depth-first through a DOM tree, converting matrix.to links
@ -43,10 +44,10 @@ export function pillifyLinks(nodes, mxEvent, pills) {
if (node.tagName === "A" && node.getAttribute("href")) {
const href = node.getAttribute("href");
const parts = parseAppLocalLink(href);
// If the link is a (localised) matrix.to link, replace it with a pill
const Pill = sdk.getComponent('elements.Pill');
if (Pill.isMessagePillUrl(href)) {
// We don't want to pill event permalinks, so those are ignored.
if (parts && !parts.eventId) {
const pillContainer = document.createElement('span');
const pill = <Pill
@ -72,8 +73,6 @@ export function pillifyLinks(nodes, mxEvent, pills) {
// to clear the pills from the last run of pillifyLinks
!node.parentElement.classList.contains("mx_AtRoomPill")
) {
const Pill = sdk.getComponent('elements.Pill');
let currentTextNode = node;
const roomNotifTextNodes = [];

34
src/utils/sets.ts Normal file
View file

@ -0,0 +1,34 @@
/*
Copyright 2021 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.
*/
/**
* Determines if two sets are different through a shallow comparison.
* @param a The first set. Must be defined.
* @param b The second set. Must be defined.
* @returns True if they are different, false otherwise.
*/
export function setHasDiff<T>(a: Set<T>, b: Set<T>): boolean {
if (a.size === b.size) {
// When the lengths are equal, check to see if either set is missing an element from the other.
if (Array.from(b).some(i => !a.has(i))) return true;
if (Array.from(a).some(i => !b.has(i))) return true;
// if all the keys are common, say so
return false;
} else {
return true; // different lengths means they are naturally diverged
}
}

81
src/utils/space.ts Normal file
View file

@ -0,0 +1,81 @@
/*
Copyright 2021 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 {Room} from "matrix-js-sdk/src/models/room";
import {MatrixClient} from "matrix-js-sdk/src/client";
import {EventType} from "matrix-js-sdk/src/@types/event";
import {calculateRoomVia} from "../utils/permalinks/Permalinks";
import Modal from "../Modal";
import SpaceSettingsDialog from "../components/views/dialogs/SpaceSettingsDialog";
import AddExistingToSpaceDialog from "../components/views/dialogs/AddExistingToSpaceDialog";
import CreateRoomDialog from "../components/views/dialogs/CreateRoomDialog";
import createRoom, {IOpts} from "../createRoom";
export const shouldShowSpaceSettings = (cli: MatrixClient, space: Room) => {
const userId = cli.getUserId();
return space.getMyMembership() === "join"
&& (space.currentState.maySendStateEvent(EventType.RoomAvatar, userId)
|| space.currentState.maySendStateEvent(EventType.RoomName, userId)
|| space.currentState.maySendStateEvent(EventType.RoomTopic, userId)
|| space.currentState.maySendStateEvent(EventType.RoomJoinRules, userId));
};
export const makeSpaceParentEvent = (room: Room, canonical = false) => ({
type: EventType.SpaceParent,
content: {
"via": calculateRoomVia(room),
"canonical": canonical,
},
state_key: room.roomId,
});
export const showSpaceSettings = (cli: MatrixClient, space: Room) => {
Modal.createTrackedDialog("Space Settings", "", SpaceSettingsDialog, {
matrixClient: cli,
space,
}, /*className=*/null, /*isPriority=*/false, /*isStatic=*/true);
};
export const showAddExistingRooms = async (cli: MatrixClient, space: Room) => {
return Modal.createTrackedDialog(
"Space Landing",
"Add Existing",
AddExistingToSpaceDialog,
{
matrixClient: cli,
onCreateRoomClick: showCreateNewRoom,
space,
},
"mx_AddExistingToSpaceDialog_wrapper",
).finished;
};
export const showCreateNewRoom = async (cli: MatrixClient, space: Room) => {
const modal = Modal.createTrackedDialog<[boolean, IOpts]>(
"Space Landing",
"Create Room",
CreateRoomDialog,
{
defaultPublic: space.getJoinRule() === "public",
parentSpace: space,
},
);
const [shouldCreate, opts] = await modal.finished;
if (shouldCreate) {
await createRoom(opts);
}
};