fix(e2ee): command-by-reaction/emote and reply by </mx-reply>.

chore(deps): upgrade matrix-js-sdk, and refactor for it. bump version.
This commit is contained in:
vulet 2021-08-06 20:13:36 +08:00
parent 44e4138b80
commit 6273452876
5 changed files with 54 additions and 17 deletions

View file

@ -18,6 +18,7 @@ const matrixTokenLogin = async () => {
+ '====================================================',
);
}
matrixClient.setGlobalErrorOnUnknownDevices(config.matrix.manualVerify);
matrixClient.startClient();
});
};

View file

@ -18,7 +18,7 @@ matrixClient.on('RoomMember.membership', (event, member) => {
});
matrixClient.on('event', async (event) => {
if (event.isEncrypted()) await event._decryptionPromise;
if (event.isEncrypted()) await matrixClient.decryptEventIfNeeded(event, { emit: false, isRetry: false });
if (event.getSender() === matrixClient.credentials.userId) return matrix.utils.selfReact(event);
if (!event.getContent()['m.relates_to']) return;
if (event.event.unsigned.age > 10000) return;
@ -27,9 +27,8 @@ matrixClient.on('event', async (event) => {
});
matrixClient.on('Room.timeline', async (event, member, toStartOfTimeline) => {
matrixClient.setGlobalErrorOnUnknownDevices(config.matrix.manualVerify);
if (toStartOfTimeline) return;
if (event.isEncrypted()) await event._decryptionPromise;
if (event.isEncrypted()) await matrixClient.decryptEventIfNeeded(event, { emit: false, isRetry: false });
if (event.getType() !== 'm.room.message') return;
if (event.getSender() === matrixClient.credentials.userId) return;
if (event.event.unsigned.age > 10000) return;

View file

@ -1,6 +1,6 @@
{
"name": "ligh7hau5",
"version": "2.0.0",
"version": "2.1.0",
"description": "A Matrix to Fediverse client",
"main": "main.js",
"scripts": {
@ -21,7 +21,7 @@
"axios": "^0.21.1",
"form-data": "^3.0.0",
"jsdom": "^16.6.0",
"matrix-js-sdk": "^9.11.0",
"matrix-js-sdk": "^12.2.0",
"node-localstorage": "^2.2.1",
"olm": "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.4.tgz",
"qs": "^6.10.1"

View file

@ -1,3 +1,5 @@
const { MatrixEvent } = require('matrix-js-sdk/lib/models/event');
const sendError = async (event, roomId, e) => {
e.response ? error = `Error(${e.response.status}): ${e.response.data.error}`
: e.data ? error = `Error(${e.errcode}): ${e.data.error}`
@ -64,12 +66,28 @@ const eventHandler = (args, roomId, command, event) => {
registrar[command] && registrar[command].runQuery.apply(null, args);
};
/**
matrixClient.fetchRoomEvent() does not return an Event class
however, this class is necessary for decryption, so reinstate it.
afterwards, decrypt.
*/
const fetchEncryptedOrNot = async (roomId, event) => {
const fetchedEvent = await matrixClient.fetchRoomEvent(roomId, event.event_id)
const realEvent = new MatrixEvent(fetchedEvent);
if (realEvent.isEncrypted()) {
await matrixClient.decryptEventIfNeeded(realEvent, { emit: false, isRetry: false });
}
return realEvent;
}
module.exports.sendError = sendError;
module.exports.addReact = addReact;
module.exports.eventHandler = eventHandler;
module.exports.fetchEncryptedOrNot = fetchEncryptedOrNot
module.exports.editNoticeHTML = (roomId, event, html, plain) => matrixClient.sendMessage(roomId, {
body: ` * ${plain || html.replace(/<[^<]+?>/g, '')}`,
formatted_body: ` * ${html}`,
@ -89,11 +107,11 @@ module.exports.editNoticeHTML = (roomId, event, html, plain) => matrixClient.sen
module.exports.handleReact = async (event) => {
const roomId = event.event.room_id;
if (!event.getContent()['m.relates_to']) return;
const reaction = event.getContent()['m.relates_to'];
if (!reaction) return;
const metaEvent = await matrixClient.fetchRoomEvent(roomId, reaction.event_id);
if (!metaEvent.content.meta || metaEvent.sender !== config.matrix.user) return;
const args = metaEvent.content.meta.split(' ');
const metaEvent = await fetchEncryptedOrNot(roomId, reaction);
if (!metaEvent.getContent().meta || metaEvent.sender !== config.matrix.user) return;
const args = metaEvent.getContent().meta.split(' ');
isMeta = ['status', 'reblog', 'mention', 'redact', 'unreblog'];
if (!isMeta.includes(args[0])) return;
let command = [];
@ -107,10 +125,10 @@ module.exports.handleReact = async (event) => {
module.exports.handleReply = async (event) => {
const roomId = event.event.room_id;
if(!event.getContent()['m.relates_to']['m.in_reply_to']) return;
const reply = event.getContent()['m.relates_to']['m.in_reply_to'];
if (!reply) return;
const metaEvent = await matrixClient.fetchRoomEvent(roomId, reply.event_id);
if (!metaEvent.content.meta || metaEvent.sender !== config.matrix.user) return;
const metaEvent = await fetchEncryptedOrNot(roomId, reply);
if (!metaEvent.getContent().meta || metaEvent.sender !== config.matrix.user) return;
const args = metaEvent.content.meta.split(' ');
args.push(event.event.content.formatted_body.trim().split('</mx-reply>')[1]);
isMeta = ['status', 'reblog', 'mention', 'redact', 'unreblog'];
@ -123,8 +141,8 @@ module.exports.handleReply = async (event) => {
module.exports.selfReact = async (event) => {
if (event.getType() !== 'm.room.message') return;
if (event.event.unsigned.age > 10000) return;
if (!event.getContent().meta) return;
const { meta } = event.getContent();
if (!meta) return;
const type = meta.split(' ')[0];
if (type === 'redact' || type === 'unreblog') addReact(event, '🗑️️');
if (type === 'status' || type === 'reblog' || type === 'mention') {

View file

@ -14,6 +14,11 @@
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"
integrity sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==
"@types/retry@^0.12.0":
version "0.12.1"
resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.1.tgz#d8f1c0d0dc23afad6dc16a9e993a0865774b4065"
integrity sha512-xoDlM2S4ortawSWORYqsdU+2rxdh4LRW9ytc3zmT37RIKQh6IHyKwwtKhKis9ah8ol07DCkZxPt8BBvPjC6v4g==
abab@^2.0.3, abab@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.5.tgz#c0b678fb32d60fc1219c784d6a826fe385aeb79a"
@ -506,10 +511,10 @@ loglevel@^1.7.1:
resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.7.1.tgz#005fde2f5e6e47068f935ff28573e125ef72f197"
integrity sha512-Hesni4s5UkWkwCGJMQGAh71PaLUmKFM60dHvq0zi/vDhhrzuk+4GgNbTXJ12YYQJn6ZKBDNIjYcuQGKudvqrIw==
matrix-js-sdk@^9.11.0:
version "9.11.0"
resolved "https://registry.yarnpkg.com/matrix-js-sdk/-/matrix-js-sdk-9.11.0.tgz#57ba60dfbcf2b32917eb6d7232d0e81cf0de0d3a"
integrity sha512-wP28ybOxyQ7lbC48QddRORYr8atEwbTqDOsu8H6u9jTTgB2qqczI/bkSoXHtutODuSeLY5x0UuwLcxVCy4yxVQ==
matrix-js-sdk@^12.2.0:
version "12.2.0"
resolved "https://registry.yarnpkg.com/matrix-js-sdk/-/matrix-js-sdk-12.2.0.tgz#e1dc7ddac054289cb24ee3d11dba8a5ba5ddecf5"
integrity sha512-foSs3uKRc6uvFNhgY35eErBvLWVDd5RNIxxsdFKlmU3B+70YUf3BP3petyBNW34ORyOqNdX36IiApfLo3npNEw==
dependencies:
"@babel/runtime" "^7.12.5"
another-json "^0.2.0"
@ -517,6 +522,7 @@ matrix-js-sdk@^9.11.0:
bs58 "^4.0.1"
content-type "^1.0.4"
loglevel "^1.7.1"
p-retry "^4.5.0"
qs "^6.9.6"
request "^2.88.2"
unhomoglyph "^1.0.6"
@ -576,6 +582,14 @@ optionator@^0.8.1:
type-check "~0.3.2"
word-wrap "~1.2.3"
p-retry@^4.5.0:
version "4.6.1"
resolved "https://registry.yarnpkg.com/p-retry/-/p-retry-4.6.1.tgz#8fcddd5cdf7a67a0911a9cf2ef0e5df7f602316c"
integrity sha512-e2xXGNhZOZ0lfgR9kL34iGlU8N/KO0xZnQxVEwdeOvpqNDQfdnxIYizvWtK8RglUa3bGqI8g0R/BdfzLMxRkiA==
dependencies:
"@types/retry" "^0.12.0"
retry "^0.13.1"
parse5@6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/parse5/-/parse5-6.0.1.tgz#e1a1c085c569b3dc08321184f19a39cc27f7c30b"
@ -644,6 +658,11 @@ request@^2.88.2:
tunnel-agent "^0.6.0"
uuid "^3.3.2"
retry@^0.13.1:
version "0.13.1"
resolved "https://registry.yarnpkg.com/retry/-/retry-0.13.1.tgz#185b1587acf67919d63b357349e03537b2484658"
integrity sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg==
safe-buffer@^5.0.1, safe-buffer@^5.1.2:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"