Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: updated chat related functionality to work with the updated chat schema #2286

Merged
merged 30 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1ca936b
Initial commit
disha1202 Jun 8, 2024
c42da5e
Updated the UI of chat screen
disha1202 Jun 27, 2024
3742346
feat: added support to create group chat
disha1202 Jun 30, 2024
06f4af4
implemented group chats
disha1202 Jul 2, 2024
c18b97b
Implemented logic to send messages in group chat
disha1202 Jul 7, 2024
cbde8f7
refactor chat functionality
disha1202 Jul 12, 2024
9950269
fix format
disha1202 Jul 12, 2024
77a392c
refactor code
disha1202 Jul 13, 2024
eb54545
fix failing tests
disha1202 Jul 15, 2024
09427e6
Merge branch 'develop' of https://github.com/PalisadoesFoundation/tal…
disha1202 Jul 15, 2024
6fbaaa2
removed unwanted code
disha1202 Jul 15, 2024
8fffdc5
fix: formatting issues and reverted unwanted changes
disha1202 Jul 15, 2024
f76dc3e
removed unwanted code
disha1202 Jul 15, 2024
d4864a8
implemented reply functionality for direct chat
disha1202 Jul 20, 2024
75fa6c3
implemented reply functionality
disha1202 Jul 27, 2024
58086e2
Updated chat schema
disha1202 Jul 28, 2024
4d6f9a6
fix: contact card ui
disha1202 Jul 31, 2024
a68dcb4
removed unwanted code
disha1202 Jul 31, 2024
15385ae
fix: create group chat functionality
disha1202 Aug 18, 2024
1ee07f3
Merge branch 'develop' into schema-update
disha1202 Sep 21, 2024
4525924
fix: formatting issues
disha1202 Sep 21, 2024
f4b6fd0
fix: formatting issue
disha1202 Sep 21, 2024
74899fd
fix: formatting issue
disha1202 Sep 21, 2024
450b587
fix: type errors
disha1202 Sep 21, 2024
b1790e2
fix: tsdoc error
disha1202 Sep 21, 2024
fe66f0a
fix: formatting errors
disha1202 Sep 21, 2024
352b6b5
improve code coverage
disha1202 Sep 21, 2024
9a661e5
improved code coverage
disha1202 Sep 22, 2024
182eded
Merge branch 'develop' into schema-update
disha1202 Oct 17, 2024
e6e5c24
Update Chat.tsx
disha1202 Oct 17, 2024
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
38 changes: 38 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,32 @@ enum MaritalStatus {
WIDOWED
}

type Chat {
_id: ID!
isGroup: Boolean!
name: String
createdAt: DateTime!
creator: User
messages: [ChatMessage]
organization: Organization
updatedAt: DateTime!
users: [User!]!
admins: [User]
lastMessageId: String
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Use ID type instead of String for lastMessageId

The lastMessageId field represents an identifier and is currently typed as String. For consistency and type safety, consider using the ID scalar type.

Apply this diff to update the type:

-  lastMessageId: String
+  lastMessageId: ID
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
lastMessageId: String
lastMessageId: ID

}
Comment on lines +539 to +551
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider using pagination for the messages field in the Chat type

The messages field is defined as messages: [ChatMessage]. If a chat contains many messages, returning all messages in a single query could lead to performance issues. It's recommended to use a paginated connection for the messages field to improve performance and scalability.


