allow imagedetect to pull from room history

This commit is contained in:
murm 2023-03-16 11:32:14 -04:00
parent eee2239364
commit 3957b17645
3 changed files with 28 additions and 30 deletions

4
app.js
View file

@ -22,7 +22,7 @@ import { reloadImageConnections } from "./utils/image.js";
// main services // main services
// import { Client } from "oceanic.js"; // import { Client } from "oceanic.js";
import * as sdk from "matrix-js-sdk"; import * as sdk from "matrix-js-sdk";
const AutojoinRoomsMixin = sdk.AutojoinRoomsMixin; // const AutojoinRoomsMixin = sdk.AutojoinRoomsMixin;
@ -184,7 +184,7 @@ esmBot ${esmBotVersion} (${process.env.GIT_REV})
matrixClient.on("Room.timeline", event.bind(null,matrixClient)); matrixClient.on("Room.timeline", event.bind(null,matrixClient));
logger.log("info", "Finished loading events."); logger.log("info", "Finished loading events.");
matrixClient.startClient({ initialSyncLimit: 1 }).then(() => logger.log("info", "Client started!")); matrixClient.startClient({ initialSyncLimit: 2 }).then(() => logger.log("info", "Client started!"));
} }
init(); init();

View file

@ -87,7 +87,6 @@ export default async function (matrixClient, event, room, toStartOfTimeline) {
} }
} else { } else {
const mxcUri = await matrixClient.uploadContent(result.contents); const mxcUri = await matrixClient.uploadContent(result.contents);
console.log(mxcUri.content_uri)
// TODO: make info object get width, height, and mime from natives so i dont need to read the buffer // TODO: make info object get width, height, and mime from natives so i dont need to read the buffer
const imgsize = sizeOf(result.contents) const imgsize = sizeOf(result.contents)
await matrixClient.sendImageMessage(event.event.room_id, mxcUri.content_uri, {h: imgsize.height, w: imgsize.width, mimetype: `image/${imgsize.type}`, size: result.contents.length, thumbnail_info: {h: imgsize.height, w: imgsize.width, mimetype: `image/${imgsize.type}`, size: result.contents.length}}, result.name) await matrixClient.sendImageMessage(event.event.room_id, mxcUri.content_uri, {h: imgsize.height, w: imgsize.width, mimetype: `image/${imgsize.type}`, size: result.contents.length, thumbnail_info: {h: imgsize.height, w: imgsize.width, mimetype: `image/${imgsize.type}`, size: result.contents.length}}, result.name)
@ -102,7 +101,7 @@ export default async function (matrixClient, event, room, toStartOfTimeline) {
logger.log("error", error.stack) logger.log("error", error.stack)
try { try {
const errorcontent = { const errorcontent = {
body: `Message: ${clean(err)}\n\nStack Trace: ${clean(err.stack)}`, body: `Message: ${clean(error)}\n\nStack Trace: ${clean(error.stack)}`,
msgtype: "m.text" msgtype: "m.text"
} }
matrixClient.sendEvent(event.event.room_id, "m.room.message", errorcontent, "", (err, res) => { matrixClient.sendEvent(event.event.room_id, "m.room.message", errorcontent, "", (err, res) => {

View file

@ -1,5 +1,8 @@
import { request } from "undici"; import { request } from "undici";
import { getType } from "./image.js"; import { getType } from "./image.js";
import * as sdk from "matrix-js-sdk";
import { EventTimelineSet } from "matrix-js-sdk";
import { logger } from "./logger.js";
const tenorURLs = [ const tenorURLs = [
"tenor.com", "tenor.com",
@ -116,8 +119,7 @@ const urlFromMxc = async (mxcUri) => {
const checkImages = async (message, extraReturnTypes, video, sticker) => { const checkImages = async (message, extraReturnTypes, video, sticker) => {
let type; let type;
// console.log(message) if (typeof message.content.info) {
if (typeof message.content.info !== undefined) {
if (message.content.msgtype == "m.image") { if (message.content.msgtype == "m.image") {
const url = await urlFromMxc(message.content.url) const url = await urlFromMxc(message.content.url)
const fileNameNoExtension = message.content.body.slice(0, message.content.body.lastIndexOf(".")); const fileNameNoExtension = message.content.body.slice(0, message.content.body.lastIndexOf("."));
@ -152,14 +154,8 @@ const checkImages = async (message, extraReturnTypes, video, sticker) => {
export default async (client, cmdMessage, interaction, options, extraReturnTypes = false, video = false, sticker = false, singleMessage = false) => { export default async (client, cmdMessage, interaction, options, extraReturnTypes = false, video = false, sticker = false, singleMessage = false) => {
// we start by determining whether or not we're dealing with an interaction or a message // we start by determining whether or not we're dealing with an interaction or a message
if (cmdMessage) { if (cmdMessage) {
// console.log(cmdMessage)
// let channel = await client.getRoom(cmdMessage.room_id);
// console.log(channel)
// check if the message is a reply to another message
// console.log(cmdMessage.content['m.relates_to'])
if (cmdMessage.content['m.relates_to'] !== undefined) { if (cmdMessage.content['m.relates_to'] !== undefined) {
const replyMessage = await client.fetchRoomEvent(cmdMessage.room_id, cmdMessage.content['m.relates_to']['m.in_reply_to'].event_id) const replyMessage = await client.fetchRoomEvent(cmdMessage.room_id, cmdMessage.content['m.relates_to']['m.in_reply_to'].event_id)
// console.log(replyMessage)
if (replyMessage) { if (replyMessage) {
const replyResult = await checkImages(replyMessage, extraReturnTypes, video, sticker); const replyResult = await checkImages(replyMessage, extraReturnTypes, video, sticker);
if (replyResult !== false) return replyResult; if (replyResult !== false) return replyResult;
@ -169,22 +165,25 @@ export default async (client, cmdMessage, interaction, options, extraReturnTypes
const result = await checkImages(cmdMessage, extraReturnTypes, video, sticker); const result = await checkImages(cmdMessage, extraReturnTypes, video, sticker);
if (result !== false) return result; if (result !== false) return result;
} }
// if (!singleMessage) { if (!singleMessage) {
// // if there aren't any replies or interaction attachments then iterate over the last few messages in the channel // if there aren't any replies or interaction attachments then iterate over the last few messages in the channel
// try { try {
// const channel = (interaction ? interaction : cmdMessage).channel ?? await client.rest.channels.get((interaction ? interaction : cmdMessage).channelID); const channel = cmdMessage.room_id;
// const messages = await channel.getMessages(); await client.store.getPendingEvents(channel)
// // iterate over each message const room = await client.getRoom(channel)
// for (const message of messages) { const timeline = await room.getLiveTimeline()
// const result = await checkImages(message, extraReturnTypes, video, sticker); let pagination = timeline.setPaginationToken(null, sdk.EventTimeline.BACKWARDS)
// if (result === false) { for (const event of timeline.events.reverse()) {
// continue; const result = await checkImages(event.event, extraReturnTypes, video, sticker);
// } else { if (result === false) {
// return result; continue;
// } } else {
// } return result;
// } catch { }
// // no-op }
// } } catch (error) {
// } // log error
logger.log("error", error)
}
}
}; };