public async Task <IActionResult> CreateMessage(int userId, NewMessageRequest model)
        {
            if (userId != User.Id)
            {
                return(Unauthorized(new { message = "Unauthorized" }));
            }

            var message = await _messageService.Create(userId, model);

            await _messagesHub.Clients.User(model.RecipientId.ToString()).SendAsync("receiveMessage", message);

            return(CreatedAtRoute("GetMessageById", new { userId, id = message.Id }, message));
        }
Esempio n. 2
0
        // Create new message
        public async Task <NewMessageResponse> Create(int userId, NewMessageRequest model)
        {
            // Must find sender for automapping
            var user = await _userService.GetUserWithPhotos(userId);

            model.SenderId = user.Id;
            // Check if they are matched or not
            if (await _likeService.AreMatched(model.SenderId, model.RecipientId) == false)
            {
                throw new AppException("Can not send message to an unmatched user");
            }
            if (await _context.Likes.AnyAsync(l =>
                                              l.LikerId == model.SenderId && l.Unmatched ||
                                              l.LikeeId == model.SenderId && l.Unmatched))
            {
                throw new AppException("Can not send message to an unmatched user");
            }

            if (model.Type == MessageType.Text.ToString())
            {
                model.Content = model.Content.Trim();
            }

            if (await _userService.GetUserWithPhotos(model.RecipientId) == null)
            {
                throw new KeyNotFoundException("User not found");
            }

            var message = _mapper.Map <Message>(model);

            _context.Add(message);

            if (await _context.SaveChangesAsync() > 0)
            {
                return(_mapper.Map <NewMessageResponse>(message));
            }

            throw new AppException("Send messaged failed");
        }
Esempio n. 3
0
        public override async Task GetNewMessages(NewMessageRequest request,
                                                  IServerStreamWriter <MessageModel> responseStream,
                                                  ServerCallContext context)
        {
            List <MessageModel> messageModels = new List <MessageModel>
            {
                new MessageModel
                {
                    Age       = 3,
                    FirstName = "Mitiku",
                    LastName  = "Ok"
                },
                new MessageModel
                {
                    Age       = 4,
                    FirstName = "safsdaf",
                    LastName  = "afdaf"
                },
                new MessageModel
                {
                    Age       = 4,
                    FirstName = "fd",
                    LastName  = "asdfb"
                },
                new MessageModel
                {
                    Age       = 5,
                    FirstName = "wertw",
                    LastName  = "afdaf"
                }
            };

            foreach (var msg in messageModels)
            {
                await Task.Delay(10000);

                await responseStream.WriteAsync(msg);
            }
        }
Esempio n. 4
0
            private void HandleLoggedInRequests()
            {
                while (loggedIn)
                {
                    req = (Request)br.Read();
                    if (req.SessionID != sessionID)
                    {
                        return;
                    }

                    switch (req.Type)
                    {
                    case RequestType.NewChat:
                        NewChatRequest NCReq = (NewChatRequest)req;
                        (success, info, reason) = server.database.CreateChat(NCReq.participants);

                        if (success)
                        {
                            foreach (Username user in info.participants)
                            {
                                if (server.writersPerUsername.TryGetValue(user, out var writer))
                                {
                                    writer.Write(new ChatCreatedResponse(info, sessionID));
                                }
                            }
                        }
                        else
                        {
                            resp = new FailResponse(reason, sessionID);
                        }
                        info   = null;
                        reason = null;
                        break;

                    case RequestType.NewMessage:
                        NewMessageRequest NMReq = (NewMessageRequest)req;
                        (success, users, msg, reason) = server.database.AddMessage(NMReq.chatType, NMReq.chatID, NMReq.message);

                        // (future) confirm receiving the message

                        foreach (Username user in users)
                        {
                            server.writersPerUsername.TryGetValue(user, out var writer);
                            if (writer != null)
                            {
                                writer.Write(new AddMessageResponse(NMReq.chatType, NMReq.chatID, msg, sessionID));
                            }
                        }

                        msg    = null;
                        users  = null;
                        reason = null;
                        break;

                    case RequestType.DeleteMessage:
                        DeleteMessageRequest DMReq = (DeleteMessageRequest)req;
                        (success, users, reason) = server.database.DeleteMessage(DMReq.chatType, DMReq.chatID, DMReq.dateTime);

                        if (success)
                        {
                            foreach (var user in users)
                            {
                                if (server.writersPerUsername.TryGetValue(user, out var writer))
                                {
                                    writer.Write(new DeleteMessageResponse(DMReq.chatType, DMReq.chatID, DMReq.dateTime, sessionID));
                                }
                            }
                        }
                        break;

                    case RequestType.GetOnlineContacts:
                        OnlineContactsRequest OCReq = (OnlineContactsRequest)req;
                        (success, users, reason) = server.database.GetOnlineContacts(OCReq.username);

                        if (success)
                        {
                            resp = new OnlineContactsResponse(users, sessionID);
                        }
                        break;

                    case RequestType.ChangePassword:
                        ChangePasswordRequest CPReq = (ChangePasswordRequest)req;
                        (success, reason) = server.database.ChangePassword(CPReq.username, CPReq.oldPassword, CPReq.newPassword);

                        if (success)
                        {
                            resp = new SuccessResponse(sessionID);
                        }
                        else
                        {
                            resp = new FailResponse(reason, sessionID);
                        }
                        break;

                    default:
                        resp = new FailResponse("Unsupported request type detected.", sessionID);
                        break;
                    }

                    req = null;

                    if (resp != null)
                    {
                        bw.Write(resp);
                        //resp.Send(bw);

                        resp = null;
                        if (!loggedIn)
                        {
                            return;
                        }
                    }
                }
            }