type ChatMessage {
_id: ID!
createdAt: DateTime!
chatMessageBelongsTo: Chat!
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Rename chatMessageBelongsTo to chat for clarity

The field chatMessageBelongsTo can be renamed to chat to improve readability and adhere to common naming conventions.

Apply this diff to rename the field:

 type ChatMessage {
   _id: ID!
   createdAt: DateTime!
-  chatMessageBelongsTo: Chat!
+  chat: Chat!
   messageContent: String!

Committable suggestion was skipped due to low confidence.

messageContent: String!
type: String!
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Define an enum for the type field instead of using String!

The type field indicates the message type. Using an enum can enforce valid values and enhance schema robustness.

First, define the enum:

enum ChatMessageType {
  TEXT
  IMAGE
  VIDEO
  // Add other message types as needed
}

Then update the type field:

   messageContent: String!
-  type: String!
+  type: ChatMessageType!
   replyTo: ChatMessage

replyTo: ChatMessage
sender: User!
deletedBy: [User]
updatedAt: DateTime!
}

type MaximumLengthError implements FieldError {
message: String!
path: [String!]!
Expand Down Expand Up @@ -665,6 +691,7 @@ type Mutation {
): Advertisement!
createAgendaCategory(input: CreateAgendaCategoryInput!): AgendaCategory!
createComment(data: CommentInput!, postId: ID!): Comment
createChat(data: chatInput!): Chat!
createDirectChat(data: createChatInput!): DirectChat!
createDonation(
amount: Float!
Expand Down Expand Up @@ -739,6 +766,7 @@ type Mutation {
revokeRefreshTokenForUser: Boolean!
saveFcmToken(token: String): Boolean!
sendMembershipRequest(organizationId: ID!): MembershipRequest!
sendMessageToChat(chatId: ID!, messageContent: String!, type: String!, replyTo: ID): ChatMessage!
sendMessageToDirectChat(
chatId: ID!
messageContent: String!
Expand Down Expand Up @@ -1066,6 +1094,8 @@ type Query {
checkAuth: User!
customDataByOrganization(organizationId: ID!): [UserCustomData!]!
customFieldsByOrganization(id: ID!): [OrganizationCustomField]
chatById(id: ID!): Chat!
chatsByUserId(id: ID!): [Chat]
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider paginating the chatsByUserId query

The chatsByUserId query returns a list of Chat objects. To handle cases where a user is part of many chats, consider implementing pagination to improve performance and client-side handling.

Update the query signature:

-  chatsByUserId(id: ID!): [Chat]
+  chatsByUserId(id: ID!, after: String, first: Int): ChatConnection

Define the ChatConnection types:

type ChatConnection {
  edges: [ChatEdge]
  pageInfo: PageInfo
}

type ChatEdge {
  node: Chat
  cursor: String
}

type PageInfo {
  hasNextPage: Boolean!
  endCursor: String
}

directChatsByUserID(id: ID!): [DirectChat]
directChatsMessagesByChatID(id: ID!): [DirectChatMessage]
directChatById(id: ID!): DirectChat
Expand Down Expand Up @@ -1166,6 +1196,7 @@ enum Status {

type Subscription {
directMessageChat: MessageChat
messageSentToChat(userId: ID!): ChatMessage
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Correct the argument of messageSentToChat subscription to chatId

The messageSentToChat subscription currently takes userId: ID! as an argument. Since it's intended to subscribe to messages sent to a specific chat, the argument should be chatId: ID!.

Apply this diff to correct the argument:

 type Subscription {
   directMessageChat: MessageChat
-  messageSentToChat(userId: ID!): ChatMessage
+  messageSentToChat(chatId: ID!): ChatMessage
   messageSentToDirectChat(userId: ID!): DirectChatMessage

Committable suggestion was skipped due to low confidence.

messageSentToDirectChat(userId: ID!): DirectChatMessage
messageSentToGroupChat(userId: ID!): GroupChatMessage
onPluginUpdate: Plugin
Expand Down Expand Up @@ -1593,3 +1624,10 @@ input createUserFamilyInput {
title: String!
userIds: [ID!]!
}

input chatInput {
isGroup: Boolean!
organizationId: ID
userIds: [ID!]!
name: String
}
Comment on lines +1628 to +1633
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure the name field is required when isGroup is true

In the chatInput type, the name field is currently optional. However, when creating a group chat (isGroup: true), a name is essential for identification and user experience. Consider enforcing this by making the name field non-nullable or implementing validation logic to ensure a name is provided when isGroup is true.

139 changes: 135 additions & 4 deletions src/GraphQl/Mutations/OrganizationMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,93 @@ export const CREATE_DIRECT_CHAT = gql`
}
`;

export const CREATE_CHAT = gql`
mutation createChat(
$userIds: [ID!]!
$organizationId: ID
$isGroup: Boolean!
$name: String
) {
createChat(
data: {
userIds: $userIds
organizationId: $organizationId
isGroup: $isGroup
name: $name
}
) {
_id
}
}
`;
Comment on lines +88 to +106
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Inconsistent naming: Use 'title' instead of 'name' for consistency

In the CREATE_CHAT mutation, the parameter $name is used. However, in existing mutations like CREATE_GROUP_CHAT, the parameter $title is used for the group chat name. To maintain consistency across your API, consider using the same parameter name in all related mutations.

Apply this diff to align the parameter naming:

 export const CREATE_CHAT = gql`
   mutation createChat(
       $userIds: [ID!]!
       $organizationId: ID
       $isGroup: Boolean!
-      $name: String
+      $title: String
     ) {
       createChat(
         data: {
           userIds: $userIds
           organizationId: $organizationId
           isGroup: $isGroup
-          name: $name
+          title: $title
         }
       ) {
         _id
       }
     }
   `;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export const CREATE_CHAT = gql`
mutation createChat(
$userIds: [ID!]!
$organizationId: ID
$isGroup: Boolean!
$name: String
) {
createChat(
data: {
userIds: $userIds
organizationId: $organizationId
isGroup: $isGroup
name: $name
}
) {
_id
}
}
`;
export const CREATE_CHAT = gql`
mutation createChat(
$userIds: [ID!]!
$organizationId: ID
$isGroup: Boolean!
$title: String
) {
createChat(
data: {
userIds: $userIds
organizationId: $organizationId
isGroup: $isGroup
title: $title
}
) {
_id
}
}
`;


export const SEND_MESSAGE_TO_CHAT = gql`
mutation sendMessageToChat(
$chatId: ID!
$replyTo: ID
$messageContent: String!
$type: String!
) {
sendMessageToChat(
chatId: $chatId
replyTo: $replyTo
messageContent: $messageContent
type: $type
) {
_id
createdAt
messageContent
replyTo {
_id
createdAt
messageContent
sender {
_id
firstName
lastName
}
updatedAt
}
sender {
_id
firstName
lastName
}
updatedAt
}
}
`;

export const SEND_MESSAGE_TO_DIRECT_CHAT = gql`
mutation sendMessageToDirectChat($chatId: ID!, $messageContent: String!) {
sendMessageToDirectChat(chatId: $chatId, messageContent: $messageContent) {
mutation sendMessageToDirectChat(
$chatId: ID!
$replyTo: ID
$messageContent: String!
) {
sendMessageToDirectChat(
chatId: $chatId
replyTo: $replyTo
messageContent: $messageContent
) {
_id
createdAt
messageContent
replyTo {
_id
createdAt
messageContent
receiver {
_id
firstName
lastName
}
sender {
_id
firstName
lastName
}
updatedAt
}
Comment on lines +146 to +174
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider refactoring to unify message sending mutations

Both SEND_MESSAGE_TO_DIRECT_CHAT and SEND_MESSAGE_TO_GROUP_CHAT mutations are similar in structure and functionality to the SEND_MESSAGE_TO_CHAT mutation. To adhere to the DRY (Don't Repeat Yourself) principle and reduce code duplication, consider deprecating these specific mutations in favor of the unified SEND_MESSAGE_TO_CHAT mutation that handles both direct and group chats.

receiver {
_id
firstName
Expand All @@ -107,11 +188,30 @@ export const SEND_MESSAGE_TO_DIRECT_CHAT = gql`
`;

export const SEND_MESSAGE_TO_GROUP_CHAT = gql`
mutation sendMessageToGroupChat($chatId: ID!, $messageContent: String!) {
sendMessageToGroupChat(chatId: $chatId, messageContent: $messageContent) {
mutation sendMessageToGroupChat(
$chatId: ID!
$replyTo: ID
$messageContent: String!
) {
sendMessageToGroupChat(
chatId: $chatId
replyTo: $replyTo
messageContent: $messageContent
) {
_id
createdAt
messageContent
replyTo {
_id
createdAt
messageContent
sender {
_id
firstName
lastName
}
updatedAt
}
Comment on lines +191 to +214
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider refactoring to unify message sending mutations

Similar to SEND_MESSAGE_TO_DIRECT_CHAT, the SEND_MESSAGE_TO_GROUP_CHAT mutation duplicates functionality now provided by SEND_MESSAGE_TO_CHAT. Refactoring to use the unified mutation can simplify the API and reduce maintenance overhead.

sender {
_id
firstName
Expand Down Expand Up @@ -164,6 +264,37 @@ export const MESSAGE_SENT_TO_DIRECT_CHAT = gql`
}
`;

export const MESSAGE_SENT_TO_CHAT = gql`
subscription messageSentToChat($userId: ID!) {
messageSentToChat(userId: $userId) {
_id
createdAt
chatMessageBelongsTo {
_id
}
messageContent
replyTo {
_id
createdAt
messageContent
sender {
_id
firstName
lastName
}
updatedAt
}
type
sender {
_id
firstName
lastName
}
updatedAt
}
}
`;

export const MESSAGE_SENT_TO_GROUP_CHAT = gql`
subscription messageSentToGroupChat($userId: ID!) {
messageSentToGroupChat(userId: $userId) {
Expand Down
Loading
Loading