forked from cadence/out-of-your-element
		
	Add setting for d->m URL previews
This commit is contained in:
		
							parent
							
								
									efd7cb3fd4
								
							
						
					
					
						commit
						6b919d2a82
					
				
					 6 changed files with 51 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
const {test} = require("supertape")
 | 
			
		||||
const {messageToEvent} = require("./message-to-event")
 | 
			
		||||
const data = require("../../../test/data")
 | 
			
		||||
const Ty = require("../../types")
 | 
			
		||||
const {db} = require("../../passthrough")
 | 
			
		||||
 | 
			
		||||
test("message2event embeds: nothing but a field", async t => {
 | 
			
		||||
	const events = await messageToEvent(data.message_with_embeds.nothing_but_a_field, data.guild.general, {})
 | 
			
		||||
| 
						 | 
				
			
			@ -351,3 +351,16 @@ test("message2event embeds: if discord creates an embed preview for a discord ch
 | 
			
		|||
		"m.mentions": {}
 | 
			
		||||
	}])
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("message2event embeds: nothing generated if embeds are disabled in settings", async t => {
 | 
			
		||||
	db.prepare("UPDATE guild_space SET url_preview = 0 WHERE guild_id = ?").run(data.guild.general.id)
 | 
			
		||||
	const events = await messageToEvent(data.message_with_embeds.youtube_video, data.guild.general)
 | 
			
		||||
	t.deepEqual(events, [{
 | 
			
		||||
		$type: "m.room.message",
 | 
			
		||||
		msgtype: "m.text",
 | 
			
		||||
		body: "https://youtu.be/kDMHHw8JqLE?si=NaqNjVTtXugHeG_E\n\n\nJutomi I'm gonna make these sounds in your walls tonight",
 | 
			
		||||
		format: "org.matrix.custom.html",
 | 
			
		||||
		formatted_body: `<a href="https://youtu.be/kDMHHw8JqLE?si=NaqNjVTtXugHeG_E">https://youtu.be/kDMHHw8JqLE?si=NaqNjVTtXugHeG_E</a><br><br><br>Jutomi I'm gonna make these sounds in your walls tonight`,
 | 
			
		||||
		"m.mentions": {}
 | 
			
		||||
	}])
 | 
			
		||||
})
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -597,7 +597,12 @@ async function messageToEvent(message, guild, options = {}, di) {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	// Then embeds
 | 
			
		||||
	const urlPreviewEnabled = select("guild_space", "url_preview", {guild_id: guild?.id}).pluck().get() ?? 1
 | 
			
		||||
	for (const embed of message.embeds || []) {
 | 
			
		||||
		if (!urlPreviewEnabled && !message.author?.bot) {
 | 
			
		||||
			continue // show embeds for everyone if enabled, or bot users only if disabled (bots often send content in embeds)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (embed.type === "image") {
 | 
			
		||||
			continue // Matrix's own URL previews are fine for images.
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
BEGIN TRANSACTION;
 | 
			
		||||
 | 
			
		||||
ALTER TABLE guild_space ADD COLUMN url_preview INTEGER NOT NULL DEFAULT 1;
 | 
			
		||||
 | 
			
		||||
COMMIT;
 | 
			
		||||
							
								
								
									
										1
									
								
								src/db/orm-defs.d.ts
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								src/db/orm-defs.d.ts
									
										
									
									
										vendored
									
									
								
							| 
						 | 
				
			
			@ -34,6 +34,7 @@ export type Models = {
 | 
			
		|||
		space_id: string
 | 
			
		||||
		privacy_level: number
 | 
			
		||||
		presence: 0 | 1
 | 
			
		||||
		url_preview: 0 | 1
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	guild_active: {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -102,13 +102,24 @@ block body
 | 
			
		|||
      #autocreate-loading
 | 
			
		||||
 | 
			
		||||
  if space_id
 | 
			
		||||
    h3.mt32.fs-category URL preview
 | 
			
		||||
    .s-card
 | 
			
		||||
      form.d-flex.ai-center.g8
 | 
			
		||||
        label.s-label.fl-grow1(for="url-preview")
 | 
			
		||||
          | Show Discord's URL previews on Matrix
 | 
			
		||||
          p.s-description Shows info about links posted to chat. Discord's previews are generally better quality than Synapse's, especially for social media and videos.
 | 
			
		||||
        - let value = !!select("guild_space", "url_preview", {guild_id}).pluck().get()
 | 
			
		||||
        input(type="hidden" name="guild_id" value=guild_id)
 | 
			
		||||
        input.s-toggle-switch.order-last#autocreate(name="url_preview" type="checkbox" hx-post="/api/url-preview" hx-indicator="#url-preview-loading" hx-disabled-elt="this" checked=value autocomplete="off")
 | 
			
		||||
        #url-preview-loading
 | 
			
		||||
 | 
			
		||||
    h3.mt32.fs-category Presence
 | 
			
		||||
    .s-card
 | 
			
		||||
      form.d-flex.ai-center.g8
 | 
			
		||||
        label.s-label.fl-grow1(for="presence")
 | 
			
		||||
          | Show online statuses on Matrix
 | 
			
		||||
          p.s-description This might cause lag on really big Discord servers.
 | 
			
		||||
        - let value = !!select("guild_space", "presence", {guild_id}).pluck().get()
 | 
			
		||||
        - value = !!select("guild_space", "presence", {guild_id}).pluck().get()
 | 
			
		||||
        input(type="hidden" name="guild_id" value=guild_id)
 | 
			
		||||
        input.s-toggle-switch.order-last#autocreate(name="presence" type="checkbox" hx-post="/api/presence" hx-indicator="#presence-loading" hx-disabled-elt="this" checked=value autocomplete="off")
 | 
			
		||||
        #presence-loading
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -27,6 +27,10 @@ const schema = {
 | 
			
		|||
		guild_id: z.string(),
 | 
			
		||||
		autocreate: z.string().optional()
 | 
			
		||||
	}),
 | 
			
		||||
	urlPreview: z.object({
 | 
			
		||||
		guild_id: z.string(),
 | 
			
		||||
		url_preview: z.string().optional()
 | 
			
		||||
	}),
 | 
			
		||||
	presence: z.object({
 | 
			
		||||
		guild_id: z.string(),
 | 
			
		||||
		presence: z.string().optional()
 | 
			
		||||
| 
						 | 
				
			
			@ -57,6 +61,16 @@ as.router.post("/api/autocreate", defineEventHandler(async event => {
 | 
			
		|||
	return null // 204
 | 
			
		||||
}))
 | 
			
		||||
 | 
			
		||||
as.router.post("/api/url-preview", defineEventHandler(async event => {
 | 
			
		||||
	const parsedBody = await readValidatedBody(event, schema.urlPreview.parse)
 | 
			
		||||
	const managed = await auth.getManagedGuilds(event)
 | 
			
		||||
	if (!managed.has(parsedBody.guild_id)) throw createError({status: 403, message: "Forbidden", data: "Can't change settings for a guild you don't have Manage Server permissions in"})
 | 
			
		||||
 | 
			
		||||
	db.prepare("UPDATE guild_space SET url_preview = ? WHERE guild_id = ?").run(+!!parsedBody.url_preview, parsedBody.guild_id)
 | 
			
		||||
 | 
			
		||||
	return null // 204
 | 
			
		||||
}))
 | 
			
		||||
 | 
			
		||||
as.router.post("/api/presence", defineEventHandler(async event => {
 | 
			
		||||
	const parsedBody = await readValidatedBody(event, schema.presence.parse)
 | 
			
		||||
	const managed = await auth.getManagedGuilds(event)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue