mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
Create email and Reply functionality
This commit is contained in:
1 parent
15a8db1293
commit
b83a890f48
18 files changed
+229
-179
No files matched your search
@@ -121,15 +121,15 @@ export async function generateEmail(identifier: string): Promise<string> {
|
||||
const email = result[0]?.phone_email;
|
||||
|
||||
if (!email) {
|
||||
const emailUser = await getDefaultProfileNames(identifier);
|
||||
const userProfileNames = await getDefaultProfileNames(identifier);
|
||||
|
||||
if (!emailUser) {
|
||||
if (!userProfileNames) {
|
||||
throw new Error('Must provide a fullName or phoneNumber option to create an email');
|
||||
}
|
||||
|
||||
const emailProvider = config.email.provider || 'project-error.dev';
|
||||
|
||||
const newEmail = `${emailUser}@${emailProvider}`;
|
||||
const newEmail = `${userProfileNames[0]}@${emailProvider}`.trim().toLowerCase();
|
||||
const query = 'UPDATE users SET phone_email = ? WHERE identifier = ?';
|
||||
await pool.query(query, [newEmail, identifier]);
|
||||
mainLogger.debug(`Inserting email into Database: ${newEmail}`);
|
||||
|
||||
@@ -240,7 +240,7 @@ export class Player {
|
||||
* Set the stored phone email for a user
|
||||
**/
|
||||
public setEmail(email: string): void {
|
||||
this._email = email;
|
||||
this._email = email.trim().toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
import { EmailEvents, EmailMessageInput } from '../../typings/email';
|
||||
import { EmailEvents, EmailMessageInput, IEmail, IEmailMessage } from '../../typings/email';
|
||||
import { mainLogger } from './sv_logger';
|
||||
import { getIdentifierByEmail, getPlayer, getSource } from './functions';
|
||||
import { IEmail, IEmailMessage } from '../../phone/src/common/typings/email';
|
||||
import { pool } from './db';
|
||||
import config from '../utils/config';
|
||||
import { getPlayerInfo } from './players/sv_players';
|
||||
|
||||
const emailLogger = mainLogger.child({ module: 'contact' });
|
||||
const emailLogger = mainLogger.child({ module: 'email' });
|
||||
|
||||
export interface UnformattedEmailMessage {
|
||||
message_id: number;
|
||||
@@ -125,7 +122,7 @@ async function sendEmail(
|
||||
email_id_or_subject: number | string,
|
||||
parent_id?: number,
|
||||
) {
|
||||
let email_id = email_id_or_subject === 'number' ? email_id_or_subject : undefined;
|
||||
let email_id = typeof email_id_or_subject === 'number' ? email_id_or_subject : undefined;
|
||||
if (typeof email_id_or_subject === 'string') {
|
||||
const emailQuery = `INSERT INTO npwd_emails (subject) VALUES (?)`;
|
||||
const [emailResult] = await pool.query(emailQuery, [email_id_or_subject]);
|
||||
@@ -168,7 +165,7 @@ onNet(EmailEvents.FETCH_INBOX, async () => {
|
||||
emailLogger.error(`Failed to fetch email inbox, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(EmailEvents.FETCH_INBOX_ERROR, _source, e);
|
||||
emitNet(EmailEvents.SEND_EMAIL_ERROR, _source, 'APPS_EMAIL_FETCH_INBOX_ERROR');
|
||||
}
|
||||
});
|
||||
|
||||
@@ -176,37 +173,59 @@ onNet(
|
||||
EmailEvents.SEND_EMAIL,
|
||||
async ({ email_id, subject, parent_id, body, receivers }: EmailMessageInput) => {
|
||||
const _source = getSource();
|
||||
|
||||
if (!body || !receivers) {
|
||||
emitNet(EmailEvents.SEND_EMAIL_ERROR, _source, 'APPS_EMAIL_SEND_EMAIL_BAD_INPUT');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const player = getPlayer(_source);
|
||||
if (!player) {
|
||||
throw new Error('Couldnt find player');
|
||||
}
|
||||
|
||||
const parsedReceivers = receivers
|
||||
.split(',')
|
||||
.map((email: string) => email.trim().toLowerCase());
|
||||
|
||||
const mappedReceivers = await Promise.all(
|
||||
receivers
|
||||
.split(',')
|
||||
.map((s: string) => s.trim())
|
||||
.map(async (email: string) => {
|
||||
const identifier = await getIdentifierByEmail(email);
|
||||
return { email, identifier };
|
||||
}),
|
||||
parsedReceivers.map(async (email: string) => {
|
||||
const identifier = await getIdentifierByEmail(email);
|
||||
return { email, identifier };
|
||||
}),
|
||||
);
|
||||
|
||||
const email = await sendEmail(
|
||||
player.identifier,
|
||||
player.getEmail(),
|
||||
mappedReceivers,
|
||||
mappedReceivers.filter((r) => !!r.identifier),
|
||||
body,
|
||||
email_id || subject,
|
||||
parent_id || null,
|
||||
);
|
||||
|
||||
const failedReceivers = mappedReceivers.filter((r) => !r.identifier);
|
||||
|
||||
if (failedReceivers.length) {
|
||||
failedReceivers.map((r) =>
|
||||
emailLogger.debug(`Email receiver not found in DB: "${r.email}"`, {
|
||||
source: _source,
|
||||
}),
|
||||
);
|
||||
|
||||
if (failedReceivers.length === mappedReceivers.length) {
|
||||
emitNet(EmailEvents.SEND_EMAIL_ERROR, _source, 'APPS_EMAIL_SEND_EMAIL_ERROR_NOT_FOUND');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
emitNet(EmailEvents.SEND_EMAIL_SUCCESS, _source, email);
|
||||
} catch (e) {
|
||||
emailLogger.error(`Failed to send email, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(EmailEvents.SEND_EMAIL_ERROR, _source, e);
|
||||
emitNet(EmailEvents.SEND_EMAIL_ERROR, _source, 'GENERIC_UNEXPECTED_ERROR');
|
||||
}
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user