Skip to content

Commit

Permalink
feat(plugin): plugin function update
Browse files Browse the repository at this point in the history
1. add right sep width setting;
2. callback api and function
3. new version
  • Loading branch information
MorvanZhou committed Mar 28, 2024
1 parent 2283f93 commit 1c0b988
Show file tree
Hide file tree
Showing 26 changed files with 634 additions and 105 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = rethink-note
version = 0.2.1
version = 0.2.2
author = MorvanZhou
author_email = morvanzhou@hotmail.com
description = note taking app
Expand Down
2 changes: 1 addition & 1 deletion src/rethink/controllers/node/node_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async def update_node(
requestId=req.requestId,
node=None
)
n, code = await core.node.update(
n, old_n, code = await core.node.update(
uid=td.uid,
nid=req.nid,
md=req.md,
Expand Down
41 changes: 39 additions & 2 deletions src/rethink/controllers/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ def __render(
code = const.Code.OK
try:
if method_name == "render_plugin_home":
html = plugin.render_plugin_home()
html = plugin.render_plugin_home(language=td.language)
elif method_name == "render_editor_side":
html = plugin.render_editor_side()
html = plugin.render_editor_side(uid=td.uid, nid=req.nid, md=req.md, language=td.language)
else:
html = ""
code = const.Code.INVALID_SETTING
Expand All @@ -129,4 +129,41 @@ async def render_editor_side(
td: TokenDecode,
req: schemas.plugin.RenderPluginRequest,
) -> schemas.plugin.RenderPluginResponse:
code = const.Code.OK
if req.nid == "":
code = const.Code.INVALID_SETTING
if code != const.Code.OK:
return schemas.plugin.RenderPluginResponse(
code=code.value,
message=const.get_msg_by_code(code, td.language),
requestId=req.requestId,
html=""
)

return __render(td, req, "render_editor_side")


async def plugin_call(
req: schemas.plugin.PluginCallRequest,
) -> schemas.plugin.PluginCallResponse:
plugins = get_plugins()
try:
plugin = plugins[req.pluginId]
except KeyError:
code = const.Code.PLUGIN_NOT_FOUND
return schemas.plugin.PluginCallResponse(
code=code.value,
message=const.get_msg_by_code(code, const.Language.EN.value),
requestId=req.pluginId,
method=req.method,
data=None,
)
data = plugin.handle_api_call(req.method, req.data)
return schemas.plugin.PluginCallResponse(
code=const.Code.OK.value,
message=const.get_msg_by_code(const.Code.OK, const.Language.EN.value),
requestId=req.requestId,
pluginId=req.pluginId,
method=req.method,
data=data,
)
20 changes: 19 additions & 1 deletion src/rethink/controllers/schemas/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import List, Any

from pydantic import BaseModel, NonNegativeInt, Field

Expand All @@ -22,10 +22,28 @@ class Plugin(BaseModel):
class RenderPluginRequest(BaseModel):
pluginId: str = Field(max_length=const.PLUGIN_ID_MAX_LENGTH)
requestId: str = Field(default="", max_length=const.REQUEST_ID_MAX_LENGTH)
nid: str = Field(default="", max_length=const.NID_MAX_LENGTH)
md: str = Field(default="", max_length=const.MD_MAX_LENGTH)


class RenderPluginResponse(BaseModel):
code: NonNegativeInt
message: str
requestId: str
html: str


class PluginCallRequest(BaseModel):
pluginId: str = Field(max_length=const.PLUGIN_ID_MAX_LENGTH)
method: str
data: Any
requestId: str = Field(default="", max_length=const.REQUEST_ID_MAX_LENGTH)


class PluginCallResponse(BaseModel):
code: NonNegativeInt
message: str
requestId: str
pluginId: str
method: str
data: Any
4 changes: 4 additions & 0 deletions src/rethink/controllers/schemas/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Settings(BaseModel):
editorMode: Literal["ir", "wysiwyg"]
editorFontSize: NonNegativeInt
editorCodeTheme: str
editorSepRightWidth: float
editorSideCurrentToolId: str

email: str
nickname: str
Expand Down Expand Up @@ -85,4 +87,6 @@ class UpdateSettingsRequest(BaseModel):
editorMode: Literal["ir", "wysiwyg", ""] = ""
editorFontSize: int = -1
editorCodeTheme: CODE_THEME_TYPES = ""
editorSepRightWidth: float = -1.
editorSideCurrentToolId: str = ""
requestId: str = Field(default="", max_length=const.REQUEST_ID_MAX_LENGTH)
4 changes: 4 additions & 0 deletions src/rethink/controllers/user/user_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def __get_user(u: dict) -> schemas.user.UserInfoResponse.User:
editorMode=u_settings["editorMode"],
editorFontSize=u_settings["editorFontSize"],
editorCodeTheme=u_settings["editorCodeTheme"],
editorSepRightWidth=u_settings.get("editorSepRightWidth", 200),
editorSideCurrentToolId=u_settings.get("editorSideCurrentToolId", ""),
),
)

Expand Down Expand Up @@ -181,5 +183,7 @@ async def update_settings(
editor_mode=req.editorMode,
editor_font_size=req.editorFontSize,
editor_code_theme=req.editorCodeTheme,
editor_sep_right_width=req.editorSepRightWidth,
editor_side_current_tool_id=req.editorSideCurrentToolId,
)
return __return_user_resp(u, code, req.requestId)
4 changes: 2 additions & 2 deletions src/rethink/core/files/importing/async_tasks/obsidian/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async def upload_obsidian_task( # noqa: C901
)
md = meta.title + "\n\n" + md
nid = existed_path2nid[full_path]
n, code = await core.node.update(
n, _, code = await core.node.update(
uid=uid,
nid=nid,
md=md,
Expand Down Expand Up @@ -192,7 +192,7 @@ async def upload_obsidian_task( # noqa: C901
n, code = await core.node.get(uid=uid, nid=nid)
if code != const.Code.OK:
continue
n, code = await core.node.update(
n, _, code = await core.node.update(
uid=uid,
nid=nid,
md=n["md"],
Expand Down
26 changes: 14 additions & 12 deletions src/rethink/core/node.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import datetime
from pathlib import Path
from typing import List, Optional, Set, Tuple, Dict, Any
Expand Down Expand Up @@ -200,25 +201,26 @@ async def update( # noqa: C901
nid: str,
md: str,
refresh_on_same_md: bool = False,
) -> Tuple[Optional[tps.Node], const.Code]:
) -> Tuple[Optional[tps.Node], Optional[tps.Node], const.Code]:
if regex.NID.match(nid) is None:
return None, const.Code.NODE_NOT_EXIST
return None, None, const.Code.NODE_NOT_EXIST
md = md.strip()
if len(md) > const.MD_MAX_LENGTH:
return None, const.Code.NOTE_EXCEED_MAX_LENGTH
return None, None, const.Code.NOTE_EXCEED_MAX_LENGTH
u, code = await user.get(uid=uid)
if code != const.Code.OK:
return None, const.Code.ACCOUNT_OR_PASSWORD_ERROR
return None, None, const.Code.ACCOUNT_OR_PASSWORD_ERROR
if await user.user_space_not_enough(u=u):
return None, const.Code.USER_SPACE_NOT_ENOUGH
return None, None, const.Code.USER_SPACE_NOT_ENOUGH

title, body, snippet = utils.preprocess_md(md)

n, code = await get(uid=uid, nid=nid)
if code != const.Code.OK:
return None, code
return None, None, code
old_n = copy.deepcopy(n)
if n["md"] == md and not refresh_on_same_md:
return n, const.Code.OK
return n, old_n, const.Code.OK

old_md_size = len(n["md"].encode("utf-8"))
new_data = {
Expand All @@ -230,7 +232,7 @@ async def update( # noqa: C901
from_nodes = await client.coll.nodes.find({"id": {"$in": n["fromNodeIds"]}}).to_list(length=None)
for from_node in from_nodes:
new_md = utils.change_link_title(md=from_node["md"], nid=nid, new_title=title)
n, code = await update(uid=uid, nid=from_node["id"], md=new_md)
n, old_n, code = await update(uid=uid, nid=from_node["id"], md=new_md)
if code != const.Code.OK:
logger.info(f"update fromNode {from_node['id']} failed")
new_data["title"] = title
Expand All @@ -243,7 +245,7 @@ async def update( # noqa: C901
new_data["toNodeIds"], code = await __flush_to_node_ids(
nid=n["id"], orig_to_nid=n["toNodeIds"], new_md=md)
if code != const.Code.OK:
return None, code
return None, old_n, code

if not config.is_local_db():
doc = await client.coll.nodes.find_one_and_update(
Expand All @@ -259,11 +261,11 @@ async def update( # noqa: C901
)
if res.modified_count != 1:
logger.error(f"update node {nid} failed")
return None, const.Code.OPERATION_FAILED
return None, old_n, const.Code.OPERATION_FAILED
doc = await client.coll.nodes.find_one({"id": nid})

if doc is None:
return None, const.Code.NODE_NOT_EXIST
return None, old_n, const.Code.NODE_NOT_EXIST
await __set_linked_nodes(
docs=[doc],
with_disabled=False,
Expand All @@ -276,7 +278,7 @@ async def update( # noqa: C901
code = await client.search.update(uid=uid, doc=SearchDoc(nid=nid, title=title, body=body))
if code != const.Code.OK:
logger.error(f"update search index failed, code: {code}")
return doc, code
return doc, old_n, code


async def to_trash(uid: str, nid: str) -> const.Code:
Expand Down
10 changes: 10 additions & 0 deletions src/rethink/core/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ async def update_settings( # noqa: C901
editor_mode: Literal["", "ir", "wysiwyg"] = "",
editor_font_size: int = -1,
editor_code_theme: tps.CODE_THEME_TYPES = "",
editor_sep_right_width: float = -1,
editor_side_current_tool_id: str = "",
) -> Tuple[Optional[tps.UserMeta], const.Code]:
u, code = await get(uid=uid)
if code != const.Code.OK:
Expand Down Expand Up @@ -150,6 +152,14 @@ async def update_settings( # noqa: C901
return None, const.Code.INVALID_SETTING
new_data["settings.editorCodeTheme"] = editor_code_theme

if editor_sep_right_width != -1 and editor_sep_right_width != u["settings"].get("editorSepRightWidth", None):
if editor_sep_right_width <= 0:
return None, const.Code.INVALID_SETTING
new_data["settings.editorSepRightWidth"] = editor_sep_right_width

if editor_side_current_tool_id != u["settings"].get("editorSideCurrentToolId", None):
new_data["settings.editorSideCurrentToolId"] = editor_side_current_tool_id

res = await client.coll.users.update_one(
{"id": uid},
{"$set": new_data},
Expand Down
Loading

0 comments on commit 1c0b988

Please sign in to comment.