Skip to content

Commit

Permalink
handle new format for community note
Browse files Browse the repository at this point in the history
  • Loading branch information
bwsarge committed Dec 18, 2024
1 parent 288a723 commit 7b3e97d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ interface ControversialResponse {
interface CommunityNoteReturn
extends Pick<CommunityNote, "en" | "cn" | "links"> {
isControversial: boolean // Add your new field
isVideo: boolean
isAccessBlocked: boolean
}

interface L1CategoryResponse {
Expand Down Expand Up @@ -74,10 +76,7 @@ async function determineNeedsChecking(input: {
}

const data = { ...input }
const response = await callAPI<TrivialResponse>(
"determineNeedsChecking",
data
)
const response = await callAPI<TrivialResponse>("getNeedsChecking", data)
return response.data.needsChecking
}

Expand Down Expand Up @@ -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,
}
}
}
Expand All @@ -142,9 +140,12 @@ async function getCommunityNote(input: {

// Timeout logic
const timeoutPromise = new Promise<never>((_, 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,24 @@ 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 {
communityNoteData = await getCommunityNote({
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)
}
Expand Down Expand Up @@ -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 || "",
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 || "",
Expand Down
1 change: 1 addition & 0 deletions functions/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7b3e97d

Please sign in to comment.