start on d->m removing reactions
This commit is contained in:
		
							parent
							
								
									8d3ac665c9
								
							
						
					
					
						commit
						4f807159ba
					
				
					 3 changed files with 56 additions and 1 deletions
				
			
		
							
								
								
									
										36
									
								
								d2m/actions/remove-reaction.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								d2m/actions/remove-reaction.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,36 @@
 | 
				
			||||||
 | 
					// @ts-check
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Ty = require("../../types")
 | 
				
			||||||
 | 
					const assert = require("assert").strict
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const passthrough = require("../../passthrough")
 | 
				
			||||||
 | 
					const {discord, sync, db, select} = passthrough
 | 
				
			||||||
 | 
					/** @type {import("../../matrix/api")} */
 | 
				
			||||||
 | 
					const api = sync.require("../../matrix/api")
 | 
				
			||||||
 | 
					/** @type {import("./register-user")} */
 | 
				
			||||||
 | 
					const registerUser = sync.require("./register-user")
 | 
				
			||||||
 | 
					/** @type {import("../actions/create-room")} */
 | 
				
			||||||
 | 
					const createRoom = sync.require("../actions/create-room")
 | 
				
			||||||
 | 
					/** @type {import("../../matrix/file")} */
 | 
				
			||||||
 | 
					const file = sync.require("../../matrix/file")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @param {import("discord-api-types/v10").GatewayMessageReactionRemoveDispatchData} data
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					async function removeReaction(data) {
 | 
				
			||||||
 | 
						const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(data.channel_id)
 | 
				
			||||||
 | 
						if (!roomID) return
 | 
				
			||||||
 | 
						const eventIDForMessage = select("event_message", "event_id", "WHERE message_id = ? AND part = 0").pluck().get(data.message_id)
 | 
				
			||||||
 | 
						if (!eventIDForMessage) return
 | 
				
			||||||
 | 
						const mxid = select("sim", "mxid", "WHERE discord_id = ?").pluck().get(data.user_id)
 | 
				
			||||||
 | 
						if (!mxid) return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/** @type {Ty.Pagination<Ty.Event.Outer<Ty.Event.M_Reaction>>} */
 | 
				
			||||||
 | 
						const relations = await api.getRelations(roomID, eventIDForMessage, "m.annotation")
 | 
				
			||||||
 | 
						const eventIDForReaction = relations.chunk.find(e => e.sender === mxid && e.content["m.relates_to"].key === data.emoji) // TODO: get the key from the emoji
 | 
				
			||||||
 | 
						if (!eventIDForReaction) return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						await api.redactEvent(roomID, eventIDForReaction, mxid)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.exports.removeReaction = removeReaction
 | 
				
			||||||
| 
						 | 
					@ -1,7 +1,7 @@
 | 
				
			||||||
// @ts-check
 | 
					// @ts-check
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Ty = require("../types")
 | 
					const Ty = require("../types")
 | 
				
			||||||
const assert = require("assert")
 | 
					const assert = require("assert").strict
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const passthrough = require("../passthrough")
 | 
					const passthrough = require("../passthrough")
 | 
				
			||||||
const { discord, sync, db } = passthrough
 | 
					const { discord, sync, db } = passthrough
 | 
				
			||||||
| 
						 | 
					@ -109,6 +109,18 @@ function getJoinedMembers(roomID) {
 | 
				
			||||||
	return mreq.mreq("GET", `/client/v3/rooms/${roomID}/joined_members`)
 | 
						return mreq.mreq("GET", `/client/v3/rooms/${roomID}/joined_members`)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @param {string} roomID
 | 
				
			||||||
 | 
					 * @param {string} eventID
 | 
				
			||||||
 | 
					 * @param {string?} [relType]
 | 
				
			||||||
 | 
					 * @returns {Promise<Ty.Pagination<Ty.Event.Outer<any>>>}
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					function getRelations(roomID, eventID, relType) {
 | 
				
			||||||
 | 
						let path = `/client/v1/rooms/${roomID}/relations/${eventID}`
 | 
				
			||||||
 | 
						if (relType) path += `/${relType}`
 | 
				
			||||||
 | 
						return mreq.mreq("GET", path)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @param {string} roomID
 | 
					 * @param {string} roomID
 | 
				
			||||||
 * @param {string} type
 | 
					 * @param {string} type
 | 
				
			||||||
| 
						 | 
					@ -207,6 +219,7 @@ module.exports.getEvent = getEvent
 | 
				
			||||||
module.exports.getAllState = getAllState
 | 
					module.exports.getAllState = getAllState
 | 
				
			||||||
module.exports.getStateEvent = getStateEvent
 | 
					module.exports.getStateEvent = getStateEvent
 | 
				
			||||||
module.exports.getJoinedMembers = getJoinedMembers
 | 
					module.exports.getJoinedMembers = getJoinedMembers
 | 
				
			||||||
 | 
					module.exports.getRelations = getRelations
 | 
				
			||||||
module.exports.sendState = sendState
 | 
					module.exports.sendState = sendState
 | 
				
			||||||
module.exports.sendEvent = sendEvent
 | 
					module.exports.sendEvent = sendEvent
 | 
				
			||||||
module.exports.redactEvent = redactEvent
 | 
					module.exports.redactEvent = redactEvent
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										6
									
								
								types.d.ts
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								types.d.ts
									
										
									
									
										vendored
									
									
								
							| 
						 | 
					@ -215,3 +215,9 @@ export namespace R {
 | 
				
			||||||
		event_id: string
 | 
							event_id: string
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export type Pagination<T> {
 | 
				
			||||||
 | 
						chunk: T[]
 | 
				
			||||||
 | 
						next_batch?: string
 | 
				
			||||||
 | 
						prev_match?: string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue