Responses: Combine data from multi-part messages
This commit is contained in:
@@ -16,9 +16,9 @@ const utils = sync.require("../../matrix/utils")
|
|||||||
* @returns {AsyncGenerator<{[k in keyof InteractionMethods]?: Parameters<InteractionMethods[k]>[2]}>}
|
* @returns {AsyncGenerator<{[k in keyof InteractionMethods]?: Parameters<InteractionMethods[k]>[2]}>}
|
||||||
*/
|
*/
|
||||||
async function* _interact({data}, {api}) {
|
async function* _interact({data}, {api}) {
|
||||||
const row = from("event_message").join("message_room", "message_id").join("historical_channel_room", "historical_room_index")
|
const events = from("event_message").join("message_room", "message_id").join("historical_channel_room", "historical_room_index")
|
||||||
.select("event_id", "room_id").where({message_id: data.target_id}).get()
|
.select("event_id", "room_id").where({message_id: data.target_id}).and("ORDER BY reaction_part").all()
|
||||||
if (!row) {
|
if (!events.length) {
|
||||||
return yield {createInteractionResponse: {
|
return yield {createInteractionResponse: {
|
||||||
type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource,
|
type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource,
|
||||||
data: {
|
data: {
|
||||||
@@ -35,17 +35,23 @@ async function* _interact({data}, {api}) {
|
|||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
|
||||||
const reactions = await api.getFullRelations(row.room_id, row.event_id, "m.annotation")
|
const reactions = []
|
||||||
|
for (const event of events) {
|
||||||
|
const reactionsOnEvent = await api.getFullRelations(event.room_id, event.event_id, "m.annotation")
|
||||||
|
reactions.push(...reactionsOnEvent)
|
||||||
|
}
|
||||||
|
|
||||||
/** @type {Map<string, string[]>} */
|
/** @type {Map<string, Set<string>>} reaction key (the emoji) -> displaynames */
|
||||||
const inverted = new Map()
|
const inverted = new Map()
|
||||||
for (const reaction of reactions) {
|
for (const reaction of reactions) {
|
||||||
if (utils.eventSenderIsFromDiscord(reaction.sender)) continue
|
if (utils.eventSenderIsFromDiscord(reaction.sender)) continue
|
||||||
const key = reaction.content["m.relates_to"].key
|
const key = reaction.content["m.relates_to"].key
|
||||||
const displayname = select("member_cache", "displayname", {mxid: reaction.sender, room_id: row.room_id}).pluck().get() || reaction.sender
|
const displayname = select("member_cache", "displayname", {mxid: reaction.sender, room_id: events[0].room_id}).pluck().get() || reaction.sender
|
||||||
if (!inverted.has(key)) inverted.set(key, [])
|
if (!inverted.has(key)) {
|
||||||
|
inverted.set(key, new Set())
|
||||||
|
}
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
inverted.get(key).push(displayname)
|
inverted.get(key).add(displayname)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inverted.size === 0) {
|
if (inverted.size === 0) {
|
||||||
@@ -55,7 +61,7 @@ async function* _interact({data}, {api}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return yield {editOriginalInteractionResponse: {
|
return yield {editOriginalInteractionResponse: {
|
||||||
content: [...inverted.entries()].map(([key, value]) => `${key} ⮞ ${value.join(" ⬩ ")}`).join("\n"),
|
content: [...inverted.entries()].sort((a, b) => b[1].size - a[1].size).map(([key, value]) => `${key} ⮞ ${[...value].join(" ⬩ ")}`).join("\n"),
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ test("reactions: different response if nobody reacted", async t => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test("reactions: shows reactions if there are some, ignoring discord users", async t => {
|
test("reactions: shows reactions if there are some, ignoring discord users", async t => {
|
||||||
let called = 1
|
let called = 0
|
||||||
const msgs = await fromAsync(_interact({
|
const msgs = await fromAsync(_interact({
|
||||||
data: {
|
data: {
|
||||||
target_id: "1126786462646550579"
|
target_id: "1126786462646550579"
|
||||||
@@ -51,6 +51,7 @@ test("reactions: shows reactions if there are some, ignoring discord users", asy
|
|||||||
}, {
|
}, {
|
||||||
api: {
|
api: {
|
||||||
async getFullRelations(roomID, eventID) {
|
async getFullRelations(roomID, eventID) {
|
||||||
|
called++
|
||||||
t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe")
|
t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe")
|
||||||
t.equal(eventID, "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg")
|
t.equal(eventID, "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg")
|
||||||
return [{
|
return [{
|
||||||
@@ -97,3 +98,57 @@ test("reactions: shows reactions if there are some, ignoring discord users", asy
|
|||||||
)
|
)
|
||||||
t.equal(called, 1)
|
t.equal(called, 1)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test("reactions: combines reactions from multiple events", async t => {
|
||||||
|
let called = 0
|
||||||
|
const msgs = await fromAsync(_interact({
|
||||||
|
data: {
|
||||||
|
target_id: "1141501302736695316"
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
api: {
|
||||||
|
async getFullRelations(roomID, eventID) {
|
||||||
|
called++
|
||||||
|
t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe")
|
||||||
|
if (eventID === "$mtR8cJqM4fKno1bVsm8F4wUVqSntt2sq6jav1lyavuA") {
|
||||||
|
return [{
|
||||||
|
sender: "@cadence:cadence.moe",
|
||||||
|
content: {
|
||||||
|
"m.relates_to": {
|
||||||
|
key: "🐈",
|
||||||
|
rel_type: "m.annotation"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
sender: "@rnl:cadence.moe",
|
||||||
|
content: {
|
||||||
|
"m.relates_to": {
|
||||||
|
key: "🐈",
|
||||||
|
rel_type: "m.annotation"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
} else if (eventID === "$51f4yqHinwnSbPEQ9dCgoyy4qiIJSX0QYYVUnvwyTCI") {
|
||||||
|
return [{
|
||||||
|
sender: "@cadence:cadence.moe",
|
||||||
|
content: {
|
||||||
|
"m.relates_to": {
|
||||||
|
key: "🐈⬛",
|
||||||
|
rel_type: "m.annotation"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
} else {
|
||||||
|
t.fail(`tried to read reactions on wrong event ${eventID}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
t.equal(msgs.length, 2)
|
||||||
|
t.equal(
|
||||||
|
msgs[1].editOriginalInteractionResponse.content,
|
||||||
|
"🐈 ⮞ cadence [they] ⬩ @rnl:cadence.moe"
|
||||||
|
+ "\n🐈⬛ ⮞ cadence [they]"
|
||||||
|
)
|
||||||
|
t.equal(called, 2)
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user