Skip to content

Commit

Permalink
remove uses of group_role view
Browse files Browse the repository at this point in the history
  • Loading branch information
sipec committed Jul 30, 2024
1 parent 716a496 commit 33d77d6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 188 deletions.
10 changes: 4 additions & 6 deletions web/components/contract/market-topics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,16 @@ export function MarketTopics(props: TopicRowProps) {
const { contract, dashboards, isSpiceMarket } = props
const user = useUser()
const isCreator = contract.creatorId === user?.id
const adminGroups = useGroupsWhereUserHasRole(user?.id)
const myEditableGroupIds = useGroupsWhereUserHasRole(user?.id)
const isMod = useAdminOrMod()
const canEdit = isMod || isCreator || (adminGroups && adminGroups.length > 0)
const canEdit =
isMod || isCreator || (myEditableGroupIds && myEditableGroupIds.length > 0)

const topicPickerProps = useTopicsWithContract(contract.id, props.topics)
const { topics } = topicPickerProps

const canEditGroup = (groupId: string) =>
isCreator ||
isMod ||
// if user has admin role in that group
!!(adminGroups && adminGroups.some((g) => g.group_id === groupId))
isCreator || isMod || !!myEditableGroupIds?.includes(groupId)

return (
<>
Expand Down
46 changes: 1 addition & 45 deletions web/hooks/use-group-supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from 'web/lib/supabase/group'
import {
getGroupsWhereUserHasRole,
getMyGroupRoles,
listGroupsBySlug,
} from 'web/lib/supabase/groups'
import { usePersistentInMemoryState } from './use-persistent-in-memory-state'
Expand Down Expand Up @@ -120,19 +119,13 @@ export function useGroupRole(
return isMod ? 'admin' : userRole
}

export type Member = Row<'group_role'>

export async function getTranslatedMemberRole(
groupId: string | undefined,
user: User | null | undefined
) {
if (user && groupId) {
try {
const { data } = await getMemberRole(user, groupId)
if (data.length == 0) {
return null
}
return (data[0]?.role ?? 'member') as GroupRole
return await getMemberRole(user, groupId)
} catch (e) {
console.error(e)
}
Expand All @@ -156,43 +149,6 @@ export function useListGroupsBySlug(groupSlugs: string[]) {
return useAsyncData(groupSlugs, listGroupsBySlug)
}

export function useMyGroupRoles(userId: string | undefined) {
return useAsyncData(userId, getMyGroupRoles)
}

export function useGroupsWhereUserHasRole(userId: string | undefined) {
return useAsyncData(userId, getGroupsWhereUserHasRole)
}

export const useGroupRoles = (user: User | undefined | null) => {
const [roles, setRoles] =
useState<Awaited<ReturnType<typeof getMyGroupRoles>>>()

useEffect(() => {
if (user)
getMyGroupRoles(user.id).then((roles) =>
setRoles(
roles?.sort(
(a, b) =>
(b.role === 'admin' ? 2 : b.role === 'moderator' ? 1 : 0) -
(a.role === 'admin' ? 2 : a.role === 'moderator' ? 1 : 0)
)
)
)
}, [])

const groups: Group[] =
roles?.map((g) => ({
id: g.group_id!,
name: g.group_name!,
slug: g.group_slug!,
privacyStatus: g.privacy_status as any,
totalMembers: g.total_members!,
creatorId: g.creator_id!,
createdTime: g.createdtime!,
postIds: [],
importanceScore: 0,
})) ?? []

return { roles, groups }
}
112 changes: 4 additions & 108 deletions web/lib/supabase/group.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,20 @@
import { run } from 'common/supabase/utils'
import { uniqBy } from 'lodash'
import { GroupRole as GroupRoleType } from 'common/group'
import { User } from '../firebase/users'
import { db } from './db'
import { convertGroup } from 'common/supabase/groups'

// functions called for one group
export async function getNumGroupMembers(groupId: string) {
const { count } = await run(
db
.from('group_members')
.select('*', { head: true, count: 'exact' })
.eq('group_id', groupId)
)
return count as number
}

export async function getGroupOfRole(groupId: string, role: GroupRoleType) {
const roleMembers = await run(
db
.from('group_role')
.select('*')
.eq('group_id', groupId)
.eq('role', role)
.order('name')
)
return roleMembers
}
import { GroupRole } from 'common/group'

export const MEMBER_LOAD_NUM = 50

export async function getGroupMembers(
groupId: string,
offset: number,
start?: number
) {
const followers = await run(
db
.from('group_role')
.select('*')
.eq('group_id', groupId)
.eq('role', 'member')
.order('name')
.range(
start ? start : offset * MEMBER_LOAD_NUM,
offset * MEMBER_LOAD_NUM + MEMBER_LOAD_NUM - 1
)
)
return followers
}

export async function getMemberRole(user: User, groupId: string) {
const followers = await run(
const { data } = await run(
db
.from('group_role')
.from('group_members')
.select('role')
.eq('group_id', groupId)
.eq('member_id', user.id)
)
return followers
return data[0]?.role ?? ('member' as GroupRole)
}

export async function getGroupContractIds(groupId: string) {
Expand All @@ -68,66 +24,6 @@ export async function getGroupContractIds(groupId: string) {
return groupContractIds.data.map((gids) => gids.contract_id)
}

export async function searchUserInGroup(
groupId: string,
prompt: string,
limit: number
) {
if (prompt === '') {
const { data } = await run(
db
.from('group_role')
.select('*')
.eq('group_id', groupId)
.order('name')
.limit(limit)
)
return data
}

const [{ data: exactData }, { data: prefixData }, { data: containsData }] =
await Promise.all([
run(
db
.from('group_role')
.select('*')
.eq('group_id', groupId)
.or(`username.ilike.${prompt},name.ilike.${prompt}`)
.limit(limit)
),
run(
db
.from('group_role')
.select('*')
.eq('group_id', groupId)
.or(`username.ilike.${prompt}%,name.ilike.${prompt}%`)
.limit(limit)
),
run(
db
.from('group_role')
.select('*')
.eq('group_id', groupId)
.or(`username.ilike.%${prompt}%,name.ilike.%${prompt}%`)
.limit(limit)
),
])
return uniqBy(
[...exactData, ...prefixData, ...containsData],
'member_id'
).slice(0, limit)
}

export const getGroupWithFields = async (groupId: string) => {
const { data } = await run(db.from('groups').select('*').eq('id', groupId))
if (data && data.length > 0) {
const result = data[0]
return convertGroup(result)
} else {
return null
}
}

export async function getGroup(groupId: string) {
const { data } = await run(
db.from('groups').select('data, id').eq('id', groupId)
Expand Down
32 changes: 3 additions & 29 deletions web/lib/supabase/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,17 @@ export async function getMemberGroupsCount(userId: string) {
return count
}

export type GroupAndRoleType = Row<'group_role'>

// gets all groups where the user is an admin or moderator
export async function getGroupsWhereUserHasRole(userId: string) {
const { data } = await run(
db
.from('group_role')
.select()
.from('group_members')
.select('group_id')
.eq('member_id', userId)
.or('role.eq.admin,role.eq.moderator')
)

return data as GroupAndRoleType[]
}

export async function getMyGroupRoles(userId: string) {
const { data } = await run(
db
.from('group_role')
.select()
.eq('member_id', userId)
.order('createdtime', { ascending: false })
)
return data
return data.map((row) => row.group_id)
}

export async function getGroupBySlug(groupSlug: string) {
Expand Down Expand Up @@ -108,19 +95,6 @@ export async function listGroupsBySlug(groupSlugs: string[]) {
return []
}

export async function getYourNonPrivateNonModeratorGroups(userId: string) {
const { data } = await run(
db
.from('group_role')
.select('*')
.eq('member_id', userId)
.neq('role', 'moderator')
.neq('role', 'admin')
.order('createdtime', { ascending: false })
)
return data ?? []
}

export async function unfollowTopic(groupId: string, userId: string) {
await run(
db
Expand Down

0 comments on commit 33d77d6

Please sign in to comment.