feat: complete Ancestor quote bot with production-ready Docker support
This commit is contained in:
205
node_modules/matrix-js-sdk/lib/content-helpers.js
generated
vendored
205
node_modules/matrix-js-sdk/lib/content-helpers.js
generated
vendored
@@ -1,34 +1,49 @@
|
||||
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
|
||||
/*
|
||||
Copyright 2018 - 2022 The Matrix.org Foundation C.I.C.
|
||||
"use strict";
|
||||
|
||||
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 { MsgType } from "./@types/event.js";
|
||||
import { M_TEXT, REFERENCE_RELATION } from "./@types/extensible_events.js";
|
||||
import { isProvided } from "./extensible_events_v1/utilities.js";
|
||||
import { M_ASSET, LocationAssetType, M_LOCATION, M_TIMESTAMP } from "./@types/location.js";
|
||||
import { M_TOPIC } from "./@types/topic.js";
|
||||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.makeBeaconInfoContent = exports.makeBeaconContent = exports.getTextForLocationEvent = void 0;
|
||||
exports.makeEmoteMessage = makeEmoteMessage;
|
||||
exports.makeHtmlEmote = makeHtmlEmote;
|
||||
exports.makeHtmlMessage = makeHtmlMessage;
|
||||
exports.makeHtmlNotice = makeHtmlNotice;
|
||||
exports.makeLocationContent = void 0;
|
||||
exports.makeNotice = makeNotice;
|
||||
exports.makeTextMessage = makeTextMessage;
|
||||
exports.parseTopicContent = exports.parseLocationEvent = exports.parseBeaconInfoContent = exports.parseBeaconContent = exports.makeTopicContent = void 0;
|
||||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
||||
var _event = require("./@types/event");
|
||||
var _extensible_events = require("./@types/extensible_events");
|
||||
var _utilities = require("./extensible_events_v1/utilities");
|
||||
var _location = require("./@types/location");
|
||||
var _topic = require("./@types/topic");
|
||||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
||||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } /*
|
||||
Copyright 2018 - 2022 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.
|
||||
*/
|
||||
/**
|
||||
* Generates the content for a HTML Message event
|
||||
* @param body - the plaintext body of the message
|
||||
* @param htmlBody - the HTML representation of the message
|
||||
* @returns
|
||||
*/
|
||||
export function makeHtmlMessage(body, htmlBody) {
|
||||
function makeHtmlMessage(body, htmlBody) {
|
||||
return {
|
||||
msgtype: MsgType.Text,
|
||||
msgtype: _event.MsgType.Text,
|
||||
format: "org.matrix.custom.html",
|
||||
body: body,
|
||||
formatted_body: htmlBody
|
||||
@@ -41,9 +56,9 @@ export function makeHtmlMessage(body, htmlBody) {
|
||||
* @param htmlBody - the HTML representation of the notice
|
||||
* @returns
|
||||
*/
|
||||
export function makeHtmlNotice(body, htmlBody) {
|
||||
function makeHtmlNotice(body, htmlBody) {
|
||||
return {
|
||||
msgtype: MsgType.Notice,
|
||||
msgtype: _event.MsgType.Notice,
|
||||
format: "org.matrix.custom.html",
|
||||
body: body,
|
||||
formatted_body: htmlBody
|
||||
@@ -56,9 +71,9 @@ export function makeHtmlNotice(body, htmlBody) {
|
||||
* @param htmlBody - the HTML representation of the emote
|
||||
* @returns
|
||||
*/
|
||||
export function makeHtmlEmote(body, htmlBody) {
|
||||
function makeHtmlEmote(body, htmlBody) {
|
||||
return {
|
||||
msgtype: MsgType.Emote,
|
||||
msgtype: _event.MsgType.Emote,
|
||||
format: "org.matrix.custom.html",
|
||||
body: body,
|
||||
formatted_body: htmlBody
|
||||
@@ -70,9 +85,9 @@ export function makeHtmlEmote(body, htmlBody) {
|
||||
* @param body - the plaintext body of the emote
|
||||
* @returns
|
||||
*/
|
||||
export function makeTextMessage(body) {
|
||||
function makeTextMessage(body) {
|
||||
return {
|
||||
msgtype: MsgType.Text,
|
||||
msgtype: _event.MsgType.Text,
|
||||
body: body
|
||||
};
|
||||
}
|
||||
@@ -82,9 +97,9 @@ export function makeTextMessage(body) {
|
||||
* @param body - the plaintext body of the notice
|
||||
* @returns
|
||||
*/
|
||||
export function makeNotice(body) {
|
||||
function makeNotice(body) {
|
||||
return {
|
||||
msgtype: MsgType.Notice,
|
||||
msgtype: _event.MsgType.Notice,
|
||||
body: body
|
||||
};
|
||||
}
|
||||
@@ -94,18 +109,18 @@ export function makeNotice(body) {
|
||||
* @param body - the plaintext body of the emote
|
||||
* @returns
|
||||
*/
|
||||
export function makeEmoteMessage(body) {
|
||||
function makeEmoteMessage(body) {
|
||||
return {
|
||||
msgtype: MsgType.Emote,
|
||||
msgtype: _event.MsgType.Emote,
|
||||
body: body
|
||||
};
|
||||
}
|
||||
|
||||
/** Location content helpers */
|
||||
|
||||
export const getTextForLocationEvent = (uri, assetType, timestamp, description) => {
|
||||
const getTextForLocationEvent = (uri, assetType, timestamp, description) => {
|
||||
const date = `at ${new Date(timestamp).toISOString()}`;
|
||||
const assetName = assetType === LocationAssetType.Self ? "User" : undefined;
|
||||
const assetName = assetType === _location.LocationAssetType.Self ? "User" : undefined;
|
||||
const quotedDescription = description ? `"${description}"` : undefined;
|
||||
return [assetName, "Location", quotedDescription, uri, date].filter(Boolean).join(" ");
|
||||
};
|
||||
@@ -118,23 +133,24 @@ export const getTextForLocationEvent = (uri, assetType, timestamp, description)
|
||||
* @param assetType - the (optional) asset type of this location e.g. "m.self"
|
||||
* @param text - optional. A text for the location
|
||||
*/
|
||||
export const makeLocationContent = (text, uri, timestamp, description, assetType) => {
|
||||
const defaultedText = text ?? getTextForLocationEvent(uri, assetType || LocationAssetType.Self, timestamp, description);
|
||||
exports.getTextForLocationEvent = getTextForLocationEvent;
|
||||
const makeLocationContent = (text, uri, timestamp, description, assetType) => {
|
||||
const defaultedText = text !== null && text !== void 0 ? text : getTextForLocationEvent(uri, assetType || _location.LocationAssetType.Self, timestamp, description);
|
||||
const timestampEvent = timestamp ? {
|
||||
[M_TIMESTAMP.name]: timestamp
|
||||
[_location.M_TIMESTAMP.name]: timestamp
|
||||
} : {};
|
||||
return _objectSpread({
|
||||
msgtype: MsgType.Location,
|
||||
msgtype: _event.MsgType.Location,
|
||||
body: defaultedText,
|
||||
geo_uri: uri,
|
||||
[M_LOCATION.name]: {
|
||||
[_location.M_LOCATION.name]: {
|
||||
description,
|
||||
uri
|
||||
},
|
||||
[M_ASSET.name]: {
|
||||
type: assetType || LocationAssetType.Self
|
||||
[_location.M_ASSET.name]: {
|
||||
type: assetType || _location.LocationAssetType.Self
|
||||
},
|
||||
[M_TEXT.name]: defaultedText
|
||||
[_extensible_events.M_TEXT.name]: defaultedText
|
||||
}, timestampEvent);
|
||||
};
|
||||
|
||||
@@ -142,58 +158,51 @@ export const makeLocationContent = (text, uri, timestamp, description, assetType
|
||||
* Parse location event content and transform to
|
||||
* a backwards compatible modern m.location event format
|
||||
*/
|
||||
export const parseLocationEvent = wireEventContent => {
|
||||
const location = M_LOCATION.findIn(wireEventContent);
|
||||
const asset = M_ASSET.findIn(wireEventContent);
|
||||
const timestamp = M_TIMESTAMP.findIn(wireEventContent);
|
||||
const text = M_TEXT.findIn(wireEventContent);
|
||||
const geoUri = location?.uri ?? wireEventContent?.geo_uri;
|
||||
const description = location?.description;
|
||||
const assetType = asset?.type ?? LocationAssetType.Self;
|
||||
const fallbackText = text ?? wireEventContent.body;
|
||||
return makeLocationContent(fallbackText, geoUri, timestamp ?? undefined, description, assetType);
|
||||
exports.makeLocationContent = makeLocationContent;
|
||||
const parseLocationEvent = wireEventContent => {
|
||||
var _location$uri, _asset$type;
|
||||
const location = _location.M_LOCATION.findIn(wireEventContent);
|
||||
const asset = _location.M_ASSET.findIn(wireEventContent);
|
||||
const timestamp = _location.M_TIMESTAMP.findIn(wireEventContent);
|
||||
const text = _extensible_events.M_TEXT.findIn(wireEventContent);
|
||||
const geoUri = (_location$uri = location === null || location === void 0 ? void 0 : location.uri) !== null && _location$uri !== void 0 ? _location$uri : wireEventContent === null || wireEventContent === void 0 ? void 0 : wireEventContent.geo_uri;
|
||||
const description = location === null || location === void 0 ? void 0 : location.description;
|
||||
const assetType = (_asset$type = asset === null || asset === void 0 ? void 0 : asset.type) !== null && _asset$type !== void 0 ? _asset$type : _location.LocationAssetType.Self;
|
||||
const fallbackText = text !== null && text !== void 0 ? text : wireEventContent.body;
|
||||
return makeLocationContent(fallbackText, geoUri, timestamp !== null && timestamp !== void 0 ? timestamp : undefined, description, assetType);
|
||||
};
|
||||
|
||||
/**
|
||||
* Topic event helpers
|
||||
*/
|
||||
|
||||
export const makeTopicContent = (topic, htmlTopic) => {
|
||||
const renderings = [];
|
||||
// Put HTML first because clients will render the first type in
|
||||
// the array that they understand
|
||||
if (isProvided(htmlTopic)) {
|
||||
exports.parseLocationEvent = parseLocationEvent;
|
||||
const makeTopicContent = (topic, htmlTopic) => {
|
||||
const renderings = [{
|
||||
body: topic,
|
||||
mimetype: "text/plain"
|
||||
}];
|
||||
if ((0, _utilities.isProvided)(htmlTopic)) {
|
||||
renderings.push({
|
||||
body: htmlTopic,
|
||||
mimetype: "text/html"
|
||||
});
|
||||
}
|
||||
if (isProvided(topic)) {
|
||||
renderings.push({
|
||||
body: topic,
|
||||
mimetype: "text/plain"
|
||||
});
|
||||
}
|
||||
return {
|
||||
topic,
|
||||
[M_TOPIC.name]: {
|
||||
"m.text": renderings
|
||||
}
|
||||
[_topic.M_TOPIC.name]: renderings
|
||||
};
|
||||
};
|
||||
export const parseTopicContent = content => {
|
||||
const mtopicParent = M_TOPIC.findIn(content);
|
||||
const mtopic = Array.isArray(mtopicParent) ? mtopicParent : mtopicParent?.["m.text"];
|
||||
// TODO remove support for the old malformed m.topic arrays after a few releases (only allow array in m.text)
|
||||
// https://github.com/matrix-org/matrix-js-sdk/pull/4984#pullrequestreview-3174251065
|
||||
//const mtopic = M_TOPIC.findIn<MTopicContent>(content)?.["m.text"];
|
||||
exports.makeTopicContent = makeTopicContent;
|
||||
const parseTopicContent = content => {
|
||||
var _mtopic$find$body, _mtopic$find, _mtopic$find2;
|
||||
const mtopic = _topic.M_TOPIC.findIn(content);
|
||||
if (!Array.isArray(mtopic)) {
|
||||
return {
|
||||
text: content.topic ?? undefined
|
||||
text: content.topic
|
||||
};
|
||||
}
|
||||
const text = mtopic?.find(r => !isProvided(r.mimetype) || r.mimetype === "text/plain")?.body ?? content.topic ?? undefined;
|
||||
const html = mtopic?.find(r => r.mimetype === "text/html")?.body;
|
||||
const text = (_mtopic$find$body = mtopic === null || mtopic === void 0 || (_mtopic$find = mtopic.find(r => !(0, _utilities.isProvided)(r.mimetype) || r.mimetype === "text/plain")) === null || _mtopic$find === void 0 ? void 0 : _mtopic$find.body) !== null && _mtopic$find$body !== void 0 ? _mtopic$find$body : content.topic;
|
||||
const html = mtopic === null || mtopic === void 0 || (_mtopic$find2 = mtopic.find(r => r.mimetype === "text/html")) === null || _mtopic$find2 === void 0 ? void 0 : _mtopic$find2.body;
|
||||
return {
|
||||
text,
|
||||
html
|
||||
@@ -203,53 +212,59 @@ export const parseTopicContent = content => {
|
||||
/**
|
||||
* Beacon event helpers
|
||||
*/
|
||||
|
||||
export const makeBeaconInfoContent = (timeout, isLive, description, assetType, timestamp) => ({
|
||||
exports.parseTopicContent = parseTopicContent;
|
||||
const makeBeaconInfoContent = (timeout, isLive, description, assetType, timestamp) => ({
|
||||
description,
|
||||
timeout,
|
||||
live: isLive,
|
||||
[M_TIMESTAMP.name]: timestamp || Date.now(),
|
||||
[M_ASSET.name]: {
|
||||
type: assetType ?? LocationAssetType.Self
|
||||
[_location.M_TIMESTAMP.name]: timestamp || Date.now(),
|
||||
[_location.M_ASSET.name]: {
|
||||
type: assetType !== null && assetType !== void 0 ? assetType : _location.LocationAssetType.Self
|
||||
}
|
||||
});
|
||||
exports.makeBeaconInfoContent = makeBeaconInfoContent;
|
||||
/**
|
||||
* Flatten beacon info event content
|
||||
*/
|
||||
export const parseBeaconInfoContent = content => {
|
||||
const parseBeaconInfoContent = content => {
|
||||
var _M_TIMESTAMP$findIn;
|
||||
const {
|
||||
description,
|
||||
timeout,
|
||||
live
|
||||
} = content;
|
||||
const timestamp = M_TIMESTAMP.findIn(content) ?? undefined;
|
||||
const asset = M_ASSET.findIn(content);
|
||||
const timestamp = (_M_TIMESTAMP$findIn = _location.M_TIMESTAMP.findIn(content)) !== null && _M_TIMESTAMP$findIn !== void 0 ? _M_TIMESTAMP$findIn : undefined;
|
||||
const asset = _location.M_ASSET.findIn(content);
|
||||
return {
|
||||
description,
|
||||
timeout,
|
||||
live,
|
||||
assetType: asset?.type,
|
||||
assetType: asset === null || asset === void 0 ? void 0 : asset.type,
|
||||
timestamp
|
||||
};
|
||||
};
|
||||
export const makeBeaconContent = (uri, timestamp, beaconInfoEventId, description) => ({
|
||||
[M_LOCATION.name]: {
|
||||
exports.parseBeaconInfoContent = parseBeaconInfoContent;
|
||||
const makeBeaconContent = (uri, timestamp, beaconInfoEventId, description) => ({
|
||||
[_location.M_LOCATION.name]: {
|
||||
description,
|
||||
uri
|
||||
},
|
||||
[M_TIMESTAMP.name]: timestamp,
|
||||
[_location.M_TIMESTAMP.name]: timestamp,
|
||||
"m.relates_to": {
|
||||
rel_type: REFERENCE_RELATION.name,
|
||||
rel_type: _extensible_events.REFERENCE_RELATION.name,
|
||||
event_id: beaconInfoEventId
|
||||
}
|
||||
});
|
||||
export const parseBeaconContent = content => {
|
||||
const location = M_LOCATION.findIn(content);
|
||||
const timestamp = M_TIMESTAMP.findIn(content) ?? undefined;
|
||||
exports.makeBeaconContent = makeBeaconContent;
|
||||
const parseBeaconContent = content => {
|
||||
var _M_TIMESTAMP$findIn2;
|
||||
const location = _location.M_LOCATION.findIn(content);
|
||||
const timestamp = (_M_TIMESTAMP$findIn2 = _location.M_TIMESTAMP.findIn(content)) !== null && _M_TIMESTAMP$findIn2 !== void 0 ? _M_TIMESTAMP$findIn2 : undefined;
|
||||
return {
|
||||
description: location?.description,
|
||||
uri: location?.uri,
|
||||
description: location === null || location === void 0 ? void 0 : location.description,
|
||||
uri: location === null || location === void 0 ? void 0 : location.uri,
|
||||
timestamp
|
||||
};
|
||||
};
|
||||
exports.parseBeaconContent = parseBeaconContent;
|
||||
//# sourceMappingURL=content-helpers.js.map
|
||||
Reference in New Issue
Block a user