PDS Admin tool make it easier to moderate your PDS with labels

Implement email notifications for new accounts #2

merged opened by tophhie.cloud targeting main from tophhie.cloud/tc-label-watcher: main

PR to support email notifications for new accounts.

Labels

None yet.

assignee

None yet.

Participants 2
AT URI
at://did:plc:ggobmtqnjirtchpwgydxoecb/sh.tangled.repo.pull/3mhfzjtycmj22
+65
Diff #1
+2
settings.toml.example
··· 12 12 backfillAccounts = true 13 13 # Listens for new accounts 14 14 listenForNewAccounts = true 15 + # Send notification for new accounts 16 + notifyOnNewAccounts = false 15 17 16 18 # Another PDS you want to watch 17 19 # [pds.your-pds-two]
+13
src/handlers/pdsSubscriber.ts
··· 6 6 import { FirehoseSubscription } from "@atcute/firehose"; 7 7 import { ComAtprotoSyncSubscribeRepos } from "@atcute/atproto"; 8 8 import { handleNewIdentityEvent } from "./handleNewIdentityEvent.js"; 9 + import { sendNewAccountNotification } from "../mailer.js"; 9 10 10 11 export const pdsSubscriber = ( 11 12 config: PDSConfig, ··· 39 40 }, 40 41 "Identity event", 41 42 ); 43 + if (config.notifyOnNewAccounts) { 44 + if (message.active == true) { 45 + await queue.add(() => 46 + sendNewAccountNotification(config.notifyEmails, { 47 + did: message.did, 48 + pds: config.host 49 + }).catch((err) => { 50 + logger.error({ err }, "Error sending new account notification email") 51 + }) 52 + ); 53 + } 54 + } 42 55 await queue.add(() => 43 56 handleNewIdentityEvent( 44 57 db,
+49
src/mailer.ts
··· 73 73 return info.join("\n"); 74 74 } 75 75 76 + export const sendNewAccountNotification = async ( 77 + emails: string[], 78 + params: { 79 + did: string; 80 + pds: string; 81 + } 82 + ) => { 83 + const { 84 + did, 85 + pds 86 + } = params; 87 + 88 + logger.info(`Sending new account notification for ${did} on ${pds}...`) 89 + 90 + const subject = `New account created on ${pds} - ${did}`; 91 + let infoText = [ 92 + `A new account has been detected on ${pds}.`, 93 + ``, 94 + `DID: ${did}`, 95 + `PDS: ${pds}` 96 + ]; 97 + const text = infoText.join("\n"); 98 + 99 + if (resend) { 100 + await resend.emails.send({ 101 + from: senderEmail, 102 + to: emails, 103 + subject, 104 + text, 105 + }); 106 + } else { 107 + if (transporter) { 108 + await transporter.sendMail({ 109 + from: senderEmail, 110 + to: emails.join(", "), 111 + subject, 112 + text, 113 + }); 114 + } else { 115 + logger.error( 116 + { 117 + error: "No transporter available", 118 + }, 119 + "Error sending email", 120 + ); 121 + } 122 + } 123 + } 124 + 76 125 export const sendLabelNotification = async ( 77 126 emails: string[], 78 127 params: {
+1
src/types/settings.ts
··· 7 7 pdsAdminPassword?: string; 8 8 backfillAccounts: boolean; 9 9 listenForNewAccounts: boolean; 10 + notifyOnNewAccounts: boolean; 10 11 } 11 12 12 13 export type LabelAction = "notify" | "takedown";

History

2 rounds 1 comment
sign up or login to add to the discussion
4 commits
expand
Added email notifications for new accounts
Fixed overload match issue
Logging
Notify for only active accounts
expand 1 comment

Looks great to me! Thank you!

pull request successfully merged
3 commits
expand
Added email notifications for new accounts
Fixed overload match issue
Logging
expand 0 comments