Nareshkumar Rao
3 years ago
8 changed files with 205 additions and 294 deletions
@ -1,80 +1,60 @@ |
|||
import { TelegramID, UserRowID, VerificationString } from "../../types"; |
|||
import { User, UserInstance } from "./User"; |
|||
|
|||
export function getUserByTelegramID( |
|||
telegramID: TelegramID, |
|||
callback: (user?: UserInstance, message?: string) => void |
|||
): void { |
|||
User.findOne({ |
|||
export async function getUserByTelegramID( |
|||
telegramID: TelegramID |
|||
): Promise<UserInstance | null> { |
|||
const user = await User.findOne({ |
|||
where: { |
|||
telegram: telegramID, |
|||
}, |
|||
}) |
|||
.then((result) => { |
|||
callback(!!result ? result : undefined); |
|||
}) |
|||
.catch(() => { |
|||
callback(undefined); |
|||
}); |
|||
}); |
|||
return user; |
|||
} |
|||
|
|||
export function getUserByRowID( |
|||
rowID: UserRowID, |
|||
callback: (user?: UserInstance, message?: string) => void |
|||
): void { |
|||
User.findOne({ |
|||
export async function getUserByRowID( |
|||
rowID: UserRowID |
|||
): Promise<UserInstance | null> { |
|||
const user = await User.findOne({ |
|||
where: { |
|||
id: rowID, |
|||
}, |
|||
}) |
|||
.then((result) => { |
|||
callback(!!result ? result : undefined); |
|||
}) |
|||
.catch(() => { |
|||
callback(undefined); |
|||
}); |
|||
}); |
|||
return user; |
|||
} |
|||
|
|||
export function getUserByVerification( |
|||
verification: VerificationString, |
|||
callback: (user?: UserInstance, message?: string) => void |
|||
): void { |
|||
User.findOne({ |
|||
export async function getUserByVerification( |
|||
verification: VerificationString |
|||
): Promise<UserInstance | null> { |
|||
const user = await User.findOne({ |
|||
where: { |
|||
verification: verification, |
|||
}, |
|||
}) |
|||
.then((result) => { |
|||
callback(!!result ? result : undefined); |
|||
}) |
|||
.catch(() => { |
|||
callback(undefined); |
|||
}); |
|||
}); |
|||
return user; |
|||
} |
|||
|
|||
export function getUserCovidPositivity(telegramID: TelegramID, callback: (isInfected?: boolean) => void): void { |
|||
getUserByTelegramID(telegramID, user => { |
|||
if (!!user) { |
|||
const infectionDuration = +user.infectionDate - Date.now(); |
|||
if (infectionDuration > 60 * 60 * 24 * 14) { |
|||
setUserCovidPositivity(telegramID, false, success => { |
|||
callback(success ? false : undefined); |
|||
}); |
|||
} else { |
|||
callback(user.isInfected); |
|||
} |
|||
} else { |
|||
callback(); |
|||
} |
|||
}); |
|||
export async function getUserCovidPositivity( |
|||
telegramID: TelegramID |
|||
): Promise<boolean> { |
|||
const user = await getUserByTelegramID(telegramID); |
|||
if (!user) throw new Error("User not found"); |
|||
const infectionDuration = +user.infectionDate - Date.now(); |
|||
if (infectionDuration > 60 * 60 * 24 * 14) { |
|||
await setUserCovidPositivity(telegramID, false); |
|||
return false; |
|||
} else { |
|||
return user.isInfected; |
|||
} |
|||
} |
|||
|
|||
export function setUserCovidPositivity(telegramID: TelegramID, infectionState: boolean, callback: (success: boolean) => void): void { |
|||
getUserByTelegramID(telegramID, user => { |
|||
if (!!user) { |
|||
user.isInfected = infectionState; |
|||
user.infectionDate = new Date(); |
|||
user.save().then(() => callback(true)).catch(() => callback(false)); |
|||
} else { callback(false) } |
|||
}); |
|||
export async function setUserCovidPositivity( |
|||
telegramID: TelegramID, |
|||
infectionState: boolean |
|||
): Promise<void> { |
|||
const user = await getUserByTelegramID(telegramID); |
|||
if (!user) throw new Error("User not found"); |
|||
user.isInfected = infectionState; |
|||
user.infectionDate = new Date(); |
|||
if (!(await user.save())) throw new Error("Could not save user state"); |
|||
} |
|||
|
@ -1,56 +1,30 @@ |
|||
import { strings_en } from "../strings"; |
|||
import { sendTelegramMessage } from "../telegram"; |
|||
import { TelegramID, UserRowID } from "../types"; |
|||
import { TelegramID } from "../types"; |
|||
import { Contact } from "./models/Contact"; |
|||
import { User } from "./models/User"; |
|||
import { getUserByRowID, getUserByTelegramID } from "./models/User.helper"; |
|||
import { User, UserInstance } from "./models/User"; |
|||
import { getUserByTelegramID } from "./models/User.helper"; |
|||
|
|||
export function addContact( |
|||
export async function addContact( |
|||
userATelegram: TelegramID, |
|||
userBTelegram: TelegramID, |
|||
callback: (success: boolean, message?: string) => void |
|||
): void { |
|||
getUserByTelegramID(userATelegram, (userA) => { |
|||
getUserByTelegramID(userBTelegram, (userB) => { |
|||
if (!userA || !userB) { |
|||
callback(false, "Could not find user."); |
|||
return; |
|||
} |
|||
userBTelegram: TelegramID |
|||
): Promise<void> { |
|||
const userA = await getUserByTelegramID(userATelegram); |
|||
const userB = await getUserByTelegramID(userBTelegram); |
|||
|
|||
Contact.create({ user: userA.id, with: userB.id }) |
|||
.then(() => { |
|||
console.log( |
|||
`Registering contact between ${userA.id} and ${userB.id}` |
|||
); |
|||
sendTelegramMessage(userB.telegram, strings_en.telegram_qr_scanned); |
|||
callback(true, "Successfully added contact"); |
|||
}) |
|||
.catch((e) => { |
|||
callback(false, e); |
|||
}); |
|||
}); |
|||
}); |
|||
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 function createUser( |
|||
telegram: TelegramID, |
|||
callback: (success: boolean, message: string) => void |
|||
): void { |
|||
User.create({ |
|||
export async function createUser( |
|||
telegram: TelegramID |
|||
): Promise<UserInstance | null> { |
|||
const user = await User.create({ |
|||
telegram: telegram, |
|||
}) |
|||
.then((user) => { |
|||
if (!user) { |
|||
callback(false, "Could not create user"); |
|||
} else { |
|||
callback(true, "Success"); |
|||
} |
|||
}) |
|||
.catch((reason) => { |
|||
if (reason.name == "SequelizeUniqueConstraintError") { |
|||
callback(false, "User already exists"); |
|||
} else { |
|||
callback(false, "Unknown error"); |
|||
} |
|||
}); |
|||
}); |
|||
return user; |
|||
} |
|||
|
@ -1,42 +1,24 @@ |
|||
import axios from "axios"; |
|||
import { TelegramID } from "./types"; |
|||
|
|||
export function setTelegramWebHook( |
|||
callback: (success: boolean) => void = () => {} |
|||
): void { |
|||
export async function setTelegramWebHook(): Promise<void> { |
|||
const url = `https://api.telegram.org/bot${process.env.TELEGRAM_TOKEN}/setWebhook`; |
|||
axios |
|||
.post(url, { |
|||
url: `${process.env.SERVER_API_URL}/${process.env.TELEGRAM_SECRET}`, |
|||
allowed_updates: [], |
|||
drop_pending_updates: true, |
|||
}) |
|||
.then((res) => { |
|||
callback(!!res); |
|||
}) |
|||
.catch((err) => { |
|||
callback(!!err); |
|||
}); |
|||
await axios.post(url, { |
|||
url: `${process.env.SERVER_API_URL}/${process.env.TELEGRAM_SECRET}`, |
|||
allowed_updates: [], |
|||
drop_pending_updates: true, |
|||
}); |
|||
} |
|||
|
|||
export function sendTelegramMessage( |
|||
export async function sendTelegramMessage( |
|||
telegramID: TelegramID, |
|||
message: string, |
|||
callback: (success: boolean) => void = () => {} |
|||
): void { |
|||
message: string |
|||
): Promise<void> { |
|||
const url = `https://api.telegram.org/bot${process.env.TELEGRAM_TOKEN}/sendMessage`; |
|||
axios |
|||
.post(url, { |
|||
chat_id: telegramID, |
|||
text: message, |
|||
}) |
|||
.then((res) => { |
|||
callback(!!res); |
|||
}) |
|||
.catch((err) => { |
|||
console.error("Problem sending Telegram message."); |
|||
callback(!!err); |
|||
}); |
|||
const response = await axios.post(url, { |
|||
chat_id: telegramID, |
|||
text: message, |
|||
}); |
|||
} |
|||
|
|||
setTelegramWebHook(); |
|||
|
Loading…
Reference in new issue