Fix reactions

- look up missing reaction keys/emojis via library
- skip, if no reaction found
- skip for missing users
- fix async function not completely awaited
This commit is contained in:
Henrik Hüttemann 2023-10-23 17:53:24 +02:00
parent ca7787a8eb
commit daeffdfcf7
No known key found for this signature in database
GPG Key ID: 9F7BD10E0A8A111E
3 changed files with 96 additions and 37 deletions

49
package-lock.json generated
View File

@ -12,6 +12,7 @@
"axios": "^1.5.0", "axios": "^1.5.0",
"dotenv": "^16.3.1", "dotenv": "^16.3.1",
"n-readlines": "^1.0.1", "n-readlines": "^1.0.1",
"node-emoji": "^2.1.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"sqlite3": "^5.1.6", "sqlite3": "^5.1.6",
"typeorm": "^0.3.17", "typeorm": "^0.3.17",
@ -1405,6 +1406,17 @@
"integrity": "sha512-+Fj43pSMwJs4KRrH/938Uf+uAELIgVBmQzg/q1YG10djyfA3TnrU8N8XzqCh/okZdszqBQTZf96idMfE5lnwTA==", "integrity": "sha512-+Fj43pSMwJs4KRrH/938Uf+uAELIgVBmQzg/q1YG10djyfA3TnrU8N8XzqCh/okZdszqBQTZf96idMfE5lnwTA==",
"dev": true "dev": true
}, },
"node_modules/@sindresorhus/is": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-3.1.2.tgz",
"integrity": "sha512-JiX9vxoKMmu8Y3Zr2RVathBL1Cdu4Nt4MuNWemt1Nc06A0RAin9c5FArkhGsyMBWfCu4zj+9b+GxtjAnE4qqLQ==",
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/sindresorhus/is?sponsor=1"
}
},
"node_modules/@sinonjs/commons": { "node_modules/@sinonjs/commons": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-3.0.0.tgz", "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-3.0.0.tgz",
@ -2498,8 +2510,6 @@
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/char-regex/-/char-regex-1.0.2.tgz", "resolved": "https://registry.npmjs.org/char-regex/-/char-regex-1.0.2.tgz",
"integrity": "sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw==", "integrity": "sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw==",
"dev": true,
"peer": true,
"engines": { "engines": {
"node": ">=10" "node": ">=10"
} }
@ -3204,6 +3214,11 @@
"integrity": "sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg==", "integrity": "sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg==",
"dev": true "dev": true
}, },
"node_modules/emojilib": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/emojilib/-/emojilib-2.4.0.tgz",
"integrity": "sha512-5U0rVMU5Y2n2+ykNLQqMoqklN9ICBT/KsvC1Gz6vqHbz2AXXGkG+Pm5rMWk/8Vjrr/mY9985Hi8DYzn1F09Nyw=="
},
"node_modules/enabled": { "node_modules/enabled": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/enabled/-/enabled-2.0.0.tgz", "resolved": "https://registry.npmjs.org/enabled/-/enabled-2.0.0.tgz",
@ -6317,6 +6332,17 @@
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-4.3.0.tgz", "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-4.3.0.tgz",
"integrity": "sha512-73sE9+3UaLYYFmDsFZnqCInzPyh3MqIwZO9cw58yIqAZhONrrabrYyYe3TuIqtIiOuTXVhsGau8hcrhhwSsDIQ==" "integrity": "sha512-73sE9+3UaLYYFmDsFZnqCInzPyh3MqIwZO9cw58yIqAZhONrrabrYyYe3TuIqtIiOuTXVhsGau8hcrhhwSsDIQ=="
}, },
"node_modules/node-emoji": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/node-emoji/-/node-emoji-2.1.0.tgz",
"integrity": "sha512-tcsBm9C6FmPN5Wo7OjFi9lgMyJjvkAeirmjR/ax8Ttfqy4N8PoFic26uqFTIgayHPNI5FH4ltUvfh9kHzwcK9A==",
"dependencies": {
"@sindresorhus/is": "^3.1.2",
"char-regex": "^1.0.2",
"emojilib": "^2.4.0",
"skin-tone": "^2.0.0"
}
},
"node_modules/node-fetch": { "node_modules/node-fetch": {
"version": "2.7.0", "version": "2.7.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
@ -7426,6 +7452,17 @@
"dev": true, "dev": true,
"peer": true "peer": true
}, },
"node_modules/skin-tone": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/skin-tone/-/skin-tone-2.0.0.tgz",
"integrity": "sha512-kUMbT1oBJCpgrnKoSr0o6wPtvRWT9W9UKvGLwfJYO2WuahZRHOpEyL1ckyMGgMWh0UdpmaoFqKKD29WTomNEGA==",
"dependencies": {
"unicode-emoji-modifier-base": "^1.0.0"
},
"engines": {
"node": ">=8"
}
},
"node_modules/slash": { "node_modules/slash": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz", "resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz",
@ -8292,6 +8329,14 @@
"url": "https://github.com/sponsors/ljharb" "url": "https://github.com/sponsors/ljharb"
} }
}, },
"node_modules/unicode-emoji-modifier-base": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/unicode-emoji-modifier-base/-/unicode-emoji-modifier-base-1.0.0.tgz",
"integrity": "sha512-yLSH4py7oFH3oG/9K+XWrz1pSi3dfUrWEnInbxMfArOfc1+33BlGPQtLsOYwvdMy11AwUBetYuaRxSPqgkq+8g==",
"engines": {
"node": ">=4"
}
},
"node_modules/unique-filename": { "node_modules/unique-filename": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/unique-filename/-/unique-filename-1.1.1.tgz", "resolved": "https://registry.npmjs.org/unique-filename/-/unique-filename-1.1.1.tgz",

View File

@ -50,6 +50,7 @@
"axios": "^1.5.0", "axios": "^1.5.0",
"dotenv": "^16.3.1", "dotenv": "^16.3.1",
"n-readlines": "^1.0.1", "n-readlines": "^1.0.1",
"node-emoji": "^2.1.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"sqlite3": "^5.1.6", "sqlite3": "^5.1.6",
"typeorm": "^0.3.17", "typeorm": "^0.3.17",

View File

@ -1,4 +1,5 @@
import { AxiosError } from 'axios' import { AxiosError } from 'axios'
import * as emoji from 'node-emoji'
import { Entity, entities } from '../Entities' import { Entity, entities } from '../Entities'
import { IdMapping } from '../entity/IdMapping' import { IdMapping } from '../entity/IdMapping'
import log from '../helpers/logger' import log from '../helpers/logger'
@ -106,8 +107,16 @@ export async function handleReactions(
): Promise<void> { ): Promise<void> {
for (const [reaction, value] of Object.entries(reactions)) { for (const [reaction, value] of Object.entries(reactions)) {
// Lookup key/emoji // Lookup key/emoji
const reactionKey: string =
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const reactionKey = (reactionKeys as any)[reaction] (reactionKeys as any)[reaction] || emoji.get(reaction.replaceAll(':', ''))
if (!reactionKey) {
log.warn(
`Could not find an emoji for ${reaction} for message ${matrixMessageId}, skipping`
)
return
}
await Promise.all(
value.usernames.map(async (rcUsername: string) => { value.usernames.map(async (rcUsername: string) => {
// generate transaction id // generate transaction id
const transactionId = Buffer.from( const transactionId = Buffer.from(
@ -116,7 +125,10 @@ export async function handleReactions(
// lookup user access token // lookup user access token
const userMapping = await getUserMappingByName(rcUsername) const userMapping = await getUserMappingByName(rcUsername)
if (!userMapping) { if (!userMapping) {
throw new Error(`Could not find user mapping for name: ${rcUsername}`) log.warn(
`Could not find user mapping for name: ${rcUsername}, skipping reaction ${reaction} for message ${matrixMessageId}`
)
return
} }
if (!userMapping.accessToken) { if (!userMapping.accessToken) {
throw new Error( throw new Error(
@ -143,6 +155,7 @@ export async function handleReactions(
userSessionOptions userSessionOptions
) )
}) })
)
} }
} }