From 3d9713c2b6fe97f6c9e1a6531ba490890f1b5971 Mon Sep 17 00:00:00 2001 From: Nareshkumar Rao Date: Fri, 13 Aug 2021 18:21:16 +0200 Subject: [PATCH] resolves #12. implements purging of old contacts --- src/db/models/Contact.helper.ts | 47 +++++++++++++++++++++++++++++++++ src/db/models/Contact.ts | 2 ++ src/db/models/User.helper.ts | 9 +++++++ src/db/utils.ts | 30 --------------------- src/routes/CodeRoute.ts | 6 ++--- src/routes/LoginRoute.ts | 9 +++---- src/routes/VerifyRoute.ts | 11 ++++---- src/telegram.ts | 5 +--- 8 files changed, 71 insertions(+), 48 deletions(-) delete mode 100644 src/db/utils.ts diff --git a/src/db/models/Contact.helper.ts b/src/db/models/Contact.helper.ts index e69de29..1883585 100644 --- a/src/db/models/Contact.helper.ts +++ b/src/db/models/Contact.helper.ts @@ -0,0 +1,47 @@ +import { Op } from "sequelize"; +import { strings_en } from "../../strings"; +import { sendTelegramMessage } from "../../telegram"; +import { TelegramID } from "../../types"; +import { Contact, ContactInterface } from "./Contact"; +import { getUserByTelegramID } from "./User.helper"; + +export async function purgeOldContacts(telegramID: TelegramID): Promise { + const user = await getUserByTelegramID(telegramID); + if (!user) throw new Error("User could not be found"); + const contacts = await Contact.findAll({ + where: { + [Op.or]: [{ user: user.id }, { with: user.id }], + }, + }); + let oldContacts: ContactInterface[] = []; + const currentTime = new Date().getTime(); + contacts.forEach((contact) => { + if (!contact.createdAt) + throw new Error("Creation time not set for contact."); + const contactAge = currentTime - contact.createdAt.getTime(); + if (contactAge > 60 * 60 * 24 * 14 * 10000) { + oldContacts.push(contact); + } + }); + oldContacts.forEach(async (contact) => { + await contact.destroy(); + }); +} + +export async function addContact( + userATelegram: TelegramID, + userBTelegram: TelegramID +): Promise { + const userA = await getUserByTelegramID(userATelegram); + const userB = await getUserByTelegramID(userBTelegram); + + if (!userA || !userB) { + throw new Error("Could not found users"); + } + + await purgeOldContacts(userATelegram); + await purgeOldContacts(userBTelegram); + + await Contact.create({ user: userA.id, with: userB.id }); + await sendTelegramMessage(userB.telegram, strings_en.telegram_qr_scanned); +} diff --git a/src/db/models/Contact.ts b/src/db/models/Contact.ts index c009cca..ad2416b 100644 --- a/src/db/models/Contact.ts +++ b/src/db/models/Contact.ts @@ -5,6 +5,8 @@ import { sequelize } from "../db"; interface ContactAttributes { user: UserRowID; with: UserRowID; + createdAt?: Date; + id?: Number; } export interface ContactInterface extends Model, diff --git a/src/db/models/User.helper.ts b/src/db/models/User.helper.ts index 9a1df10..a88d158 100644 --- a/src/db/models/User.helper.ts +++ b/src/db/models/User.helper.ts @@ -1,6 +1,15 @@ import { TelegramID, UserRowID, VerificationString } from "../../types"; import { User, UserInstance } from "./User"; +export async function createUser( + telegram: TelegramID +): Promise { + const user = await User.create({ + telegram: telegram, + }); + return user; +} + export async function getUserByTelegramID( telegramID: TelegramID ): Promise { diff --git a/src/db/utils.ts b/src/db/utils.ts deleted file mode 100644 index 3379e82..0000000 --- a/src/db/utils.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { strings_en } from "../strings"; -import { sendTelegramMessage } from "../telegram"; -import { TelegramID } from "../types"; -import { Contact } from "./models/Contact"; -import { User, UserInstance } from "./models/User"; -import { getUserByTelegramID } from "./models/User.helper"; - -export async function addContact( - userATelegram: TelegramID, - userBTelegram: TelegramID -): Promise { - const userA = await getUserByTelegramID(userATelegram); - const userB = await getUserByTelegramID(userBTelegram); - - if (!userA || !userB) { - throw new Error("Could not found users"); - } - - await Contact.create({ user: userA.id, with: userB.id }); - await sendTelegramMessage(userB.telegram, strings_en.telegram_qr_scanned); -} - -export async function createUser( - telegram: TelegramID -): Promise { - const user = await User.create({ - telegram: telegram, - }); - return user; -} diff --git a/src/routes/CodeRoute.ts b/src/routes/CodeRoute.ts index c06babb..d49a55d 100644 --- a/src/routes/CodeRoute.ts +++ b/src/routes/CodeRoute.ts @@ -1,9 +1,9 @@ -import { Request, Response } from "express"; import bcrypt from "bcrypt"; +import { Request, Response } from "express"; import QRCode, { QRCodeToDataURLOptions } from "qrcode"; -import { TelegramID, VerificationString } from "../types"; -import { User, UserInstance } from "../db/models/User"; +import { UserInstance } from "../db/models/User"; import { getUserByTelegramID } from "../db/models/User.helper"; +import { TelegramID, VerificationString } from "../types"; export async function CodeRoute(req: Request, res: Response) { if (!req.session.userTelegramID) { diff --git a/src/routes/LoginRoute.ts b/src/routes/LoginRoute.ts index c940910..020aa79 100644 --- a/src/routes/LoginRoute.ts +++ b/src/routes/LoginRoute.ts @@ -1,9 +1,8 @@ -import { Request, Response } from "express"; import crypto from "crypto"; -import { addContact, createUser } from "../db/utils"; -import { TelegramID, UserRowID } from "../types"; -import { User } from "../db/models/User"; -import { getUserByTelegramID } from "../db/models/User.helper"; +import { Request, Response } from "express"; +import { addContact } from "../db/models/Contact.helper"; +import { createUser, getUserByTelegramID } from "../db/models/User.helper"; +import { TelegramID } from "../types"; type TelegramLoginResponse = { id: TelegramID; diff --git a/src/routes/VerifyRoute.ts b/src/routes/VerifyRoute.ts index 0197ab1..8ca693f 100644 --- a/src/routes/VerifyRoute.ts +++ b/src/routes/VerifyRoute.ts @@ -1,8 +1,7 @@ import { Request, Response } from "express"; -import { User } from "../db/models/User"; +import { addContact } from "../db/models/Contact.helper"; import { getUserByVerification } from "../db/models/User.helper"; -import { addContact } from "../db/utils"; -import { UserRowID, VerificationString } from "../types"; +import { VerificationString } from "../types"; interface VerifyRequest extends Request { body: { @@ -14,7 +13,7 @@ export async function VerifyRoute(req: VerifyRequest, res: Response) { const verifiedByUser = await getUserByVerification( decodeURIComponent(req.body.id) as VerificationString ); - try{ + try { if (!!verifiedByUser) { req.session.isVerified = !!verifiedByUser; req.session.verifiedByTelegramID = verifiedByUser.telegram; @@ -32,7 +31,7 @@ export async function VerifyRoute(req: VerifyRequest, res: Response) { } else { res.status(400).send({ success: false }); } - }catch(e){ - res.status(500).send({error: e instanceof Error ? e.message : "Error"}); + } catch (e) { + res.status(500).send({ error: e instanceof Error ? e.message : "Error" }); } } diff --git a/src/telegram.ts b/src/telegram.ts index 518cbb4..63dfbe7 100644 --- a/src/telegram.ts +++ b/src/telegram.ts @@ -45,10 +45,7 @@ export async function informContacts(telegramID: TelegramID): Promise { }); } - -setTelegramWebHook() -.catch(error=>{ +setTelegramWebHook().catch((error) => { console.error("Error setting Telegram Webhook"); error instanceof Error && console.error(error.message); }); -