Skip to content

Commit

Permalink
fix: add member automatically to a public group and fix permission check
Browse files Browse the repository at this point in the history
  • Loading branch information
addegbenga committed Aug 31, 2024
1 parent 827f705 commit f740f05
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 110 deletions.
10 changes: 10 additions & 0 deletions apps/mobile/src/modules/Group/addGroup/AddGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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();
Expand All @@ -42,6 +47,7 @@ export const CreateGroup: React.FC = () => {
},
{
onSuccess(data) {
setGroupId(groupId);
// After Group Creation, first add permissions for the admin
addPermission(
{
Expand All @@ -66,13 +72,17 @@ export const CreateGroup: React.FC = () => {
{
groupId: data.id,
pubkey,
permissionData: permissionData as any,
},
{
onSuccess() {
showToast({type: 'success', title: 'Group Created successfully'});
queryClient.invalidateQueries({
queryKey: ['getAllGroups'],
});
queryClient.invalidateQueries({
queryKey: ['getAllGroupMember'],
});
queryClient.invalidateQueries({
queryKey: ['getPermissionsByUserConnected', data.id],
});
Expand Down
64 changes: 59 additions & 5 deletions apps/mobile/src/modules/Group/all/AllGroup.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<MainStackNavigationProps>();

Expand All @@ -35,13 +45,57 @@ export default function AllGroupListComponent() {
data={data.pages.flat()}
renderItem={({item}: any) => (
<TouchableOpacity
onPress={() =>
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}
>
<View style={styles.groupInfo}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const GroupChatDetail: React.FC<GroupChatDetailScreenProps> = ({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,
Expand Down Expand Up @@ -99,6 +99,7 @@ const GroupChatDetail: React.FC<GroupChatDetailScreenProps> = ({navigation, rout
mutate(
{
groupId: route.params.groupId,
permissionData: permissionData as any,
},
{
onSuccess: () => {
Expand Down
4 changes: 3 additions & 1 deletion apps/mobile/src/modules/Group/memberAction/ViewRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
useAddMember,
useAddPermissions,
useGetGroupMemberList,
useGetGroupPermission,
useGetGroupRequest,
} from 'afk_nostr_sdk';
import React, {useRef, useState} from 'react';
Expand All @@ -20,12 +21,12 @@ const GroupChatGroupRequest: React.FC<GroupChatMemberRequestScreenProps> = ({
navigation,
route,
}) => {
const theme = useTheme();
const queryClient = useQueryClient();
const [selected, setSelected] = useState<any>();
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,
});
Expand Down Expand Up @@ -67,6 +68,7 @@ const GroupChatGroupRequest: React.FC<GroupChatMemberRequestScreenProps> = ({
{
pubkey: selected?.tags.find((tag: any) => tag[0] === 'p')?.[1],
groupId: route.params.groupId,
permissionData: permissionData as any,
},
{
onSuccess() {
Expand Down
3 changes: 3 additions & 0 deletions apps/mobile/src/modules/Group/memberAction/addMember.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
useAddMember,
useAddPermissions,
useGetGroupMemberList,
useGetGroupPermission,
} from 'afk_nostr_sdk';
import {Formik} from 'formik';
import {Text, View} from 'react-native';
Expand All @@ -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);
Expand Down Expand Up @@ -58,6 +60,7 @@ export default function AddMemberView({
{
pubkey: values.pubKey,
groupId,
permissionData: permissionData as any,
},
{
onSuccess() {
Expand Down
4 changes: 3 additions & 1 deletion apps/mobile/src/modules/Group/memberAction/editGroup.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -22,6 +22,7 @@ export const EditGroup = ({
const {showToast} = useToast();
const queryClient = useQueryClient();
const {mutate} = useGroupEditMetadata();
const {data: permissionData} = useGetGroupPermission(groupId);

const initialValues = {
name: '',
Expand All @@ -37,6 +38,7 @@ export const EditGroup = ({
mutate(
{
groupId,
permissionData: permissionData as any,
meta: {
name: values.name,
about: values.about,
Expand Down
3 changes: 2 additions & 1 deletion apps/mobile/src/modules/Group/memberAction/groupAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -98,6 +98,7 @@ const GroupAdminActions = ({
{
groupId,
pubkey: memberPubKey,
permissionData: permissionData as any,
},
{
onSuccess: () => {
Expand Down
15 changes: 8 additions & 7 deletions apps/mobile/src/modules/Group/message/GroupMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const GroupChat: React.FC<GroupChatScreenProps> = ({navigation, route}) => {
pubkey: publicKey,
name: profile.data?.nip05,
replyId: replyToId ?? (null as any),
permissionData: permissionData as any,
},
{
onSuccess() {
Expand All @@ -103,13 +104,13 @@ const GroupChat: React.FC<GroupChatScreenProps> = ({navigation, route}) => {
);
}

if (permissionLoading) {
return (
<SafeAreaView style={styles.container}>
<ActivityIndicator />
</SafeAreaView>
);
}
// if (permissionLoading) {
// return (
// <SafeAreaView style={styles.container}>
// <ActivityIndicator />
// </SafeAreaView>
// );
// }

if (
!permissionData?.includes(AdminGroupPermission.ViewAccess) &&
Expand Down
18 changes: 6 additions & 12 deletions packages/afk_nostr_sdk/src/hooks/group/private/useAddMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 6 additions & 12 deletions packages/afk_nostr_sdk/src/hooks/group/private/useDeleteEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 2 additions & 12 deletions packages/afk_nostr_sdk/src/hooks/group/private/useDeleteGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit f740f05

Please sign in to comment.