1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-12-05 17:02:07 +03:00

Make ContentRepo a class for easier importing

Exporting it the way we were was causing problems for webpack way down
the line, so we export it differently here to get around that. We also
have to fix all the import references so we import the right thing.
This commit is contained in:
Travis Ralston
2019-12-20 14:06:36 -07:00
parent 1e5e705458
commit 50791e3aa7
6 changed files with 88 additions and 86 deletions

View File

@@ -1,4 +1,4 @@
import * as ContentRepo from "../../src/content-repo"; import {ContentRepo} from "../../src/content-repo";
describe("ContentRepo", function() { describe("ContentRepo", function() {
const baseUrl = "https://my.home.server"; const baseUrl = "https://my.home.server";

View File

@@ -36,7 +36,7 @@ import {createNewMatrixCall} from "./webrtc/call";
import * as utils from './utils'; import * as utils from './utils';
import {sleep} from './utils'; import {sleep} from './utils';
import {MatrixError, PREFIX_MEDIA_R0, PREFIX_UNSTABLE} from "./http-api"; import {MatrixError, PREFIX_MEDIA_R0, PREFIX_UNSTABLE} from "./http-api";
import * as contentRepo from "./content-repo"; import {ContentRepo} from "./content-repo";
import * as ContentHelpers from "./content-helpers"; import * as ContentHelpers from "./content-helpers";
import * as olmlib from "./crypto/olmlib"; import * as olmlib from "./crypto/olmlib";
import {ReEmitter} from './ReEmitter'; import {ReEmitter} from './ReEmitter';
@@ -3132,7 +3132,7 @@ MatrixClient.prototype.setAvatarUrl = function(url, callback) {
*/ */
MatrixClient.prototype.mxcUrlToHttp = MatrixClient.prototype.mxcUrlToHttp =
function(mxcUrl, width, height, resizeMethod, allowDirectLinks) { function(mxcUrl, width, height, resizeMethod, allowDirectLinks) {
return contentRepo.getHttpUriForMxc( return ContentRepo.getHttpUriForMxc(
this.baseUrl, mxcUrl, width, height, resizeMethod, allowDirectLinks, this.baseUrl, mxcUrl, width, height, resizeMethod, allowDirectLinks,
); );
}; };

View File

@@ -20,90 +20,92 @@ limitations under the License.
import * as utils from "./utils"; import * as utils from "./utils";
/** export class ContentRepo {
* Get the HTTP URL for an MXC URI. /**
* @param {string} baseUrl The base homeserver url which has a content repo. * Get the HTTP URL for an MXC URI.
* @param {string} mxc The mxc:// URI. * @param {string} baseUrl The base homeserver url which has a content repo.
* @param {Number} width The desired width of the thumbnail. * @param {string} mxc The mxc:// URI.
* @param {Number} height The desired height of the thumbnail. * @param {Number} width The desired width of the thumbnail.
* @param {string} resizeMethod The thumbnail resize method to use, either * @param {Number} height The desired height of the thumbnail.
* "crop" or "scale". * @param {string} resizeMethod The thumbnail resize method to use, either
* @param {Boolean} allowDirectLinks If true, return any non-mxc URLs * "crop" or "scale".
* directly. Fetching such URLs will leak information about the user to * @param {Boolean} allowDirectLinks If true, return any non-mxc URLs
* anyone they share a room with. If false, will return the emptry string * directly. Fetching such URLs will leak information about the user to
* for such URLs. * anyone they share a room with. If false, will return the emptry string
* @return {string} The complete URL to the content. * for such URLs.
*/ * @return {string} The complete URL to the content.
export function getHttpUriForMxc(baseUrl, mxc, width, height, */
resizeMethod, allowDirectLinks) { static getHttpUriForMxc(baseUrl, mxc, width, height,
if (typeof mxc !== "string" || !mxc) { resizeMethod, allowDirectLinks) {
return ''; if (typeof mxc !== "string" || !mxc) {
}
if (mxc.indexOf("mxc://") !== 0) {
if (allowDirectLinks) {
return mxc;
} else {
return ''; return '';
} }
} if (mxc.indexOf("mxc://") !== 0) {
let serverAndMediaId = mxc.slice(6); // strips mxc:// if (allowDirectLinks) {
let prefix = "/_matrix/media/r0/download/"; return mxc;
const params = {}; } else {
return '';
}
}
let serverAndMediaId = mxc.slice(6); // strips mxc://
let prefix = "/_matrix/media/r0/download/";
const params = {};
if (width) { if (width) {
params.width = Math.round(width); params.width = Math.round(width);
} }
if (height) { if (height) {
params.height = Math.round(height); params.height = Math.round(height);
} }
if (resizeMethod) { if (resizeMethod) {
params.method = resizeMethod; params.method = resizeMethod;
} }
if (utils.keys(params).length > 0) { if (utils.keys(params).length > 0) {
// these are thumbnailing params so they probably want the // these are thumbnailing params so they probably want the
// thumbnailing API... // thumbnailing API...
prefix = "/_matrix/media/r0/thumbnail/"; prefix = "/_matrix/media/r0/thumbnail/";
}
const fragmentOffset = serverAndMediaId.indexOf("#");
let fragment = "";
if (fragmentOffset >= 0) {
fragment = serverAndMediaId.substr(fragmentOffset);
serverAndMediaId = serverAndMediaId.substr(0, fragmentOffset);
}
return baseUrl + prefix + serverAndMediaId +
(utils.keys(params).length === 0 ? "" :
("?" + utils.encodeParams(params))) + fragment;
} }
const fragmentOffset = serverAndMediaId.indexOf("#"); /**
let fragment = ""; * Get an identicon URL from an arbitrary string.
if (fragmentOffset >= 0) { * @param {string} baseUrl The base homeserver url which has a content repo.
fragment = serverAndMediaId.substr(fragmentOffset); * @param {string} identiconString The string to create an identicon for.
serverAndMediaId = serverAndMediaId.substr(0, fragmentOffset); * @param {Number} width The desired width of the image in pixels. Default: 96.
* @param {Number} height The desired height of the image in pixels. Default: 96.
* @return {string} The complete URL to the identicon.
* @deprecated This is no longer in the specification.
*/
static getIdenticonUri(baseUrl, identiconString, width, height) {
if (!identiconString) {
return null;
}
if (!width) {
width = 96;
}
if (!height) {
height = 96;
}
const params = {
width: width,
height: height,
};
const path = utils.encodeUri("/_matrix/media/unstable/identicon/$ident", {
$ident: identiconString,
});
return baseUrl + path +
(utils.keys(params).length === 0 ? "" :
("?" + utils.encodeParams(params)));
} }
return baseUrl + prefix + serverAndMediaId +
(utils.keys(params).length === 0 ? "" :
("?" + utils.encodeParams(params))) + fragment;
}
/**
* Get an identicon URL from an arbitrary string.
* @param {string} baseUrl The base homeserver url which has a content repo.
* @param {string} identiconString The string to create an identicon for.
* @param {Number} width The desired width of the image in pixels. Default: 96.
* @param {Number} height The desired height of the image in pixels. Default: 96.
* @return {string} The complete URL to the identicon.
* @deprecated This is no longer in the specification.
*/
export function getIdenticonUri(baseUrl, identiconString, width, height) {
if (!identiconString) {
return null;
}
if (!width) {
width = 96;
}
if (!height) {
height = 96;
}
const params = {
width: width,
height: height,
};
const path = utils.encodeUri("/_matrix/media/unstable/identicon/$ident", {
$ident: identiconString,
});
return baseUrl + path +
(utils.keys(params).length === 0 ? "" :
("?" + utils.encodeParams(params)));
} }

View File

@@ -44,8 +44,8 @@ export * from "./store/indexeddb";
export * from "./store/session/webstorage"; export * from "./store/session/webstorage";
export * from "./crypto/store/memory-crypto-store"; export * from "./crypto/store/memory-crypto-store";
export * from "./crypto/store/indexeddb-crypto-store"; export * from "./crypto/store/indexeddb-crypto-store";
export * from "./content-repo";
export const ContentHelpers = import("./content-helpers"); export const ContentHelpers = import("./content-helpers");
export const ContentRepo = import("./content-repo");
export { export {
createNewMatrixCall, createNewMatrixCall,
setAudioOutput as setMatrixCallAudioOutput, setAudioOutput as setMatrixCallAudioOutput,

View File

@@ -20,7 +20,7 @@ limitations under the License.
*/ */
import {EventEmitter} from "events"; import {EventEmitter} from "events";
import * as ContentRepo from "../content-repo"; import {ContentRepo} from "../content-repo";
import * as utils from "../utils"; import * as utils from "../utils";
/** /**

View File

@@ -23,7 +23,7 @@ limitations under the License.
import {EventEmitter} from "events"; import {EventEmitter} from "events";
import {EventTimelineSet} from "./event-timeline-set"; import {EventTimelineSet} from "./event-timeline-set";
import {EventTimeline} from "./event-timeline"; import {EventTimeline} from "./event-timeline";
import * as ContentRepo from "../content-repo"; import {ContentRepo} from "../content-repo";
import * as utils from "../utils"; import * as utils from "../utils";
import {EventStatus, MatrixEvent} from "./event"; import {EventStatus, MatrixEvent} from "./event";
import {RoomMember} from "./room-member"; import {RoomMember} from "./room-member";