diff --git a/src/d2m/converters/message-to-event.js b/src/d2m/converters/message-to-event.js index 154c4611..582b26cb 100644 --- a/src/d2m/converters/message-to-event.js +++ b/src/d2m/converters/message-to-event.js @@ -349,8 +349,13 @@ async function messageToEvent(message, guild, options = {}, di) { result = `https://matrix.to/#/${roomID}/${eventID}?${via}` } else { const ts = dUtils.snowflakeToTimestampExact(messageID) - const {event_id} = await di.api.getEventForTimestamp(roomID, ts) - result = `https://matrix.to/#/${roomID}/${event_id}?${via}` + try { + const {event_id} = await di.api.getEventForTimestamp(roomID, ts) + result = `https://matrix.to/#/${roomID}/${event_id}?${via}` + } catch (e) { + // M_NOT_FOUND: Unable to find event from in direction Direction.FORWARDS + result = `[unknown event, timestamp resolution failed, in room: https://matrix.to/#/${roomID}?${via}]` + } } } else { result = `${match[0]} [event is from another server]` diff --git a/src/d2m/converters/message-to-event.test.js b/src/d2m/converters/message-to-event.test.js index a76741f5..415f48df 100644 --- a/src/d2m/converters/message-to-event.test.js +++ b/src/d2m/converters/message-to-event.test.js @@ -1,5 +1,6 @@ const {test} = require("supertape") const {messageToEvent} = require("./message-to-event") +const {MatrixServerError} = require("../../matrix/mreq") const data = require("../../../test/data") const Ty = require("../../types") @@ -267,6 +268,54 @@ test("message2event: message link that OOYE doesn't know about", async t => { t.equal(called, 3, "getEventForTimestamp, getStateEvent, and getJoinedMembers should be called once each") }) +test("message2event: message timestamp failed to fetch", async t => { + let called = 0 + const events = await messageToEvent(data.message.message_link_to_before_ooye, data.guild.general, {}, { + api: { + async getEventForTimestamp(roomID, ts) { + called++ + t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe") + throw new MatrixServerError({ + errcode: "M_NOT_FOUND", + error: "Unable to find event from 1726762095974 in direction Direction.FORWARDS" + }, {}) + }, + async getStateEvent(roomID, type, key) { // for ?via calculation + called++ + t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe") + t.equal(type, "m.room.power_levels") + t.equal(key, "") + return { + users: { + "@_ooye_bot:cadence.moe": 100 + } + } + }, + async getJoinedMembers(roomID) { // for ?via calculation + called++ + t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe") + return { + joined: { + "@_ooye_bot:cadence.moe": {display_name: null, avatar_url: null}, + "@user:matrix.org": {display_name: null, avatar_url: null} + } + } + } + } + }) + t.deepEqual(events, [{ + $type: "m.room.message", + "m.mentions": {}, + msgtype: "m.text", + body: "Me: I'll scroll up to find a certain message I'll send\n_scrolls up and clicks message links for god knows how long_\n_completely forgets what they were looking for and simply begins scrolling up to find some fun moments_\n_stumbles upon:_ " + + "[unknown event, timestamp resolution failed, in room: https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe?via=cadence.moe&via=matrix.org]", + format: "org.matrix.custom.html", + formatted_body: "Me: I'll scroll up to find a certain message I'll send
scrolls up and clicks message links for god knows how long
completely forgets what they were looking for and simply begins scrolling up to find some fun moments
stumbles upon: " + + '[unknown event, timestamp resolution failed, in room: https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe?via=cadence.moe&via=matrix.org]' + }]) + t.equal(called, 3, "getEventForTimestamp, getStateEvent, and getJoinedMembers should be called once each") +}) + test("message2event: message link from another server", async t => { const events = await messageToEvent(data.message.message_link_from_another_server, data.guild.general) t.deepEqual(events, [{