private void FillContent(Paragraph pa, MessageModel msg) { switch (msg.MsgType) { case MessageType.txt: case MessageType.bigtxt: Helper.MessageHelper.ContentInlines = null; var inlines = CSClient.Helper.MessageHelper.GetRichContent(msg.Content, false); foreach (Inline inl in inlines) { inl.BaselineAlignment = BaselineAlignment.Bottom; inl.FontSize = 14; inl.FontFamily = _FontFamily; pa.Inlines.Add(inl); if (inl is InlineUIContainer uic) { uic.Child.Uid = ViewModels.AppData.FlagEmoje + uic.Child.Uid; } } break; case MessageType.img: InlineUIContainer imgC = new InlineUIContainer(); ChatImage chatImg = new ChatImage(msg.Content, new Size(240, 135)) { DataContext = msg, HasContexMenu = false }; imgC.Child = chatImg; Binding bind = new Binding("Content") { Mode = BindingMode.OneWay }; chatImg.SetBinding(ChatImage.ImagePathProperty, bind); bind = new Binding("MessageState") { Mode = BindingMode.OneWay }; chatImg.SetBinding(ChatImage.StateProperty, bind); CSClient.Helper.MessageHelper.LoadImgContent(msg); pa.Inlines.Add(imgC); imgC.PreviewMouseLeftButtonDown += ImgC_MouseLeftButtonUp; break; case MessageType.file: case MessageType.onlinefile: InlineUIContainer uicFile = new InlineUIContainer(); FileChatItem chatFile = new FileChatItem() { DataContext = msg, IsMainView = false }; chatFile.IsInDocument = true; //msg.ResourceModel.FileState = FileStates.Completed; uicFile.Child = chatFile; Binding bindF = new Binding("Content") { Mode = BindingMode.OneWay }; chatFile.SetBinding(FileChatItem.FullNameProperty, bindF); Binding bindState = new Binding("ResourceModel.FileState") { Mode = BindingMode.TwoWay }; chatFile.SetBinding(FileChatItem.FileStateProperty, bindState); //CSClient.Helper.MessageHelper.LoadFileContent(msg); pa.Inlines.Add(uicFile); break; default: //pa.Inlines.Clear(); //pa.Inlines.Add(new Run(msg.Content) //{ // FontSize = 14, // Background = Brushes.Gray, // Foreground = Brushes.LightGray, // FontFamily = _FontFamily, //}); //pa.TextAlignment = TextAlignment.Center; pa.Inlines.Add(new Run(msg.Content) { FontSize = 14, FontFamily = _FontFamily, }); break; } pa.Inlines.Add("\r\n"); }
public async Task <SendFilesResponseViewModel> SendMessageWitFilesToUser(IList <IFormFile> files, string group, string toUsername, string fromUsername, string message) { var toUser = this.db.Users.FirstOrDefault(x => x.UserName == toUsername); var toId = toUser.Id; var toImage = toUser.ImageUrl; var fromUser = this.db.Users.FirstOrDefault(x => x.UserName == fromUsername); var fromId = fromUser.Id; var fromImage = fromUser.ImageUrl; await this.DeleteOldMessage(group); var newMessage = new ChatMessage { ApplicationUser = fromUser, Group = this.db.Groups.FirstOrDefault(x => x.Name.ToLower() == group.ToLower()), SendedOn = DateTime.UtcNow, ReceiverUsername = toUser.UserName, RecieverImageUrl = toUser.ImageUrl, }; StringBuilder messageContent = new StringBuilder(); if (message != null) { messageContent.AppendLine($"{new HtmlSanitizer().Sanitize(message.Trim())}<hr style=\"margin-bottom: 8px !important;\" />"); } StringBuilder imagesContent = new StringBuilder(); StringBuilder filesContent = new StringBuilder(); var imagesCount = files .Where(x => x.ContentType .Contains("image", StringComparison.CurrentCultureIgnoreCase)) .Count(); var result = new SendFilesResponseViewModel(); if (imagesCount > 0) { await this.hubContext.Clients.User(fromId).SendAsync("UpdateImagesUploadCount", imagesCount); result.HaveImages = true; } var filesCount = files .Where(x => !x.ContentType .Contains("image", StringComparison.CurrentCultureIgnoreCase)) .Count(); if (filesCount > 0) { await this.hubContext.Clients.User(fromId).SendAsync("UpdateFilesUploadCount", filesCount); result.HaveFiles = true; } foreach (var file in files) { var chatFile = new ChatImage { ChatMessageId = newMessage.Id, GroupId = this.db.Groups.FirstOrDefault(x => x.Name.ToLower() == group.ToLower()).Id, }; string fileUrl = string.Empty; if (file.ContentType.Contains("image", StringComparison.CurrentCultureIgnoreCase)) { fileUrl = await ApplicationCloudinary.UploadImage( this.cloudinary, file, string.Format(GlobalConstants.ChatFileName, chatFile.Id), GlobalConstants.PrivateChatImagesFolder); chatFile.Name = string.Format(GlobalConstants.ChatFileName, chatFile.Id); imagesCount--; await this.hubContext.Clients.User(fromId).SendAsync("UpdateImagesUploadCount", imagesCount); imagesContent.AppendLine($"<span onclick=\"zoomChatImage('{fileUrl}')\"><img src=\"{fileUrl}\" style=\"margin-right: 10px; width: 27px; height: 35px; margin-top: 5px;\"></span>"); } else { var fileExtension = Path.GetExtension(file.FileName); fileUrl = await ApplicationCloudinary.UploadImage( this.cloudinary, file, string.Format(GlobalConstants.ChatFileName, $"{chatFile.Id}") + fileExtension, GlobalConstants.PrivateChatImagesFolder); chatFile.Name = string.Format(GlobalConstants.ChatFileName, $"{chatFile.Id}{fileExtension}"); filesCount--; await this.hubContext.Clients.User(fromId).SendAsync("UpdateFilesUploadCount", filesCount); string[] sizes = { "B", "KB", "MB", "GB", "TB" }; double fileLength = file.Length; int order = 0; while (fileLength >= 1024 && order < sizes.Length - 1) { order++; fileLength /= 1024; } string fileSize = string.Format("{0:0.##} {1}", fileLength, sizes[order]); filesContent.AppendLine($"<p><a href=\"{fileUrl}\"><i class=\"fas fa-download\"></i> {file.FileName} - ({fileSize})</a></p>"); } chatFile.Url = fileUrl; newMessage.ChatImages.Add(chatFile); } if (imagesCount > 0) { await this.hubContext.Clients.User(fromId).SendAsync("UpdateImagesUploadCount", imagesCount); } if (filesCount > 0) { await this.hubContext.Clients.User(fromId).SendAsync("UpdateFilesUploadCount", filesCount); } if (imagesContent.Length == 0) { messageContent.AppendLine(filesContent.ToString().Trim()); } else { messageContent.AppendLine(imagesContent.ToString().Trim()); if (filesContent.Length != 0) { messageContent.AppendLine("<hr style=\"margin-bottom: 8px !important;\" />"); messageContent.AppendLine(filesContent.ToString().Trim()); } } newMessage.Content = messageContent.ToString().Trim(); this.db.ChatMessages.Add(newMessage); await this.db.SaveChangesAsync(); await this.hubContext .Clients .User(toId) .SendAsync("ReceiveMessage", fromUsername, fromImage, messageContent.ToString().Trim()); await this.ReceiveNewMessage(fromUsername, messageContent.ToString().Trim(), group); return(result); }
private void FillContent(Paragraph pa, MessageModel msg) { switch (msg.MsgType) { case MessageType.txt: case MessageType.bigtxt: IMClient.Helper.MessageHelper.ContentInlines = null; var inlines = IMClient.Helper.MessageHelper.GetRichContent(msg.Content, false); foreach (Inline inl in inlines) { inl.BaselineAlignment = BaselineAlignment.Bottom; inl.FontSize = 14; inl.FontFamily = _FontFamily; pa.Inlines.Add(inl); if (inl is InlineUIContainer uic) { uic.Child.Uid = ViewModels.AppData.FlagEmoje + uic.Child.Uid; } } break; case MessageType.img: InlineUIContainer imgC = new InlineUIContainer(); ChatImage chatImg = new ChatImage(msg.Content) { DataContext = msg, HasContexMenu = false }; chatImg.Tag = msg.ResourceModel.Key; imgC.Child = chatImg; Binding bind = new Binding("Content") { Mode = BindingMode.OneWay }; chatImg.SetBinding(ChatImage.ImagePathProperty, bind); bind = new Binding("MessageState") { Mode = BindingMode.OneWay }; chatImg.SetBinding(ChatImage.StateProperty, bind); if (msg.MessageState != MessageStates.Success) { IMClient.Helper.MessageHelper.LoadImgContent(msg); } pa.Inlines.Add(imgC); imgC.PreviewMouseLeftButtonDown += ImgC_MouseLeftButtonUp; break; case MessageType.smallvideo: case MessageType.video: InlineUIContainer videoC = new InlineUIContainer(); SmallVideo smallVideo = new SmallVideo(msg.Content, 142, 220, msg) { HasContexMenu = false }; videoC.Child = smallVideo; Binding bindV = new Binding("Content") { Mode = BindingMode.OneWay }; smallVideo.SetBinding(SmallVideo.VideoPathProperty, bindV); bindV = new Binding("ResourceModel.FileState") { Mode = BindingMode.TwoWay }; smallVideo.SetBinding(SmallVideo.FileStateProperty, bindV); bindV = new Binding("ResourceModel.RecordTime") { Mode = BindingMode.TwoWay }; smallVideo.SetBinding(SmallVideo.RecordTimeProperty, bindV); bindV = new Binding("ResourceModel.PreviewImagePath") { Mode = BindingMode.TwoWay }; smallVideo.SetBinding(SmallVideo.VideoPreviewImageProperty, bindV); pa.Inlines.Add(videoC); videoC.PreviewMouseLeftButtonDown += ImgC_MouseLeftButtonUp; break; case MessageType.file: case MessageType.onlinefile: InlineUIContainer uicFile = new InlineUIContainer(); FileChatItem chatFile = new FileChatItem() { DataContext = msg, IsMainView = false }; chatFile.IsInDocument = true; //msg.ResourceModel.FileState = FileStates.Completed; uicFile.Child = chatFile; Binding bindF = new Binding("Content") { Mode = BindingMode.OneWay }; chatFile.SetBinding(FileChatItem.FullNameProperty, bindF); Binding bindState = new Binding("ResourceModel.FileState") { Mode = BindingMode.TwoWay }; chatFile.SetBinding(FileChatItem.FileStateProperty, bindState); //IMClient.Helper.MessageHelper.LoadFileContent(msg); pa.Inlines.Add(uicFile); break; case MessageType.addgroupnotice: InlineUIContainer noticeContainer = new InlineUIContainer(); GroupNoticeCard groupCard = new GroupNoticeCard() { DataContext = msg }; groupCard.tb_tiTle.FontSize = 14; groupCard.tb_content.FontSize = 14; groupCard.tbInfo.FontSize = 12; groupCard.FontFamily = new FontFamily("微软雅黑"); noticeContainer.Child = groupCard; pa.Inlines.Add(noticeContainer); //noticeContainer.PreviewMouseLeftButtonDown += GroupCard_PreviewMouseLeftButtonDown; break; case MessageType.usercard: IMClient.Helper.MessageHelper.ContentInlines = null; //var content = msg.Content; var content = "个人名片"; var usercardinlines = IMClient.Helper.MessageHelper.GetRichContent(content, false); foreach (Inline inl in usercardinlines) { inl.BaselineAlignment = BaselineAlignment.Bottom; inl.FontSize = 14; inl.FontFamily = _FontFamily; pa.Inlines.Add(inl); if (inl is InlineUIContainer uic) { uic.Child.Uid = ViewModels.AppData.FlagEmoje + uic.Child.Uid; } } //InlineUIContainer personCard = new InlineUIContainer(); //PersonalCard card = new PersonalCard() { DataContext = msg, HasContexMenu = false }; //card.tb_PhoneNumber.FontSize = 10; //card.tb_DisplayName.FontSize = 12; //card.tbInfo.FontSize = 12; //card.FontFamily = new FontFamily("微软雅黑"); //personCard.Child = card; //pa.Inlines.Add(personCard); break; case MessageType.foreigndyn: InlineUIContainer foreigndynControl = new InlineUIContainer(); var msgCard = new HyperlinkMessageCard() { DataContext = msg, HasContexMenu = false }; // msgCard.tb_PhoneNumber.FontSize = 10; //card.tb_DisplayName.FontSize = 12; //card.tbInfo.FontSize = 12; msgCard.Width = 230; msgCard.FontSize = 12; msgCard.FontFamily = new FontFamily("微软雅黑"); foreigndynControl.Child = msgCard; pa.Inlines.Add(foreigndynControl); break; default: //pa.Inlines.Clear(); //pa.Inlines.Add(new Run(msg.Content) //{ // FontSize = 14, // Background = Brushes.Gray, // Foreground = Brushes.LightGray, // FontFamily = _FontFamily, //}); //pa.TextAlignment = TextAlignment.Center; pa.Inlines.Add(new Run(msg.Content) { FontSize = 14, FontFamily = _FontFamily, }); break; } pa.Inlines.Add("\r\n"); }