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
// import { Client } from "oceanic.js";
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));
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();

View file

@ -87,7 +87,6 @@ export default async function (matrixClient, event, room, toStartOfTimeline) {
}
} else {
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
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)
@ -102,7 +101,7 @@ export default async function (matrixClient, event, room, toStartOfTimeline) {
logger.log("error", error.stack)
try {
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"
}
matrixClient.sendEvent(event.event.room_id, "m.room.message", errorcontent, "", (err, res) => {

View file

@ -1,5 +1,8 @@
import { request } from "undici";
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 = [
"tenor.com",
@ -116,8 +119,7 @@ const urlFromMxc = async (mxcUri) => {
const checkImages = async (message, extraReturnTypes, video, sticker) => {
let type;
// console.log(message)
if (typeof message.content.info !== undefined) {
if (typeof message.content.info) {
if (message.content.msgtype == "m.image") {
const url = await urlFromMxc(message.content.url)
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) => {
// we start by determining whether or not we're dealing with an interaction or a message
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) {
const replyMessage = await client.fetchRoomEvent(cmdMessage.room_id, cmdMessage.content['m.relates_to']['m.in_reply_to'].event_id)
// console.log(replyMessage)
if (replyMessage) {
const replyResult = await checkImages(replyMessage, extraReturnTypes, video, sticker);
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);
if (result !== false) return result;
}
// if (!singleMessage) {
// // if there aren't any replies or interaction attachments then iterate over the last few messages in the channel
// try {
// const channel = (interaction ? interaction : cmdMessage).channel ?? await client.rest.channels.get((interaction ? interaction : cmdMessage).channelID);
// const messages = await channel.getMessages();
// // iterate over each message
// for (const message of messages) {
// const result = await checkImages(message, extraReturnTypes, video, sticker);
// if (result === false) {
// continue;
// } else {
// return result;
// }
// }
// } catch {
// // no-op
// }
// }
if (!singleMessage) {
// if there aren't any replies or interaction attachments then iterate over the last few messages in the channel
try {
const channel = cmdMessage.room_id;
await client.store.getPendingEvents(channel)
const room = await client.getRoom(channel)
const timeline = await room.getLiveTimeline()
let pagination = timeline.setPaginationToken(null, sdk.EventTimeline.BACKWARDS)
for (const event of timeline.events.reverse()) {
const result = await checkImages(event.event, extraReturnTypes, video, sticker);
if (result === false) {
continue;
} else {
return result;
}
}
} catch (error) {
// log error
logger.log("error", error)
}
}
};