fp/packages/scout/src/index.email.js

44 lines
1.3 KiB
JavaScript

'use strict'
/**
* watches an e-mail inbox for going live notifications
*/
import { checkEmail } from './parsers.js'
import { signalRealtime, createStreamInDb } from './signals.js'
import { Email } from './imap.js'
import fastq from "fastq";
const q = fastq.promise(handleMessage, 1)
async function handleMessage({email, msg}) {
try {
console.log(' ✏️ loading message')
const body = await email.loadMessage(msg.uid)
console.log(' ✏️ checking e-mail')
const { isMatch, url, platform, channel, displayName, date } = (await checkEmail(body))
if (isMatch) {
console.log(' ✏️✏️ signalling realtime')
await signalRealtime({ url, platform, channel, displayName, date })
console.log(' ✏️✏️ creating stream entry in db')
await createStreamInDb({ platform, channel, date })
}
console.log(' ✏️ archiving e-mail')
await email.archiveMessage(msg.uid)
} catch (e) {
// console.error('error encoutered')
console.error(` An error was encountered while handling the following e-mail message.\n${JSON.stringify(msg, null, 2)}\nError as follows.\n${e}`)
}
}
(async () => {
const email = new Email()
email.on('message', (msg) => q.push({email, msg}))
await email.connect()
})()