Browse Source

resolves #12. implements purging of old contacts

development
Nareshkumar Rao 3 years ago
parent
commit
3d9713c2b6
  1. 47
      src/db/models/Contact.helper.ts
  2. 2
      src/db/models/Contact.ts
  3. 9
      src/db/models/User.helper.ts
  4. 30
      src/db/utils.ts
  5. 6
      src/routes/CodeRoute.ts
  6. 9
      src/routes/LoginRoute.ts
  7. 5
      src/routes/VerifyRoute.ts
  8. 5
      src/telegram.ts

47
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<void> {
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<void> {
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);
}

2
src/db/models/Contact.ts

@ -5,6 +5,8 @@ import { sequelize } from "../db";
interface ContactAttributes { interface ContactAttributes {
user: UserRowID; user: UserRowID;
with: UserRowID; with: UserRowID;
createdAt?: Date;
id?: Number;
} }
export interface ContactInterface export interface ContactInterface
extends Model<ContactAttributes, ContactAttributes>, extends Model<ContactAttributes, ContactAttributes>,

9
src/db/models/User.helper.ts

@ -1,6 +1,15 @@
import { TelegramID, UserRowID, VerificationString } from "../../types"; import { TelegramID, UserRowID, VerificationString } from "../../types";
import { User, UserInstance } from "./User"; import { User, UserInstance } from "./User";
export async function createUser(
telegram: TelegramID
): Promise<UserInstance | null> {
const user = await User.create({
telegram: telegram,
});
return user;
}
export async function getUserByTelegramID( export async function getUserByTelegramID(
telegramID: TelegramID telegramID: TelegramID
): Promise<UserInstance | null> { ): Promise<UserInstance | null> {

30
src/db/utils.ts

@ -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<void> {
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<UserInstance | null> {
const user = await User.create({
telegram: telegram,
});
return user;
}

6
src/routes/CodeRoute.ts

@ -1,9 +1,9 @@
import { Request, Response } from "express";
import bcrypt from "bcrypt"; import bcrypt from "bcrypt";
import { Request, Response } from "express";
import QRCode, { QRCodeToDataURLOptions } from "qrcode"; 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 { getUserByTelegramID } from "../db/models/User.helper";
import { TelegramID, VerificationString } from "../types";
export async function CodeRoute(req: Request, res: Response) { export async function CodeRoute(req: Request, res: Response) {
if (!req.session.userTelegramID) { if (!req.session.userTelegramID) {

9
src/routes/LoginRoute.ts

@ -1,9 +1,8 @@
import { Request, Response } from "express";
import crypto from "crypto"; 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 = { type TelegramLoginResponse = {
id: TelegramID; id: TelegramID;

5
src/routes/VerifyRoute.ts

@ -1,8 +1,7 @@
import { Request, Response } from "express"; 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 { getUserByVerification } from "../db/models/User.helper";
import { addContact } from "../db/utils";
import { UserRowID, VerificationString } from "../types";
import { VerificationString } from "../types";
interface VerifyRequest extends Request { interface VerifyRequest extends Request {
body: { body: {

5
src/telegram.ts

@ -45,10 +45,7 @@ export async function informContacts(telegramID: TelegramID): Promise<void> {
}); });
} }
setTelegramWebHook()
.catch(error=>{
setTelegramWebHook().catch((error) => {
console.error("Error setting Telegram Webhook"); console.error("Error setting Telegram Webhook");
error instanceof Error && console.error(error.message); error instanceof Error && console.error(error.message);
}); });

Loading…
Cancel
Save