Skip to content

Commit

Permalink
shifted notifs to onMessageWrite
Browse files Browse the repository at this point in the history
  • Loading branch information
sarge1989 committed Dec 16, 2024
1 parent 346efad commit 994f7f8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
22 changes: 0 additions & 22 deletions functions/src/definitions/eventHandlers/onMessageUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ const onMessageUpdateV2 = onDocumentUpdated(

// If changes from not assessed to assessed
if (!preChangeSnap.data().isAssessed && messageData.isAssessed) {
// Reply the admin feed with the primary category results
sendVotingUpdate({
messageId: postChangeSnap.get("adminGroupSentMessageId"),
currentCategory: primaryCategory,
})

await postChangeSnap.ref.update({
assessedTimestamp: Timestamp.fromDate(new Date()),
Expand All @@ -55,17 +50,6 @@ const onMessageUpdateV2 = onDocumentUpdated(
await replyCommunityNoteInstances(postChangeSnap)
}
} // if either the text changed, or the primaryCategory changed, rerun rationalisation
else if (
messageData.isAssessed &&
preChangeSnap.data().primaryCategory !== primaryCategory &&
preChangeSnap.data().primaryCategory !== null
) {
sendVotingUpdate({
messageId: postChangeSnap.get("adminGroupSentMessageId"),
previousCategory: preChangeSnap.data().primaryCategory,
currentCategory: primaryCategory,
})
}

if (
!preChangeSnap.data().communityNote?.downvoted &&
Expand All @@ -80,12 +64,6 @@ const onMessageUpdateV2 = onDocumentUpdated(
})
}
}
sendVotingUpdate({
messageId: postChangeSnap.get(
"communityNote.adminGroupCommunityNoteSentMessageId"
),
downvoted: true,
})
}

if (
Expand Down
44 changes: 41 additions & 3 deletions functions/src/definitions/eventHandlers/onMessageWrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
sendCommunityNoteNotification,
sendVotingUpdate,
} from "../../services/admin/notificationService"
import { logger } from "firebase-functions/v2"

if (!admin.apps.length) {
admin.initializeApp()
Expand All @@ -15,9 +16,10 @@ const adminMessageHandler = onDocumentWritten(
secrets: ["TELEGRAM_ADMIN_BOT_TOKEN"],
},
async (event) => {
const isDocumentCreation = !event.data?.before.exists
const isDocumentCreation = !event.data?.before?.exists
const docSnap = event.data?.after
if (!docSnap) {
logger.error("Missing after data on onMessageWrite")
return
}
const adminMessageId = docSnap.get("adminGroupSentMessageId") ?? null
Expand All @@ -40,18 +42,54 @@ const adminMessageHandler = onDocumentWritten(
} else {
const before = event.data?.before
if (!before) {
logger.error("Missing before data on onMessageWrite")
return
}
const isAssessedBefore = before.get("isAssessed") ?? null
const isAssessedAfter = docSnap.get("isAssessed") ?? null
const communityNoteBefore = before.get("communityNote") ?? null
const communityNoteAfter = docSnap.get("communityNote") ?? null
const primaryCategoryBefore = before.get("primaryCategory") ?? null
const primaryCategoryAfter = docSnap.get("primaryCategory") ?? null

if (
before.get("communityNote") === null &&
docSnap.get("communityNote") !== null
communityNoteBefore === null &&
communityNoteAfter !== null
) {
//if community note was newly generated
await sendCommunityNoteNotification(
docSnap.get("communityNote"),
adminMessageId,
docSnap.ref
)
}
if (isAssessedBefore === null && isAssessedAfter) {
//if message was newly assessed
await sendVotingUpdate({
messageId: docSnap.get("adminGroupSentMessageId"),
currentCategory: primaryCategoryAfter,
})
} else if (
isAssessedAfter &&
primaryCategoryBefore !== primaryCategoryAfter &&
primaryCategoryBefore !== null
) {
//if there was a change in categories after the message was assessed
await sendVotingUpdate({
messageId: docSnap.get("adminGroupSentMessageId"),
previousCategory: primaryCategoryBefore,
currentCategory: primaryCategoryAfter,
})
}
if (!communityNoteBefore.downvoted && communityNoteAfter.downvoted) {
//if community note got downvoted
await sendVotingUpdate({
messageId: docSnap.get(
"communityNote.adminGroupCommunityNoteSentMessageId"
),
downvoted: true,
})
}
}
}
)
Expand Down

0 comments on commit 994f7f8

Please sign in to comment.