Skip to content

Commit

Permalink
fix add and remove members
Browse files Browse the repository at this point in the history
  • Loading branch information
grozer221 committed Jun 5, 2024
1 parent 4c3a690 commit ea33165
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions Geesemon.Web/GraphQL/Mutations/ChatMutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ private async Task<IEnumerable<User>> ResolveAddMembers(IResolveFieldContext con
throw exception;

var newUserChats = new List<UserChat>();
var newUsers = new List<User>();
foreach (var userId in chatsAddMembersInput.UserIds)
{
var user = await userProvider.GetByIdAsync(userId);
Expand All @@ -247,31 +248,35 @@ private async Task<IEnumerable<User>> ResolveAddMembers(IResolveFieldContext con

var userChat = await userChatManager.Get(chatsAddMembersInput.ChatId, userId);
if (userChat == null)
{
newUserChats.Add(new UserChat
{
ChatId = chatsAddMembersInput.ChatId,
UserId = user.Id,
User = user,
});
newUsers.Add(user);
}
}

await userChatManager.CreateManyAsync(newUserChats);
chatActionSubscriptionService.Notify(chat, ChatActionKind.Add, newUserChats.Select(uc => uc.UserId));

foreach (var userChat in newUserChats)
{
var user = newUsers.FirstOrDefault(u => u.Id == userChat.UserId);
var newMessage = new Message
{
ChatId = chatsAddMembersInput.ChatId,
Text = "AddedToChatMessage",
Type = MessageKind.SystemGeeseText,
GeeseTextArguments = new[] { "@" + currentIdentifier, "@" + userChat.User.Identifier }
GeeseTextArguments = ["@" + currentIdentifier, "@" + user?.Identifier]
};

newMessage = await messageProvider.CreateAsync(newMessage);
messageSubscriptionService.Notify(newMessage, MessageActionKind.Create);
chatMembersSubscriptionService.Notify(userChat.User, ChatMembersKind.Add, chat.Id);
chatMembersSubscriptionService.Notify(user, ChatMembersKind.Add, chat.Id);
}
return newUserChats.Select(uc => uc.User);
return newUsers;
}

private async Task<IEnumerable<User>> ResolveRemoveMembers(IResolveFieldContext context)
Expand Down Expand Up @@ -300,22 +305,27 @@ private async Task<IEnumerable<User>> ResolveRemoveMembers(IResolveFieldContext

chatActionSubscriptionService.Notify(chat, ChatActionKind.Delete, removeUserChats.Select(uc => uc.UserId));

var usersToRemove = new List<User>();

foreach (var userChat in removeUserChats)
{
var user = await userProvider.GetByIdAsync(userChat.UserId);
usersToRemove.Add(user);

await userChatManager.RemoveAsync(userChat);

var newMessage = new Message
{
ChatId = chatsAddMembersInput.ChatId,
Text = "RemovedFromChatMessage",
Type = MessageKind.SystemGeeseText,
GeeseTextArguments = new[] { "@" + currentIdentifier, "@" + userChat.User.Identifier }
GeeseTextArguments = ["@" + currentIdentifier, "@" + user.Identifier]
};
newMessage = await messageProvider.CreateAsync(newMessage);
messageSubscriptionService.Notify(newMessage, MessageActionKind.Create);
chatMembersSubscriptionService.Notify(userChat.User, ChatMembersKind.Delete, chat.Id);
chatMembersSubscriptionService.Notify(user, ChatMembersKind.Delete, chat.Id);
}
return removeUserChats.Select(uc => uc.User);
return usersToRemove;
}

private async Task<bool> ResolveLeaveChat(IResolveFieldContext context)
Expand Down

0 comments on commit ea33165

Please sign in to comment.