private async void SendBtn_Click(object sender, RoutedEventArgs e) { if (to_user_id != null) { Object api = settings.Values["api"]; Object id = settings.Values["userid"]; var notification = new SendChatModel() { to = to_user_id, from = id.ToString(), content = NotificationContentTB.Text, timestamp = new DateTime() }; if (api != null && id != null) { try { var httpClient = new HttpClient(); var response = await httpClient.PostAsJsonAsync(common.getIP() + "api/sendchat/" + api.ToString() + "/" + id.ToString(), notification); if (response.IsSuccessStatusCode) { var n = new ChatModel() { content = NotificationContentTB.Text, alignment = "Right" }; items.Add(n); } else { MessageDialog md = new MessageDialog("Error occur send notification. Try again later."); await md.ShowAsync(); } NotificationContentTB.Text = ""; } catch { NotificationContentTB.Text = ""; MessageDialog md = new MessageDialog("Error occur send notification. Try again later."); await md.ShowAsync(); } } } }
public ResponseMessage SendMessage([FromBody] SendChatModel sendChat) { try { if (db.sendmessage(sendChat.id_send, sendChat.id_recive, sendChat.message) > 0) { return(new ResponseMessage("sucsess", "Đã gửi", 1)); } else { return(new ResponseMessage("Lỗi :(")); } } catch (Exception) { return(new ResponseMessage("lỗi không gửi được")); } }
public ActionResult CheckProfileQuotient(SendChatModel model) { var response = _userService.GetProfileQuotient(model.SenderId, model.RevceiverId); return(Ok(APIResponse.CreateResponse(_jwtAuthentication.Value, _httpContextAccessor.HttpContext.Request, response))); }
public ActionResult SaveInvite(SendChatModel model) { var response = _userService.SaveChatInvite(model.SenderId, model.RevceiverId, model.mode); return(Ok(APIResponse.CreateResponse(_jwtAuthentication.Value, _httpContextAccessor.HttpContext.Request, response))); }
public async Task <ResultModel> SendChat([FromBody] SendChatModel model) { var ret = new ResultModel { IsSucceeded = true }; if (!SystemConfiguration.Config.CanDiscussion) { ret.IsSucceeded = false; ret.ErrorMessage = "管理员未开启讨论功能"; return(ret); } var(user, privilege) = await GetUserPrivilegeAsync(); if (user != null) { if (!user.EmailConfirmed) { ret.ErrorMessage = "没有验证邮箱"; ret.IsSucceeded = false; return(ret); } if (user.Coins < 10 && !HasAdminPrivilege(privilege)) { ret.ErrorMessage = "金币余额不足"; ret.IsSucceeded = false; return(ret); } if (string.IsNullOrWhiteSpace(model.Content)) { ret.ErrorMessage = "请输入消息内容"; ret.IsSucceeded = false; return(ret); } if (model.Content.Length > 65536) { ret.ErrorMessage = "消息内容过长"; ret.IsSucceeded = false; return(ret); } var sendTime = DateTime.Now; var content = HttpUtility.HtmlEncode(model.Content); var pid = model.ProblemId == 0 ? null : (int?)model.ProblemId; var cid = model.ContestId == 0 ? null : (int?)model.ContestId; var gid = model.GroupId == 0 ? null : (int?)model.GroupId; using (var db = new ApplicationDbContext(_dbContextOptions)) { if (cid != null) { var contest = await db.Contest.Select(i => new { i.Id, i.Config }).FirstOrDefaultAsync(i => i.Id == cid); if (contest != null) { var config = JsonConvert.DeserializeObject <ContestConfiguration>(contest.Config ?? "{}"); if (!config.CanDiscussion) { ret.ErrorMessage = "该比赛未开启讨论功能"; ret.IsSucceeded = false; return(ret); } } } var lastSubmit = await db.Discussion.OrderByDescending(i => i.SubmitTime).FirstOrDefaultAsync(i => i.UserId == user.Id); if (lastSubmit != null && (DateTime.Now - lastSubmit.SubmitTime) < TimeSpan.FromSeconds(10)) { ret.ErrorMessage = "消息发送过于频繁,请等待 10 秒后再试"; ret.IsSucceeded = false; return(ret); } var dis = new Discussion { UserId = user.Id, Content = content, SubmitTime = sendTime, ReplyId = model.ReplyId, ProblemId = pid, ContestId = cid, GroupId = gid }; db.Discussion.Add(dis); await db.SaveChangesAsync(); if (model.ReplyId != 0) { var previousDis = await db.Discussion .Include(i => i.UserInfo) .Include(i => i.Problem) .Include(i => i.Contest) .Include(i => i.Group) .Select(i => new { i.Id, i.UserId, i.ProblemId, ProblemName = i.Problem.Name, i.ContestId, ContestName = i.Contest.Name, i.GroupId, GroupName = i.Group.Name, i.Content, i.SubmitTime }).FirstOrDefaultAsync(i => i.Id == model.ReplyId); if (previousDis != null) { var link = string.Empty; var position = string.Empty; if (cid == null) { if (pid == null) { link = "/"; position = "主页"; } else { link = $"/ProblemDetails/{pid}"; position = $"题目 {pid} - {previousDis.ProblemName}"; } } else { if (pid == null) { link = $"/ContestDetails/{cid}"; position = $"比赛 {cid} - {previousDis.ContestName}"; } else { link = $"/ProblemDetails/{cid}/{pid}"; position = $"比赛 {cid} - {previousDis.ContestName},题目 {pid} - {previousDis.ProblemName}"; } } var msgContent = new MessageContent { Content = $"<h3><a href=\"/Account/{user.Id}\">{user.UserName}</a> 在 #{dis.Id} 回复了您的帖子 #{previousDis.Id}:</h3><br />" + "<div style=\"width: 90 %; overflow: auto; max-height: 300px; \">" + $"<pre style=\"white-space: pre-wrap; word-wrap: break-word;\">{new string(content.Take(128).ToArray()) + (content.Length > 128 ? "..." : string.Empty)}</pre></div>" + "<br /><hr /><br /><h3>原帖内容:</h3><br />" + "<div style=\"width: 90 %; overflow: auto; max-height: 300px; \">" + $"<pre style=\"white-space: pre-wrap; word-wrap: break-word;\">{new string(previousDis.Content.Take(128).ToArray()) + (previousDis.Content.Length > 128 ? "..." : string.Empty)}</pre></div>" + $"<br /><hr /><br /><h4>位置:{position},<a href=\"{link}\">点此前往查看</a></h4>" }; db.MessageContent.Add(msgContent); await db.SaveChangesAsync(); db.Message.Add(new Message { FromUserId = null, ToUserId = previousDis.UserId, ContentId = msgContent.Id, ReplyId = 0, SendTime = DateTime.Now, Status = 1, Title = $"您的帖子 #{previousDis.Id} 有新的回复", Type = 1 }); await db.SaveChangesAsync(); } } await _chatHub.Clients.Group($"pid-{model.ProblemId};cid-{model.ContestId};gid-{model.GroupId};") .SendAsync("ChatMessage", dis.Id, user.Id, user.UserName, $"{sendTime.ToShortDateString()} {sendTime.ToLongTimeString()}", content, model.ReplyId); } user.Experience += 5; if (!HasAdminPrivilege(privilege)) { user.Coins -= 10; } await _userManager.UpdateAsync(user); } else { ret.ErrorMessage = "没有登录"; ret.IsSucceeded = false; } return(ret); }