Skip to content

Commit

Permalink
Merge pull request #8 from RyoJerryYu/text-after-all-plugins-pass
Browse files Browse the repository at this point in the history
feat: text after all plugins pass
  • Loading branch information
RyoJerryYu authored Jun 13, 2024
2 parents a5e3cb3 + 7fedabd commit 62f476b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions memos_webhook/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ACTIVITY_TYPE_CREATED = "memos.memo.created"
ACTIVITY_TYPE_UPDATED = "memos.memo.updated"
ACTIVITY_TYPE_DELETED = "memos.memo.deleted"
12 changes: 9 additions & 3 deletions memos_webhook/plugins/base_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import betterproto.lib.google.protobuf as pb

from memos_webhook.constants import ACTIVITY_TYPE_DELETED
from memos_webhook.dependencies.memos_cli import MemosCli
from memos_webhook.proto_gen.memos.api import v1
from memos_webhook.utils.logger import logger
Expand Down Expand Up @@ -143,10 +144,15 @@ 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"Plugin task started with param: {payload.to_json()}")

res_memo = await plugin.task(payload, self.memos_cli)
self.logger.info("Background task success completed")
self.logger.info("Plugin task success completed")
assert res_memo is not None, "task should return a memo"

if payload.activity_type == ACTIVITY_TYPE_DELETED:
self.logger.info("Memo deleted, skip update memo content")
return

# update memo
if plugin.positive_tag() in res_memo.content:
Expand Down Expand Up @@ -174,4 +180,4 @@ async def execute(self, payload: v1.WebhookRequestPayload) -> None:
await self.update_memo_content(plugin, payload)
return # only execute one plugin

self.logger.info("All plugins executed")
self.logger.info(f"All plugins skipped for {payload.memo.name}")
8 changes: 6 additions & 2 deletions memos_webhook/plugins/you_get_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import aiofiles.os
import betterproto.lib.google.protobuf as pb

from memos_webhook.constants import (ACTIVITY_TYPE_CREATED,
ACTIVITY_TYPE_UPDATED)
from memos_webhook.dependencies.config import YouGetPluginConfig
from memos_webhook.dependencies.memos_cli import MemosCli
from memos_webhook.proto_gen.memos.api import v1
Expand Down Expand Up @@ -35,7 +37,7 @@ def __init__(self, cfg: YouGetPluginConfig) -> None:
self.patterns = [re.compile(pattern) for pattern in cfg.patterns]

def activity_types(self) -> list[str]:
return ["memos.memo.created", "memos.memo.updated"]
return [ACTIVITY_TYPE_CREATED, ACTIVITY_TYPE_UPDATED]

@override
def tag(self) -> str:
Expand Down Expand Up @@ -67,7 +69,9 @@ async def task(
# extract urls
urls = extract_urls(payload.memo.content, self.patterns)
if not urls:
self.logger.info("No urls found")
self.logger.info("Triggered but no urls found")
# trigger but no urls found, it may have a positive tag.
# Should update the tag and make sure it will not be triggered again
return payload.memo
self.logger.info(f"Extracted urls: {urls}")

Expand Down

0 comments on commit 62f476b

Please sign in to comment.