From 7b3e97d5b585789a8e9388b0b255f5cf37f17663 Mon Sep 17 00:00:00 2001 From: Bing Wen Tan Date: Wed, 18 Dec 2024 10:13:25 +0800 Subject: [PATCH] handle new format for community note --- .../machineLearningServer/operations.ts | 27 +++++++------- .../userGenericMessageHandlers.ts | 35 ++++++++++++++----- functions/src/types.ts | 1 + 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/functions/src/definitions/common/machineLearningServer/operations.ts b/functions/src/definitions/common/machineLearningServer/operations.ts index 0e9b860..d49b718 100644 --- a/functions/src/definitions/common/machineLearningServer/operations.ts +++ b/functions/src/definitions/common/machineLearningServer/operations.ts @@ -22,6 +22,8 @@ interface ControversialResponse { interface CommunityNoteReturn extends Pick { isControversial: boolean // Add your new field + isVideo: boolean + isAccessBlocked: boolean } interface L1CategoryResponse { @@ -74,10 +76,7 @@ async function determineNeedsChecking(input: { } const data = { ...input } - const response = await callAPI( - "determineNeedsChecking", - data - ) + const response = await callAPI("getNeedsChecking", data) return response.data.needsChecking } @@ -123,16 +122,15 @@ async function getCommunityNote(input: { if (env === "SIT") { throw new Error("Cannot call getCommunityNote in SIT environment") } - if ( - input.text?.toLowerCase().includes("test") || - input.caption?.toLowerCase().includes("test") || - env === "DEV" - ) { + if (env === "DEV") { return { en: "This is a test community note.", cn: "这是一个测试社区笔记。", links: ["https://example1.com", "https://example2.com"], - isControversial: false, + isControversial: + input.text?.toLowerCase().includes("controversial") || false, + isVideo: input.text?.toLowerCase().includes("video") || false, + isAccessBlocked: input.text?.toLowerCase().includes("blocked") || false, } } } @@ -142,9 +140,12 @@ async function getCommunityNote(input: { // Timeout logic const timeoutPromise = new Promise((_, reject) => - setTimeout(() => { - reject(new Error("The API call timed out after 60 seconds")) - }, 60000) + setTimeout( + () => { + reject(new Error("The API call timed out after 60 seconds")) + }, + env === "PROD" ? 60000 : 120000 + ) ) // API call diff --git a/functions/src/definitions/eventHandlers/userGenericMessageHandlers.ts b/functions/src/definitions/eventHandlers/userGenericMessageHandlers.ts index 2bb8953..2053032 100644 --- a/functions/src/definitions/eventHandlers/userGenericMessageHandlers.ts +++ b/functions/src/definitions/eventHandlers/userGenericMessageHandlers.ts @@ -237,9 +237,12 @@ async function newTextInstanceHandler({ ) let communityNoteData let isCommunityNoteGenerated = false - const isControversial = await determineControversial({ - text: text, - }) + let isCommunityNoteUsable = false + let communityNoteStatus = "error" + // const isControversial = await determineControversial({ + // text: text, + // }) + let isControversial = false if (needsChecking) { await sendWaitingMessage(userSnap, id) try { @@ -247,6 +250,11 @@ async function newTextInstanceHandler({ text: text, }) isCommunityNoteGenerated = true + isControversial = communityNoteData.isControversial + isCommunityNoteUsable = !( + communityNoteData.isVideo || communityNoteData.isAccessBlocked + ) + communityNoteStatus = isCommunityNoteUsable ? "generated" : "unusable" } catch (error) { functions.logger.error("Error in getCommunityNote:", error) } @@ -290,8 +298,9 @@ async function newTextInstanceHandler({ tags: {}, primaryCategory: needsChecking ? null : "irrelevant", customReply: null, + communityNoteStatus: communityNoteStatus, communityNote: - isCommunityNoteGenerated && communityNoteData + isCommunityNoteGenerated && communityNoteData && isCommunityNoteUsable ? { en: communityNoteData?.en || "", cn: communityNoteData?.cn || "", @@ -534,19 +543,26 @@ async function newImageInstanceHandler({ if (!hasMatch || (!matchedInstanceSnap && !matchedParentMessageRef)) { let communityNoteData let isCommunityNoteGenerated = false + let isCommunityNoteUsable = false let isControversial = false + let communityNoteStatus = "error" const signedUrl = (await getSignedUrl(filename)) ?? null if (signedUrl) { - isControversial = await determineControversial({ - url: signedUrl, - caption: caption ?? null, - }) + // isControversial = await determineControversial({ + // url: signedUrl, + // caption: caption ?? null, + // }) try { communityNoteData = await getCommunityNote({ url: signedUrl, caption: caption ?? null, }) isCommunityNoteGenerated = true + isControversial = communityNoteData.isControversial + isCommunityNoteUsable = !( + communityNoteData.isVideo || communityNoteData.isAccessBlocked + ) + communityNoteStatus = isCommunityNoteUsable ? "generated" : "unusable" } catch (error) { functions.logger.error("Error in getCommunityNote:", error) } @@ -602,8 +618,9 @@ async function newImageInstanceHandler({ tags: {}, primaryCategory: null, customReply: null, //string + communityNoteStatus: communityNoteStatus, communityNote: - isCommunityNoteGenerated && communityNoteData + isCommunityNoteGenerated && communityNoteData && isCommunityNoteUsable ? { en: communityNoteData?.en || "", cn: communityNoteData?.cn || "", diff --git a/functions/src/types.ts b/functions/src/types.ts index c9f6111..a5e1829 100644 --- a/functions/src/types.ts +++ b/functions/src/types.ts @@ -152,6 +152,7 @@ export type MessageData = { tags: TagsMap //tags assigned to the message primaryCategory: string | null //the category that the message has been assigned to. Either "scam", "illicit", "satire", "untrue", "misleading", "accurate", "spam", "legitimate", "irrelevant", "unsure" or "error". Note, legitimate refers to nvc-credible and irrelevant nvc-cant-tell. customReply: CustomReply | null //the admin-assigned custom reply for this message, that supercedes the default replies + communityNoteStatus: string | null //the status of the community note for this message, either "error", "generated" or "unusable" communityNote: CommunityNote | null // the gen-ai generated community note for this message instanceCount: number //the number of instances of this message adminGroupSentMessageId: string | null // The original message id of the message sent to the admin group