Skip to content

Commit

Permalink
chore: support comment.edited and rename fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Oct 4, 2024
1 parent 4276338 commit b762c04
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "ubiquity-os/kernel-telegram",
"description": "Part kernel plugin, part Telegram kernel. This bot is designed to be a bridge between UbiquityOS and Telegram.",
"ubiquity:listeners": ["issues.labeled", "issues.closed", "issues.reopened", "issue_comment.created"]
"ubiquity:listeners": ["issues.labeled", "issues.closed", "issues.reopened", "issue_comment.created", "issue_comment.edited"]
}
20 changes: 15 additions & 5 deletions src/handlers/private-notifications/issue-comment-created.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@ import { CallbackResult } from "../../types/proxy";
import { TelegramBotSingleton } from "../../types/telegram-bot-single";
import { Logger, logger } from "../../utils/logger";

export async function handleIssueCommentCreated(context: Context<"issue_comment.created", SupportedEvents["issue_comment.created"]>): Promise<CallbackResult> {

const rewardCommentRegex = /href="https:\/\/[^/]+\/?\?claim=([A-Za-z0-9+/=]+)"[^>]*>\s*\[.*?\]\s*<\/a>\s*<\/h3>\s*<h6>\s*@([a-zA-Z0-9-_]+)\s*<\/h6>/g;
// we'll have multiple permit comments to parse out here
// the regex is capturing the claim url and the github username

export async function notificationsRequiringComments(context: Context<"issue_comment.created" | "issue_comment.edited", SupportedEvents["issue_comment.created" | "issue_comment.edited"]>): Promise<CallbackResult> {
const {
adapters: { github },
payload,
logger,
} = context;

// For now only payments are supported so this naive check will be improved
// to support multiple triggers in the future

// skip if not a bot comment or not a reward comment
if(payload.comment.user?.type !== "Bot" || !payload.comment.body.match(rewardCommentRegex)) {
return { status: 200, reason: "skipped" };
}

const users = await github.retrieveStorageDataObject("userBase", false);

if (!users) {
Expand Down Expand Up @@ -98,13 +111,10 @@ async function handlePaymentNotification(username: string, claimUrlBase64String:
}

function parsePaymentComment(comment: string) {
const regex = /href="https:\/\/[^/]+\/?\?claim=([A-Za-z0-9+/=]+)"[^>]*>\s*\[.*?\]\s*<\/a>\s*<\/h3>\s*<h6>\s*@([a-zA-Z0-9-_]+)\s*<\/h6>/g;
// we'll have multiple permit comments to parse out here
// the regex is capturing the claim url and the github username

const claims: Record<string, string> = {};

for (const match of comment.matchAll(regex)) {
for (const match of comment.matchAll(rewardCommentRegex)) {
const [, claim, username] = match;
claims[username] = claim;
}
Expand Down
5 changes: 3 additions & 2 deletions src/handlers/worker-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context, SupportedEventsU } from "../types";
import { ProxyCallbacks } from "../types/proxy";
import { bubbleUpErrorComment } from "../utils/errors";
import { handleIssueCommentCreated } from "./private-notifications/issue-comment-created";
import { notificationsRequiringComments } from "./private-notifications/issue-comment-created";

/**
* The `callbacks` object defines an array of callback functions for each supported event type.
Expand All @@ -11,7 +11,8 @@ import { handleIssueCommentCreated } from "./private-notifications/issue-comment
* us to add more callbacks for a particular event without modifying the core logic.
*/
const callbacks = {
"issue_comment.created": [handleIssueCommentCreated],
"issue_comment.created": [notificationsRequiringComments],
"issue_comment.edited": [notificationsRequiringComments],
} as ProxyCallbacks;

/**
Expand Down

0 comments on commit b762c04

Please sign in to comment.