From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [101.128.125.177]) by gnuweeb.org (Postfix) with ESMTPSA id 3A7608060C; Sat, 1 Oct 2022 13:04:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1664629465; bh=2OdIvnfW2O4wtO40Oxfe/cofOrWoELP6r6TRwOhJTOY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PPYGUbBMvGV4eLTTVVCxHZ+jbIZDPNGsQ2JV2gCrlAl5ApqfU0Q9al22EhIhQN9GU cQBxxJ75AW+a/gFjjGDJCEq9nroB/lxIAn9nvmync3buu54BY1tR7yLrLbnS3im/CG X7KkjiU/nlWtO9Xx09tw6NMSvfZd6Wqg/UW2RFq98PMQ2lSl+4+hc0JXx0u5LhgOrP uM5CWZOXfrkWLLcmzd4CcN1zZYVH5PPCRtt4rky9WexADOFFy4xQ0PqWFF1MVdDLbO 6i2wFE5uMC5qOPECRnovbWvkMEgDHQ/imB36iH/Z5gpRAImsqp2zvxpSDT9tHfx4Jo yvoyitR7jEiEw== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v1 04/26] discord: Add get_email_id() in getter directory Date: Sat, 1 Oct 2022 20:03:32 +0700 Message-Id: <20221001130355.784-5-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20221001130355.784-1-kiizuha@gnuweeb.org> References: <20221001130355.784-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Add get_email_id() to get the email id of the primary key `dc_emails` database table. This function is to return an email id to insert it into the save_discord_mail() parameter because the `dc_emails` and the `dc_mail_msg` database table are relationships. Signed-off-by: Muhammad Rizki --- .../database/methods/getter/__init__.py | 4 +- .../database/methods/getter/get_email_id.py | 60 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 daemon/dscord/database/methods/getter/get_email_id.py diff --git a/daemon/dscord/database/methods/getter/__init__.py b/daemon/dscord/database/methods/getter/__init__.py index 2f4bec7..0f4b976 100644 --- a/daemon/dscord/database/methods/getter/__init__.py +++ b/daemon/dscord/database/methods/getter/__init__.py @@ -6,9 +6,11 @@ from .get_atom_urls import GetAtomURL from .get_broadcast_chats import GetBroadcastChats +from .get_email_id import GetEmailID class Getter( GetAtomURL, - GetBroadcastChats + GetBroadcastChats, + GetEmailID ): pass diff --git a/daemon/dscord/database/methods/getter/get_email_id.py b/daemon/dscord/database/methods/getter/get_email_id.py new file mode 100644 index 0000000..8c27340 --- /dev/null +++ b/daemon/dscord/database/methods/getter/get_email_id.py @@ -0,0 +1,60 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Copyright (C) 2022 Muhammad Rizki +# Copyright (C) 2022 Ammar Faizi +# + + +class GetEmailID: + + def get_email_id(self, email_id, chat_id): + ''' + Determine whether the email needs to be sent to @tg_chat_id. + - Return an email id (PK) if it needs to be sent + - Return None if it doesn't need to be sent + ''' + if self.__is_sent(email_id, chat_id): + return + + res = self.__email_id(email_id) + if not bool(res): + return + + return int(res[0]) + + + def __is_sent(self, email_id, channel_id): + ''' + Checking if this email has already been sent + or not. + - Return True if it's already been sent + ''' + q = """ + SELECT dc_emails.id, dc_mail_msg.id FROM dc_emails + LEFT JOIN dc_mail_msg + ON dc_emails.id = dc_mail_msg.email_id + WHERE dc_emails.message_id = %(email_msg_id)s + AND dc_mail_msg.channel_id = %(channel_id)s + LIMIT 1 + """ + + self.cur.execute(q, { + "email_msg_id": email_id, + "channel_id": channel_id + }) + res = self.cur.fetchone() + return bool(res) + + + def __email_id(self, email_id): + ''' + Get the email id if match with the email message_id. + - Return the result if it's match and exists + ''' + q = """ + SELECT id FROM dc_emails WHERE message_id = %(email_id)s + """ + + self.cur.execute(q, {"email_id": email_id}) + res = self.cur.fetchone() + return res -- Muhammad Rizki