接入tgbot
This commit is contained in:
+134
-2
@@ -26,7 +26,7 @@ import {
|
||||
MusicV2PlaylistRecord,
|
||||
} from './music-v2';
|
||||
import { userInfoCache } from './user-cache';
|
||||
import { dispatchWebPushNotification } from './web-push';
|
||||
import { dispatchNotificationChannels } from './notification-dispatch';
|
||||
|
||||
/**
|
||||
* Cloudflare D1 存储实现
|
||||
@@ -3083,7 +3083,7 @@ export class D1Storage implements IStorage {
|
||||
)
|
||||
.run();
|
||||
|
||||
await dispatchWebPushNotification(this, userName, notification);
|
||||
await dispatchNotificationChannels(this, userName, notification);
|
||||
} catch (err) {
|
||||
console.error('D1Storage.addNotification error:', err);
|
||||
throw err;
|
||||
@@ -3473,6 +3473,138 @@ export class D1Storage implements IStorage {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private mapTelegramBinding(row: any): import('./types').TelegramBindingRecord {
|
||||
return {
|
||||
username: row.username as string,
|
||||
telegramUserId: String(row.telegram_user_id),
|
||||
chatId: String(row.chat_id),
|
||||
telegramUsername: (row.telegram_username as string | null) || null,
|
||||
firstName: (row.first_name as string | null) || null,
|
||||
lastName: (row.last_name as string | null) || null,
|
||||
notificationsEnabled: row.notifications_enabled === 1,
|
||||
boundAt: Number(row.bound_at),
|
||||
updatedAt: Number(row.updated_at),
|
||||
};
|
||||
}
|
||||
|
||||
async getTelegramBinding(userName: string): Promise<import('./types').TelegramBindingRecord | null> {
|
||||
try {
|
||||
const row = await this.db
|
||||
.prepare('SELECT * FROM telegram_bindings WHERE username = ?')
|
||||
.bind(userName)
|
||||
.first();
|
||||
return row ? this.mapTelegramBinding(row) : null;
|
||||
} catch (err) {
|
||||
console.error('D1Storage.getTelegramBinding error:', err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async getTelegramBindingByTelegramUserId(telegramUserId: string): Promise<import('./types').TelegramBindingRecord | null> {
|
||||
try {
|
||||
const row = await this.db
|
||||
.prepare('SELECT * FROM telegram_bindings WHERE telegram_user_id = ?')
|
||||
.bind(telegramUserId)
|
||||
.first();
|
||||
return row ? this.mapTelegramBinding(row) : null;
|
||||
} catch (err) {
|
||||
console.error('D1Storage.getTelegramBindingByTelegramUserId error:', err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async upsertTelegramBinding(binding: import('./types').TelegramBindingRecord): Promise<void> {
|
||||
try {
|
||||
await this.db
|
||||
.prepare(`
|
||||
INSERT INTO telegram_bindings (
|
||||
username, telegram_user_id, chat_id, telegram_username, first_name, last_name,
|
||||
notifications_enabled, bound_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(username) DO UPDATE SET
|
||||
telegram_user_id = excluded.telegram_user_id,
|
||||
chat_id = excluded.chat_id,
|
||||
telegram_username = excluded.telegram_username,
|
||||
first_name = excluded.first_name,
|
||||
last_name = excluded.last_name,
|
||||
notifications_enabled = excluded.notifications_enabled,
|
||||
bound_at = excluded.bound_at,
|
||||
updated_at = excluded.updated_at
|
||||
`)
|
||||
.bind(
|
||||
binding.username,
|
||||
binding.telegramUserId,
|
||||
binding.chatId,
|
||||
binding.telegramUsername || null,
|
||||
binding.firstName || null,
|
||||
binding.lastName || null,
|
||||
binding.notificationsEnabled ? 1 : 0,
|
||||
binding.boundAt,
|
||||
binding.updatedAt
|
||||
)
|
||||
.run();
|
||||
} catch (err) {
|
||||
console.error('D1Storage.upsertTelegramBinding error:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteTelegramBindingByUsername(userName: string): Promise<void> {
|
||||
await this.db.prepare('DELETE FROM telegram_bindings WHERE username = ?').bind(userName).run();
|
||||
}
|
||||
|
||||
async deleteTelegramBindingByTelegramUserId(telegramUserId: string): Promise<void> {
|
||||
await this.db.prepare('DELETE FROM telegram_bindings WHERE telegram_user_id = ?').bind(telegramUserId).run();
|
||||
}
|
||||
|
||||
async getTelegramBindSession(code: string): Promise<import('./types').TelegramBindSessionRecord | null> {
|
||||
try {
|
||||
const row = await this.db
|
||||
.prepare('SELECT * FROM telegram_bind_sessions WHERE code = ?')
|
||||
.bind(code)
|
||||
.first();
|
||||
if (!row) return null;
|
||||
return {
|
||||
code: row.code as string,
|
||||
username: row.username as string,
|
||||
createdAt: Number(row.created_at),
|
||||
expiresAt: Number(row.expires_at),
|
||||
used: row.used === 1,
|
||||
};
|
||||
} catch (err) {
|
||||
console.error('D1Storage.getTelegramBindSession error:', err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async upsertTelegramBindSession(session: import('./types').TelegramBindSessionRecord): Promise<void> {
|
||||
try {
|
||||
await this.db
|
||||
.prepare(`
|
||||
INSERT INTO telegram_bind_sessions (code, username, created_at, expires_at, used)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT(code) DO UPDATE SET
|
||||
username = excluded.username,
|
||||
created_at = excluded.created_at,
|
||||
expires_at = excluded.expires_at,
|
||||
used = excluded.used
|
||||
`)
|
||||
.bind(session.code, session.username, session.createdAt, session.expiresAt, session.used ? 1 : 0)
|
||||
.run();
|
||||
} catch (err) {
|
||||
console.error('D1Storage.upsertTelegramBindSession error:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
async markTelegramBindSessionUsed(code: string): Promise<void> {
|
||||
await this.db
|
||||
.prepare('UPDATE telegram_bind_sessions SET used = 1 WHERE code = ?')
|
||||
.bind(code)
|
||||
.run();
|
||||
}
|
||||
|
||||
async getLastFavoriteCheckTime(userName: string): Promise<number> {
|
||||
try {
|
||||
const result = await this.db
|
||||
|
||||
Reference in New Issue
Block a user