RoverSign 用户活跃度记录表 记录每个用户(user_id + bot_id + bot_self_id)的最后活跃时间 通过 hook 机制自动更新,用于判断用户活跃度
| 14 | |
| 15 | |
| 16 | class RoverUserActivity(BaseBotIDModel, table=True): |
| 17 | """RoverSign 用户活跃度记录表 |
| 18 | |
| 19 | 记录每个用户(user_id + bot_id + bot_self_id)的最后活跃时间 |
| 20 | 通过 hook 机制自动更新,用于判断用户活跃度 |
| 21 | """ |
| 22 | |
| 23 | __tablename__ = "RoverUserActivity" |
| 24 | __table_args__: Dict[str, Any] = {"extend_existing": True} |
| 25 | |
| 26 | user_id: str = Field(default="", title="用户ID") |
| 27 | bot_self_id: str = Field(default="", title="机器人自身ID") |
| 28 | last_active_time: Optional[int] = Field(default=None, title="最后活跃时间") |
| 29 | |
| 30 | @classmethod |
| 31 | async def update_user_activity( |
| 32 | cls: Type[T_RoverUserActivity], |
| 33 | user_id: str, |
| 34 | bot_id: str, |
| 35 | bot_self_id: str, |
| 36 | ) -> bool: |
| 37 | """更新用户活跃时间(带数据库错误保护)""" |
| 38 | try: |
| 39 | return await cls._do_update_user_activity(user_id, bot_id, bot_self_id) |
| 40 | except Exception as e: |
| 41 | if "malformed" in str(e) or "corrupt" in str(e): |
| 42 | logger.warning(f"[库洛签到·用户活跃度] 数据库损坏,跳过活跃度更新: {e}") |
| 43 | return False |
| 44 | raise |
| 45 | |
| 46 | @classmethod |
| 47 | @with_lock |
| 48 | @with_session |
| 49 | async def _do_update_user_activity( |
| 50 | cls: Type[T_RoverUserActivity], |
| 51 | session: AsyncSession, |
| 52 | user_id: str, |
| 53 | bot_id: str, |
| 54 | bot_self_id: str, |
| 55 | ) -> bool: |
| 56 | import time |
| 57 | |
| 58 | current_time = int(time.time()) |
| 59 | |
| 60 | sql = select(cls).where( |
| 61 | and_( |
| 62 | cls.user_id == user_id, |
| 63 | cls.bot_id == bot_id, |
| 64 | cls.bot_self_id == bot_self_id, |
| 65 | ) |
| 66 | ) |
| 67 | result = await session.execute(sql) |
| 68 | existing = result.scalars().first() |
| 69 | |
| 70 | if existing: |
| 71 | existing.last_active_time = current_time |
| 72 | session.add(existing) |
| 73 | else: |
nothing calls this directly
no outgoing calls
no test coverage detected