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

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)
}
}
};