mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-13 03:40:48 +00:00
Support replies on reply (#6327)
Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
This commit is contained in:
parent
1cb4c86a19
commit
cc7240c7d9
@ -24,6 +24,7 @@ import { TextMessage } from '@telegraf/entity/types/types'
|
||||
import config from './config'
|
||||
import { PlatformWorker } from './worker'
|
||||
import { getBotCommands, getCommandsHelp } from './utils'
|
||||
import { NotificationRecord } from './types'
|
||||
|
||||
async function onStart (ctx: Context, worker: PlatformWorker): Promise<void> {
|
||||
const id = ctx.from?.id
|
||||
@ -94,14 +95,36 @@ async function onConnect (ctx: Context, worker: PlatformWorker): Promise<void> {
|
||||
await ctx.reply(`*${code}*`, { parse_mode: 'MarkdownV2' })
|
||||
}
|
||||
|
||||
async function findNotificationRecord (
|
||||
worker: PlatformWorker,
|
||||
from: number,
|
||||
replyTo: number,
|
||||
email: string
|
||||
): Promise<NotificationRecord | undefined> {
|
||||
const record = await worker.getNotificationRecord(replyTo, email)
|
||||
|
||||
if (record !== undefined) {
|
||||
return record
|
||||
}
|
||||
|
||||
const reply = await worker.getReply(from, replyTo)
|
||||
|
||||
if (reply === undefined) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
return await worker.getNotificationRecordById(reply.notificationId, email)
|
||||
}
|
||||
|
||||
async function onReply (
|
||||
id: number,
|
||||
from: number,
|
||||
message: TextMessage,
|
||||
messageId: number,
|
||||
replyTo: number,
|
||||
worker: PlatformWorker,
|
||||
username?: string
|
||||
): Promise<boolean> {
|
||||
const userRecord = await worker.getUserRecord(id)
|
||||
const userRecord = await worker.getUserRecord(from)
|
||||
|
||||
if (userRecord === undefined) {
|
||||
return false
|
||||
@ -111,12 +134,14 @@ async function onReply (
|
||||
await worker.updateTelegramUsername(userRecord, username)
|
||||
}
|
||||
|
||||
const notification = await worker.getNotificationRecord(replyTo, userRecord.email)
|
||||
const notification = await findNotificationRecord(worker, from, replyTo, userRecord.email)
|
||||
|
||||
if (notification === undefined) {
|
||||
return false
|
||||
}
|
||||
|
||||
await worker.saveReply({ replyId: messageId, telegramId: from, notificationId: notification.notificationId })
|
||||
|
||||
return await worker.reply(notification, htmlToMarkup(toHTML(message)))
|
||||
}
|
||||
|
||||
@ -140,7 +165,14 @@ export async function setUpBot (worker: PlatformWorker): Promise<Telegraf> {
|
||||
}
|
||||
|
||||
const replyTo = message.reply_to_message
|
||||
const isReplied = await onReply(id, message as TextMessage, replyTo.message_id, worker, ctx.from.username)
|
||||
const isReplied = await onReply(
|
||||
id,
|
||||
message as TextMessage,
|
||||
message.message_id,
|
||||
replyTo.message_id,
|
||||
worker,
|
||||
ctx.from.username
|
||||
)
|
||||
|
||||
if (isReplied) {
|
||||
await ctx.react('👍')
|
||||
|
@ -29,6 +29,12 @@ export interface NotificationRecord {
|
||||
telegramId: number
|
||||
}
|
||||
|
||||
export interface ReplyRecord {
|
||||
notificationId: Ref<InboxNotification>
|
||||
telegramId: number
|
||||
replyId: number
|
||||
}
|
||||
|
||||
export interface OtpRecord {
|
||||
telegramId: number
|
||||
telegramUsername?: string
|
||||
|
@ -15,8 +15,9 @@
|
||||
|
||||
import type { Collection } from 'mongodb'
|
||||
import { Account, Ref, SortingOrder } from '@hcengineering/core'
|
||||
import { InboxNotification } from '@hcengineering/notification'
|
||||
|
||||
import { UserRecord, NotificationRecord, OtpRecord } from './types'
|
||||
import { UserRecord, NotificationRecord, OtpRecord, ReplyRecord } from './types'
|
||||
import { getDB } from './storage'
|
||||
import { WorkspaceClient } from './workspace'
|
||||
import { getNewOtp } from './utils'
|
||||
@ -32,7 +33,8 @@ export class PlatformWorker {
|
||||
private constructor (
|
||||
private readonly usersStorage: Collection<UserRecord>,
|
||||
private readonly notificationsStorage: Collection<NotificationRecord>,
|
||||
private readonly otpStorage: Collection<OtpRecord>
|
||||
private readonly otpStorage: Collection<OtpRecord>,
|
||||
private readonly repliesStorage: Collection<ReplyRecord>
|
||||
) {
|
||||
this.intervalId = setInterval(
|
||||
() => {
|
||||
@ -103,10 +105,25 @@ export class PlatformWorker {
|
||||
await this.usersStorage.deleteOne({ account: _id })
|
||||
}
|
||||
|
||||
async saveReply (record: ReplyRecord): Promise<void> {
|
||||
await this.repliesStorage.insertOne(record)
|
||||
}
|
||||
|
||||
async getReply (id: number, replyTo: number): Promise<ReplyRecord | undefined> {
|
||||
return (await this.repliesStorage.findOne({ telegramId: id, replyId: replyTo })) ?? undefined
|
||||
}
|
||||
|
||||
async getNotificationRecord (id: number, email: string): Promise<NotificationRecord | undefined> {
|
||||
return (await this.notificationsStorage.findOne({ telegramId: id, email })) ?? undefined
|
||||
}
|
||||
|
||||
async getNotificationRecordById (
|
||||
notificationId: Ref<InboxNotification>,
|
||||
email: string
|
||||
): Promise<NotificationRecord | undefined> {
|
||||
return (await this.notificationsStorage.findOne({ notificationId, email })) ?? undefined
|
||||
}
|
||||
|
||||
async getUserRecord (id: number): Promise<UserRecord | undefined> {
|
||||
return (await this.usersStorage.findOne({ telegramId: id })) ?? undefined
|
||||
}
|
||||
@ -177,19 +194,20 @@ export class PlatformWorker {
|
||||
}
|
||||
|
||||
static async createStorages (): Promise<
|
||||
[Collection<UserRecord>, Collection<NotificationRecord>, Collection<OtpRecord>]
|
||||
[Collection<UserRecord>, Collection<NotificationRecord>, Collection<OtpRecord>, Collection<ReplyRecord>]
|
||||
> {
|
||||
const db = await getDB()
|
||||
const userStorage = db.collection<UserRecord>('users')
|
||||
const notificationsStorage = db.collection<NotificationRecord>('notifications')
|
||||
const otpStorage = db.collection<OtpRecord>('otp')
|
||||
const repliesStorage = db.collection<ReplyRecord>('replies')
|
||||
|
||||
return [userStorage, notificationsStorage, otpStorage]
|
||||
return [userStorage, notificationsStorage, otpStorage, repliesStorage]
|
||||
}
|
||||
|
||||
static async create (): Promise<PlatformWorker> {
|
||||
const [userStorage, notificationsStorage, otpStorage] = await PlatformWorker.createStorages()
|
||||
const [userStorage, notificationsStorage, otpStorage, repliesStorage] = await PlatformWorker.createStorages()
|
||||
|
||||
return new PlatformWorker(userStorage, notificationsStorage, otpStorage)
|
||||
return new PlatformWorker(userStorage, notificationsStorage, otpStorage, repliesStorage)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user