Improved BaseURL and DM link handling
This commit is contained in:
parent
b14749c28f
commit
dd70e4a576
9 changed files with 373 additions and 322 deletions
|
|
@ -2,6 +2,8 @@ import axios from 'axios';
|
|||
import * as ContentHelpers from "matrix-js-sdk/lib/content-helpers";
|
||||
import dataUriToBuffer from "data-uri-to-buffer";
|
||||
import ImageResize from "image-resize";
|
||||
import { AutoDiscovery } from 'matrix-js-sdk';
|
||||
import User from '../models/user';
|
||||
|
||||
export const STATE_EVENT_ROOM_DELETION_NOTICE = "im.keanu.room_deletion_notice";
|
||||
export const STATE_EVENT_ROOM_DELETED = "im.keanu.room_deleted";
|
||||
|
|
@ -867,10 +869,10 @@ class Util {
|
|||
return undefined;
|
||||
}
|
||||
|
||||
getUniqueAliasForRoomName(matrixClient, roomName, homeServer, iterationCount) {
|
||||
getUniqueAliasForRoomName(matrixClient, roomName, defaultMatrixDomainPart, iterationCount) {
|
||||
return new Promise((resolve, reject) => {
|
||||
var preferredAlias = roomName.replace(/\s/g, "").toLowerCase();
|
||||
var tryAlias = "#" + preferredAlias + ":" + homeServer;
|
||||
var tryAlias = "#" + preferredAlias + ":" + defaultMatrixDomainPart;
|
||||
matrixClient.getRoomIdForAlias(tryAlias)
|
||||
.then(ignoredid => {
|
||||
// We got a response, this means the tryAlias already exists.
|
||||
|
|
@ -885,7 +887,7 @@ class Util {
|
|||
roomName = roomName.substring(0, roomName.length - 5);
|
||||
}
|
||||
const randomChars = this.randomString(4, "abcdefghijklmnopqrstuvwxyz0123456789");
|
||||
resolve(this.getUniqueAliasForRoomName(matrixClient, roomName + "-" + randomChars, homeServer, (iterationCount || 0) + 1))
|
||||
resolve(this.getUniqueAliasForRoomName(matrixClient, roomName + "-" + randomChars, defaultMatrixDomainPart, (iterationCount || 0) + 1))
|
||||
})
|
||||
.catch(err => {
|
||||
if (err.errcode == 'M_NOT_FOUND') {
|
||||
|
|
@ -921,6 +923,34 @@ class Util {
|
|||
console.log("Failed to fetch attachment: ", err);
|
||||
});
|
||||
}
|
||||
|
||||
getMatrixBaseUrl(user, config) {
|
||||
if (user) {
|
||||
const domain = User.domainPart(user.user_id);
|
||||
if (domain) {
|
||||
const endpoint = config.getMatrixDomainPartMapping(domain);
|
||||
if (endpoint) {
|
||||
console.log("Mapped to", endpoint);
|
||||
return Promise.resolve(endpoint);
|
||||
}
|
||||
return AutoDiscovery.findClientConfig(domain)
|
||||
.then((clientConfig) => {
|
||||
const hs = clientConfig['m.homeserver'];
|
||||
if (hs && !hs.error && hs.base_url) {
|
||||
console.log("Use home server returned from well-known", hs.base_url);
|
||||
return hs.base_url;
|
||||
}
|
||||
console.log("Fallback to default server");
|
||||
return config.defaultBaseUrl;
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("Failed well-known lookup", err);
|
||||
return config.defaultBaseUrl;
|
||||
});
|
||||
}
|
||||
}
|
||||
return Promise.resolve(config.defaultBaseUrl);
|
||||
}
|
||||
}
|
||||
export default new Util();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue