public async Task PushNotify(string clientGroupName, Noftication noftication, Triple <string, string, ObjectNotificationType> notificationSetting, string customContent) { try { FilterDefinition <ClientGroup> finder = Builders <ClientGroup> .Filter.Eq("name", clientGroupName); ClientGroup clientGroup = await clientGroupRepository.FindAsync(finder); foreach (string receiver in clientGroup.UserIds) { Noftication finalNotification = new Noftication() { AuthorId = noftication.AuthorId, OwnerId = noftication.OwnerId, ReceiverId = receiver, ObjectId = noftication.ObjectId, ObjectType = notificationSetting.Item3, ObjectThumbnail = noftication.ObjectThumbnail, Content = customContent }; await nofticationRepository.AddAsync(finalNotification); var notiViewModel = mapper.Map <NotificationViewModel>(finalNotification); try { FilterDefinition <FcmInfo> user = Builders <FcmInfo> .Filter.Eq("user_id", receiver); string token = (await fcmInfoRepository.FindAsync(user)).DeviceToken; User sender = await userRepository.GetByIdAsync(ObjectId.Parse(finalNotification.AuthorId)); FirebaseAdmin.Messaging.Message mes = new FirebaseAdmin.Messaging.Message() { Token = token, Data = new Dictionary <string, string>() { { "notification", JsonConvert.SerializeObject(notiViewModel) } }, Notification = new Notification() { Title = "Quản trị viên CoStudy", Body = notiViewModel.Content, ImageUrl = configuration["AdminAvatar"] } }; string response = await FirebaseMessaging.DefaultInstance.SendAsync(mes).ConfigureAwait(true); } catch (Exception) { continue; } } } catch (Exception) { //Do nothing } }
public async Task <NotificationViewModel> AddNoftication(AddNofticationRequest request) { Noftication noftication = NofticationAdapter.FromRequest(request); await nofticationRepository.AddAsync(noftication); return(mapper.Map <NotificationViewModel>(noftication)); }
public async Task <string> Upvote(string postId) { try { var currentuser = Feature.CurrentUser(httpContextAccessor, userRepository); var currentPost = await postRepository.GetByIdAsync(ObjectId.Parse(postId)); var builderUpvote = Builders <UpVote> .Filter; var filterExistUpvote = builderUpvote.Eq(ObjectVoteId, postId) & builderUpvote.Eq(UpVoteBy, currentuser.OId) & builderUpvote.Eq(IsDeleted, false); var existUpvote = await upVoteRepository.FindAsync(filterExistUpvote); if (existUpvote != null) { return(UserUpvoteAlready); } else if (existUpvote == null) { var builderDownVote = Builders <DownVote> .Filter; var filterExistDownVote = builderDownVote.Eq(ObjectVoteId, postId) & builderDownVote.Eq(DownVoteBy, currentuser.OId) & builderDownVote.Eq(IsDeleted, false); var existDownVote = await downVoteRepository.FindAsync(filterExistDownVote); if (existDownVote != null) { existDownVote.IsDeleted = true; await downVoteRepository.DeleteAsync(existDownVote.Id); } var upvote = new UpVote() { ObjectVoteId = postId, UpVoteBy = currentuser.OId }; await upVoteRepository.AddAsync(upvote); } var notificationDetail = new Noftication() { AuthorId = currentuser.OId, OwnerId = currentPost.AuthorId, ObjectId = currentPost.OId, ObjectThumbnail = currentPost.Title }; await fcmRepository.PushNotify(currentPost.AuthorId, notificationDetail, NotificationContent.UpvotePostNotification); await userService.AddPoint(currentPost.AuthorId, currentPost.OId, PointAdded.Upvote); return(UpvoteSuccess); } catch (Exception) { return("Có lỗi xảy ra. "); } }
public string RemoveViolenceReply() { var comments = replyCommentRepository.GetAll().Where(x => x.Status == ItemStatus.Active); var deletedComment = 0; foreach (var comment in comments) { try { var isViolence = commentService.IsViolenceReply(comment.OId); if (isViolence == true) { var notificationDetail = new Noftication() { AuthorId = "60b5f2623d52db390d464e3e", OwnerId = "60b5f2623d52db390d464e3e", ObjectId = comment.OId, ObjectThumbnail = comment.Content }; fcmRepository.PushNotify(comment.AuthorId, notificationDetail, NotificationContent.ApproveReplyReportNotification, $"Phản hồi của bạn đã bị xóa bởi vì vi phạm quy định của chúng tôi. ").ConfigureAwait(true); deletedComment++; comment.Status = ItemStatus.Blocked; replyCommentRepository.Update(comment, comment.Id); var report = new Report() { ObjectId = comment.OId, ObjectType = Feature.GetTypeName(comment), CreatedDate = DateTime.Now, IsApproved = true, ApprovedBy = "60b5f2623d52db390d464e3e", ApproveDate = DateTime.Now, AuthorId = comment.AuthorId, Reason = new System.Collections.Generic.List <string>() { "606bc1ddd01f5aa1a3e282f5" } }; reportRepository.Add(report); } } catch (Exception) { //Do nothing } } return($"Task complete with {deletedComment} reply comments deleted at {DateTime.Now.ToUniversalTime()}"); }
public string RemoveViolencePost() { var posts = postRepository.GetAll().Where(x => x.Status == ItemStatus.Active); var deletedPost = 0; foreach (var post in posts) { try { var isViolence = postService.IsViolencePost(post.OId); if (isViolence == true) { var notificationDetail = new Noftication() { AuthorId = "60b5f2623d52db390d464e3e", OwnerId = "60b5f2623d52db390d464e3e", ObjectId = post.OId, ObjectThumbnail = post.Title }; fcmRepository.PushNotify(post.AuthorId, notificationDetail, NotificationContent.ApprovePostReportNotification, $"Bài viết của bạn đã bị xóa vì vi phạm quy định của chúng tôi. ").ConfigureAwait(true); deletedPost++; post.Status = ItemStatus.Blocked; postRepository.Update(post, post.Id); var report = new Report() { ObjectId = post.OId, ObjectType = Feature.GetTypeName(post), CreatedDate = DateTime.Now, IsApproved = true, ApprovedBy = "60b5f2623d52db390d464e3e", ApproveDate = DateTime.Now, AuthorId = post.AuthorId, Reason = new System.Collections.Generic.List <string>() { "606bc1ddd01f5aa1a3e282f5" } }; reportRepository.Add(report); } } catch (Exception) { continue; } } return($"Task success with {deletedPost} post deleted at {DateTime.Now.ToUniversalTime()}"); }
public static AddNofticationResponse ToResponse(Noftication noftication) { return(new AddNofticationResponse() { Id = noftication.Id.ToString(), AuthorId = noftication.AuthorId, OwnerId = noftication.OwnerId, Content = noftication.Content, CreatedDate = noftication.CreatedDate, ModifiedDate = noftication.ModifiedDate, Status = noftication.Status, IsRead = noftication.IsRead }); }
public async Task <PostViewModel> AddPost(AddPostRequest request) { var currentUser = Feature.CurrentUser(httpContextAccessor, userRepository); var post = PostAdapter.FromRequest(request); post.AuthorId = currentUser.Id.ToString(); await postRepository.AddAsync(post); await fcmRepository.AddToGroup(new AddUserToGroupRequest() { GroupName = post.OId, Type = Feature.GetTypeName(post), UserIds = new List <string> { currentUser.OId } }); foreach (var item in request.Fields) { item.ObjectId = post.OId; } await levelService.AddObjectLevel(request.Fields); var userMatchs = await GetUsersMatchPostField(post); foreach (var user in userMatchs) { var followBuilder = Builders <Follow> .Filter; var followFilter = followBuilder.Eq("from_id", user.OId) & followBuilder.Eq("to_id", currentUser.OId); var follows = await followRepository.FindListAsync(followFilter); follows.ForEach(async x => { var notificationDetail = new Noftication() { AuthorId = currentUser.OId, OwnerId = currentUser.OId, ObjectId = post.OId, ObjectThumbnail = post.Title }; await fcmRepository.PushNotify(x.FromId, notificationDetail, NotificationContent.AddPostNotification); }); } var response = mapper.Map <PostViewModel>(post); return(response); }
public async Task <ReportViewModel> ReplyReport(CreateReplyReportRequest request) { User currentUser = Feature.CurrentUser(httpContextAccessor, userRepository); ReplyComment comment = await replyCommentRepository.GetByIdAsync(ObjectId.Parse(request.ReplyId)); Report data = new Report() { AuthorId = currentUser.OId, Reason = request.Reason.ToList(), ObjectId = request.ReplyId, ObjectType = Feature.GetTypeName(comment) }; await fcmRepository.AddToGroup( new AddUserToGroupRequest { GroupName = comment.AuthorId, Type = Feature.GetTypeName(currentUser), UserIds = new List <string> { comment.AuthorId } } ); await reportRepository.AddAsync(data); var notificationDetail = new Noftication() { AuthorId = currentUser.OId, OwnerId = currentUser.OId, ObjectId = comment.OId, ObjectThumbnail = comment.Content }; //Bài viết của bạn đã bị báo cáo. await fcmRepository.PushNotify(comment.AuthorId, notificationDetail, NotificationContent.CommentReportNotification, "Phản hồi của bạn đã bị báo cáo. "); return(mapper.Map <ReportViewModel>(data)); }
public async Task <string> AddFollowingsAsync(AddFollowerRequest request) { var currentUser = Feature.CurrentUser(_httpContextAccessor, userRepository); foreach (var item in request.Followers) { var user = await userRepository.GetByIdAsync(ObjectId.Parse(item)); if (user != null) { var filter = Builders <Follow> .Filter; var finder = filter.Eq(FromId, currentUser.OId) & filter.Eq(ToId, item); var existFollowing = await followRepository.FindAsync(finder); if (existFollowing != null) { return(UserFollowAlready); } var follow = new Follow() { FromId = currentUser.OId, ToId = item, FollowDate = DateTime.Now }; await followRepository.AddAsync(follow); var notificationDetail = new Noftication() { AuthorId = currentUser.OId, OwnerId = item, ObjectId = currentUser.OId, ObjectThumbnail = string.Empty }; await fcmRepository.PushNotify(item, notificationDetail, NotificationContent.FollowNotification); } } return(FollowSuccess); }
public async Task <IEnumerable <ReportViewModel> > Approve(IEnumerable <string> ids) { List <Report> dataToApprove = new List <Report>(); foreach (string item in ids) { Report rp = await reportRepository.GetByIdAsync(ObjectId.Parse(item)); if (rp != null) { dataToApprove.Add(rp); } } User currentUser = Feature.CurrentUser(httpContextAccessor, userRepository); foreach (Report item in dataToApprove) { #region Find the same object var reportBuilders = Builders <Report> .Filter; var reportFilter = reportBuilders.Eq("object_id", item.ObjectId); var sameObjects = (await reportRepository.FindListAsync(reportFilter)).ToList(); sameObjects.ForEach(async x => { x.IsApproved = true; x.ModifiedDate = DateTime.Now; x.ApprovedBy = currentUser.OId; x.ApproveDate = DateTime.Now; await reportRepository.UpdateAsync(x, x.Id); }); #endregion if (item.ObjectType.Contains("Post")) { Post post = await postRepository.GetByIdAsync(ObjectId.Parse(item.ObjectId)); post.Status = ItemStatus.Deleted; post.ModifiedDate = DateTime.Now; await postRepository.UpdateAsync(post, post.Id); var user = await userRepository.GetByIdAsync(ObjectId.Parse(post.AuthorId)); if (user == null) { throw new Exception("Không tìm thấy người dùng. "); } var notificationDetail = new Noftication() { AuthorId = currentUser.OId, OwnerId = currentUser.OId, ObjectId = post.OId, ObjectThumbnail = post.Title }; await fcmRepository.PushNotify(post.AuthorId, notificationDetail, NotificationContent.ApprovePostReportNotification, $"Bài viết của bạn đã bị xóa bởi quản trị viên. "); //Báo cáo của bạn đã được duyệt. await fcmRepository.PushNotify(item.AuthorId, notificationDetail, NotificationContent.ApprovePostReportNotification, $"Báo cáo của bạn về bài viết của{user.FirstName} {user.LastName} đã được xem xét. "); } else if (item.ObjectType.Contains("ReplyComment")) { ReplyComment replyComment = await replyCommentRepository.GetByIdAsync(ObjectId.Parse(item.ObjectId)); replyComment.Status = ItemStatus.Deleted; replyComment.ModifiedDate = DateTime.Now; await replyCommentRepository.UpdateAsync(replyComment, replyComment.Id); var user = await userRepository.GetByIdAsync(ObjectId.Parse(replyComment.AuthorId)); if (user == null) { throw new Exception("Không tìm thấy người dùng. "); } var notificationDetail = new Noftication() { AuthorId = currentUser.OId, OwnerId = currentUser.OId, ObjectId = replyComment.OId, ObjectThumbnail = replyComment.Content }; //Báo cáo của bạn đã được duyệt. await fcmRepository.PushNotify(item.AuthorId, notificationDetail, NotificationContent.ApproveReplyReportNotification, $"Báo cáo của bạn về phản hồi của {user.FirstName} {user.LastName} đã được xem xét. "); await fcmRepository.PushNotify(replyComment.AuthorId, notificationDetail, NotificationContent.ApproveReplyReportNotification, $"Phản hồi của bạn đã bị xóa bởi quản trị viên. "); } else if (item.ObjectType.Contains("Comment")) { Comment comment = await commentRepository.GetByIdAsync(ObjectId.Parse(item.ObjectId)); comment.Status = ItemStatus.Deleted; comment.ModifiedDate = DateTime.Now; await commentRepository.UpdateAsync(comment, comment.Id); var user = await userRepository.GetByIdAsync(ObjectId.Parse(comment.AuthorId)); if (user == null) { throw new Exception("Không tìm thấy người dùng. "); } var notificationDetail = new Noftication() { AuthorId = currentUser.OId, OwnerId = currentUser.OId, ObjectId = comment.OId, ObjectThumbnail = comment.Content }; //Báo cáo của bạn đã được duyệt. await fcmRepository.PushNotify(item.AuthorId, notificationDetail, NotificationContent.ApproveCommentReportNotification, $"Báo cáo của bạn về bình luận của {user.FirstName} {user.LastName} đã được xem xét."); await fcmRepository.PushNotify(comment.AuthorId, notificationDetail, NotificationContent.ApproveCommentReportNotification, $"Bình luận của bạn đã bị xóa bởi quản trị viên. "); } } return(mapper.Map <IEnumerable <ReportViewModel> >(dataToApprove)); }
public async Task PushNotify(string clientGroupName, Noftication noftication, Triple <string, string, ObjectNotificationType> notificationSetting) { try { FilterDefinition <ClientGroup> finder = Builders <ClientGroup> .Filter.Eq("name", clientGroupName); ClientGroup clientGroup = await clientGroupRepository.FindAsync(finder); foreach (string receiver in clientGroup.UserIds) { var receiverUser = await userRepository.GetByIdAsync(ObjectId.Parse(receiver)); if (receiverUser.TurnOfNotification != null) { if (receiverUser.TurnOfNotification.Any(x => x == noftication.ObjectId)) { return; } } Noftication finalNotification = new Noftication() { AuthorId = noftication.AuthorId, OwnerId = noftication.OwnerId, ReceiverId = receiver, ObjectId = noftication.ObjectId, ObjectType = notificationSetting.Item3, ObjectThumbnail = noftication.ObjectThumbnail, }; //Người tạo ra thông báo. string creator = noftication.AuthorId; User userCreator = await userRepository.GetByIdAsync(ObjectId.Parse(creator)); //Chủ sở hữu post string owner = noftication.OwnerId; User userOwner = await userRepository.GetByIdAsync(ObjectId.Parse(owner)); if (owner == creator) { if (receiver == owner) { finalNotification.Content = $"Bạn {notificationSetting.Item2} chính mình"; } else if (receiver != owner) { finalNotification.Content = $"{userCreator.FirstName} {userCreator.LastName} {notificationSetting.Item2} họ. "; } } else if (owner != creator) { //owner: Trung //receiver: trung //Creator: Thắng if (receiver != owner) { finalNotification.Content = $"{userCreator.FirstName} {userCreator.LastName} {notificationSetting.Item2} {userOwner.FirstName} {userOwner.LastName}. "; } else if (receiver != creator) { finalNotification.Content = $"{userCreator.FirstName} {userCreator.LastName} {notificationSetting.Item2} bạn. "; } } await nofticationRepository.AddAsync(finalNotification); var notiViewModel = mapper.Map <NotificationViewModel>(finalNotification); if (!(owner == creator && receiver == owner && creator == receiver)) { try { FilterDefinition <FcmInfo> user = Builders <FcmInfo> .Filter.Eq("user_id", receiver); string token = (await fcmInfoRepository.FindAsync(user)).DeviceToken; User sender = await userRepository.GetByIdAsync(ObjectId.Parse(finalNotification.AuthorId)); FirebaseAdmin.Messaging.Message mes = new FirebaseAdmin.Messaging.Message() { Token = token, Data = new Dictionary <string, string>() { { "notification", JsonConvert.SerializeObject(notiViewModel) } }, Notification = new Notification() { Title = $"{sender.FirstName} {sender.LastName}", Body = notiViewModel.Content, ImageUrl = sender.AvatarHash } }; string response = await FirebaseMessaging.DefaultInstance.SendAsync(mes).ConfigureAwait(true); } catch (Exception) { continue; } } } } catch (Exception) { //Do nothing } }