diff --git a/import.sql b/import.sql index 4754cfb..71936fd 100644 --- a/import.sql +++ b/import.sql @@ -1,6 +1,7 @@ # comment this out if your server has a different table/location for phone_number # or if it already exists in the users table ALTER TABLE `users` ADD COLUMN `phone_number` VARCHAR(20) DEFAULT NULL; +ALTER TABLE `users` ADD COLUMN `email` VARCHAR(255) DEFAULT NULL; INSERT INTO `items` (`name`, `label`, `weight`, `rare`, `can_remove`) VALUES ('phone', 'Phone', 1, 0, 1); INSERT INTO `items` (`name`, `label`, `weight`, `rare`, `can_remove`) VALUES ('dphone', 'Destroyed Phone', 1, 0, 1); @@ -174,3 +175,50 @@ CREATE TABLE IF NOT EXISTS npwd_phone_gallery ( image varchar(255) DEFAULT NULL, PRIMARY KEY (id) ); + +CREATE TABLE IF NOT EXISTS npwd_emails ( + id int(11) NOT NULL AUTO_INCREMENT, + subject varchar(80) NOT NULL, + PRIMARY KEY (id) +); + +CREATE TABLE IF NOT EXISTS npwd_emails_messages ( + id int(11) NOT NULL AUTO_INCREMENT, + parent_id int(11) DEFAULT NULL, + email_id int(11) NOT NULL, + sender varchar(255) NOT NULL, + sender_identifier varchar(255), + send_date int(11) NOT NULL, + body TEXT, + PRIMARY KEY (id) +); + +CREATE TABLE IF NOT EXISTS npwd_emails_receivers ( + id int(12) NOT NULL AUTO_INCREMENT, + message_id int(11) NOT NULL, + receiver varchar(255) NOT NULL, + receiver_identifier varchar(255) NOT NULL, + read_at int(11) DEFAULT NULL, + PRIMARY KEY (id) +); + +CREATE TABLE IF NOT EXISTS npwd_emails_phone_actions ( + id int(11) NOT NULL AUTO_INCREMENT, + message_id int(11) NOT NULL, + label varchar(255) NOT NULL, + close_phone int(1) DEFAULT 0, + delete_email int(1) DEFAULT 1, + href text NOT NULL, + PRIMARY KEY (id) +); + +CREATE TABLE IF NOT EXISTS npwd_emails_external_actions ( + id int(11) NOT NULL AUTO_INCREMENT, + message_id int(11) NOT NULL, + label varchar(255) NOT NULL, + close_phone int(1) DEFAULT 0, + delete_email int(1) DEFAULT 1, + event_name varchar(255) NOT NULL, + event_arg text, + PRIMARY KEY (id) +); diff --git a/phone/src/common/typings/email.ts b/phone/src/common/typings/email.ts index 5be6a34..236ee84 100644 --- a/phone/src/common/typings/email.ts +++ b/phone/src/common/typings/email.ts @@ -17,13 +17,13 @@ export interface IEmailMessage { receivers: string[]; sender: string; sendDate: number; - subject: string; body: string; + phoneActions?: Array; + externalActions?: Array; } export interface IEmail { id: number; + subject: string; messages: IEmailMessage[]; - phoneActions?: Array; - externalActions?: Array; } diff --git a/resources/server/sv_email.ts b/resources/server/sv_email.ts index 6243f41..27adbaf 100644 --- a/resources/server/sv_email.ts +++ b/resources/server/sv_email.ts @@ -5,11 +5,29 @@ import { pool } from './db'; const emailLogger = mainLogger.child({ module: 'contact' }); -async function fetchInbox(email: string): Promise { +async function fetchInbox(identifier: string): Promise { const query = ` - SELECT * FROM npwd_emails WHERE id = ? + SELECT + npwd_emails.subject, + npwd_emails_messages.parent_id, + npwd_emails_messages.email_id, + npwd_emails_messages.sender, + npwd_emails_messages.sender_identifier, + npwd_emails_messages.send_date, + npwd_emails_messages.body, + npwd_emails_receivers.id, + npwd_emails_receivers.receiver, + npwd_emails_receivers.receiver_identifier + FROM ( + SELECT id, read_at, message_id FROM npwd_emails_receivers WHERE npwd_emails_receivers.receiver_identifier = ? + ) as er + LEFT OUTER JOIN npwd_emails_messages ON npwd_emails_messages.id = er.message_id + LEFT OUTER JOIN npwd_emails_receivers ON npwd_emails_receivers.message_id = npwd_emails_messages.id + LEFT OUTER JOIN npwd_emails ON npwd_emails.id = npwd_emails_messages.email_id + WHERE npwd_emails_receivers.receiver_identifier = ? + ORDER BY npwd_emails_messages.send_date DESC `; - const result = await pool.query(query, [email]); + const result = await pool.query(query, [identifier]); return result[0]; }