diff options
| author | Adam Soltys <[email protected]> | 2022-09-07 13:11:12 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-09-07 13:11:12 -0700 |
| commit | 06b59b585ac9af7be106b9c1271d251ba20f6630 (patch) | |
| tree | d01db8fa25ade19c534f9ac34b11c3bff1ac507d /lib/mail.js | |
| parent | ca5ccaf2deeba7eda2c16fd1b7d33aa4c25e960f (diff) | |
| download | coinos-server-06b59b585ac9af7be106b9c1271d251ba20f6630.tar.gz coinos-server-06b59b585ac9af7be106b9c1271d251ba20f6630.zip | |
clean up
Diffstat (limited to 'lib/mail.js')
| -rw-r--r-- | lib/mail.js | 144 |
1 files changed, 23 insertions, 121 deletions
diff --git a/lib/mail.js b/lib/mail.js index 537614d2..a146d128 100644 --- a/lib/mail.js +++ b/lib/mail.js @@ -1,132 +1,34 @@ -import { emit } from "$lib/sockets"; import app from "$app"; import config from "$config"; -import fs from "fs"; -import Imap from "imap"; -import { inspect } from "util"; import persist from "./persist"; const emails = persist("data/emails.json"); -import { rates } from "./store"; -let imap = new Imap(config.imap); -function openInbox(cb) { - imap.openBox("INBOX", true, cb); -} +app.post("/email", async (req, res) => { + try { + let file = persist("data/emails.json"); + file.emails = [...file.emails, req.body]; -app.post("/contact-sil", (req, res) => { - const mailgun = require("mailgun-js")(config.mailgun); - let data = { - subject: "Contact form", - text: JSON.stringify(req.body), - from: "[email protected]", - to: "[email protected]" - }; + try { + var postmark = await import("postmark"); - mailgun.messages().send(data); - res.send(req.body); -}); - -app.post("/contact", (req, res) => { - const mailgun = require("mailgun-js")(config.mailgun); - let data = { - subject: "Contact form", - text: JSON.stringify(req.body), - from: "[email protected]", - to: "[email protected]" - }; - - mailgun.messages().send(data); - res.send(req.body); -}); + var client = new postmark.ServerClient(config.postmark); -imap.once("ready", function() { - openInbox(function(err, box) { - if (err) throw err; - setInterval(() => { - let f = imap.seq.fetch(`${box.messages.total - 5}:*`, { - bodies: ["TEXT"], - struct: true + await client.sendEmail({ + From: "[email protected]", + To: "[email protected]", + Subject: req.body.subject || "Email Signup", + HtmlBody: JSON.stringify(req.body), + TextBody: JSON.stringify(req.body), + MessageStream: "outbound" }); - f.on("message", function(msg, seqno) { - const prefix = "(#" + seqno + ") "; - msg.on("body", function(stream, info) { - let buffer = ""; - stream.on("data", function(chunk) { - //stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt', {flags:'a+'}); - buffer += chunk.toString("utf8"); - }); - stream.once("end", async function() { - let accounts = config.imap.accounts; - let a = Object.keys(accounts).find(a => buffer.includes(a)); - let user_id = accounts[a]; - - if (!emails[seqno] && user_id) { - let cad = parseFloat( - buffer - .replace(/.*\$(.*)\.\d\d \(CAD\).*/gms, "$1") - .replace(",", "") - ); - emails[seqno] = cad; - - try { - await db.transaction(async transaction => { - const account = await db.Account.findOne({ - where: { - user_id, - asset: config.liquid.btcasset, - pubkey: null - }, - include: { - model: db.User, - as: "user" - }, - lock: transaction.LOCK.UPDATE, - transaction - }); - - let { user } = account; - - let rate = store.rates["CAD"] * 1.02; - let amount = Math.round((cad / rate) * 100000000); - - await account.increment({ balance: amount }, { transaction }); - - let payment = await db.Payment.create({ - account_id: account.id, - user_id: account.user_id, - hash: `Interac deposit: ${cad} CAD`, - amount, - currency: "CAD", - rate, - received: true, - tip: 0, - confirmed: true, - address: "", - network: "COINOS" - }); - payment = payment.get({ plain: true }); - payment.account = account.get({ plain: true }); - - emit(user.username, "account", account); - emit(user.username, "payment", payment); - - l("interac deposit detected for", user.username, cad, amount); - }); - } catch (e) { - console.log(e); - } - } - }); - }); - msg.once("end", function() { - // console.log(prefix + 'Finished'); - }); - - imap.seq.addFlags(seqno, ["Seen"]); - }); - }, 10000); - }); + res.send({ ok: true }); + } catch (e) { + console.log("problem sending email", e); + res.code(500).send(e.message); + } + } catch (e) { + console.log(e); + res.code(500).send(e.message); + } }); - -imap.connect(); |
