Exemplo n.º 1
0
        public async Task <NotificationTypeDto> CopyAsync(Guid id)
        {
            using (var dbContext = _dbContextScopeFactory.Create())
            {
                var item = await _notificationTypeRepository.FindAsync(p => p.Id == id);

                var copyItem = NotificationType.Create(
                    item.Name, item.Description, item.Template, item.ModuleCode, item.ActionType);
                _notificationTypeRepository.Add(copyItem);
                dbContext.SaveChanges();
                await GetListAsync(true);

                return(_mapper.Map <NotificationTypeDto>(copyItem));
            }
        }
Exemplo n.º 2
0
        public async Task PushNotifyDetail(string clientGroupName, NotificationDetail notificationDetail)
        {
            FilterDefinition <ClientGroup> finder = Builders <ClientGroup> .Filter.Eq("name", clientGroupName);

            ClientGroup clientGroup = await clientGroupRepository.FindAsync(finder);

            User currentUser = Feature.CurrentUser(httpContextAccessor, userRepository);

            foreach (string receiverId in clientGroup.UserIds)
            {
                var existNotificationDetailBuilder = Builders <NotificationDetail> .Filter;
                var existNotificationDetailFilter  =
                    existNotificationDetailBuilder.Eq("creator_id", notificationDetail.CreatorId)
                    & existNotificationDetailBuilder.Eq("receiver_id", receiverId)
                    & existNotificationDetailBuilder.Eq("notification_object_id",
                                                        notificationDetail.NotificationObjectId);

                var existNotificationDetail =
                    await notificationDetailRepository.FindAsync(existNotificationDetailFilter);

                var finalNotificationDetail = new NotificationDetail();

                if (existNotificationDetail != null)
                {
                    finalNotificationDetail = existNotificationDetail;
                    finalNotificationDetail.ModifiedDate = DateTime.Now;
                    finalNotificationDetail.IsDeleted    = false;
                    await notificationDetailRepository.UpdateAsync(finalNotificationDetail, finalNotificationDetail.Id);
                }
                else
                {
                    finalNotificationDetail.CreatorId            = notificationDetail.CreatorId;
                    finalNotificationDetail.ReceiverId           = receiverId;
                    finalNotificationDetail.NotificationObjectId = notificationDetail.NotificationObjectId;
                    await notificationDetailRepository.AddAsync(finalNotificationDetail);
                }

                ;

                var notificationObject =
                    await notificationObjectRepository.GetByIdAsync(
                        ObjectId.Parse(finalNotificationDetail.NotificationObjectId));

                var owner = await userRepository.GetByIdAsync(ObjectId.Parse(notificationObject.OwnerId));

                var receiver = await userRepository.GetByIdAsync(ObjectId.Parse(receiverId));

                var creator = await userRepository.GetByIdAsync(ObjectId.Parse(finalNotificationDetail.CreatorId));

                var notificationTypeFilter =
                    Builders <NotificationType> .Filter.Eq("code", notificationObject.NotificationType);

                var notificationType = await notificationTypeRepository.FindAsync(notificationTypeFilter);

                string notifyContent = string.Empty;

                if (owner.OId == creator.OId)
                {
                    if (receiver.OId == owner.OId)
                    {
                        notifyContent = $"Bạn {notificationType.ContentTemplate} chính mình";
                    }
                    else if (receiver.OId != owner.OId)
                    {
                        notifyContent = $"{creator.LastName} {notificationType.ContentTemplate} họ";
                    }
                }
                else if (owner.OId != creator.OId)
                {
                    if (receiver.OId != owner.OId)
                    {
                        notifyContent = $"{creator.LastName} {notificationType.ContentTemplate} {owner.LastName}";
                    }
                    else if (receiver.OId != creator.OId)
                    {
                        notifyContent = $"{creator.LastName} {notificationType.ContentTemplate} bạn";
                    }
                }


                var notificationToPush = new PushedNotificationViewModel()
                {
                    AuthorName   = $"{creator.FirstName} {creator.LastName}",
                    AuthorAvatar = creator.AvatarHash,
                    AuthorId     = creator.OId,
                    Content      = notifyContent,
                    CreatedDate  = DateTime.Now,
                    IsRead       = false,
                    ModifiedDate = DateTime.Now,
                    ObjectId     = notificationObject.ObjectId,
                    OId          = notificationObject.OId,
                    OwnerId      = owner.OId,
                    Status       = ItemStatus.Active
                };

                switch (notificationType.Code)
                {
                case "ADD_POST_NOTIFY":
                case "UPVOTE_POST_NOTIFY":
                case "DOWNVOTE_POST_NOTIFY":
                    notificationToPush.NotificationType = PushedNotificationType.Post;
                    break;

                case "UPVOTE_COMMENT_NOTIFY":
                case "DOWNVOTE_COMMENT_NOTIFY":
                case "COMMENT_NOTIFY":
                    notificationToPush.NotificationType = PushedNotificationType.Comment;
                    break;

                case "UPVOTE_REPLY_NOTIFY":
                case "DOWNVOTE_REPLY_NOTIFY":
                case "REPLY_COMMENT_NOTIFY":
                    notificationToPush.NotificationType = PushedNotificationType.Reply;
                    break;

                case "FOLLOW_NOTIFY":
                    notificationToPush.NotificationType = PushedNotificationType.User;
                    break;

                default:
                    notificationToPush.NotificationType = PushedNotificationType.Other;
                    break;
                }

                try
                {
                    if (!(owner.OId == creator.OId && receiver.OId == owner.OId))
                    {
                        var userFilter = Builders <FcmInfo> .Filter.Eq("user_id", receiverId);

                        string token = (await fcmInfoRepository.FindAsync(userFilter)).DeviceToken;

                        FirebaseAdmin.Messaging.Message mes = new FirebaseAdmin.Messaging.Message()
                        {
                            Token = token,
                            Data  = new Dictionary <string, string>()
                            {
                                { "notification", JsonConvert.SerializeObject(notificationToPush) }
                            },
                            Notification = new Notification()
                            {
                                Title    = creator.LastName,
                                Body     = notifyContent,
                                ImageUrl = creator.AvatarHash
                            }
                        };
                        string response = await FirebaseMessaging.DefaultInstance.SendAsync(mes).ConfigureAwait(true);
                    }
                }
                catch (Exception)
                {
                    // do nothing
                }
            }
        }