46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
from typing import Dict, List, Optional
|
|
|
|
from db.base import BaseDBOperator
|
|
from db.connection import DBConnectionManager
|
|
|
|
|
|
class EmojiAssetDB(BaseDBOperator):
|
|
"""表情资产查询。
|
|
|
|
说明:
|
|
1. 这里单独抽出查询类,避免自动回复插件为了拿表情库再去依赖后台蓝图;
|
|
2. 查询只关心消息表里的原始表情记录,不负责语义解析和匹配打分;
|
|
3. 后续无论后台页面、自动回复还是其他插件,都可以复用同一份表情资产数据源。
|
|
"""
|
|
|
|
def __init__(self, db_manager: DBConnectionManager):
|
|
super().__init__(db_manager)
|
|
|
|
def get_recent_emoji_assets(self, limit: int = 500) -> List[Dict]:
|
|
"""获取近期表情消息记录。"""
|
|
sql = """
|
|
SELECT message_id, group_id, sender, timestamp, message_type, attachment_url, image_path
|
|
FROM messages
|
|
WHERE message_type IN ('47', '1048625', '1090519089')
|
|
AND attachment_url IS NOT NULL
|
|
AND attachment_url <> ''
|
|
ORDER BY timestamp DESC
|
|
LIMIT %s
|
|
"""
|
|
return self.execute_query(sql, (limit,)) or []
|
|
|
|
def get_emoji_asset_by_md5(self, md5: str) -> Optional[Dict]:
|
|
"""根据 md5 获取最近一条表情记录。"""
|
|
sql = """
|
|
SELECT message_id, group_id, sender, timestamp, message_type, attachment_url, image_path
|
|
FROM messages
|
|
WHERE message_type IN ('47', '1048625', '1090519089')
|
|
AND attachment_url IS NOT NULL
|
|
AND attachment_url <> ''
|
|
AND attachment_url LIKE %s
|
|
ORDER BY timestamp DESC
|
|
LIMIT 1
|
|
"""
|
|
return self.execute_query(sql, (f'%md5="{md5}"%',), fetch_one=True)
|