You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-12-05 17:02:07 +03:00
Switch back to plain export functions instead of class
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import {ContentRepo} from "../../src/content-repo";
|
import {getHttpUriForMxc, getIdenticonUri} from "../../src/content-repo";
|
||||||
|
|
||||||
describe("ContentRepo", function() {
|
describe("ContentRepo", function() {
|
||||||
const baseUrl = "https://my.home.server";
|
const baseUrl = "https://my.home.server";
|
||||||
@@ -7,7 +7,7 @@ describe("ContentRepo", function() {
|
|||||||
it("should do nothing to HTTP URLs when allowing direct links", function() {
|
it("should do nothing to HTTP URLs when allowing direct links", function() {
|
||||||
const httpUrl = "http://example.com/image.jpeg";
|
const httpUrl = "http://example.com/image.jpeg";
|
||||||
expect(
|
expect(
|
||||||
ContentRepo.getHttpUriForMxc(
|
getHttpUriForMxc(
|
||||||
baseUrl, httpUrl, undefined, undefined, undefined, true,
|
baseUrl, httpUrl, undefined, undefined, undefined, true,
|
||||||
),
|
),
|
||||||
).toEqual(httpUrl);
|
).toEqual(httpUrl);
|
||||||
@@ -15,25 +15,25 @@ describe("ContentRepo", function() {
|
|||||||
|
|
||||||
it("should return the empty string HTTP URLs by default", function() {
|
it("should return the empty string HTTP URLs by default", function() {
|
||||||
const httpUrl = "http://example.com/image.jpeg";
|
const httpUrl = "http://example.com/image.jpeg";
|
||||||
expect(ContentRepo.getHttpUriForMxc(baseUrl, httpUrl)).toEqual("");
|
expect(getHttpUriForMxc(baseUrl, httpUrl)).toEqual("");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return a download URL if no width/height/resize are specified",
|
it("should return a download URL if no width/height/resize are specified",
|
||||||
function() {
|
function() {
|
||||||
const mxcUri = "mxc://server.name/resourceid";
|
const mxcUri = "mxc://server.name/resourceid";
|
||||||
expect(ContentRepo.getHttpUriForMxc(baseUrl, mxcUri)).toEqual(
|
expect(getHttpUriForMxc(baseUrl, mxcUri)).toEqual(
|
||||||
baseUrl + "/_matrix/media/r0/download/server.name/resourceid",
|
baseUrl + "/_matrix/media/r0/download/server.name/resourceid",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return the empty string for null input", function() {
|
it("should return the empty string for null input", function() {
|
||||||
expect(ContentRepo.getHttpUriForMxc(null)).toEqual("");
|
expect(getHttpUriForMxc(null)).toEqual("");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return a thumbnail URL if a width/height/resize is specified",
|
it("should return a thumbnail URL if a width/height/resize is specified",
|
||||||
function() {
|
function() {
|
||||||
const mxcUri = "mxc://server.name/resourceid";
|
const mxcUri = "mxc://server.name/resourceid";
|
||||||
expect(ContentRepo.getHttpUriForMxc(baseUrl, mxcUri, 32, 64, "crop")).toEqual(
|
expect(getHttpUriForMxc(baseUrl, mxcUri, 32, 64, "crop")).toEqual(
|
||||||
baseUrl + "/_matrix/media/r0/thumbnail/server.name/resourceid" +
|
baseUrl + "/_matrix/media/r0/thumbnail/server.name/resourceid" +
|
||||||
"?width=32&height=64&method=crop",
|
"?width=32&height=64&method=crop",
|
||||||
);
|
);
|
||||||
@@ -42,7 +42,7 @@ describe("ContentRepo", function() {
|
|||||||
it("should put fragments from mxc:// URIs after any query parameters",
|
it("should put fragments from mxc:// URIs after any query parameters",
|
||||||
function() {
|
function() {
|
||||||
const mxcUri = "mxc://server.name/resourceid#automade";
|
const mxcUri = "mxc://server.name/resourceid#automade";
|
||||||
expect(ContentRepo.getHttpUriForMxc(baseUrl, mxcUri, 32)).toEqual(
|
expect(getHttpUriForMxc(baseUrl, mxcUri, 32)).toEqual(
|
||||||
baseUrl + "/_matrix/media/r0/thumbnail/server.name/resourceid" +
|
baseUrl + "/_matrix/media/r0/thumbnail/server.name/resourceid" +
|
||||||
"?width=32#automade",
|
"?width=32#automade",
|
||||||
);
|
);
|
||||||
@@ -51,7 +51,7 @@ describe("ContentRepo", function() {
|
|||||||
it("should put fragments from mxc:// URIs at the end of the HTTP URI",
|
it("should put fragments from mxc:// URIs at the end of the HTTP URI",
|
||||||
function() {
|
function() {
|
||||||
const mxcUri = "mxc://server.name/resourceid#automade";
|
const mxcUri = "mxc://server.name/resourceid#automade";
|
||||||
expect(ContentRepo.getHttpUriForMxc(baseUrl, mxcUri)).toEqual(
|
expect(getHttpUriForMxc(baseUrl, mxcUri)).toEqual(
|
||||||
baseUrl + "/_matrix/media/r0/download/server.name/resourceid#automade",
|
baseUrl + "/_matrix/media/r0/download/server.name/resourceid#automade",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -59,25 +59,25 @@ describe("ContentRepo", function() {
|
|||||||
|
|
||||||
describe("getIdenticonUri", function() {
|
describe("getIdenticonUri", function() {
|
||||||
it("should do nothing for null input", function() {
|
it("should do nothing for null input", function() {
|
||||||
expect(ContentRepo.getIdenticonUri(null)).toEqual(null);
|
expect(getIdenticonUri(null)).toEqual(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should set w/h by default to 96", function() {
|
it("should set w/h by default to 96", function() {
|
||||||
expect(ContentRepo.getIdenticonUri(baseUrl, "foobar")).toEqual(
|
expect(getIdenticonUri(baseUrl, "foobar")).toEqual(
|
||||||
baseUrl + "/_matrix/media/unstable/identicon/foobar" +
|
baseUrl + "/_matrix/media/unstable/identicon/foobar" +
|
||||||
"?width=96&height=96",
|
"?width=96&height=96",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should be able to set custom w/h", function() {
|
it("should be able to set custom w/h", function() {
|
||||||
expect(ContentRepo.getIdenticonUri(baseUrl, "foobar", 32, 64)).toEqual(
|
expect(getIdenticonUri(baseUrl, "foobar", 32, 64)).toEqual(
|
||||||
baseUrl + "/_matrix/media/unstable/identicon/foobar" +
|
baseUrl + "/_matrix/media/unstable/identicon/foobar" +
|
||||||
"?width=32&height=64",
|
"?width=32&height=64",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should URL encode the identicon string", function() {
|
it("should URL encode the identicon string", function() {
|
||||||
expect(ContentRepo.getIdenticonUri(baseUrl, "foo#bar", 32, 64)).toEqual(
|
expect(getIdenticonUri(baseUrl, "foo#bar", 32, 64)).toEqual(
|
||||||
baseUrl + "/_matrix/media/unstable/identicon/foo%23bar" +
|
baseUrl + "/_matrix/media/unstable/identicon/foo%23bar" +
|
||||||
"?width=32&height=64",
|
"?width=32&height=64",
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -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 {ContentRepo} from "./content-repo";
|
import {getHttpUriForMxc} 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 getHttpUriForMxc(
|
||||||
this.baseUrl, mxcUrl, width, height, resizeMethod, allowDirectLinks,
|
this.baseUrl, mxcUrl, width, height, resizeMethod, allowDirectLinks,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -20,8 +20,7 @@ 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.
|
* Get the HTTP URL for an MXC URI.
|
||||||
* @param {string} baseUrl The base homeserver url which has a content repo.
|
* @param {string} baseUrl The base homeserver url which has a content repo.
|
||||||
* @param {string} mxc The mxc:// URI.
|
* @param {string} mxc The mxc:// URI.
|
||||||
@@ -35,7 +34,7 @@ export class ContentRepo {
|
|||||||
* for such URLs.
|
* for such URLs.
|
||||||
* @return {string} The complete URL to the content.
|
* @return {string} The complete URL to the content.
|
||||||
*/
|
*/
|
||||||
static getHttpUriForMxc(baseUrl, mxc, width, height,
|
export function getHttpUriForMxc(baseUrl, mxc, width, height,
|
||||||
resizeMethod, allowDirectLinks) {
|
resizeMethod, allowDirectLinks) {
|
||||||
if (typeof mxc !== "string" || !mxc) {
|
if (typeof mxc !== "string" || !mxc) {
|
||||||
return '';
|
return '';
|
||||||
@@ -75,9 +74,9 @@ export class ContentRepo {
|
|||||||
return baseUrl + prefix + serverAndMediaId +
|
return baseUrl + prefix + serverAndMediaId +
|
||||||
(utils.keys(params).length === 0 ? "" :
|
(utils.keys(params).length === 0 ? "" :
|
||||||
("?" + utils.encodeParams(params))) + fragment;
|
("?" + utils.encodeParams(params))) + fragment;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an identicon URL from an arbitrary string.
|
* Get an identicon URL from an arbitrary string.
|
||||||
* @param {string} baseUrl The base homeserver url which has a content repo.
|
* @param {string} baseUrl The base homeserver url which has a content repo.
|
||||||
* @param {string} identiconString The string to create an identicon for.
|
* @param {string} identiconString The string to create an identicon for.
|
||||||
@@ -86,7 +85,7 @@ export class ContentRepo {
|
|||||||
* @return {string} The complete URL to the identicon.
|
* @return {string} The complete URL to the identicon.
|
||||||
* @deprecated This is no longer in the specification.
|
* @deprecated This is no longer in the specification.
|
||||||
*/
|
*/
|
||||||
static getIdenticonUri(baseUrl, identiconString, width, height) {
|
export function getIdenticonUri(baseUrl, identiconString, width, height) {
|
||||||
if (!identiconString) {
|
if (!identiconString) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -107,5 +106,4 @@ export class ContentRepo {
|
|||||||
return baseUrl + path +
|
return baseUrl + path +
|
||||||
(utils.keys(params).length === 0 ? "" :
|
(utils.keys(params).length === 0 ? "" :
|
||||||
("?" + utils.encodeParams(params)));
|
("?" + utils.encodeParams(params)));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ limitations under the License.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {EventEmitter} from "events";
|
import {EventEmitter} from "events";
|
||||||
import {ContentRepo} from "../content-repo";
|
import {getHttpUriForMxc, getIdenticonUri} from "../content-repo";
|
||||||
import * as utils from "../utils";
|
import * as utils from "../utils";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -269,13 +269,13 @@ RoomMember.prototype.getAvatarUrl =
|
|||||||
if (!rawUrl && !allowDefault) {
|
if (!rawUrl && !allowDefault) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const httpUrl = ContentRepo.getHttpUriForMxc(
|
const httpUrl = getHttpUriForMxc(
|
||||||
baseUrl, rawUrl, width, height, resizeMethod, allowDirectLinks,
|
baseUrl, rawUrl, width, height, resizeMethod, allowDirectLinks,
|
||||||
);
|
);
|
||||||
if (httpUrl) {
|
if (httpUrl) {
|
||||||
return httpUrl;
|
return httpUrl;
|
||||||
} else if (allowDefault) {
|
} else if (allowDefault) {
|
||||||
return ContentRepo.getIdenticonUri(
|
return getIdenticonUri(
|
||||||
baseUrl, this.userId, width, height,
|
baseUrl, this.userId, width, height,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {ContentRepo} from "../content-repo";
|
import {getHttpUriForMxc, getIdenticonUri} 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";
|
||||||
@@ -811,11 +811,11 @@ Room.prototype.getAvatarUrl = function(baseUrl, width, height, resizeMethod,
|
|||||||
|
|
||||||
const mainUrl = roomAvatarEvent ? roomAvatarEvent.getContent().url : null;
|
const mainUrl = roomAvatarEvent ? roomAvatarEvent.getContent().url : null;
|
||||||
if (mainUrl) {
|
if (mainUrl) {
|
||||||
return ContentRepo.getHttpUriForMxc(
|
return getHttpUriForMxc(
|
||||||
baseUrl, mainUrl, width, height, resizeMethod,
|
baseUrl, mainUrl, width, height, resizeMethod,
|
||||||
);
|
);
|
||||||
} else if (allowDefault) {
|
} else if (allowDefault) {
|
||||||
return ContentRepo.getIdenticonUri(
|
return getIdenticonUri(
|
||||||
baseUrl, this.roomId, width, height,
|
baseUrl, this.roomId, width, height,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user