Skip to content

Commit

Permalink
add nodeDisplayMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
MorvanZhou committed Oct 29, 2023
1 parent a95d351 commit fad1150
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/rethink/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Code(Enum):
CAPTCHA_ERROR = auto() # 15
CAPTCHA_EXPIRED = auto() # 16
NOTE_EXCEED_MAX_LENGTH = auto() # 17
INVALID_NODE_DISPLAY_METHOD = auto() # 18


@dataclass
Expand All @@ -57,6 +58,7 @@ class CodeMessage:
Code.CAPTCHA_ERROR: CodeMessage(zh="验证码输入错误", en="Captcha not match"),
Code.CAPTCHA_EXPIRED: CodeMessage(zh="验证码已过期", en="Captcha expired"),
Code.NOTE_EXCEED_MAX_LENGTH: CodeMessage(zh="内容超过最大长度", en="Content exceed max length"),
Code.INVALID_NODE_DISPLAY_METHOD: CodeMessage(zh="无效的展示方式", en="Invalid display method"),
}

DEFAULT_USER = {
Expand Down Expand Up @@ -151,3 +153,8 @@ def __str__(self):
"""),
]
}


class NodeDisplayMethod(Enum):
CARD = 0
LIST = auto() # 1
2 changes: 2 additions & 0 deletions src/rethink/controllers/schemas/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class User(BaseModel):
avatar: Annotated[str, AfterValidator(empty_str_to_http_url)]
createdAt: str
language: Literal["en", "zh"]
nodeDisplayMethod: NonNegativeInt

code: NonNegativeInt
message: str
Expand Down Expand Up @@ -49,4 +50,5 @@ class UpdateRequest(BaseModel):
nickname: str = ""
avatar: str = ""
language: Literal["en", "zh"] = ""
nodeDisplayMethod: int = -1
requestId: str = ""
3 changes: 3 additions & 0 deletions src/rethink/controllers/user/user_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def get_user(
avatar=u["avatar"],
createdAt=datetime2str(u["_id"].generation_time),
language=u["language"],
nodeDisplayMethod=u["nodeDisplayMethod"],
)
)

Expand All @@ -107,6 +108,7 @@ def update_user(
nickname=req.nickname,
avatar=req.avatar,
language=req.language,
node_display_method=req.nodeDisplayMethod,
)
return schemas.user.UserInfoResponse(
requestId=req.requestId,
Expand All @@ -118,5 +120,6 @@ def update_user(
avatar=u["avatar"],
createdAt=datetime2str(u["_id"].generation_time),
language=u["language"],
nodeDisplayMethod=u["nodeDisplayMethod"],
),
)
1 change: 1 addition & 0 deletions src/rethink/models/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def create_node(md: str):
"recentCursorSearchSelectedNIds": [n0["id"], n1["id"]],
"recentSearch": [],
"language": language,
"nodeDisplayMethod": const.NodeDisplayMethod.CARD.value,
}
_ = COLL.users.insert_one(u)
unids: UserNodeIds = {
Expand Down
1 change: 1 addition & 0 deletions src/rethink/models/tps.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class UserMeta(TypedDict):
recentSearch: List[str]
recentCursorSearchSelectedNIds: List[str]
language: str
nodeDisplayMethod: int


class UserNodeIds(TypedDict):
Expand Down
9 changes: 9 additions & 0 deletions src/rethink/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def add(
"recentCursorSearchSelectedNIds": [],
"recentSearch": [],
"language": language,
"nodeDisplayMethod": const.NodeDisplayMethod.CARD.value,
}
res = COLL.users.insert_one(data)
if not res.acknowledged:
Expand All @@ -61,6 +62,7 @@ def update(
nickname: str = "",
avatar: str = "",
language: str = "",
node_display_method: int = -1,
) -> Tuple[Optional[tps.UserMeta], const.Code]:
u, code = get(uid=uid)
if code != const.Code.OK:
Expand All @@ -87,6 +89,12 @@ def update(
elif not const.Language.is_valid(language):
return None, const.Code.INVALID_LANGUAGE

if node_display_method == -1:
node_display_method = u["nodeDisplayMethod"]
else:
if node_display_method > len(const.NodeDisplayMethod) or node_display_method < 0:
return None, const.Code.INVALID_NODE_DISPLAY_METHOD

res = COLL.users.update_one(
{"id": uid},
{"$set": {
Expand All @@ -96,6 +104,7 @@ def update(
"avatar": avatar,
"modifiedAt": datetime.datetime.now(tz=utc),
"language": language,
"nodeDisplayMethod": node_display_method,
}}
)
if res.modified_count != 1:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def test_update_user(self):
"requestId": "xxx",
"nickname": "new nickname",
"avatar": "http://new.avatar/aa.png",
"nodeDisplayMethod": const.NodeDisplayMethod.LIST.value,
}, headers={"token": self.token})
rj = resp.json()
self.assertEqual(0, rj["code"])
Expand All @@ -95,6 +96,7 @@ def test_update_user(self):
self.assertEqual(0, rj["code"])
self.assertEqual("new nickname", rj["user"]["nickname"])
self.assertEqual("http://new.avatar/aa.png", rj["user"]["avatar"])
self.assertEqual(const.NodeDisplayMethod.LIST.value, rj["user"]["nodeDisplayMethod"])
self.assertEqual("xxx", rj["requestId"])

def test_recent_search(self):
Expand Down
12 changes: 11 additions & 1 deletion tests/test_models_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ def test_user(self):
self.assertEqual(const.Code.OK, code)
self.assertEqual("ccc", u["nickname"])

u, code = models.user.update(uid=_id, email="[email protected]", hashed="1", nickname="2", avatar="3")
u, code = models.user.update(
uid=_id,
email="[email protected]",
hashed="1",
nickname="2",
avatar="3",
language="zh",
node_display_method=const.NodeDisplayMethod.LIST.value,
)
self.assertEqual(const.Code.OK, code)

u, code = models.user.get(_id)
Expand All @@ -49,6 +57,8 @@ def test_user(self):
self.assertEqual("1", u["hashed"])
self.assertEqual("2", u["nickname"])
self.assertEqual("3", u["avatar"])
self.assertEqual("zh", u["language"])
self.assertEqual(const.NodeDisplayMethod.LIST.value, u["nodeDisplayMethod"])

code = models.user.disable(uid=_id)
self.assertEqual(const.Code.OK, code)
Expand Down

0 comments on commit fad1150

Please sign in to comment.