Skip to content

Commit

Permalink
update search
Browse files Browse the repository at this point in the history
  • Loading branch information
MorvanZhou committed Nov 3, 2023
1 parent 611db0d commit 6443687
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 46 deletions.
6 changes: 3 additions & 3 deletions src/rethink/controllers/schemas/search.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional, Sequence
from typing import List, Sequence

from pydantic import Field, BaseModel, NonNegativeInt

Expand Down Expand Up @@ -36,11 +36,11 @@ class AddToRecentSearchHistRequest(BaseModel):

class PutRecentSearchRequest(BaseModel):
requestId: str
nid: str
query: str


class GetRecentSearchResponse(BaseModel):
code: NonNegativeInt
message: str
requestId: str
nodes: List[NodesInfoResponse.Data.NodeInfo]
queries: List[str]
13 changes: 3 additions & 10 deletions src/rethink/controllers/search/node_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,13 @@ def get_recent(
requestId=rid,
queries=[],
)
nodes = models.search.get_recent_search(uid=td.uid)
queries = models.search.get_recent_search(uid=td.uid)
code = const.Code.OK
return schemas.search.GetRecentSearchResponse(
code=code.value,
message=const.get_msg_by_code(code, td.language),
requestId=rid,
nodes=[schemas.node.NodesInfoResponse.Data.NodeInfo(
id=n["id"],
title=n["title"],
snippet=n["snippet"],
type=n["type"],
createdAt=datetime2str(n["_id"].generation_time),
modifiedAt=datetime2str(n["modifiedAt"]),
) for n in nodes]
queries=queries,
)


Expand All @@ -120,7 +113,7 @@ def put_recent(
message=const.get_msg_by_code(td.code, td.language),
requestId=req.requestId,
)
code = models.search.put_recent_search(uid=td.uid, nid=req.nid)
code = models.search.put_recent_search(uid=td.uid, query=req.query)
return schemas.base.AcknowledgeResponse(
code=code.value,
message=const.get_msg_by_code(code, td.language),
Expand Down
21 changes: 9 additions & 12 deletions src/rethink/models/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,26 +109,23 @@ def add_recent_cursor_search(
return const.Code.OK


def get_recent_search(uid: str) -> List[tps.Node]:
if not user.is_exist(uid=uid):
return []
def get_recent_search(uid: str) -> List[str]:
doc = COLL.users.find_one({"id": uid})
nodes = COLL.nodes.find({"id": {"$in": doc["recentSearch"]}})
return sorted(list(nodes), key=lambda x: doc["recentSearch"].index(x["id"]))
if doc is None:
return []
return doc["recentSearch"]


def put_recent_search(uid: str, nid: str) -> const.Code:
if not user.is_exist(uid=uid):
return const.Code.ACCOUNT_OR_PASSWORD_ERROR
def put_recent_search(uid: str, query: str) -> const.Code:
doc = COLL.users.find_one({"id": uid})
if COLL.unids.count_documents({"id": uid, "nodeIds": {"$in": [nid]}}) == 0:
return const.Code.NODE_NOT_EXIST
if doc is None:
return const.Code.ACCOUNT_OR_PASSWORD_ERROR
rns = doc["recentSearch"]
try:
rns.remove(nid)
rns.remove(query)
except ValueError:
pass
rns.insert(0, nid)
rns.insert(0, query)
if len(rns) > 20:
rns = rns[:20]
_ = COLL.users.update_one(
Expand Down
8 changes: 8 additions & 0 deletions src/rethink/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,17 @@ def jwt_decode(token: str) -> dict:


INTERNAL_LINK_PTN = re.compile(r"\[\[(.*?)]]")
INTERNAL_IMG_PTN = re.compile(r"!\[\[(.*?)]]")


def replace_inner_link(md: str, filename2nid: Dict[str, str]) -> str:
# image
for match in list(INTERNAL_IMG_PTN.finditer(md))[::-1]:
span = match.span()
filepath = match.group(1)
md = f"{md[: span[0]]}![{filepath}](/img/{filepath}){md[span[1]:]}"

# link
for match in list(INTERNAL_LINK_PTN.finditer(md))[::-1]:
span = match.span()
filename = match.group(1)
Expand Down
13 changes: 2 additions & 11 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,11 @@ def test_update_user(self):
self.assertEqual("xxx", rj["requestId"])

def test_recent_search(self):
resp = self.client.put("/api/node", json={
"requestId": "xxx",
"md": "node1\ntext",
"type": const.NodeType.MARKDOWN.value,
}, headers={"token": self.token})
rj = resp.json()
self.assertEqual(0, rj["code"])

resp = self.client.put(
"/api/search/recent",
json={
"requestId": "xxx",
"nid": rj["node"]["id"],
"query": "aaa",
}, headers={"token": self.token})
rj = resp.json()
self.assertEqual(0, rj["code"])
Expand All @@ -126,8 +118,7 @@ def test_recent_search(self):
rj = resp.json()
self.assertEqual(0, rj["code"])
self.assertEqual("xxx", rj["requestId"])
self.assertEqual(len(rj["nodes"]), 1)
self.assertEqual(rj["nodes"][0]["title"], "node1")
self.assertEqual(["aaa"], rj["queries"])

def test_node(self):
resp = self.client.post(
Expand Down
14 changes: 4 additions & 10 deletions tests/test_models_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,12 @@ def test_to_trash(self):

def test_search(self):
code = models.search.put_recent_search(self.uid, "a")
self.assertEqual(const.Code.NODE_NOT_EXIST, code)

n1, code = models.node.add(
uid=self.uid, md="title\ntext", type_=const.NodeType.MARKDOWN.value
)
self.assertEqual(const.Code.OK, code)
code = models.search.put_recent_search(self.uid, n1["id"])
self.assertEqual(const.Code.OK, code)
models.search.put_recent_search(self.uid, "c")
models.search.put_recent_search(self.uid, "b")

nodes = models.search.get_recent_search(self.uid)
self.assertEqual(1, len(nodes))
self.assertEqual(n1["id"], nodes[0]["id"])
queries = models.search.get_recent_search(self.uid)
self.assertEqual(["b", "c", "a"], queries)

def test_batch(self):
ns = []
Expand Down

0 comments on commit 6443687

Please sign in to comment.