Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ const result = await leaveConversation(conversationId, isSilent);
| `conversationId` | `string` | **Required** |
| `isSilent` | `boolean` | If `true`, send a system message to the conversation to notify the action |

#### Delete conversation
- Use `useConversation` hook to get `deleteConversation` function.

```typescript
import {useConversation} from 'rn-firebase-chat';

const {deleteConversation} = useConversation();

const result = await deleteConversation(conversationId, softDelete);
```
| Parameter | Type | Description |
| :--------------- | :-------- | :------------------------------------------------------------------------ |
| `conversationId` | `string` | **Required** |
| `softDelete` | `boolean` | If `true`, just delete the conversation from user's list. Otherwise, completely delete the conversation from lists of other members as well. Message history will not be cleared on both case but cannot be accessed. |

## Contributing

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
Expand Down
5 changes: 4 additions & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ const useTypingIndicator = (
const useConversation = () => {
const firebaseInstance = useRef(FirestoreServices.getInstance()).current;

return { leaveConversation: firebaseInstance.leaveConversation };
return {
leaveConversation: firebaseInstance.leaveConversation,
deleteConversation: firebaseInstance.deleteConversation,
};
};

export { useChatContext, useChatSelector, useTypingIndicator, useConversation };
72 changes: 72 additions & 0 deletions src/services/firebase/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,4 +823,76 @@ export class FirestoreServices {
return false;
}
};

checkConversationExist = async (id: string) => {
const conversation = await firestore()
.collection(
this.getUrlWithPrefix(
`${FireStoreCollection.users}/${this.userId}/${FireStoreCollection.conversations}`
)
)
.doc(id)
.get();

return conversation?.exists;
};

/**
* delete conversation from list
* @param forAllMembers indicates whether to remove conversation for all other members or simply remove from user's list
*/
deleteConversation = async (
conversationId: string,
forAllMembers?: boolean
): Promise<boolean> => {
try {
const isConversationExist =
!!conversationId && (await this.checkConversationExist(conversationId));
if (!isConversationExist) {
console.error('Conversation does not exist');
return false;
}

/** Get conversation ref from current user's conversations collection */
const userConversation = firestore()
.collection(
this.getUrlWithPrefix(
`${FireStoreCollection.users}/${this.userId}/${FireStoreCollection.conversations}`
)
)
.doc(conversationId);

/** Get ID array of conversation's partners (exclude current user) */
const partnerIds = (
(await userConversation.get())?.data() as ConversationProps
)?.members?.filter((e) => e !== this.userId);

/** Delete latest message of that conversation for user (exclude from list) */
await userConversation.delete();
if (!forAllMembers) return true;

/** Delete latest message of that conversation for all other partners */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we split the function even smaller?

const partnerBatch = firestore().batch();
partnerIds?.forEach(async (id) => {
if (id) {
const doc = firestore()
.collection(
this.getUrlWithPrefix(
`${FireStoreCollection.users}/${id}/${FireStoreCollection.conversations}`
)
)
.doc(conversationId);
partnerBatch.delete(doc);
}
});
await partnerBatch.commit();

return true;
} catch (e) {
if (e instanceof Error) {
throw new Error(e.message);
}
return false;
}
};
}