From ded1ab5d937485afd36091f612ddcdefa8d311a1 Mon Sep 17 00:00:00 2001 From: Joan Date: Wed, 18 Apr 2018 15:11:05 +0200 Subject: [PATCH] Sql messageSend() and messageSendWithUser() --- services/db/changes/1.0.3/02-messageSend.sql | 17 ++++++ .../changes/1.0.3/03-messageSendWithUser.sql | 57 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 services/db/changes/1.0.3/02-messageSend.sql create mode 100644 services/db/changes/1.0.3/03-messageSendWithUser.sql diff --git a/services/db/changes/1.0.3/02-messageSend.sql b/services/db/changes/1.0.3/02-messageSend.sql new file mode 100644 index 000000000..cb099222c --- /dev/null +++ b/services/db/changes/1.0.3/02-messageSend.sql @@ -0,0 +1,17 @@ +USE `vn`; +DROP function IF EXISTS `messageSend`; + +DELIMITER $$ +USE `vn`$$ +CREATE DEFINER=`root`@`%` FUNCTION `messageSend`(vRecipient VARCHAR(255) CHARSET utf8, vMessage TEXT CHARSET utf8) RETURNS int(11) +BEGIN + DECLARE result INT; + DECLARE vSenderFk INT; + + SELECT id INTO vSenderFk + FROM account.user WHERE `name` = account.userGetName(); + + RETURN (SELECT messageSendWithUser(vSenderFk, vRecipient, vMessage)); +END$$ + +DELIMITER ; diff --git a/services/db/changes/1.0.3/03-messageSendWithUser.sql b/services/db/changes/1.0.3/03-messageSendWithUser.sql new file mode 100644 index 000000000..c5b83f77f --- /dev/null +++ b/services/db/changes/1.0.3/03-messageSendWithUser.sql @@ -0,0 +1,57 @@ +USE `vn`; +DROP function IF EXISTS `messageSendWithUser`; + +DELIMITER $$ +USE `vn`$$ +CREATE DEFINER=`root`@`%` FUNCTION `messageSendWithUser`(vSenderFK INT, vRecipient VARCHAR(255) CHARSET utf8, vMessage TEXT CHARSET utf8) RETURNS int(11) +BEGIN + + DECLARE vCount INT; + DECLARE vUuid VARCHAR(255); + DECLARE vSendDate DATETIME DEFAULT NOW(); + DECLARE vSender VARCHAR(255) CHARSET utf8; + + SELECT `name` INTO vSender + FROM account.user WHERE id = vSenderFK; + + SET vRecipient = LOWER(vRecipient); + + DROP TEMPORARY TABLE IF EXISTS tRecipients; + CREATE TEMPORARY TABLE tRecipients + SELECT u.name finalRecipient + FROM account.mailAlias a + JOIN account.mailAliasAccount aa ON aa.mailAlias = a.id + JOIN account.user u ON u.id = aa.account + WHERE a.alias = vRecipient COLLATE utf8_unicode_ci + AND u.name != vSender + AND u.active + UNION + SELECT u.name FROM account.user u + WHERE u.name = vRecipient + AND u.active; + + SELECT COUNT(*) INTO vCount FROM tRecipients; + + IF vCount = 0 THEN + RETURN vCount; + END IF; + + SET vUuid = UUID(); + + INSERT INTO message + SET uuid = vUuid, + sender = vSender, + recipient = vRecipient, + message = vMessage, + sendDate = vSendDate; + + INSERT INTO messageInbox (uuid, sender, recipient, finalRecipient, message, sendDate) + SELECT vUuid, vSender, vRecipient, finalRecipient, vMessage, vSendDate + FROM tRecipients; + + DROP TEMPORARY TABLE tRecipients; + RETURN vCount; +END$$ + +DELIMITER ; +