Remove grouplinks on frontend
This commit is contained in:
parent
f894f74a13
commit
07a1c12460
|
@ -32,8 +32,8 @@ import { Group, MAX_ID_LENGTH } from '../../common/group'
|
||||||
import { getPseudoProbability } from '../../common/pseudo-numeric'
|
import { getPseudoProbability } from '../../common/pseudo-numeric'
|
||||||
import { JSONContent } from '@tiptap/core'
|
import { JSONContent } from '@tiptap/core'
|
||||||
import { uniq, zip } from 'lodash'
|
import { uniq, zip } from 'lodash'
|
||||||
import { Bet } from 'common/bet'
|
import { Bet } from '../../common/bet'
|
||||||
import { createGroupLinks } from 'functions/src/on-update-group'
|
import { createGroupLinks } from './on-update-group'
|
||||||
|
|
||||||
const descScehma: z.ZodType<JSONContent> = z.lazy(() =>
|
const descScehma: z.ZodType<JSONContent> = z.lazy(() =>
|
||||||
z.intersection(
|
z.intersection(
|
||||||
|
@ -137,33 +137,6 @@ export const createmarket = newEndpoint({}, async (req, auth) => {
|
||||||
const slug = await getSlug(question)
|
const slug = await getSlug(question)
|
||||||
const contractRef = firestore.collection('contracts').doc()
|
const contractRef = firestore.collection('contracts').doc()
|
||||||
|
|
||||||
let group = null
|
|
||||||
if (groupId) {
|
|
||||||
const groupDocRef = firestore.collection('groups').doc(groupId)
|
|
||||||
const groupDoc = await groupDocRef.get()
|
|
||||||
if (!groupDoc.exists) {
|
|
||||||
throw new APIError(400, 'No group exists with the given group ID.')
|
|
||||||
}
|
|
||||||
|
|
||||||
group = groupDoc.data() as Group
|
|
||||||
if (
|
|
||||||
!group.memberIds.includes(user.id) &&
|
|
||||||
!group.anyoneCanJoin &&
|
|
||||||
group.creatorId !== user.id
|
|
||||||
) {
|
|
||||||
throw new APIError(
|
|
||||||
400,
|
|
||||||
'User must be a member/creator of the group or group must be open to add markets to it.'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (!group.contractIds.includes(contractRef.id))
|
|
||||||
await groupDocRef.update({
|
|
||||||
contractIds: uniq([...group.contractIds, contractRef.id]),
|
|
||||||
})
|
|
||||||
// We'll update the group links manually here bc we have the user's id and won't in the trigger
|
|
||||||
await createGroupLinks(group, [contractRef.id], auth.uid)
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
'creating contract for',
|
'creating contract for',
|
||||||
user.username,
|
user.username,
|
||||||
|
@ -195,6 +168,33 @@ export const createmarket = newEndpoint({}, async (req, auth) => {
|
||||||
|
|
||||||
await contractRef.create(contract)
|
await contractRef.create(contract)
|
||||||
|
|
||||||
|
let group = null
|
||||||
|
if (groupId) {
|
||||||
|
const groupDocRef = firestore.collection('groups').doc(groupId)
|
||||||
|
const groupDoc = await groupDocRef.get()
|
||||||
|
if (!groupDoc.exists) {
|
||||||
|
throw new APIError(400, 'No group exists with the given group ID.')
|
||||||
|
}
|
||||||
|
|
||||||
|
group = groupDoc.data() as Group
|
||||||
|
if (
|
||||||
|
!group.memberIds.includes(user.id) &&
|
||||||
|
!group.anyoneCanJoin &&
|
||||||
|
group.creatorId !== user.id
|
||||||
|
) {
|
||||||
|
throw new APIError(
|
||||||
|
400,
|
||||||
|
'User must be a member/creator of the group or group must be open to add markets to it.'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (!group.contractIds.includes(contractRef.id)) {
|
||||||
|
await createGroupLinks(group, [contractRef.id], auth.uid)
|
||||||
|
await groupDocRef.update({
|
||||||
|
contractIds: uniq([...group.contractIds, contractRef.id]),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const providerId = user.id
|
const providerId = user.id
|
||||||
|
|
||||||
if (outcomeType === 'BINARY' || outcomeType === 'PSEUDO_NUMERIC') {
|
if (outcomeType === 'BINARY' || outcomeType === 'PSEUDO_NUMERIC') {
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
import * as functions from 'firebase-functions'
|
import * as functions from 'firebase-functions'
|
||||||
import * as admin from 'firebase-admin'
|
import * as admin from 'firebase-admin'
|
||||||
import { Group, GroupLink } from '../../common/group'
|
import { Group, GroupLink } from '../../common/group'
|
||||||
import { getContract } from './utils'
|
import { getContract, log } from './utils'
|
||||||
import { uniq } from 'lodash'
|
import { uniq } from 'lodash'
|
||||||
|
import { removeUndefinedProps } from '../../common/util/object'
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
export const onUpdateGroup = functions.firestore
|
export const onUpdateGroup = functions.firestore
|
||||||
.document('groups/{groupId}')
|
.document('groups/{groupId}')
|
||||||
.onUpdate(async (change, context) => {
|
.onUpdate(async (change) => {
|
||||||
const prevGroup = change.before.data() as Group
|
const prevGroup = change.before.data() as Group
|
||||||
const group = change.after.data() as Group
|
const group = change.after.data() as Group
|
||||||
const userId = context.auth?.uid
|
|
||||||
|
|
||||||
// ignore the update we just made
|
// Ignore the activity update we just made
|
||||||
if (prevGroup.mostRecentActivityTime !== group.mostRecentActivityTime)
|
if (prevGroup.mostRecentActivityTime !== group.mostRecentActivityTime)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -24,19 +24,6 @@ export const onUpdateGroup = functions.firestore
|
||||||
//TODO: create notification with isSeeOnHref set to the group's /group/slug/questions url
|
//TODO: create notification with isSeeOnHref set to the group's /group/slug/questions url
|
||||||
// but first, let the new /group/slug/chat notification permeate so that we can differentiate between the two
|
// but first, let the new /group/slug/chat notification permeate so that we can differentiate between the two
|
||||||
}
|
}
|
||||||
if (prevGroup.contractIds.length != group.contractIds.length) {
|
|
||||||
if (prevGroup.contractIds.length < group.contractIds.length)
|
|
||||||
await createGroupLinks(
|
|
||||||
group,
|
|
||||||
group.contractIds.slice(prevGroup.contractIds.length),
|
|
||||||
userId
|
|
||||||
)
|
|
||||||
else
|
|
||||||
await removeGroupLinks(
|
|
||||||
group,
|
|
||||||
group.contractIds.slice(prevGroup.contractIds.length)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
await firestore
|
await firestore
|
||||||
.collection('groups')
|
.collection('groups')
|
||||||
|
@ -51,28 +38,31 @@ export async function createGroupLinks(
|
||||||
) {
|
) {
|
||||||
for (const contractId of contractIds) {
|
for (const contractId of contractIds) {
|
||||||
const contract = await getContract(contractId)
|
const contract = await getContract(contractId)
|
||||||
if (
|
if (!contract?.groupSlugs?.includes(group.slug)) {
|
||||||
contract?.groupSlugs?.includes(group.slug) &&
|
await firestore
|
||||||
contract?.groupLinks?.map((gl) => gl.groupId).includes(group.id)
|
.collection('contracts')
|
||||||
)
|
.doc(contractId)
|
||||||
continue
|
.update({
|
||||||
await firestore
|
groupSlugs: uniq([group.slug, ...(contract?.groupSlugs ?? [])]),
|
||||||
.collection('contracts')
|
})
|
||||||
.doc(contractId)
|
}
|
||||||
.update({
|
if (!contract?.groupLinks?.map((gl) => gl.groupId).includes(group.id)) {
|
||||||
groupSlugs: uniq([group.slug, ...(contract?.groupSlugs ?? [])]),
|
await firestore
|
||||||
groupLinks: [
|
.collection('contracts')
|
||||||
{
|
.doc(contractId)
|
||||||
groupId: group.id,
|
.update({
|
||||||
name: group.name,
|
groupLinks: [
|
||||||
slug: group.slug,
|
removeUndefinedProps({
|
||||||
userId,
|
groupId: group.id,
|
||||||
createdTime: Date.now(),
|
name: group.name,
|
||||||
} as GroupLink,
|
slug: group.slug,
|
||||||
contract?.groupLinks?.filter((link) => link.groupId !== group.id) ??
|
userId,
|
||||||
[],
|
createdTime: Date.now(),
|
||||||
],
|
}) as GroupLink,
|
||||||
})
|
...(contract?.groupLinks ?? []),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ export function ContractGroupsList(props: {
|
||||||
ignoreGroupIds: groupLinks.map((g) => g.groupId),
|
ignoreGroupIds: groupLinks.map((g) => g.groupId),
|
||||||
}}
|
}}
|
||||||
setSelectedGroup={(group) =>
|
setSelectedGroup={(group) =>
|
||||||
group && addContractToGroup(group, contract.id, user.id)
|
group && addContractToGroup(group, contract, user.id)
|
||||||
}
|
}
|
||||||
selectedGroup={undefined}
|
selectedGroup={undefined}
|
||||||
creator={user}
|
creator={user}
|
||||||
|
@ -62,9 +62,7 @@ export function ContractGroupsList(props: {
|
||||||
<Button
|
<Button
|
||||||
color={'gray-white'}
|
color={'gray-white'}
|
||||||
size={'xs'}
|
size={'xs'}
|
||||||
onClick={() =>
|
onClick={() => removeContractFromGroup(group, contract, user.id)}
|
||||||
removeContractFromGroup(group, contract.id, user.id)
|
|
||||||
}
|
|
||||||
>
|
>
|
||||||
<XIcon className="h-4 w-4 text-gray-500" />
|
<XIcon className="h-4 w-4 text-gray-500" />
|
||||||
</Button>
|
</Button>
|
||||||
|
|
|
@ -12,7 +12,6 @@ import { useState } from 'react'
|
||||||
import { useMemberGroups, useOpenGroups } from 'web/hooks/use-group'
|
import { useMemberGroups, useOpenGroups } from 'web/hooks/use-group'
|
||||||
import { User } from 'common/user'
|
import { User } from 'common/user'
|
||||||
import { searchInAny } from 'common/util/parse'
|
import { searchInAny } from 'common/util/parse'
|
||||||
import { uniq } from 'lodash'
|
|
||||||
|
|
||||||
export function GroupSelector(props: {
|
export function GroupSelector(props: {
|
||||||
selectedGroup: Group | undefined
|
selectedGroup: Group | undefined
|
||||||
|
@ -28,11 +27,14 @@ export function GroupSelector(props: {
|
||||||
const [isCreatingNewGroup, setIsCreatingNewGroup] = useState(false)
|
const [isCreatingNewGroup, setIsCreatingNewGroup] = useState(false)
|
||||||
const { showSelector, showLabel, ignoreGroupIds } = options
|
const { showSelector, showLabel, ignoreGroupIds } = options
|
||||||
const [query, setQuery] = useState('')
|
const [query, setQuery] = useState('')
|
||||||
const availableGroups = uniq(
|
const openGroups = useOpenGroups()
|
||||||
useOpenGroups()
|
const availableGroups = openGroups
|
||||||
.concat(useMemberGroups(creator?.id) ?? [])
|
.concat(
|
||||||
.filter((group) => !ignoreGroupIds?.includes(group.id))
|
(useMemberGroups(creator?.id) ?? []).filter(
|
||||||
)
|
(g) => !openGroups.map((og) => og.id).includes(g.id)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.filter((group) => !ignoreGroupIds?.includes(group.id))
|
||||||
const filteredGroups = availableGroups.filter((group) =>
|
const filteredGroups = availableGroups.filter((group) =>
|
||||||
searchInAny(query, group.name)
|
searchInAny(query, group.name)
|
||||||
)
|
)
|
||||||
|
|
|
@ -15,6 +15,8 @@ import {
|
||||||
listenForValue,
|
listenForValue,
|
||||||
listenForValues,
|
listenForValues,
|
||||||
} from './utils'
|
} from './utils'
|
||||||
|
import { Contract } from 'common/contract'
|
||||||
|
import { updateContract } from 'web/lib/firebase/contracts'
|
||||||
|
|
||||||
export const groups = coll<Group>('groups')
|
export const groups = coll<Group>('groups')
|
||||||
|
|
||||||
|
@ -131,14 +133,29 @@ export async function leaveGroup(group: Group, userId: string): Promise<void> {
|
||||||
|
|
||||||
export async function addContractToGroup(
|
export async function addContractToGroup(
|
||||||
group: Group,
|
group: Group,
|
||||||
contractId: string,
|
contract: Contract,
|
||||||
userId: string
|
userId: string
|
||||||
) {
|
) {
|
||||||
if (!canModifyGroupContracts(group, userId)) return
|
if (!canModifyGroupContracts(group, userId)) return
|
||||||
|
const newGroupLinks = [
|
||||||
|
...(contract.groupLinks ?? []),
|
||||||
|
{
|
||||||
|
groupId: group.id,
|
||||||
|
createdTime: Date.now(),
|
||||||
|
slug: group.slug,
|
||||||
|
userId,
|
||||||
|
name: group.name,
|
||||||
|
} as GroupLink,
|
||||||
|
]
|
||||||
|
// It's good to update the contract first, so the on-update-group trigger doesn't re-add them
|
||||||
|
await updateContract(contract.id, {
|
||||||
|
groupSlugs: uniq([...(contract.groupSlugs ?? []), group.slug]),
|
||||||
|
groupLinks: newGroupLinks,
|
||||||
|
})
|
||||||
|
|
||||||
if (!group.contractIds.includes(contractId)) {
|
if (!group.contractIds.includes(contract.id)) {
|
||||||
return await updateGroup(group, {
|
return await updateGroup(group, {
|
||||||
contractIds: uniq([...group.contractIds, contractId]),
|
contractIds: uniq([...group.contractIds, contract.id]),
|
||||||
})
|
})
|
||||||
.then(() => group)
|
.then(() => group)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
|
@ -150,13 +167,24 @@ export async function addContractToGroup(
|
||||||
|
|
||||||
export async function removeContractFromGroup(
|
export async function removeContractFromGroup(
|
||||||
group: Group,
|
group: Group,
|
||||||
contractId: string,
|
contract: Contract,
|
||||||
userId: string
|
userId: string
|
||||||
) {
|
) {
|
||||||
if (!canModifyGroupContracts(group, userId)) return
|
if (!canModifyGroupContracts(group, userId)) return
|
||||||
|
|
||||||
if (group.contractIds.includes(contractId)) {
|
if (contract.groupLinks?.map((l) => l.groupId).includes(group.id)) {
|
||||||
const newContractIds = group.contractIds.filter((id) => id !== contractId)
|
const newGroupLinks = contract.groupLinks?.filter(
|
||||||
|
(link) => link.slug !== group.slug
|
||||||
|
)
|
||||||
|
await updateContract(contract.id, {
|
||||||
|
groupSlugs:
|
||||||
|
contract.groupSlugs?.filter((slug) => slug !== group.slug) ?? [],
|
||||||
|
groupLinks: newGroupLinks ?? [],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (group.contractIds.includes(contract.id)) {
|
||||||
|
const newContractIds = group.contractIds.filter((id) => id !== contract.id)
|
||||||
return await updateGroup(group, {
|
return await updateGroup(group, {
|
||||||
contractIds: uniq(newContractIds),
|
contractIds: uniq(newContractIds),
|
||||||
})
|
})
|
||||||
|
|
|
@ -19,7 +19,7 @@ import {
|
||||||
import { formatMoney } from 'common/util/format'
|
import { formatMoney } from 'common/util/format'
|
||||||
import { removeUndefinedProps } from 'common/util/object'
|
import { removeUndefinedProps } from 'common/util/object'
|
||||||
import { ChoicesToggleGroup } from 'web/components/choices-toggle-group'
|
import { ChoicesToggleGroup } from 'web/components/choices-toggle-group'
|
||||||
import { addContractToGroup, getGroup } from 'web/lib/firebase/groups'
|
import { getGroup } from 'web/lib/firebase/groups'
|
||||||
import { Group } from 'common/group'
|
import { Group } from 'common/group'
|
||||||
import { useTracking } from 'web/hooks/use-tracking'
|
import { useTracking } from 'web/hooks/use-tracking'
|
||||||
import { useWarnUnsavedChanges } from 'web/hooks/use-warn-unsaved-changes'
|
import { useWarnUnsavedChanges } from 'web/hooks/use-warn-unsaved-changes'
|
||||||
|
|
|
@ -555,7 +555,7 @@ function AddContractButton(props: { group: Group; user: User }) {
|
||||||
Promise.all(
|
Promise.all(
|
||||||
contracts.map(async (contract) => {
|
contracts.map(async (contract) => {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
await addContractToGroup(group, contract.id, user.id)
|
await addContractToGroup(group, contract, user.id)
|
||||||
})
|
})
|
||||||
).then(() => {
|
).then(() => {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user