Skip to content

Commit

Permalink
Merge pull request #7 from RyoJerryYu/feat-update-multi-for-plugins
Browse files Browse the repository at this point in the history
feat: update multi for plugins
  • Loading branch information
RyoJerryYu authored Jun 12, 2024
2 parents b7e0020 + 31ff16d commit a5e3cb3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
1 change: 0 additions & 1 deletion memos_webhook/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ async def webhook_handler(
"""The new webhook handler, use protojson."""
dict_json = await req.json()
logger.debug(f"webhook handler received request: {dict_json}")
logger.debug(f"type: {type(dict_json)}")

proto_payload = v1.WebhookRequestPayload().from_dict(dict_json)
background_tasks.add_task(webhook_task, proto_payload, executor)
Expand Down
16 changes: 10 additions & 6 deletions memos_webhook/plugins/base_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@

class PluginProtocol(Protocol):
"""The protocol that plugin executor trully need a plugin to implement.
You should implement `BasePlugin` instead of this protocol.
Unless you know what you are doing."""

def positive_tag(self) -> str: ...
def negative_tag(self) -> str: ...
async def task(self, payload: v1.WebhookRequestPayload, memos_cli: MemosCli) -> v1.Memo: ...
async def task(
self, payload: v1.WebhookRequestPayload, memos_cli: MemosCli
) -> v1.Memo: ...
def should_trigger(self, payload: v1.WebhookRequestPayload) -> bool: ...


Expand Down Expand Up @@ -60,7 +63,9 @@ def tag(self) -> str:
...

@abstractmethod
async def task(self, payload: v1.WebhookRequestPayload, memos_cli: MemosCli) -> v1.Memo:
async def task(
self, payload: v1.WebhookRequestPayload, memos_cli: MemosCli
) -> v1.Memo:
"""The webhook task function.
Return the modified memo, and the plugin will auto update the memo with modified content and negative tag.
Expand Down Expand Up @@ -138,9 +143,7 @@ async def update_memo_content(
Once the task triggered, will replace the `#tag` with `#tag/done`.
If the `#tag` not exists, will add the `#tag/done` to first line.
"""
self.logger.debug(
f"Background task started with param: {payload.to_json()}"
)
self.logger.debug(f"Background task started with param: {payload.to_json()}")

res_memo = await plugin.task(payload, self.memos_cli)
self.logger.info("Background task success completed")
Expand Down Expand Up @@ -169,5 +172,6 @@ async def execute(self, payload: v1.WebhookRequestPayload) -> None:
continue

await self.update_memo_content(plugin, payload)
return # only execute one plugin

self.logger.info("All plugins executed")
11 changes: 5 additions & 6 deletions memos_webhook/plugins/you_get_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def additional_trigger(self, payload: v1.WebhookRequestPayload) -> bool:
return False

@override
async def task(self, payload: v1.WebhookRequestPayload, memos_cli: MemosCli) -> v1.Memo:
async def task(
self, payload: v1.WebhookRequestPayload, memos_cli: MemosCli
) -> v1.Memo:
memo_name = payload.memo.name
self.logger.info(f"Start {self.cfg.name} webhook task for memo: {memo_name}")

Expand All @@ -66,7 +68,7 @@ async def task(self, payload: v1.WebhookRequestPayload, memos_cli: MemosCli) ->
urls = extract_urls(payload.memo.content, self.patterns)
if not urls:
self.logger.info("No urls found")
return
return payload.memo
self.logger.info(f"Extracted urls: {urls}")

# mkdir
Expand Down Expand Up @@ -124,7 +126,4 @@ async def task(self, payload: v1.WebhookRequestPayload, memos_cli: MemosCli) ->
)
self.logger.info(f"Set memo resources: {[res.name for res in all_resources]}")

return v1.Memo(
name=memo_name,
content=payload.memo.content,
)
return payload.memo # you-get plugin do not modify memo content

0 comments on commit a5e3cb3

Please sign in to comment.