diff --git a/apps/mobile/src/modules/Group/addGroup/AddGroup.tsx b/apps/mobile/src/modules/Group/addGroup/AddGroup.tsx index b56b0e29..04acb9a7 100644 --- a/apps/mobile/src/modules/Group/addGroup/AddGroup.tsx +++ b/apps/mobile/src/modules/Group/addGroup/AddGroup.tsx @@ -5,8 +5,10 @@ import { useAddPermissions, useAuth, useCreateGroup, + useGetGroupPermission, } from 'afk_nostr_sdk'; import {Formik} from 'formik'; +import {useState} from 'react'; import {Text, View} from 'react-native'; import {SafeAreaView} from 'react-native-safe-area-context'; @@ -18,6 +20,9 @@ import stylesheet from './styles'; export const CreateGroup: React.FC = () => { const styles = useStyles(stylesheet); + const [groupId, setGroupId] = useState(); + const {data: permissionData} = useGetGroupPermission(groupId as any); + const {publicKey: pubkey} = useAuth(); const {showToast} = useToast(); const queryClient = useQueryClient(); @@ -42,6 +47,7 @@ export const CreateGroup: React.FC = () => { }, { onSuccess(data) { + setGroupId(groupId); // After Group Creation, first add permissions for the admin addPermission( { @@ -66,6 +72,7 @@ export const CreateGroup: React.FC = () => { { groupId: data.id, pubkey, + permissionData: permissionData as any, }, { onSuccess() { @@ -73,6 +80,9 @@ export const CreateGroup: React.FC = () => { queryClient.invalidateQueries({ queryKey: ['getAllGroups'], }); + queryClient.invalidateQueries({ + queryKey: ['getAllGroupMember'], + }); queryClient.invalidateQueries({ queryKey: ['getPermissionsByUserConnected', data.id], }); diff --git a/apps/mobile/src/modules/Group/all/AllGroup.tsx b/apps/mobile/src/modules/Group/all/AllGroup.tsx index c35c2bda..763e8638 100644 --- a/apps/mobile/src/modules/Group/all/AllGroup.tsx +++ b/apps/mobile/src/modules/Group/all/AllGroup.tsx @@ -1,5 +1,12 @@ import {useNavigation} from '@react-navigation/native'; -import {useGetGroupList} from 'afk_nostr_sdk'; +import {useQueryClient} from '@tanstack/react-query'; +import { + AdminGroupPermission, + useAddMember, + useAddPermissions, + useAuth, + useGetGroupList, +} from 'afk_nostr_sdk'; import { ActivityIndicator, FlatList, @@ -16,7 +23,10 @@ import stylesheet from './styles'; export default function AllGroupListComponent() { const {data, isPending} = useGetGroupList({}); - + const {mutate: addMember} = useAddMember(); + const queryClient = useQueryClient(); + const {mutate: addPermission} = useAddPermissions(); + const {publicKey} = useAuth(); const styles = useStyles(stylesheet); const navigation = useNavigation(); @@ -35,13 +45,57 @@ export default function AllGroupListComponent() { data={data.pages.flat()} renderItem={({item}: any) => ( + onPress={() => { + // Check if the group is pubic, if yes add the use to the group. + if ( + (item?.tags.find((tag: any) => tag[0] === 'access')?.[1] || 'public') === + 'public' && + publicKey !== item?.tags.find((tag: any) => tag[0] === 'p')?.[1] + ) { + // Add the member to the group + addMember( + { + groupId: item.originalGroupId, + pubkey: publicKey, + }, + { + onSuccess() { + // After successfully adding external member by pubkey, give them default view access. + addPermission( + { + groupId: item.originalGroupId, + pubkey: publicKey, + permissionName: [AdminGroupPermission.ViewAccess], + }, + { + onSuccess() { + queryClient.invalidateQueries({queryKey: ['getAllGroupMember']}); + queryClient.invalidateQueries({ + queryKey: ['getPermissionsByUserConnected', item.originalGroupId], + }); + + navigation.navigate('GroupChat', { + groupId: item.originalGroupId, + groupName: item.content, + groupAccess: + item?.tags.find((tag: any) => tag[0] === 'access')?.[1] || 'public', + }); + }, + onError() { + console.error('Something went wrong joining this group'); + }, + }, + ); + }, + }, + ); + } navigation.navigate('GroupChat', { groupId: item.originalGroupId, groupName: item.content, groupAccess: item?.tags.find((tag: any) => tag[0] === 'access')?.[1] || 'public', - }) - } + }); + }} style={styles.groupItem} > diff --git a/apps/mobile/src/modules/Group/groupDetail/GroupChatDetail.tsx b/apps/mobile/src/modules/Group/groupDetail/GroupChatDetail.tsx index b40b4738..c4f3f0c9 100644 --- a/apps/mobile/src/modules/Group/groupDetail/GroupChatDetail.tsx +++ b/apps/mobile/src/modules/Group/groupDetail/GroupChatDetail.tsx @@ -25,7 +25,7 @@ const GroupChatDetail: React.FC = ({navigation, rout const {publicKey: pubKey} = useAuth(); const queryClient = useQueryClient(); const {showToast} = useToast(); - + const {data: permissionData} = useGetGroupPermission(route.params.groupId); const {data: groupMetaData} = useGetGroupMetadata({groupId: route.params.groupId, pubKey}); const memberListData = useGetGroupMemberList({ groupId: route.params.groupId, @@ -99,6 +99,7 @@ const GroupChatDetail: React.FC = ({navigation, rout mutate( { groupId: route.params.groupId, + permissionData: permissionData as any, }, { onSuccess: () => { diff --git a/apps/mobile/src/modules/Group/memberAction/ViewRequest.tsx b/apps/mobile/src/modules/Group/memberAction/ViewRequest.tsx index 007e4337..a66356b9 100644 --- a/apps/mobile/src/modules/Group/memberAction/ViewRequest.tsx +++ b/apps/mobile/src/modules/Group/memberAction/ViewRequest.tsx @@ -4,6 +4,7 @@ import { useAddMember, useAddPermissions, useGetGroupMemberList, + useGetGroupPermission, useGetGroupRequest, } from 'afk_nostr_sdk'; import React, {useRef, useState} from 'react'; @@ -20,12 +21,12 @@ const GroupChatGroupRequest: React.FC = ({ navigation, route, }) => { - const theme = useTheme(); const queryClient = useQueryClient(); const [selected, setSelected] = useState(); const {showToast} = useToast(); const {mutate} = useAddMember(); const {mutate: addPermission} = useAddPermissions(); + const {data: permissionData} = useGetGroupPermission(route.params.groupId as any); const viewGroupRequest = useGetGroupRequest({ groupId: route.params.groupId, }); @@ -67,6 +68,7 @@ const GroupChatGroupRequest: React.FC = ({ { pubkey: selected?.tags.find((tag: any) => tag[0] === 'p')?.[1], groupId: route.params.groupId, + permissionData: permissionData as any, }, { onSuccess() { diff --git a/apps/mobile/src/modules/Group/memberAction/addMember.tsx b/apps/mobile/src/modules/Group/memberAction/addMember.tsx index 227c61bf..71537fae 100644 --- a/apps/mobile/src/modules/Group/memberAction/addMember.tsx +++ b/apps/mobile/src/modules/Group/memberAction/addMember.tsx @@ -4,6 +4,7 @@ import { useAddMember, useAddPermissions, useGetGroupMemberList, + useGetGroupPermission, } from 'afk_nostr_sdk'; import {Formik} from 'formik'; import {Text, View} from 'react-native'; @@ -25,6 +26,7 @@ export default function AddMemberView({ }); const {mutate: addMember} = useAddMember(); const {mutate: addPermission} = useAddPermissions(); + const {data: permissionData} = useGetGroupPermission(groupId as any); const queryClient = useQueryClient(); const {showToast} = useToast(); const styles = useStyles(stylesheet); @@ -58,6 +60,7 @@ export default function AddMemberView({ { pubkey: values.pubKey, groupId, + permissionData: permissionData as any, }, { onSuccess() { diff --git a/apps/mobile/src/modules/Group/memberAction/editGroup.tsx b/apps/mobile/src/modules/Group/memberAction/editGroup.tsx index 1a369370..4a37c8f8 100644 --- a/apps/mobile/src/modules/Group/memberAction/editGroup.tsx +++ b/apps/mobile/src/modules/Group/memberAction/editGroup.tsx @@ -1,6 +1,6 @@ import {NDKEvent} from '@nostr-dev-kit/ndk'; import {useQueryClient} from '@tanstack/react-query'; -import {useGroupEditMetadata} from 'afk_nostr_sdk'; +import {useGetGroupPermission, useGroupEditMetadata} from 'afk_nostr_sdk'; import {Formik} from 'formik'; import {Text, View} from 'react-native'; import {SafeAreaView} from 'react-native-safe-area-context'; @@ -22,6 +22,7 @@ export const EditGroup = ({ const {showToast} = useToast(); const queryClient = useQueryClient(); const {mutate} = useGroupEditMetadata(); + const {data: permissionData} = useGetGroupPermission(groupId); const initialValues = { name: '', @@ -37,6 +38,7 @@ export const EditGroup = ({ mutate( { groupId, + permissionData: permissionData as any, meta: { name: values.name, about: values.about, diff --git a/apps/mobile/src/modules/Group/memberAction/groupAction.tsx b/apps/mobile/src/modules/Group/memberAction/groupAction.tsx index 1dcc4fec..742ac2c8 100644 --- a/apps/mobile/src/modules/Group/memberAction/groupAction.tsx +++ b/apps/mobile/src/modules/Group/memberAction/groupAction.tsx @@ -34,7 +34,7 @@ const GroupAdminActions = ({ handleClose: () => void; groupId: string; }) => { - const {data: permissionData, isPending: permissionLoading} = useGetGroupPermission(groupId); + const {data: permissionData} = useGetGroupPermission(groupId); const {mutate: removeMember} = useRemoveMember(); const {mutate: addPermissions} = useAddPermissions(); const queryClient = useQueryClient(); @@ -98,6 +98,7 @@ const GroupAdminActions = ({ { groupId, pubkey: memberPubKey, + permissionData: permissionData as any, }, { onSuccess: () => { diff --git a/apps/mobile/src/modules/Group/message/GroupMessage.tsx b/apps/mobile/src/modules/Group/message/GroupMessage.tsx index 8e6fdc82..29102df1 100644 --- a/apps/mobile/src/modules/Group/message/GroupMessage.tsx +++ b/apps/mobile/src/modules/Group/message/GroupMessage.tsx @@ -77,6 +77,7 @@ const GroupChat: React.FC = ({navigation, route}) => { pubkey: publicKey, name: profile.data?.nip05, replyId: replyToId ?? (null as any), + permissionData: permissionData as any, }, { onSuccess() { @@ -103,13 +104,13 @@ const GroupChat: React.FC = ({navigation, route}) => { ); } - if (permissionLoading) { - return ( - - - - ); - } + // if (permissionLoading) { + // return ( + // + // + // + // ); + // } if ( !permissionData?.includes(AdminGroupPermission.ViewAccess) && diff --git a/packages/afk_nostr_sdk/src/hooks/group/private/useAddMember.ts b/packages/afk_nostr_sdk/src/hooks/group/private/useAddMember.ts index 71be9180..6e92c047 100644 --- a/packages/afk_nostr_sdk/src/hooks/group/private/useAddMember.ts +++ b/packages/afk_nostr_sdk/src/hooks/group/private/useAddMember.ts @@ -2,25 +2,19 @@ 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} = useAuth(); return useMutation({ mutationKey: ['addMemberGroup', ndk], - mutationFn: async (data: {pubkey: string; groupId: string}) => { - const hasPermission = await checkGroupPermission({ - groupId: data.groupId, - ndk, - pubkey: publicKey, - action: AdminGroupPermission.AddMember, - }); - - if (!hasPermission) { + mutationFn: async (data: { + pubkey: string; + groupId: string; + permissionData?: AdminGroupPermission[]; + }) => { + if (data?.permissionData && !data.permissionData.includes(AdminGroupPermission.AddMember)) { throw new Error('You do not have permission to add a member to this group'); } else { const event = new NDKEvent(ndk); diff --git a/packages/afk_nostr_sdk/src/hooks/group/private/useDeleteEvent.ts b/packages/afk_nostr_sdk/src/hooks/group/private/useDeleteEvent.ts index e2c6a83e..117485d7 100644 --- a/packages/afk_nostr_sdk/src/hooks/group/private/useDeleteEvent.ts +++ b/packages/afk_nostr_sdk/src/hooks/group/private/useDeleteEvent.ts @@ -2,25 +2,19 @@ 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], - mutationFn: async (data: {id: string; groupId: string}) => { - const hasPermission = await checkGroupPermission({ - groupId: data.groupId, - ndk, - pubkey, - action: AdminGroupPermission.DeleteEvent, - }); - - if (!hasPermission) { + mutationFn: async (data: { + id: string; + groupId: string; + permissionData?: AdminGroupPermission[]; + }) => { + if (data.permissionData && !data.permissionData.includes(AdminGroupPermission.DeleteEvent)) { throw new Error('You do not have permission to delete this event'); } else { const event = new NDKEvent(ndk); diff --git a/packages/afk_nostr_sdk/src/hooks/group/private/useDeleteGroup.ts b/packages/afk_nostr_sdk/src/hooks/group/private/useDeleteGroup.ts index 44116b56..2c2915e1 100644 --- a/packages/afk_nostr_sdk/src/hooks/group/private/useDeleteGroup.ts +++ b/packages/afk_nostr_sdk/src/hooks/group/private/useDeleteGroup.ts @@ -2,26 +2,16 @@ 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}) => { - const hasPermission = await checkGroupPermission({ - groupId: data.groupId, - ndk, - pubkey, - action: AdminGroupPermission.DeleteGroup, - }); - - if (!hasPermission) { + mutationFn: async (data: {groupId: string; permissionData: AdminGroupPermission[]}) => { + if (data.permissionData && !data.permissionData.includes(AdminGroupPermission.DeleteGroup)) { throw new Error('You do not have permission to delete group'); } else { const event = new NDKEvent(ndk); diff --git a/packages/afk_nostr_sdk/src/hooks/group/private/useEditGroupStatus.ts b/packages/afk_nostr_sdk/src/hooks/group/private/useEditGroupStatus.ts index f1b205cb..f42d6f12 100644 --- a/packages/afk_nostr_sdk/src/hooks/group/private/useEditGroupStatus.ts +++ b/packages/afk_nostr_sdk/src/hooks/group/private/useEditGroupStatus.ts @@ -2,9 +2,7 @@ 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 = { @@ -15,19 +13,18 @@ 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}) => { - const hasPermission = await checkGroupPermission({ - groupId: data.groupId, - ndk, - pubkey, - action: AdminGroupPermission.EditGroupStatus, - }); - - if (!hasPermission) { + mutationFn: async (data: { + groupId: string; + status: GroupStatus; + permissionData: AdminGroupPermission[]; + }) => { + if ( + data.permissionData && + !data.permissionData.includes(AdminGroupPermission.EditGroupStatus) + ) { throw new Error('You do not have permission to edit status'); } else { const event = new NDKEvent(ndk); diff --git a/packages/afk_nostr_sdk/src/hooks/group/private/useGetGroupMessage.ts b/packages/afk_nostr_sdk/src/hooks/group/private/useGetGroupMessage.ts index e2e61ede..38bed899 100644 --- a/packages/afk_nostr_sdk/src/hooks/group/private/useGetGroupMessage.ts +++ b/packages/afk_nostr_sdk/src/hooks/group/private/useGetGroupMessage.ts @@ -28,10 +28,10 @@ export const useGetGroupMessages = (options: UseGetActiveGroupListOptions) => { const events = await ndk.fetchEvents({ '#h': [options.groupId], kinds: [NDKKind.GroupNote, NDKKind.GroupReply], - authors: [options?.authors], + // authors: [options?.authors], search: options?.search, until: pageParam || Math.round(Date.now() / 1000), - limit: options?.limit || 20, + limit: options?.limit || 100, }); const eventMap = new Map(); diff --git a/packages/afk_nostr_sdk/src/hooks/group/private/useGroupEditMetadata.ts b/packages/afk_nostr_sdk/src/hooks/group/private/useGroupEditMetadata.ts index 184fe69f..a027670e 100644 --- a/packages/afk_nostr_sdk/src/hooks/group/private/useGroupEditMetadata.ts +++ b/packages/afk_nostr_sdk/src/hooks/group/private/useGroupEditMetadata.ts @@ -2,9 +2,7 @@ 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 = { @@ -23,19 +21,15 @@ interface UseGetGroupMetaData { export const useGroupEditMetadata = () => { const {ndk} = useNostrContext(); - const {publicKey: pubkey} = useAuth(); return useMutation({ mutationKey: ['editGroupMetadata', ndk], - mutationFn: async (data: {groupId: string; meta: UpdateMetaData}) => { - const hasPermission = checkGroupPermission({ - groupId: data.groupId, - ndk, - pubkey, - action: AdminGroupPermission.EditMetadata, - }); - - if (!hasPermission) { + mutationFn: async (data: { + groupId: string; + meta: UpdateMetaData; + permissionData: AdminGroupPermission[]; + }) => { + if (data.permissionData && !data.permissionData.includes(AdminGroupPermission.EditMetadata)) { throw new Error('You do not have permission to edit metadata'); } else { const editedTag = objectToTagArray(data.meta); diff --git a/packages/afk_nostr_sdk/src/hooks/group/private/useRemoveMember.ts b/packages/afk_nostr_sdk/src/hooks/group/private/useRemoveMember.ts index 96d42814..1c76de2e 100644 --- a/packages/afk_nostr_sdk/src/hooks/group/private/useRemoveMember.ts +++ b/packages/afk_nostr_sdk/src/hooks/group/private/useRemoveMember.ts @@ -2,25 +2,19 @@ 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}) => { - const hasPermission = await checkGroupPermission({ - groupId: data.groupId, - ndk, - pubkey, - action: AdminGroupPermission.RemoveUser, - }); - - if (!hasPermission) { + 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); diff --git a/packages/afk_nostr_sdk/src/hooks/group/private/useRemovePermissions.ts b/packages/afk_nostr_sdk/src/hooks/group/private/useRemovePermissions.ts index 15f4dbbe..209d5942 100644 --- a/packages/afk_nostr_sdk/src/hooks/group/private/useRemovePermissions.ts +++ b/packages/afk_nostr_sdk/src/hooks/group/private/useRemovePermissions.ts @@ -4,7 +4,6 @@ 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 useRemovePermissions = () => { @@ -15,17 +14,14 @@ export const useRemovePermissions = () => { mutationKey: ['removePermissions', ndk], mutationFn: async (data: { pubkey: string; + permissionData: AdminGroupPermission[]; permissionName: AdminGroupPermission[]; groupId: string; }) => { - const hasPermission = await checkGroupPermission({ - groupId: data.groupId, - ndk, - pubkey, - action: AdminGroupPermission.DeleteGroup, - }); - - if (!hasPermission) { + if ( + data.permissionData && + !data.permissionData.includes(AdminGroupPermission.RemovePermission) + ) { throw new Error('You do not have access to remove permission'); } else { const event = new NDKEvent(ndk); diff --git a/packages/afk_nostr_sdk/src/hooks/group/private/useSendGroupMessage.ts b/packages/afk_nostr_sdk/src/hooks/group/private/useSendGroupMessage.ts index ff69d4c8..5492f99b 100644 --- a/packages/afk_nostr_sdk/src/hooks/group/private/useSendGroupMessage.ts +++ b/packages/afk_nostr_sdk/src/hooks/group/private/useSendGroupMessage.ts @@ -2,14 +2,11 @@ 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'; // TODO export const useSendGroupMessages = () => { const {ndk} = useNostrContext(); - const {publicKey: pubkey} = useAuth(); return useMutation({ mutationKey: ['sendGroupMessage', ndk], @@ -19,15 +16,9 @@ export const useSendGroupMessages = () => { groupId: string; name?: string; replyId: string; + permissionData: AdminGroupPermission[]; }) => { - const hasPermission = await checkGroupPermission({ - groupId: data.groupId, - ndk, - pubkey, - action: AdminGroupPermission.ViewAccess, - }); - - if (!hasPermission) { + if (data.permissionData && !data.permissionData.includes(AdminGroupPermission.ViewAccess)) { throw new Error('You do not have permission to send message'); } else { const event = new NDKEvent(ndk);