public static CommentDetailsView ToCommentDetailsView(this CommentRecordViewModel comment) { var commentDetailView = new CommentDetailsView() { Content = comment.Content, Created = comment.Created, CreatedByAdmin = comment.CreatedByAdmin, Creator = comment.Creator, FileMimeType = comment.FileMimeType, FileURL = comment.FileURL, Id = comment.Id.ToString(), Parent = comment.Parent.GetValueOrDefault().ToString(), Root = comment.Root.GetValueOrDefault().ToString(), UpvoteCount = comment.UpvoteCount, UserHasUpvoted = comment.UserHasUpvoted, UserVoted = comment.UserVoted, CreatedByCurrentUser = comment.CreatedByCurrentUser, IsNew = comment.IsNew, Modified = comment.Modified, GroupId = comment.GroupId, CommentGroupType = comment.CommentGroupType, Pings = comment.Pings,// .PingsSequence, CreatorName = comment.CreatorName, FileName = comment.FileURL, Fullname = comment.Fullname, GroupName = comment.GroupName, PostOnURL = comment.PostOnURL,//.CommentURL, ProfilePictureUrl = comment.Creator }; return(commentDetailView); }
private async Task PrepareCommentNotifications(CommentRecordViewModel comment, string currentUserId) { //Group chat case if (comment.CommentGroupType == CommentGroupType.GroupChat) { await PrepareGroupChatNotifications(comment, currentUserId); } //Profile chat case if (comment.CommentGroupType == CommentGroupType.ProfileChat) { await PrepareProfileChatNotifications(comment, currentUserId); } //post on profile case else if (comment.CommentGroupType == CommentGroupType.Group && comment.Root == null) { //await PrepareGroupMainCommentsNotifications(comment, currentUserId); } //profile page case: send notification for all profile follower else if (comment.CommentGroupType == CommentGroupType.Profile && comment.Root == null) { // await PrepareProfileMainCommentsNotifications(comment, currentUserId); } //reply on comment case: send notification for all user who was comment on the post else if (comment.Root != null) { // await PrepareReplyNotifications(comment, currentUserId); } }
public async Task <IActionResult> Edit(CommentRecordViewModel model) { if (!await _authorizationService.AuthorizeAsync(User, Intelli.Comments.Permissions.ManageComments)) { return(Unauthorized()); } var commentRecord = await _commentsRepository.FindByIdAsync(model.Id); commentRecord.Content = model.Content; await _commentsRepository.UpdateAsync(commentRecord); // await _session.CommitAsync(); _notifier.Success(H["Comment has been saved."]); return(RedirectToAction(nameof(Edit), new { id = model.Id })); }
public async Task <IActionResult> PutComment(int id, CommentRecordViewModel comment) { // var currentUserId = _userManager.GetUserId(User); comment.Modified = DateTime.Now; ModelState.Remove("Modified"); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // CommentRecord commentModel = await _context.Comment.SingleOrDefaultAsync(c => c.Id == comment.Id); var commentModel = await _commentRepository.FindByIdAsync(id); if (id != comment.Id || commentModel == null) { return(BadRequest()); } commentModel.Content = comment.Content; commentModel.UpdatedUtc = DateTime.Now; commentModel.Pings = comment.Pings != null?string.Join(",", comment.Pings) : null; // _context.Entry(commentModel).State = EntityState.Modified; // CreateTransaction(comment, currentUserId, CommentTransactionType.Edit); try { //await _context.SaveChangesAsync(); await _commentRepository.UpdateAsync(commentModel); } catch (Exception) { if (!(await CommentExists(id))) { return(NotFound()); } else { throw; } } return(Ok(comment)); }
private async Task <byte[]> GetCommentAttachedFile(CommentRecordViewModel comment) { byte[] file = null; if (!string.IsNullOrEmpty(comment.FileURL)) { // This way you can check if the given file exists. if (await _customFileStore.GetFileInfoAsync(comment.FileURL) == null) { // return "Create the file first!"; return(file); } // If you want to extract the content of the file use a StreamReader to read the stream. using (var stream = await _customFileStore.GetFileStreamAsync(comment.FileURL)) { file = new byte[stream.Length]; int numBytesToRead = (int)stream.Length; int numBytesRead = 0; while (numBytesToRead > 0) { int n = stream.Read(file, numBytesRead, numBytesToRead); if (n == 0) { break; } numBytesRead += n; numBytesToRead -= n; } } await _customFileStore.TryDeleteFileAsync(comment.FileURL); } return(file); }
public async Task <IEnumerable <CommentRecordViewModel> > GetComments(string contentItemId, string currentuser) { // var currentUserId = _userManager.GetUserId(User); var currentUserId = currentuser; var commentViewModelList = new List <CommentRecordViewModel>(); var currentCommentsId = new List <int>(); var comments = await _commentRepository.GetCommentsAsync(contentItemId, 0); var currentCommentList = comments // .Where(c => (currentUserGroupIds.Contains(c.GroupId) || c.GroupId == currentUserId) && c.Parent == null && c.CommentGroupTypeId != CommentGroupType.GroupChat) .OrderByDescending(c => c.CreatedUtc); // .Skip(index) // .Take(NumberOfLoadingItems); if (currentCommentList != null) { var rootIdList = new List <int>(); //preparing post comments foreach (var comment in currentCommentList) { var commentViewModel = new CommentRecordViewModel() { Content = comment.Content, Created = comment.CreatedUtc, CreatedByAdmin = comment.CreatedByAdmin, CreatedByCurrentUser = comment.Creator == currentUserId, Creator = comment.Creator, FileMimeType = comment.FileMimeType, FileURL = comment.FileURL, // Fullname = comment.FullName, Id = comment.Id, IsNew = comment.IsNew, Modified = comment.UpdatedUtc, Parent = comment.Parent, UpvoteCount = comment.UpvoteCount, UserHasUpvoted = !string.IsNullOrEmpty(comment.UserVoted) && comment.UserVoted.Split(',').Any(o => o == currentUserId), UserVoted = comment.UserVoted, // Pings = !string.IsNullOrEmpty(comment.Pings) ? comment.Pings.Split(',') : null, GroupId = comment.GroupId, // GroupName = comment.CommentGroupTypeId == CommentGroupType.Profile ? comment.Creator == currentUserId ? " your profile " : (comment.GenderId == Gender.Female ? "her profile" : "his profile") : comment.GroupName + " group", //PostOnURL = comment.PostOnURL }; rootIdList.Add(comment.Id); commentViewModelList.Add(commentViewModel); } currentCommentsId.AddRange(rootIdList); //Get all post child list, where root id=post id // var postChildList = new List<CommentViewModel>(); if (rootIdList.Count >= 1) { //add parent comment childs //var commentChilds = _context.Comment.Where(c => rootIdList.Contains(c.Root)); // var commentChilds = _context.Comment.Where(c => rootIdList.Contains(c.Root)) // .Join(_context.Users, c => c.Creator, u => u.Id, (comment, user) => // new // { // comment.Content, // comment.Created, // comment.CreatedByAdmin, // CreatedByCurrentUser = comment.Creator, // comment.Creator, // comment.FileMimeType, // comment.FileURL, // FullName = user.UserName, // comment.Id, // comment.IsNew, // comment.Modified, // comment.Parent, // comment.UpvoteCount, // comment.UserHasUpvoted, // comment.UserVoted, // comment.Pings, // comment.Root // }).OrderBy(c => c.Created); // // foreach (var comment in commentChilds) // { // //preparing child comments // var commentViewModel = new CommentRecordViewModel() // { // Content = comment.Content, // Created = comment.Created, // CreatedByAdmin = comment.CreatedByAdmin, // CreatedByCurrentUser = comment.Creator == currentUserId, // Creator = comment.Creator, // FileMimeType = comment.FileMimeType, // FileURL = comment.FileURL, // Fullname = comment.FullName, // Root = comment.Root, // Id = comment.Id, // IsNew = comment.IsNew, // Modified = comment.Modified, // Parent = comment.Parent, // UpvoteCount = comment.UpvoteCount, // UserHasUpvoted = !string.IsNullOrEmpty(comment.UserVoted) && (comment.UserVoted.Split(',').Any(o => o == currentUserId)), // UserVoted = comment.UserVoted, // Pings = !string.IsNullOrEmpty(comment.Pings) ? comment.Pings.Split(',') : null // }; // commentViewModelList.Add(commentViewModel); // currentCommentsId.Add(comment.Id); // } } SetCurrentCommentsNotificationsAsSeen(currentUserId, currentCommentsId); } return(commentViewModelList); }
private Task PrepareProfileChatNotifications(CommentRecordViewModel comment, string currentUserId) { var friendId = comment.GroupId.Split("__").SingleOrDefault(f => f != currentUserId);//select the other user.__ is the friends id seperator var friendConnection = NotificationHub.Connections.SingleOrDefault(o => o.UserId == friendId); //todo // var friendNotificationNum = 123;// _context.Notification.Count(n => n.ToUserId == friendId && n.IsSeen == false); //prepare the current post info /* * var currentComment = new CommentRecordViewModel() * { * Content = comment.Content, * Created = comment.Created, * CreatedByAdmin = false, * CreatedByCurrentUser = false, * Creator = comment.Creator, * FileMimeType = comment.FileMimeType, * FileURL = comment.FileURL, * Fullname = comment.Fullname, * Id = comment.Id, * IsNew = true, * Modified = comment.Modified, * Parent = comment.Parent, * UpvoteCount = comment.UpvoteCount, * UserHasUpvoted = false, * Pings = comment.Pings, * CommentGroupType = comment.CommentGroupType, * FileName = comment.FileName, * GroupId = comment.GroupId, * // ProfilePictureUrl = comment.ProfilePictureUrl, * Root = comment.Root, * UserVoted = comment.UserVoted, * CreatorName = User.Identity.Name, * // GroupName = GetGommentGroupName(comment) * }; */ //add notifications to other member if (friendId != null) { /*var notification = new Notification * { * Content = comment.Content, * CreatedUtc = DateTime.Now, * GroupId = comment.GroupId, * IsSeen = false, * FromUserId = currentUserId, * NotificationType = NotificationType.NewChatMessage, * Title = string.Format(" sent a message to you"), * CorrelationId = comment.Id.ToString(), * Event = "Message", * ToUserId = friendId * }; * * if (!string.IsNullOrEmpty(comment.FileURL)) * { * notification.Title = string.Format(" sent an attachment to you "); * } * // _context.Notification.Add(notification); * await _notificationRepository.InsertAsync(notification);*/ } //send notifications to current connected comment group member if (friendConnection != null) { //todo // await _realTimeNotifier.HubContext().Clients.Client(friendConnection.ConnectionID).SendAsync("RefreshChatNotificationNum", friendNotificationNum, currentComment); } return(Task.CompletedTask); }
private async Task PrepareGroupChatNotifications(CommentRecordViewModel comment, string userId) { //get all current group cliend connection id, and send the message to them. var groupsMember = new[] { new { GroupId = 1, MemberId = 2, GroupName = "dddd" } }; var groupMembersQuery = _session.QueryIndex <ContentPickerFieldIndex>(x => x.SelectedContentItemId == comment.ContentItemId); var groupMemberIdResults = (await groupMembersQuery.ListAsync()).Select(x => x.ContentItemId); // var groupsMember = _context.GroupMember.Where(g => g.GroupId == comment.GroupId && g.MemberId != currentUserId) // .Join(_context.Group, gm => gm.GroupId, g => g.Id, (gm, g) => new // { // gm.GroupId, // gm.MemberId, // GroupName = g.Name // }); // get all the connected participant notification number. // get all the connected participant notification number. var connectedMemberNotificationNum = NotificationHub.Connections .Where(uId => uId.UserId != userId); // .GroupJoin(_unitOfWork.Notification // .Find(o => o.GroupId == comment.GroupId && o.IsSeen == false), uId => uId, n => n.ToUserId, // (UserId, NotificationNum) => new KeyValuePair<string, int>(UserId, NotificationNum.Count() + 1)); // return (await _session.QueryIndex<AutoroutePartIndex>(o => o.ContentItemId != context.ContentItem.ContentItemId && o.Path == path).CountAsync()) ; //prepare the current post info /*var currentComment = new CommentRecordViewModel() * { * Content = comment.Content, * Created = comment.Created, * CreatedByAdmin = false, * CreatedByCurrentUser = false, * Creator = comment.Creator, * FileMimeType = comment.FileMimeType, * FileURL = comment.FileURL, * Fullname = comment.Fullname, * Id = comment.Id, * IsNew = true, * Modified = comment.Modified, * Parent = comment.Parent, * UpvoteCount = comment.UpvoteCount, * UserHasUpvoted = false, * Pings = comment.Pings, * CommentGroupType = comment.CommentGroupType, * FileName = comment.FileName, * GroupId = comment.GroupId, * // ProfilePictureUrl = comment.ProfilePictureUrl, * Root = comment.Root, * UserVoted = comment.UserVoted, * CreatorName = User.Identity.Name, * // GroupName = GetGommentGroupName(comment) * };*/ /*foreach (var member in groupsMember) * { * //add notifications to other member * // if (member.MemberId != null) * // { * var notification = new Notification * { * Content = comment.Content, * CreatedUtc = DateTime.Now, * GroupId = comment.GroupId, * IsSeen = false, * FromUserId = userId, * NotificationType = NotificationType.NewChatMessage, * Title = string.Format(" sent a message {0}", string.IsNullOrEmpty(member.GroupName) ? "" : string.Format(" in {0} chat session ", member.GroupName)), //need to improve in next version * CorrelationId = comment.Id.ToString(), * Event = "Comment", * ToUserId = member.MemberId.ToString() * }; * * if (!string.IsNullOrEmpty(comment.FileURL)) * { * notification.Title = string.Format(" sent an attachment {0}", string.IsNullOrEmpty(member.GroupName) ? "" : string.Format(" in {0} chat session ", member.GroupName)); //need to improve in next version * } * * await _notificationRepository.InsertAsync(notification); * // _context.Notification.Add(notification); * * // } * }*/ //todo //send notifications to current connected comment group member /*foreach (var conn in connectedMemberNotificationNum) * { * if (conn.ConnectionID != null) * { * await _realTimeNotifier.HubContext().Clients.Client(conn.ConnectionID).SendAsync("RefreshChatNotificationNum", conn.NotificationNum, currentComment); * // await _hubContext.Clients.Client(conn.ConnectionID).SendAsync("ReceiveMessage", currentComment, conn.NotificationNum); * } * }*/ }
public async Task <IActionResult> PostComment(CommentRecordViewModel comment) { // string userId = SignInManager // .AuthenticationManager // .AuthenticationResponseGrant.Identity.GetUserId(); byte[] file = await GetCommentAttachedFile(comment); // var currentUserId = _userManager.GetUserId(User); //comment.Id = Guid.NewGuid().ToString(); comment.CreatedByCurrentUser = false; // comment.Creator = currentUserId; comment.IsNew = true; comment.CreatedByCurrentUser = true; #region Get the current comment root Id CommentRecord parent = null; if (comment.Parent != null) { // parent = _context.Comment.SingleOrDefault(o => o.Id == comment.Parent); parent = await _commentRepository.FindByIdAsync(comment.Parent.GetValueOrDefault()); if (parent != null) { // if (string.IsNullOrEmpty(parent.Parent)) if (parent.Parent != null) { comment.Root = parent.Id; } else { comment.Root = parent.Root; } } } #endregion if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var commentModel = new CommentRecord() { Content = comment.Content ?? "", CreatedUtc = DateTime.Now, CreatedByAdmin = false, Creator = comment.Creator, FileMimeType = comment.FileMimeType, FileURL = comment.FileURL, // Id = comment.Id, ContentItemId = comment.ContentItemId, Parent = comment.Parent, Root = comment.Root, UpvoteCount = 0, UserHasUpvoted = false, UserVoted = "", CreatedByCurrentUser = false, IsNew = false, UpdatedUtc = DateTime.Now, Pings = comment.Pings != null?string.Join(",", comment.Pings) : null, File = file, GroupId = parent != null ? parent.GroupId : comment.GroupId, CommentGroupType = parent != null ? parent.CommentGroupType : comment.CommentGroupType }; comment.GroupId = commentModel.GroupId; comment.CommentGroupType = commentModel.CommentGroupType; // _context.Comment.Add(commentModel); await _commentRepository.InsertAsync(commentModel); comment.Id = commentModel.Id; comment.CreatedUtc = commentModel.CreatedUtc; // CreateTransaction(comment, currentUserId, CommentTransactionType.Add); #region prepare all Notifications types /* * //maybe could use field indexes to get group members * var groupMembersQuery = _session.QueryIndex<ContentPickerFieldIndex>(x =>x.SelectedContentItemId == comment.ContentItemId); * var groupMemberIdResults = (await groupMembersQuery.ListAsync()).Select(x=> x.ContentItemId); */ //push notifications await _pushNotificationService.PushCommentToGroupMembers(comment.ContentItemId, comment.ToCommentDetailsView()); //await _pushNotificationService.PushNotifications(connectedUserNotificationNumList, _commentsRetrievalService.GetCommentView(newComment.Id)?.ToCommentDetailsView()); //await PrepareCommentNotifications(comment, comment.Creator); #endregion // await _context.SaveChangesAsync(); return(Json(comment)); }
public async Task <IActionResult> Edit(int id, string localId) { if (!await _authorizationService.AuthorizeAsync(User, Intelli.Comments.Permissions.ManageComments)) { return(Unauthorized()); } var newLocalId = string.IsNullOrWhiteSpace(localId) ? Guid.NewGuid().ToString() : localId; // var availableActivities = _activityLibrary.ListActivities(); var comment = await _commentsRepository.FindByIdAsync(id); if (comment == null) { return(NotFound()); } // var workflow = _workflowManager.NewWorkflow(workflowType); // var workflowContext = await _workflowManager.CreateWorkflowExecutionContextAsync(workflowType, workflow); // var activityContexts = await Task.WhenAll(workflowType.Activities.Select(async x => await _workflowManager.CreateActivityExecutionContextAsync(x, x.Properties))); // var workflowCount = await _session.QueryIndex<WorkflowIndex>(x => x.WorkflowTypeId == workflowType.WorkflowTypeId).CountAsync(); // // var activityThumbnailShapes = new List<dynamic>(); // var index = 0; // // foreach (var activity in availableActivities) // { // activityThumbnailShapes.Add(await BuildActivityDisplay(activity, index++, id, newLocalId, "Thumbnail")); // } // // var activityDesignShapes = new List<dynamic>(); // index = 0; // // foreach (var activityContext in activityContexts) // { // activityDesignShapes.Add(await BuildActivityDisplay(activityContext, index++, id, newLocalId, "Design")); // } // // var activitiesDataQuery = activityContexts.Select(x => new // { // Id = x.ActivityRecord.ActivityId, // X = x.ActivityRecord.X, // Y = x.ActivityRecord.Y, // Name = x.ActivityRecord.Name, // IsStart = x.ActivityRecord.IsStart, // IsEvent = x.Activity.IsEvent(), // Outcomes = x.Activity.GetPossibleOutcomes(workflowContext, x).ToArray() // }); // var workflowTypeData = new // { // Id = workflowType.Id, // Name = workflowType.Name, // IsEnabled = workflowType.IsEnabled, // Activities = activitiesDataQuery.ToArray(), // Transitions = workflowType.Transitions // }; var viewModel = new CommentRecordViewModel() { Content = comment.Content, Created = comment.CreatedUtc, CreatedByAdmin = comment.CreatedByAdmin, // CreatedByCurrentUser = comment.Creator , Creator = comment.Creator, FileMimeType = comment.FileMimeType, FileURL = comment.FileURL, // Fullname = comment.FullName, Id = comment.Id, IsNew = comment.IsNew, Modified = comment.UpdatedUtc, Parent = comment.Parent, UpvoteCount = comment.UpvoteCount, // UserHasUpvoted = // !string.IsNullOrEmpty(comment.UserVoted) && // comment.UserVoted.Split(',').Any(o => o == currentUserId), UserVoted = comment.UserVoted, // Pings = !string.IsNullOrEmpty(comment.Pings) ? comment.Pings.Split(',') : null, GroupId = comment.GroupId, // GroupName = comment.CommentGroupTypeId == CommentGroupType.Profile ? comment.Creator == currentUserId ? " your profile " : (comment.GenderId == Gender.Female ? "her profile" : "his profile") : comment.GroupName + " group", //PostOnURL = comment.PostOnURL }; return(View(viewModel)); }