//Send Message to the server public async Task SendMessage(string ServerKey, string GroupKey, string message) { string fromUserKey = Context.User?.FindFirst("UserKey")?.Value; string fromUserName = Context.User?.FindFirst("Sub")?.Value; if (!string.IsNullOrWhiteSpace(GroupKey) && !string.IsNullOrWhiteSpace(message)) { var b = new MediaUserResponse { type = "Text", message = message }; var obj = await _ServerService.StoreMessageChatAsync(fromUserKey, GroupKey, b); if (obj.IsSuccess) { await Clients.Group(GroupKey).SendAsync("TextServerReceive", new { server_key = ServerKey, text_server_key = GroupKey, from_user_key = fromUserKey, from_user_name = fromUserName, message = message, Type = "Text" }); } else { Console.WriteLine(obj.Error); } } }
public async Task <IActionResult> SendImage([FromForm] MediaUserResponse res) { string fromEmail = HttpContext.User?.FindFirst("Email")?.Value; var obj = await UserService.StoreMessageChat(fromEmail, res.to, res); var u = await UserService.Getuser(fromEmail); if (!obj.IsSuccess) { Console.WriteLine(obj.Error); // change it to log later return(StatusCode(500)); } var response = new { to = res.to, type = "Image", FilePath = obj.FilePath }; await _hubContext.Clients.User(res.to).SendAsync("imagerec", u.UserName, response); // Hosted image url sent to the receiver return(Ok(new { FilePath = obj.FilePath })); }
public async Task SendPrivate(string user, string message) { string email = Context.UserIdentifier; var b = new MediaUserResponse { to = user, type = "Text", message = message }; ApplicationUser u = await UserService.Getuser(email); var obj = await UserService.StoreMessageChat(Context.UserIdentifier, user, b); if (obj.IsSuccess) { await Clients.User(user).SendAsync("RecievePrivate", u.UserName, b); } else { Console.WriteLine(obj.Error); } }
public async Task <StoreGroupMessageResponse> StoreMessageChatAsync(string FromUserKey, string MessageChannelKey, MediaUserResponse res) { try{ ApplicationUser User1 = await _UserManager.FindByIdAsync(FromUserKey); if (User1 == null) { return(new StoreGroupMessageResponse { IsSuccess = false, Error = "couldnt find the user" }); } string mes = string.Empty; string ImageName = string.Empty; if (string.Equals(res.type, "Text")) { mes = res.message; } else if (string.Equals(res.type, "Image")) // Implement a method to store images /* string name = await StoreImage(res.image); * if(name != null){ * ImageName = name; * } * else {return new StoreGroupMessageResponse{IsSuccess = false,Error = "Image Couldn't be stored"};}*/ { } // Link it to a Message channel or text channel by assigning a key to Messsage channel key Media media = new Media { Key = Guid.NewGuid().ToString(), TimeUtc = DateTimeOffset.Now.ToUnixTimeSeconds(), SendFrom = User1.Id, Type = res.type, Message = mes, Image = ImageName, SendFromEmail = User1.Email, MessageChannelKey = MessageChannelKey }; //Store the messages await _Context.Medias.AddAsync(media); await _Context.SaveChangesAsync(); return(new StoreGroupMessageResponse { IsSuccess = true, Error = null, Type = res.type, FilePath = ImageName }); } catch (Exception e) { return(new StoreGroupMessageResponse { IsSuccess = false, Error = e.Message, Type = res.type, FilePath = string.Empty }); } }
//To store chats public async Task <StoreMessageResponse> StoreMessageChat(string From_Email, string To_Email, MediaUserResponse res) { ApplicationUser User1 = await _UserManager.FindByEmailAsync(From_Email); ApplicationUser User2 = await _UserManager.FindByEmailAsync(To_Email); if (User1 == null || User2 == null) { return(new StoreMessageResponse { IsSuccess = false, Error = "Image couldn't be stored as user1 or user2 is null" }); } string mes = string.Empty; string ImageName = string.Empty; if (string.Equals(res.type, "Text")) { mes = res.message; } else if (string.Equals(res.type, "Image")) { string name = await StoreImage(res.image); if (name != null) { ImageName = name; } else { return(new StoreMessageResponse { IsSuccess = false, Error = "Image Couldn't be stored" }); } } Media media = new Media { Key = Guid.NewGuid().ToString(), TimeUtc = DateTimeOffset.Now.ToUnixTimeSeconds(), SendFrom = User1.Id, Type = res.type, Message = mes, Image = ImageName, SendFromEmail = From_Email }; //Check if the relation exists //if not then create a chat string Chatid = GetPrivateChatId(User1, User2); if (Chatid != null) { media.ChatId = Chatid; } else { PrivateChat chat = await MakePrivateChat(User1, User2); media.ChatId = chat.Key; } await Context.Medias.AddAsync(media); await Context.SaveChangesAsync(); return(new StoreMessageResponse { IsSuccess = true, Error = null, Type = res.type, FilePath = ImageName }); }