forked from cadence/out-of-your-element
		
	get room mentions formatting working
This commit is contained in:
		
							parent
							
								
									0f4f404160
								
							
						
					
					
						commit
						5326b7d6be
					
				
					 6 changed files with 124 additions and 8 deletions
				
			
		| 
						 | 
				
			
			@ -20,11 +20,11 @@ function getDiscordParseCallbacks(message, useHTML) {
 | 
			
		|||
			}
 | 
			
		||||
		},
 | 
			
		||||
		channel: node => {
 | 
			
		||||
			const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(node.id)
 | 
			
		||||
			if (roomID && useHTML) {
 | 
			
		||||
				return "https://matrix.to/#/" + roomID
 | 
			
		||||
			const {room_id, name, nick} = db.prepare("SELECT room_id, name, nick FROM channel_room WHERE channel_id = ?").get(node.id)
 | 
			
		||||
			if (room_id && useHTML) {
 | 
			
		||||
				return `<a href="https://matrix.to/#/${room_id}">#${nick || name}</a>`
 | 
			
		||||
			} else {
 | 
			
		||||
				return "#" + node.id
 | 
			
		||||
				return `#${nick || name}`
 | 
			
		||||
			}
 | 
			
		||||
		},
 | 
			
		||||
		role: node =>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -27,9 +27,9 @@ test("message2event: simple room mention", async t => {
 | 
			
		|||
	t.deepEqual(events, [{
 | 
			
		||||
		$type: "m.room.message",
 | 
			
		||||
		msgtype: "m.text",
 | 
			
		||||
		body: "@crunch god: Tell me about Phil, renowned martial arts master and creator of the Chin Trick",
 | 
			
		||||
		body: "#main",
 | 
			
		||||
		format: "org.matrix.custom.html",
 | 
			
		||||
		formatted_body: '<a href="https://matrix.to/#/@_ooye_crunch_god:cadence.moe">@crunch god</a> Tell me about Phil, renowned martial arts master and creator of the Chin Trick'
 | 
			
		||||
		formatted_body: '<a href="https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe">#main</a>'
 | 
			
		||||
	}])
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,8 +12,9 @@ const discordPackets = sync.require("./discord-packets")
 | 
			
		|||
class DiscordClient {
 | 
			
		||||
	/**
 | 
			
		||||
	 * @param {string} discordToken
 | 
			
		||||
	 * @param {boolean} listen whether to set up the event listeners for OOYE to operate
 | 
			
		||||
	 */
 | 
			
		||||
	constructor(discordToken) {
 | 
			
		||||
	constructor(discordToken, listen) {
 | 
			
		||||
		this.discordToken = discordToken
 | 
			
		||||
		this.snow = new SnowTransfer(discordToken)
 | 
			
		||||
		this.cloud = new CloudStorm(discordToken, {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										2
									
								
								index.js
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								index.js
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -13,7 +13,7 @@ Object.assign(passthrough, {config, sync, db})
 | 
			
		|||
 | 
			
		||||
const DiscordClient = require("./d2m/discord-client")
 | 
			
		||||
 | 
			
		||||
const discord = new DiscordClient(config.discordToken)
 | 
			
		||||
const discord = new DiscordClient(config.discordToken, true)
 | 
			
		||||
passthrough.discord = discord
 | 
			
		||||
 | 
			
		||||
const as = require("./m2d/appservice")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										58
									
								
								scripts/save-channel-names-to-db.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								scripts/save-channel-names-to-db.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,58 @@
 | 
			
		|||
// @ts-check
 | 
			
		||||
 | 
			
		||||
const sqlite = require("better-sqlite3")
 | 
			
		||||
const HeatSync = require("heatsync")
 | 
			
		||||
 | 
			
		||||
const config = require("../config")
 | 
			
		||||
const passthrough = require("../passthrough")
 | 
			
		||||
const db = new sqlite("db/ooye.db")
 | 
			
		||||
 | 
			
		||||
const sync = new HeatSync({watchFS: false})
 | 
			
		||||
 | 
			
		||||
Object.assign(passthrough, {config, sync, db})
 | 
			
		||||
 | 
			
		||||
const DiscordClient = require("../d2m/discord-client")
 | 
			
		||||
 | 
			
		||||
const discord = new DiscordClient(config.discordToken, false)
 | 
			
		||||
passthrough.discord = discord
 | 
			
		||||
 | 
			
		||||
;(async () => {
 | 
			
		||||
	await discord.cloud.connect()
 | 
			
		||||
   console.log("Discord gateway started")
 | 
			
		||||
 | 
			
		||||
   const f = event => onPacket(discord, event, () => discord.cloud.off("event", f))
 | 
			
		||||
   discord.cloud.on("event", f)
 | 
			
		||||
})()
 | 
			
		||||
 | 
			
		||||
const expectedGuilds = new Set()
 | 
			
		||||
 | 
			
		||||
const prepared = db.prepare("UPDATE channel_room SET name = ? WHERE channel_id = ?")
 | 
			
		||||
 | 
			
		||||
/** @param {DiscordClient} discord */
 | 
			
		||||
function onPacket(discord, event, unsubscribe) {
 | 
			
		||||
   if (event.t === "READY") {
 | 
			
		||||
      for (const obj of event.d.guilds) {
 | 
			
		||||
         expectedGuilds.add(obj.id)
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
   } else if (event.t === "GUILD_CREATE") {
 | 
			
		||||
      expectedGuilds.delete(event.d.id)
 | 
			
		||||
 | 
			
		||||
      // Store the channel.
 | 
			
		||||
      for (const channel of event.d.channels || []) {
 | 
			
		||||
         prepared.run(channel.name, channel.id)
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      // Checked them all?
 | 
			
		||||
      if (expectedGuilds.size === 0) {
 | 
			
		||||
         discord.cloud.disconnect()
 | 
			
		||||
         unsubscribe()
 | 
			
		||||
 | 
			
		||||
         // I don't know why node keeps running.
 | 
			
		||||
         setTimeout(() => {
 | 
			
		||||
            console.log("Stopping now.")
 | 
			
		||||
            process.exit()
 | 
			
		||||
         }, 1500).unref()
 | 
			
		||||
      }
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										57
									
								
								test/data.js
									
										
									
									
									
								
							
							
						
						
									
										57
									
								
								test/data.js
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -216,6 +216,63 @@ module.exports = {
 | 
			
		|||
			flags: 0,
 | 
			
		||||
			components: []
 | 
			
		||||
		},
 | 
			
		||||
		simple_room_mention: {
 | 
			
		||||
			type: 0,
 | 
			
		||||
			tts: false,
 | 
			
		||||
			timestamp: "2023-07-10T20:04:25.939000+00:00",
 | 
			
		||||
			referenced_message: null,
 | 
			
		||||
			pinned: false,
 | 
			
		||||
			nonce: "1128054139385806848",
 | 
			
		||||
			mentions: [],
 | 
			
		||||
			mention_roles: [],
 | 
			
		||||
			mention_everyone: false,
 | 
			
		||||
			member: {
 | 
			
		||||
				roles: [
 | 
			
		||||
					"112767366235959296",  "118924814567211009",
 | 
			
		||||
					"204427286542417920",  "199995902742626304",
 | 
			
		||||
					"222168467627835392",  "238028326281805825",
 | 
			
		||||
					"259806643414499328",  "265239342648131584",
 | 
			
		||||
					"271173313575780353",  "287733611912757249",
 | 
			
		||||
					"225744901915148298",  "305775031223320577",
 | 
			
		||||
					"318243902521868288",  "348651574924541953",
 | 
			
		||||
					"349185088157777920",  "378402925128712193",
 | 
			
		||||
					"392141548932038658",  "393912152173576203",
 | 
			
		||||
					"482860581670486028",  "495384759074160642",
 | 
			
		||||
					"638988388740890635",  "373336013109461013",
 | 
			
		||||
					"530220455085473813",  "454567553738473472",
 | 
			
		||||
					"790724320824655873",  "1123518980456452097",
 | 
			
		||||
					"1040735082610167858", "695946570482450442",
 | 
			
		||||
					"1123460940935991296", "849737964090556488"
 | 
			
		||||
				],
 | 
			
		||||
				premium_since: null,
 | 
			
		||||
				pending: false,
 | 
			
		||||
				nick: null,
 | 
			
		||||
				mute: false,
 | 
			
		||||
				joined_at: "2015-11-11T09:55:40.321000+00:00",
 | 
			
		||||
				flags: 0,
 | 
			
		||||
				deaf: false,
 | 
			
		||||
				communication_disabled_until: null,
 | 
			
		||||
				avatar: null
 | 
			
		||||
			},
 | 
			
		||||
			id: "1128054143064494233",
 | 
			
		||||
			flags: 0,
 | 
			
		||||
			embeds: [],
 | 
			
		||||
			edited_timestamp: null,
 | 
			
		||||
			content: "<#112760669178241024>",
 | 
			
		||||
			components: [],
 | 
			
		||||
			channel_id: "266767590641238027",
 | 
			
		||||
			author: {
 | 
			
		||||
				username: "kumaccino",
 | 
			
		||||
				public_flags: 128,
 | 
			
		||||
				id: "113340068197859328",
 | 
			
		||||
				global_name: "kumaccino",
 | 
			
		||||
				discriminator: "0",
 | 
			
		||||
				avatar_decoration: null,
 | 
			
		||||
				avatar: "b48302623a12bc7c59a71328f72ccb39"
 | 
			
		||||
			},
 | 
			
		||||
			attachments: [],
 | 
			
		||||
			guild_id: "112760669178241024"
 | 
			
		||||
		},
 | 
			
		||||
		simple_message_link: {
 | 
			
		||||
			id: "1126788210308161626",
 | 
			
		||||
			type: 0,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue