Skip to content

Commit

Permalink
Revert "don't notify or update group embeddings during The Merge"
Browse files Browse the repository at this point in the history
This reverts commit 1e00129.
  • Loading branch information
sipec committed Mar 12, 2024
1 parent b4775f5 commit 70a5c95
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions backend/functions/src/triggers/on-update-contract.ts
@@ -1,5 +1,6 @@
import * as functions from 'firebase-functions'
import {
getContractSupabase,
getUser,
log,
processPaginated,
Expand All @@ -8,9 +9,15 @@ import {
import { createCommentOrUpdatedContractNotification } from 'shared/create-notification'
import { Contract, CPMMMultiContract, MultiContract } from 'common/contract'
import * as admin from 'firebase-admin'
import { isEqual, pick } from 'lodash'
import { difference, isEqual, pick } from 'lodash'
import { secrets } from 'common/secrets'
import { createSupabaseClient } from 'shared/supabase/init'
import {
createSupabaseClient,
createSupabaseDirectClient,
} from 'shared/supabase/init'
import { upsertGroupEmbedding } from 'shared/helpers/embeddings'
import { addContractToFeed } from 'shared/create-feed'
import { DAY_MS } from 'common/util/time'

type AnyContract = Contract & CPMMMultiContract & MultiContract
const propsThatTriggerRevalidation: (keyof AnyContract)[] = [
Expand Down Expand Up @@ -45,9 +52,43 @@ export const onUpdateContract = functions
const contract = change.after.data() as Contract
const previousContract = change.before.data() as Contract
const { eventId } = context
const { closeTime, question } = contract
const { closeTime, question, groupLinks } = contract

const db = createSupabaseClient()
const pg = createSupabaseDirectClient()

// Update group embeddings if group links changed
const previousGroupIds = (previousContract.groupLinks ?? []).map(
(gl) => gl.groupId
)
const currentGroupIds = (groupLinks ?? []).map((gl) => gl.groupId)
const onlyNewGroupIds = difference(currentGroupIds, previousGroupIds)
const differentGroupIds = onlyNewGroupIds.concat(
difference(previousGroupIds, currentGroupIds)
)
await Promise.all(
differentGroupIds.map(async (groupId) =>
upsertGroupEmbedding(pg, groupId)
)
)
// Adding a contract to a group is ~similar~ to creating a new contract in that group
if (
onlyNewGroupIds.length > 0 &&
contract.createdTime > Date.now() - 2 * DAY_MS &&
contract.visibility === 'public'
) {
const contractWithScore = await getContractSupabase(contract.id)
if (!contractWithScore) return
await addContractToFeed(
contractWithScore,
['contract_in_group_you_are_in'],
'new_contract',
[contractWithScore.creatorId],
{
idempotencyKey: contractWithScore.id + '_new_contract',
}
)
}

if (
(previousContract.closeTime !== closeTime ||
Expand Down

0 comments on commit 70a5c95

Please sign in to comment.