mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-23 16:56:07 +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 config from './config'
|
||||||
import { PlatformWorker } from './worker'
|
import { PlatformWorker } from './worker'
|
||||||
import { getBotCommands, getCommandsHelp } from './utils'
|
import { getBotCommands, getCommandsHelp } from './utils'
|
||||||
|
import { NotificationRecord } from './types'
|
||||||
|
|
||||||
async function onStart (ctx: Context, worker: PlatformWorker): Promise<void> {
|
async function onStart (ctx: Context, worker: PlatformWorker): Promise<void> {
|
||||||
const id = ctx.from?.id
|
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' })
|
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 (
|
async function onReply (
|
||||||
id: number,
|
from: number,
|
||||||
message: TextMessage,
|
message: TextMessage,
|
||||||
|
messageId: number,
|
||||||
replyTo: number,
|
replyTo: number,
|
||||||
worker: PlatformWorker,
|
worker: PlatformWorker,
|
||||||
username?: string
|
username?: string
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
const userRecord = await worker.getUserRecord(id)
|
const userRecord = await worker.getUserRecord(from)
|
||||||
|
|
||||||
if (userRecord === undefined) {
|
if (userRecord === undefined) {
|
||||||
return false
|
return false
|
||||||
@ -111,12 +134,14 @@ async function onReply (
|
|||||||
await worker.updateTelegramUsername(userRecord, username)
|
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) {
|
if (notification === undefined) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await worker.saveReply({ replyId: messageId, telegramId: from, notificationId: notification.notificationId })
|
||||||
|
|
||||||
return await worker.reply(notification, htmlToMarkup(toHTML(message)))
|
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 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) {
|
if (isReplied) {
|
||||||
await ctx.react('👍')
|
await ctx.react('👍')
|
||||||
|
@ -29,6 +29,12 @@ export interface NotificationRecord {
|
|||||||
telegramId: number
|
telegramId: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ReplyRecord {
|
||||||
|
notificationId: Ref<InboxNotification>
|
||||||
|
telegramId: number
|
||||||
|
replyId: number
|
||||||
|
}
|
||||||
|
|
||||||
export interface OtpRecord {
|
export interface OtpRecord {
|
||||||
telegramId: number
|
telegramId: number
|
||||||
telegramUsername?: string
|
telegramUsername?: string
|
||||||
|
@ -15,8 +15,9 @@
|
|||||||
|
|
||||||
import type { Collection } from 'mongodb'
|
import type { Collection } from 'mongodb'
|
||||||
import { Account, Ref, SortingOrder } from '@hcengineering/core'
|
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 { getDB } from './storage'
|
||||||
import { WorkspaceClient } from './workspace'
|
import { WorkspaceClient } from './workspace'
|
||||||
import { getNewOtp } from './utils'
|
import { getNewOtp } from './utils'
|
||||||
@ -32,7 +33,8 @@ export class PlatformWorker {
|
|||||||
private constructor (
|
private constructor (
|
||||||
private readonly usersStorage: Collection<UserRecord>,
|
private readonly usersStorage: Collection<UserRecord>,
|
||||||
private readonly notificationsStorage: Collection<NotificationRecord>,
|
private readonly notificationsStorage: Collection<NotificationRecord>,
|
||||||
private readonly otpStorage: Collection<OtpRecord>
|
private readonly otpStorage: Collection<OtpRecord>,
|
||||||
|
private readonly repliesStorage: Collection<ReplyRecord>
|
||||||
) {
|
) {
|
||||||
this.intervalId = setInterval(
|
this.intervalId = setInterval(
|
||||||
() => {
|
() => {
|
||||||
@ -103,10 +105,25 @@ export class PlatformWorker {
|
|||||||
await this.usersStorage.deleteOne({ account: _id })
|
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> {
|
async getNotificationRecord (id: number, email: string): Promise<NotificationRecord | undefined> {
|
||||||
return (await this.notificationsStorage.findOne({ telegramId: id, email })) ?? 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> {
|
async getUserRecord (id: number): Promise<UserRecord | undefined> {
|
||||||
return (await this.usersStorage.findOne({ telegramId: id })) ?? undefined
|
return (await this.usersStorage.findOne({ telegramId: id })) ?? undefined
|
||||||
}
|
}
|
||||||
@ -177,19 +194,20 @@ export class PlatformWorker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async createStorages (): Promise<
|
static async createStorages (): Promise<
|
||||||
[Collection<UserRecord>, Collection<NotificationRecord>, Collection<OtpRecord>]
|
[Collection<UserRecord>, Collection<NotificationRecord>, Collection<OtpRecord>, Collection<ReplyRecord>]
|
||||||
> {
|
> {
|
||||||
const db = await getDB()
|
const db = await getDB()
|
||||||
const userStorage = db.collection<UserRecord>('users')
|
const userStorage = db.collection<UserRecord>('users')
|
||||||
const notificationsStorage = db.collection<NotificationRecord>('notifications')
|
const notificationsStorage = db.collection<NotificationRecord>('notifications')
|
||||||
const otpStorage = db.collection<OtpRecord>('otp')
|
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> {
|
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