Example #1
0
        private static void GetUserTest(User.UserClient client)
        {
            var getUser = client.GetUserInfo(new UserRequest
            {
                Id = 2
            });

            Console.WriteLine(getUser);
        }
Example #2
0
        public override async Task EventStream(IAsyncStreamReader <Event> requestStream,
                                               IServerStreamWriter <Event> responseStream, ServerCallContext context)
        {
            try
            {
                while (await requestStream.MoveNext())
                {
                    var currentEvent = requestStream.Current;
                    Console.WriteLine(currentEvent.ToString());
                    var userInfoRequest = new GetUserInfoRequest
                    {
                        Userid = currentEvent.SenderInfo.Userid
                    };
                    var senderInfo = _userClient.GetUserInfo(userInfoRequest);
                    // check if this is an initial message that is sent on the first connection'
                    //currentEvent.SenderInfo
                    if (currentEvent.SenderInfo.IsInit)
                    {
                        ActiveRequests.TryAdd(currentEvent.SenderInfo.Userid, responseStream);
                    }
                    else if (currentEvent.Message != null)
                    {
                        Console.WriteLine(currentEvent.Message.Id);
                        var message =
                            Message.CreateMessageFromRequest(currentEvent.Message, currentEvent.SenderInfo.Userid, _db);
                        // if the group is not null, add many fields for user read
                        if (message.GroupRef != null)
                        {
                            foreach (var member in message.GroupRefNavigation.GroupMembers)
                            {
                                await _db.UsersReads.AddAsync(new UsersRead
                                {
                                    MessageRefNavigation = message,
                                    MessageStatus        = 0,
                                    UserId = member.Userid
                                });

                                // change the notification templates depending on group or no group
                                // send user a notification when they are offline, otherwise send them the event
                                ActiveRequests.TryGetValue(currentEvent.Message.ReceiverUserId,
                                                           out var receiver);
                                if (receiver != null)
                                {
                                    try
                                    {
                                        await _notificationClient.SendNotificationByUserIdAsync(
                                            new UserIdNotificationRequest
                                        {
                                            Message = $"New Message: {currentEvent.Message.Message_}",
                                            Title   = $"{senderInfo.UserName}",
                                            Userid  = member.Userid
                                        });
                                    }
                                    catch (Exception)
                                    {
                                        // don't do anything, sometimes unregistered tokens don't get notified.
                                    }
                                }
                            }
                        }
                        // if group is null, this is a peer to peer message, only add one read.
                        else
                        {
                            await _db.UsersReads.AddAsync(new UsersRead
                            {
                                MessageRefNavigation = message,
                                MessageStatus        = 0,
                                UserId = requestStream.Current.Message.ReceiverUserId
                            });
                        }
                        await _db.Messages.AddAsync(message);

                        // get the active request if the user is currently subscribed
                        ActiveRequests.TryGetValue(currentEvent.Message.ReceiverUserId,
                                                   out var receiverResponse);
                        // user is currently online!
                        if (receiverResponse != null)
                        {
                            // just send the entire event over
                            await receiverResponse.WriteAsync(currentEvent);
                        }
                        else
                        {
                            // send user a notification when they are offline
                            await _notificationClient.SendNotificationByUserIdAsync(new UserIdNotificationRequest
                            {
                                Message = $"New Message: {currentEvent.Message.Message_}",
                                Title   = $"{senderInfo.UserName}",
                                Userid  = currentEvent.Message.ReceiverUserId
                            });
                        }

                        await _db.SaveChangesAsync();
                    }
                    else if (currentEvent.GroupCreated != null)
                    {
                        //var groupMembers = currentEvent.GroupCreated.
                        var group = new Group
                        {
                            // empty message list
                            Messages = { },
                            Title    = currentEvent.GroupCreated.Title,
                            Uuid     = Guid.NewGuid(),
                        };
                        await _db.Groups.AddAsync(group);

                        var groupMembers = currentEvent.GroupCreated.GroupMemberIds.Select(x => new GroupMember
                        {
                            Userid             = x,
                            GroupRefNavigation = group,
                            // everyone is not admin by default
                            IsAdmin = false,
                        });
                        await _db.GroupMembers.AddRangeAsync(groupMembers);

                        var creatorMember = new GroupMember
                        {
                            Userid             = currentEvent.GroupCreated.GroupCreator,
                            IsAdmin            = true,
                            GroupRefNavigation = group
                        };
                        foreach (var memberIds in currentEvent.GroupCreated.GroupMemberIds)
                        {
                            // get the active request if the user is currently subscribed
                            ActiveRequests.TryGetValue(memberIds,
                                                       out var receiverResponse);
                            if (receiverResponse != null)
                            {
                                // just send the entire event over
                                await receiverResponse.WriteAsync(currentEvent);
                            }
                            else
                            {
                                // send user a notification when they are offline
                                await _notificationClient.SendNotificationByUserIdAsync(new UserIdNotificationRequest
                                {
                                    Message = $"{currentEvent.GroupCreated.Title}",
                                    Title   = $"You have been added to a group!",
                                    Userid  = memberIds
                                });
                            }
                        }
                        await _db.SaveChangesAsync();
                    }
                    else if (currentEvent.MessageRead != null)
                    {
                        Console.WriteLine("message read");
                        _db.UsersReads.First(x => x.MessageRefNavigation.Uuid == Guid.Parse(currentEvent.MessageRead.MessageId)).MessageStatus = (int)currentEvent.MessageRead.MessageStatus;
                        // get the active request if the user is currently subscribed
                        var message = _db.Messages.First(x =>
                                                         x.Uuid == Guid.Parse(currentEvent.MessageRead.MessageId));
                        ActiveRequests.TryGetValue(message.AuthorId,
                                                   out var receiverResponse);
                        // user is currently online!
                        if (receiverResponse != null)
                        {
                            // just send the entire event over
                            await receiverResponse.WriteAsync(currentEvent);
                        }
                        // no need to send notification for read receipts


                        await _db.SaveChangesAsync();
                    }
                }
                // unfortunately, by this stage we might not have the key (userid) so we have to take the inefficient route and find it using linq
                // then remove it using the key, which searches for it again. Mitigate this by running it in an asynchronous task
                // so we can let this run while we remove the active user.
                await Task.Run(() =>
                {
                    // remove the request as an active request before it ends
                    var activeRequest = ActiveRequests.FirstOrDefault(x => x.Value == responseStream);
                    ActiveRequests.Remove(activeRequest.Key, out var request);
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                // same thing here
                await Task.Run(() =>
                {
                    // remove any references to the current request on error
                    var activeRequest = ActiveRequests.FirstOrDefault(x => x.Value == responseStream);
                    ActiveRequests.Remove(activeRequest.Key, out var request);
                });
            }
        }