Skip to content

Commit 92aa930

Browse files
committed
fix rls error
1 parent be15d5d commit 92aa930

4 files changed

Lines changed: 37 additions & 17 deletions

File tree

components/home/AddChannelModal.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {
55
createChannelSchema,
66
} from '@/types/client/channel';
77
import { zodResolver } from '@hookform/resolvers/zod';
8-
import { Dispatch, SetStateAction, useRef, useState } from 'react';
8+
import { Dispatch, SetStateAction, useEffect, useRef, useState } from 'react';
99
import { useForm } from 'react-hook-form';
10-
import { useSupabaseClient } from '@supabase/auth-helpers-react';
10+
import { useSupabaseClient, useUser } from '@supabase/auth-helpers-react';
1111
import { createChannel } from '@/services/channels.service';
1212
import { PostgrestError } from '@supabase/supabase-js';
1313
import {
@@ -34,8 +34,10 @@ export default function AddChannelModal({
3434

3535
const supabase = useSupabaseClient();
3636

37+
const user = useUser();
38+
3739
const getUserServerPerms = useGetUserPermsForServer();
38-
getUserServerPerms(supabase, serverId);
40+
3941
const userServerPerms = useUserServerPerms();
4042

4143
const {
@@ -54,10 +56,18 @@ export default function AddChannelModal({
5456
},
5557
});
5658

59+
useEffect(() => {
60+
if (getUserServerPerms) {
61+
if (user) {
62+
getUserServerPerms(supabase, serverId, user.id);
63+
}
64+
}
65+
}, [user, getUserServerPerms, supabase, serverId]);
66+
5767
const onSubmit = async (formData: CreateChannelInput) => {
5868
//
59-
console.log(serverId);
60-
console.log(userPerms & ChannelPermissions.MANAGE_MESSAGES);
69+
// console.log(serverId);
70+
// console.log(userPerms & ChannelPermissions.MANAGE_MESSAGES);
6171
const { data, error } = await createChannel(
6272
supabase,
6373
serverId,

components/home/ServerList.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ export default function ServerList() {
3131
const getServers = useGetServers();
3232

3333
const getUserServerPerms = useGetUserPermsForServer();
34-
getUserServerPerms(supabase, expanded);
3534
const userServerPerms = useUserServerPerms();
3635
console.log(userServerPerms & ServerPermissions.OWNER);
3736

3837
useEffect(() => {
3938
if (getServers) {
4039
if (user) {
40+
getUserServerPerms(supabase, expanded, user.id);
4141
getServers(supabase, user.id);
4242
}
4343
}
44-
}, [getServers, supabase, user]);
44+
}, [getServers, supabase, user, getUserServerPerms, expanded]);
4545

4646
//TODO: add isServer check
4747

@@ -116,7 +116,9 @@ export default function ServerList() {
116116
}
117117
})}
118118
</div>
119-
{userServerPerms & ServerPermissions.MANAGE_MESSAGES ? (
119+
{userServerPerms & ServerPermissions.MANAGE_MESSAGES ||
120+
userServerPerms & ServerPermissions.OWNER ||
121+
userServerPerms & ServerPermissions.ADMINISTRATOR ? (
120122
<Tooltip
121123
className="z-20 !opacity-100 font-semibold "
122124
style={{

lib/store.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ export interface UserPermsState {
155155
getUserPerms: (supabase: SupabaseClient<Database>, channelId: number) => void;
156156
getUserPermsForServer: (
157157
supabase: SupabaseClient<Database>,
158-
server_id: number
158+
server_id: number,
159+
userId?: string
159160
) => void;
160161
}
161162

@@ -176,10 +177,11 @@ const useUserPermsStore = create<UserPermsState>()((set) => ({
176177
set({ userPerms: data });
177178
}
178179
},
179-
getUserPermsForServer: async (supabase, serverId) => {
180+
getUserPermsForServer: async (supabase, serverId, userId) => {
180181
const { data, error } = await getCurrentUserServerPermissions(
181182
supabase,
182-
serverId
183+
serverId,
184+
userId
183185
);
184186

185187
if (error) {

services/server.service.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,12 @@ export type KickUserResponseError = KickUserResponse['error'];
230230

231231
export async function getCurrentUserServerPermissions(
232232
supabase: SupabaseClient<Database>,
233-
server_id: number
233+
server_id: number,
234+
userId?: string
234235
) {
235236
return await supabase.rpc('get_permission_flags_for_server_user', {
236237
s_id: server_id,
237-
p_id: (await supabase.auth.getUser()).data.user?.id!,
238+
p_id: userId ? userId : (await supabase.auth.getUser()).data.user?.id!,
238239
});
239240
}
240241

@@ -285,12 +286,17 @@ export async function getServerIdFromMessageId(
285286
message_id: number
286287
) {
287288
return await supabase
288-
.rpc('get_server_id_of_message', { m_id: message_id }).single();
289+
.rpc('get_server_id_of_message', { m_id: message_id })
290+
.single();
289291
}
290292

291-
type GetServerIdFromMessageIdResponse = Awaited<ReturnType<typeof getServerIdFromMessageId>>;
292-
export type GetServerIdFromMessageIdResponseSuccess = GetServerIdFromMessageIdResponse['data'];
293-
export type GetServerIdFromMessageIdResponseError = GetServerIdFromMessageIdResponse['error'];
293+
type GetServerIdFromMessageIdResponse = Awaited<
294+
ReturnType<typeof getServerIdFromMessageId>
295+
>;
296+
export type GetServerIdFromMessageIdResponseSuccess =
297+
GetServerIdFromMessageIdResponse['data'];
298+
export type GetServerIdFromMessageIdResponseError =
299+
GetServerIdFromMessageIdResponse['error'];
294300

295301
export async function addServerIcon(
296302
supabase: SupabaseClient<Database>,

0 commit comments

Comments
 (0)