2024-02-04 04:18:54 +08:00
|
|
|
from tortoise import fields
|
2021-05-20 18:37:51 +08:00
|
|
|
|
2024-02-04 04:18:54 +08:00
|
|
|
from zhenxun.configs.config import Config
|
|
|
|
|
from zhenxun.services.db_context import Model
|
2021-05-20 18:37:51 +08:00
|
|
|
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
|
|
|
class FriendUser(Model):
|
2023-02-18 18:46:54 +08:00
|
|
|
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
|
|
|
|
"""自增id"""
|
2024-02-04 04:18:54 +08:00
|
|
|
user_id = fields.CharField(255, unique=True, description="用户id")
|
2023-02-18 18:46:54 +08:00
|
|
|
"""用户id"""
|
2024-02-04 04:18:54 +08:00
|
|
|
user_name = fields.CharField(max_length=255, default="", description="用户名称")
|
2023-02-18 18:46:54 +08:00
|
|
|
"""用户名称"""
|
2024-02-04 04:18:54 +08:00
|
|
|
nickname = fields.CharField(max_length=255, null=True, description="用户自定义昵称")
|
2023-02-18 18:46:54 +08:00
|
|
|
"""私聊下自定义昵称"""
|
2024-02-25 03:18:34 +08:00
|
|
|
platform = fields.CharField(255, null=True, description="平台")
|
|
|
|
|
"""平台"""
|
2021-05-20 18:37:51 +08:00
|
|
|
|
2025-01-07 14:36:22 +08:00
|
|
|
class Meta: # pyright: ignore [reportIncompatibleVariableOverride]
|
2023-02-18 18:46:54 +08:00
|
|
|
table = "friend_users"
|
|
|
|
|
table_description = "好友信息数据表"
|
2021-05-20 18:37:51 +08:00
|
|
|
|
|
|
|
|
@classmethod
|
2024-02-25 03:18:34 +08:00
|
|
|
async def get_user_name(cls, user_id: str) -> str:
|
|
|
|
|
"""获取好友用户名称
|
|
|
|
|
|
2022-07-23 14:09:17 +08:00
|
|
|
参数:
|
2024-02-25 03:18:34 +08:00
|
|
|
user_id: 用户id
|
2021-07-30 21:21:51 +08:00
|
|
|
"""
|
2024-02-25 03:18:34 +08:00
|
|
|
if user := await cls.get_or_none(user_id=user_id):
|
2021-05-20 18:37:51 +08:00
|
|
|
return user.user_name
|
2023-02-18 18:46:54 +08:00
|
|
|
return ""
|
2021-05-20 18:37:51 +08:00
|
|
|
|
|
|
|
|
@classmethod
|
2024-02-25 03:18:34 +08:00
|
|
|
async def get_user_nickname(cls, user_id: str) -> str:
|
|
|
|
|
"""获取用户昵称
|
|
|
|
|
|
2022-07-23 14:09:17 +08:00
|
|
|
参数:
|
2024-02-25 03:18:34 +08:00
|
|
|
user_id: 用户id
|
2021-07-30 21:21:51 +08:00
|
|
|
"""
|
2024-02-25 03:18:34 +08:00
|
|
|
if user := await cls.get_or_none(user_id=user_id):
|
2021-05-20 18:37:51 +08:00
|
|
|
if user.nickname:
|
2021-12-01 14:03:34 +08:00
|
|
|
_tmp = ""
|
2023-02-18 18:46:54 +08:00
|
|
|
if black_word := Config.get_config("nickname", "BLACK_WORD"):
|
2021-12-16 11:16:28 +08:00
|
|
|
for x in user.nickname:
|
|
|
|
|
_tmp += "*" if x in black_word else x
|
2021-12-01 14:03:34 +08:00
|
|
|
return _tmp
|
2021-07-30 21:21:51 +08:00
|
|
|
return ""
|
2021-05-20 18:37:51 +08:00
|
|
|
|
|
|
|
|
@classmethod
|
2024-02-25 03:18:34 +08:00
|
|
|
async def set_user_nickname(
|
|
|
|
|
cls,
|
|
|
|
|
user_id: str,
|
|
|
|
|
nickname: str,
|
|
|
|
|
uname: str | None = None,
|
|
|
|
|
platform: str | None = None,
|
|
|
|
|
):
|
|
|
|
|
"""设置用户昵称
|
|
|
|
|
|
2022-07-23 14:09:17 +08:00
|
|
|
参数:
|
2024-02-25 03:18:34 +08:00
|
|
|
user_id: 用户id
|
|
|
|
|
nickname: 昵称
|
|
|
|
|
uname: 用户昵称
|
|
|
|
|
platform: 平台
|
2021-07-30 21:21:51 +08:00
|
|
|
"""
|
2024-02-25 03:18:34 +08:00
|
|
|
defaults = {"nickname": nickname}
|
|
|
|
|
if uname is not None:
|
|
|
|
|
defaults["user_name"] = uname
|
|
|
|
|
if platform is not None:
|
|
|
|
|
defaults["platform"] = platform
|
2024-02-04 04:18:54 +08:00
|
|
|
await cls.update_or_create(
|
2024-02-25 03:18:34 +08:00
|
|
|
user_id=user_id,
|
|
|
|
|
defaults=defaults,
|
2024-02-04 04:18:54 +08:00
|
|
|
)
|
2023-04-14 11:45:45 +08:00
|
|
|
|
|
|
|
|
@classmethod
|
2024-02-25 03:18:34 +08:00
|
|
|
def _run_script(cls):
|
|
|
|
|
return [
|
2024-12-10 19:49:11 +08:00
|
|
|
"ALTER TABLE friend_users "
|
|
|
|
|
"ALTER COLUMN user_id TYPE character varying(255);",
|
|
|
|
|
"ALTER TABLE friend_users "
|
|
|
|
|
"ADD COLUMN platform character varying(255) default 'qq';",
|
2024-02-25 03:18:34 +08:00
|
|
|
]
|