Skip to content

Commit

Permalink
fix: permission hook
Browse files Browse the repository at this point in the history
  • Loading branch information
addegbenga committed Aug 31, 2024
1 parent ac4292b commit e6f10f7
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 116 deletions.
2 changes: 0 additions & 2 deletions apps/mobile/src/modules/Group/groupDetail/GroupChatDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ const GroupChatDetail: React.FC<GroupChatDetailScreenProps> = ({navigation, rout
</Modalize>
<Modalize ref={addMemberModalizeRef}>
<AddMemberView
permissionData={permissionData as any}
handleClose={() => addMemberModalizeRef.current?.close()}
groupId={route.params.groupId ? route.params.groupId : ''}
/>
Expand All @@ -103,7 +102,6 @@ const GroupChatDetail: React.FC<GroupChatDetailScreenProps> = ({navigation, rout
mutate(
{
groupId: route.params.groupId,
permissionData: permissionData as any,
},
{
onSuccess: () => {
Expand Down
3 changes: 0 additions & 3 deletions apps/mobile/src/modules/Group/memberAction/addMember.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ import stylesheet from './styles';
export default function AddMemberView({
groupId,
handleClose,
permissionData,
}: {
groupId: string;
handleClose: () => void;
permissionData: AdminGroupPermission[];
}) {
const groupMembers = useGetGroupMemberList({
groupId,
Expand Down Expand Up @@ -61,7 +59,6 @@ export default function AddMemberView({
{
pubkey: values.pubKey,
groupId,
permissionData: permissionData as any,
},
{
onSuccess() {
Expand Down
3 changes: 1 addition & 2 deletions apps/mobile/src/modules/Group/memberAction/editGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import stylesheet from '../addGroup/styles';
export const EditGroup = ({
groupId,
handleClose,
permissionData,
}: {
groupId: string;
handleClose: () => void;
Expand All @@ -39,7 +38,7 @@ export const EditGroup = ({
mutate(
{
groupId,
permissionData: permissionData as any,

meta: {
name: values.name,
about: values.about,
Expand Down
1 change: 0 additions & 1 deletion apps/mobile/src/modules/Group/memberAction/groupAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ const GroupAdminActions = ({
{
groupId,
pubkey: memberPubKey,
permissionData: permissionData as any,
},
{
onSuccess: () => {
Expand Down
1 change: 0 additions & 1 deletion apps/mobile/src/modules/Group/message/GroupMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const GroupChat: React.FC<GroupChatScreenProps> = ({navigation, route}) => {
pubkey: publicKey,
name: profile.data?.nip05,
replyId: replyToId ?? (null as any),
permissionData: permissionData as any,
},
{
onSuccess() {
Expand Down
26 changes: 17 additions & 9 deletions packages/afk_nostr_sdk/src/hooks/group/private/useAddMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import {NDKEvent, NDKKind} from '@nostr-dev-kit/ndk';
import {useMutation} from '@tanstack/react-query';

import {useNostrContext} from '../../../context/NostrContext';
import {useAuth} from '../../../store';
import {AdminGroupPermission} from './useAddPermissions';
import {checkGroupPermission} from './useGetPermission';

export const useAddMember = () => {
const {ndk} = useNostrContext();
const {publicKey: pubkey} = useAuth();

return useMutation({
mutationKey: ['addMemberGroup', ndk],
Expand All @@ -14,17 +17,22 @@ export const useAddMember = () => {
groupId: string;
permissionData?: AdminGroupPermission[];
}) => {
if (data?.permissionData && !data.permissionData.includes(AdminGroupPermission.AddMember)) {
const event = new NDKEvent(ndk);
const hasPermission = await checkGroupPermission({
groupId: data.groupId,
ndk,
pubkey,
action: AdminGroupPermission.AddMember,
});
if (!hasPermission) {
throw new Error('You do not have permission to add a member to this group');
} else {
const event = new NDKEvent(ndk);
event.kind = NDKKind.GroupAdminAddUser;
event.tags = [
['d', data.groupId],
['p', data.pubkey],
];
return event.publish();
}
event.kind = NDKKind.GroupAdminAddUser;
event.tags = [
['d', data.groupId],
['p', data.pubkey],
];
return event.publish();
},
});
};
29 changes: 19 additions & 10 deletions packages/afk_nostr_sdk/src/hooks/group/private/useDeleteEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,38 @@ import {NDKEvent} from '@nostr-dev-kit/ndk';
import {useMutation} from '@tanstack/react-query';

import {useNostrContext} from '../../../context/NostrContext';
import {useAuth} from '../../../store';
import {AdminGroupPermission} from './useAddPermissions';
import {checkGroupPermission} from './useGetPermission';

// TODO
export const useDeleteEvent = () => {
const {ndk} = useNostrContext();
const {publicKey: pubkey} = useAuth();
return useMutation({
mutationKey: ['deleteEventGroup', ndk],
mutationKey: ['deleteEventGroup'],
mutationFn: async (data: {
id: string;
groupId: string;
permissionData?: AdminGroupPermission[];
}) => {
if (data.permissionData && !data.permissionData.includes(AdminGroupPermission.DeleteEvent)) {
const event = new NDKEvent(ndk);
const hasPermission = await checkGroupPermission({
groupId: data.groupId,
ndk,
pubkey,
action: AdminGroupPermission.DeleteEvent,
});

if (!hasPermission) {
throw new Error('You do not have permission to delete this event');
} else {
const event = new NDKEvent(ndk);
event.kind = 9005; //NDKKind.GroupAdminDeleteEvent;
event.tags = [
['d', data.groupId],
['e', data.id],
];
return event.publish();
}
event.kind = 9005; //NDKKind.GroupAdminDeleteEvent;
event.tags = [
['d', data.groupId],
['e', data.id],
];
return event.publish();
},
});
};
26 changes: 18 additions & 8 deletions packages/afk_nostr_sdk/src/hooks/group/private/useDeleteGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@ import {NDKEvent} from '@nostr-dev-kit/ndk';
import {useMutation} from '@tanstack/react-query';

import {useNostrContext} from '../../../context/NostrContext';
import {useAuth} from '../../../store';
import {AdminGroupPermission} from './useAddPermissions';
import {checkGroupPermission} from './useGetPermission';

// TODO
export const useDeleteGroup = () => {
const {ndk} = useNostrContext();
const {publicKey: pubkey} = useAuth();

return useMutation({
mutationKey: ['deleteGroup', ndk],
mutationFn: async (data: {groupId: string; permissionData: AdminGroupPermission[]}) => {
if (data.permissionData && !data.permissionData.includes(AdminGroupPermission.DeleteGroup)) {
mutationKey: ['deleteGroup'],
mutationFn: async (data: {groupId: string}) => {
const event = new NDKEvent(ndk);
const hasPermission = await checkGroupPermission({
groupId: data.groupId,
ndk,
pubkey,
action: AdminGroupPermission.DeleteGroup,
});

if (!hasPermission) {
throw new Error('You do not have permission to delete group');
} else {
const event = new NDKEvent(ndk);
event.kind = 9008; // NDKKind.GroupAdminDeleteGroup;
event.tags = [['d', data.groupId]];
return event.publish();
}

event.kind = 9008; // NDKKind.GroupAdminDeleteGroup;
event.tags = [['d', data.groupId]];
return event.publish();
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {NDKEvent, NDKKind} from '@nostr-dev-kit/ndk';
import {useMutation} from '@tanstack/react-query';

import {useNostrContext} from '../../../context/NostrContext';
import {useAuth} from '../../../store';
import {AdminGroupPermission} from './useAddPermissions';
import {checkGroupPermission} from './useGetPermission';
import {objectToTagArray} from './util';

type GroupStatus = {
Expand All @@ -13,25 +15,25 @@ type GroupStatus = {
// TODO
export const useGroupEditStatus = () => {
const {ndk} = useNostrContext();
const {publicKey: pubkey} = useAuth();

return useMutation({
mutationKey: ['editGroupStatus', ndk],
mutationFn: async (data: {
groupId: string;
status: GroupStatus;
permissionData: AdminGroupPermission[];
}) => {
if (
data.permissionData &&
!data.permissionData.includes(AdminGroupPermission.EditGroupStatus)
) {
mutationKey: ['editGroupStatus'],
mutationFn: async (data: {groupId: string; status: GroupStatus}) => {
const event = new NDKEvent(ndk);
const hasPermission = await checkGroupPermission({
groupId: data.groupId,
ndk,
pubkey,
action: AdminGroupPermission.EditGroupStatus,
});

if (!hasPermission) {
throw new Error('You do not have permission to edit status');
} else {
const event = new NDKEvent(ndk);
event.kind = NDKKind.GroupAdminEditStatus;
event.tags = [['d', data.groupId], objectToTagArray(data.status)[0]];
return event.publish();
}
event.kind = NDKKind.GroupAdminEditStatus;
event.tags = [['d', data.groupId], objectToTagArray(data.status)[0]];
return event.publish();
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const fetchPermissions = async ({
kinds: [9003],
'#h': [groupId],
'#p': [pubkey],
since: 0,
});

if (!events || events.size === 0) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {NDKEvent, NDKKind} from '@nostr-dev-kit/ndk';
import {useMutation, useQuery} from '@tanstack/react-query';

import {useNostrContext} from '../../../context/NostrContext';
import {useAuth} from '../../../store';
import {AdminGroupPermission} from './useAddPermissions';
import {checkGroupPermission} from './useGetPermission';
import {objectToTagArray} from './util';

type UpdateMetaData = {
Expand All @@ -21,26 +23,33 @@ interface UseGetGroupMetaData {

export const useGroupEditMetadata = () => {
const {ndk} = useNostrContext();
const {publicKey: pubkey} = useAuth();

return useMutation({
mutationKey: ['editGroupMetadata', ndk],
mutationFn: async (data: {
groupId: string;
meta: UpdateMetaData;
permissionData: AdminGroupPermission[];
permissionData?: AdminGroupPermission[];
}) => {
if (data.permissionData && !data.permissionData.includes(AdminGroupPermission.EditMetadata)) {
const event = new NDKEvent(ndk);
const hasPermission = checkGroupPermission({
groupId: data.groupId,
ndk,
pubkey,
action: AdminGroupPermission.EditMetadata,
});

if (!hasPermission) {
throw new Error('You do not have permission to edit metadata');
} else {
const editedTag = objectToTagArray(data.meta);
}
const editedTag = objectToTagArray(data.meta);

const event = new NDKEvent(ndk);
event.content = data.meta.name;
event.kind = NDKKind.GroupAdminEditMetadata;
event.tags = [['h', data.groupId], ['d', data.groupId], ...editedTag];
event.content = data.meta.name;
event.kind = NDKKind.GroupAdminEditMetadata;
event.tags = [['h', data.groupId], ['d', data.groupId], ...editedTag];

return event.publish();
}
return event.publish();
},
});
};
Expand Down
39 changes: 22 additions & 17 deletions packages/afk_nostr_sdk/src/hooks/group/private/useRemoveMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,37 @@ import {NDKEvent, NDKKind} from '@nostr-dev-kit/ndk';
import {useMutation} from '@tanstack/react-query';

import {useNostrContext} from '../../../context/NostrContext';
import {useAuth} from '../../../store';
import {AdminGroupPermission} from './useAddPermissions';
import {checkGroupPermission} from './useGetPermission';

export const useRemoveMember = () => {
const {ndk} = useNostrContext();
const {publicKey: pubkey} = useAuth();

return useMutation({
mutationKey: ['removeMemberGroup'],
mutationFn: async (data: {
pubkey: string;
groupId: string;
permissionData: AdminGroupPermission[];
}) => {
if (!data.permissionData && !data.permissionData.includes(AdminGroupPermission.RemoveUser)) {
throw new Error('You do not have permission to remove member');
} else {
const event = new NDKEvent(ndk);
event.kind = NDKKind.GroupAdminRemoveUser;
event.tags = [
['h', data.groupId],
['d', data.groupId],
['p', data.pubkey],
];
mutationFn: async (data: {pubkey: string; groupId: string}) => {
const event = new NDKEvent(ndk);
const hasPermission = await checkGroupPermission({
groupId: data.groupId,
ndk,
pubkey,
action: AdminGroupPermission.RemoveUser,
});

event.publish();
return event;
if (!hasPermission) {
throw new Error('You do not have permission to remove member');
}
event.kind = NDKKind.GroupAdminRemoveUser;
event.tags = [
['h', data.groupId],
['d', data.groupId],
['p', data.pubkey],
];

event.publish();
return event;
},
});
};
Loading

0 comments on commit e6f10f7

Please sign in to comment.