finally got the thread's early messages working
This commit is contained in:
		
							parent
							
								
									180708b60e
								
							
						
					
					
						commit
						6d1635539b
					
				
					 11 changed files with 321 additions and 32 deletions
				
			
		| 
						 | 
				
			
			@ -4,14 +4,10 @@ const assert = require("assert")
 | 
			
		|||
 | 
			
		||||
const passthrough = require("../../passthrough")
 | 
			
		||||
const { discord, sync, db } = passthrough
 | 
			
		||||
/** @type {import("../converters/message-to-event")} */
 | 
			
		||||
const messageToEvent = sync.require("../converters/message-to-event")
 | 
			
		||||
/** @type {import("../converters/thread-to-announcement")} */
 | 
			
		||||
const threadToAnnouncement = sync.require("../converters/thread-to-announcement")
 | 
			
		||||
/** @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")
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @param {string} parentRoomID
 | 
			
		||||
| 
						 | 
				
			
			@ -21,24 +17,10 @@ const createRoom = sync.require("../actions/create-room")
 | 
			
		|||
async function announceThread(parentRoomID, threadRoomID, thread) {
 | 
			
		||||
   /** @type {string?} */
 | 
			
		||||
   const creatorMxid = db.prepare("SELECT mxid FROM sim WHERE discord_id = ?").pluck().get(thread.owner_id)
 | 
			
		||||
   /** @type {string?} */
 | 
			
		||||
   const branchedFromEventID = db.prepare("SELECT event_id FROM event_message WHERE message_id = ?").get(thread.id)
 | 
			
		||||
 | 
			
		||||
   const msgtype = creatorMxid ? "m.emote" : "m.text"
 | 
			
		||||
   const template = creatorMxid ? "started a thread:" : "Thread started:"
 | 
			
		||||
   let body = `${template} ${thread.name} https://matrix.to/#/${threadRoomID}`
 | 
			
		||||
   let html = `${template} <a href="https://matrix.to/#/${threadRoomID}">${thread.name}</a>`
 | 
			
		||||
   const content = await threadToAnnouncement.threadToAnnouncement(parentRoomID, threadRoomID, creatorMxid, thread, {api})
 | 
			
		||||
 | 
			
		||||
   const mentions = {}
 | 
			
		||||
 | 
			
		||||
   await api.sendEvent(parentRoomID, "m.room.message", {
 | 
			
		||||
      msgtype,
 | 
			
		||||
      body: `${template} ,
 | 
			
		||||
      format: "org.matrix.custom.html",
 | 
			
		||||
      formatted_body: "",
 | 
			
		||||
      "m.mentions": mentions
 | 
			
		||||
 | 
			
		||||
   }, creatorMxid)
 | 
			
		||||
	await api.sendEvent(parentRoomID, "m.room.message", content, creatorMxid)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports.announceThread = announceThread
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -70,12 +70,15 @@ async function channelToKState(channel, guild) {
 | 
			
		|||
		avatarEventContent.url = await file.uploadDiscordFileToMxc(avatarEventContent.discord_path) // TODO: somehow represent future values in kstate (callbacks?), while still allowing for diffing, so test cases don't need to touch the media API
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	let history_visibility = "invited"
 | 
			
		||||
	if (channel["thread_metadata"]) history_visibility = "world_readable"
 | 
			
		||||
 | 
			
		||||
	const channelKState = {
 | 
			
		||||
		"m.room.name/": {name: convertedName},
 | 
			
		||||
		"m.room.topic/": {topic: convertedTopic},
 | 
			
		||||
		"m.room.avatar/": avatarEventContent,
 | 
			
		||||
		"m.room.guest_access/": {guest_access: "can_join"},
 | 
			
		||||
		"m.room.history_visibility/": {history_visibility: "invited"},
 | 
			
		||||
		"m.room.history_visibility/": {history_visibility},
 | 
			
		||||
		[`m.space.parent/${spaceID}`]: {
 | 
			
		||||
			via: ["cadence.moe"], // TODO: put the proper server here
 | 
			
		||||
			canonical: true
 | 
			
		||||
| 
						 | 
				
			
			@ -234,19 +237,23 @@ async function _unbridgeRoom(channelID) {
 | 
			
		|||
 * @returns {Promise<string[]>}
 | 
			
		||||
 */
 | 
			
		||||
async function _syncSpaceMember(channel, spaceID, roomID) {
 | 
			
		||||
	console.error(channel)
 | 
			
		||||
	console.error("syncing space for", roomID)
 | 
			
		||||
	const spaceKState = await roomToKState(spaceID)
 | 
			
		||||
	let spaceEventContent = {}
 | 
			
		||||
	if (
 | 
			
		||||
		channel.type !== DiscordTypes.ChannelType.PrivateThread // private threads do not belong in the space (don't offer people something they can't join)
 | 
			
		||||
		|| channel["thread_metadata"]?.archived // archived threads do not belong in the space (don't offer people conversations that are no longer relevant)
 | 
			
		||||
		&& !channel["thread_metadata"]?.archived // archived threads do not belong in the space (don't offer people conversations that are no longer relevant)
 | 
			
		||||
	) {
 | 
			
		||||
		spaceEventContent = {
 | 
			
		||||
			via: ["cadence.moe"] // TODO: use the proper server
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	console.error(spaceEventContent)
 | 
			
		||||
	const spaceDiff = ks.diffKState(spaceKState, {
 | 
			
		||||
		[`m.space.child/${roomID}`]: spaceEventContent
 | 
			
		||||
	})
 | 
			
		||||
	console.error(spaceDiff)
 | 
			
		||||
	return applyKStateDiffToRoom(spaceID, spaceDiff)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue