예제 #1
0
        //Notification Messages

        private static void _send_notification_message(Guid applicationId, List <Pair> users, Notification not)
        {
            try
            {
                if (not == null || !not.SubjectType.HasValue || !not.Action.HasValue)
                {
                    return;
                }

                EmailTemplates.Initialize(applicationId);

                List <NotificationMessage> messageLst = NotificationController._get_notification_messages_info(
                    applicationId, users, not.SubjectType.Value, not.Action.Value);

                List <Guid> receiverIds = messageLst.Where(m => m != null && m.ReceiverUserID.HasValue)
                                          .Select(m => m.ReceiverUserID.Value).Distinct().ToList();

                List <EmailAddress> emailList = UsersController.get_users_main_email(receiverIds);
                List <PhoneNumber>  phoneList = UsersController.get_users_main_phone(receiverIds);

                List <NotificationMessage> sentMessages = new List <NotificationMessage>();

                SortedSet <Guid> emailSentTo = new SortedSet <Guid>();
                SortedSet <Guid> smsSentTo   = new SortedSet <Guid>();

                messageLst.Where(m => m.ReceiverUserID.HasValue).ToList()
                .ForEach(m =>
                {
                    m.Subject = Expressions.replace(m.Subject, ref not.ReplacementDic, Expressions.Patterns.AutoTag);
                    m.Text    = Expressions.replace(m.Text, ref not.ReplacementDic, Expressions.Patterns.AutoTag);

                    if (m.Media == Media.Email &&
                        !emailSentTo.Any(u => u == m.ReceiverUserID) && emailList.Any(e => e.UserID == m.ReceiverUserID))
                    {
                        m.Action      = not.Action.Value;
                        m.RefItemID   = not.RefItemID.Value;
                        m.SubjectType = not.SubjectType.Value;
                        m.UserStatus  =
                            users.Where(u => (Guid)u.First == m.ReceiverUserID).Select(u => (UserStatus)u.Second).First();
                        m.EmailAddress = emailList.Where(e => e.UserID == m.ReceiverUserID).First();

                        ThreadPool.QueueUserWorkItem(new WaitCallback(m.send_email), applicationId);

                        emailSentTo.Add(m.ReceiverUserID.Value);
                    }

                    if (m.Media == Media.SMS &&
                        !smsSentTo.Any(u => u == m.ReceiverUserID) && phoneList.Any(p => p.UserID == m.ReceiverUserID))
                    {
                        m.Action      = not.Action.Value;
                        m.RefItemID   = not.RefItemID.Value;
                        m.SubjectType = not.SubjectType.Value;
                        m.UserStatus  =
                            users.Where(u => (Guid)u.First == m.ReceiverUserID).Select(u => (UserStatus)u.Second).First();
                        m.PhoneNumber = phoneList.Where(p => p.UserID == m.ReceiverUserID).First();
                        ThreadPool.QueueUserWorkItem(new WaitCallback(m.send_sms));

                        smsSentTo.Add(m.ReceiverUserID.Value);
                    }
                });
            }
            catch (Exception ex)
            {
                LogController.save_error_log(applicationId, null, "send_notification_messages", ex, ModuleIdentifier.NTFN);
            }
        }
예제 #2
0
        private static void _send_notification(Guid applicationId, Notification info)
        {
            if (!RaaiVanConfig.Modules.Notifications(applicationId))
            {
                return;
            }

            if (!info.Action.HasValue || info.Action.Value == ActionType.None ||
                !info.SubjectType.HasValue || info.SubjectType.Value == SubjectType.None)
            {
                return;
            }

            List <Pair> users = new List <Pair>();

            if (info.UserID.HasValue && info.UserID != info.Sender.UserID)
            {
                users.Add(new Pair(info.UserID.Value, UserStatus.Owner));
            }

            List <Guid> userIds = new List <Guid>();

            List <Guid> mentionedUserIds = info.Action == ActionType.Post || info.Action == ActionType.Share || info.Action == ActionType.Comment ?
                                           Expressions.get_tagged_items(info.Description, "User").Where(u => u.ID.HasValue && u.ID != info.UserID)
                                           .Select(u => u.ID.Value).ToList() : new List <Guid>();

            info.Description = PublicMethods.markup2plaintext(applicationId,
                                                              Expressions.replace(info.Description, Expressions.Patterns.HTMLTag, " "));

            switch (info.Action.Value)
            {
            case ActionType.Like:
                switch (info.SubjectType.Value)
                {
                case SubjectType.Node:
                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Owner)) == null)
                    {
                        userIds = CNController.get_node_creators(applicationId, info.RefItemID.Value).Select(
                            u => u.User.UserID.Value).ToList();
                    }
                    foreach (Guid _uid in userIds)
                    {
                        users.Add(new Pair(_uid, UserStatus.Owner));
                    }

                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Member)) == null)
                    {
                        userIds = CNController.get_member_user_ids(applicationId,
                                                                   info.RefItemID.Value, NodeMemberStatuses.Accepted);
                    }
                    foreach (Guid _uid in userIds)
                    {
                        users.Add(new Pair(_uid, UserStatus.Member));
                    }

                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Expert)) == null)
                    {
                        userIds = CNController.get_experts(applicationId, info.RefItemID.Value).Select(
                            u => u.User.UserID.Value).ToList();
                    }
                    foreach (Guid _usr in userIds)
                    {
                        users.Add(new Pair(_usr, UserStatus.Expert));
                    }

                    Node node = CNController.get_node(applicationId, info.RefItemID.Value, true);
                    if (node != null)
                    {
                        info.SubjectName = node.Name;
                        info.Description = node.Description;
                        info.Info        = "{\"NodeType\":\"" + Base64.encode(node.NodeType) + "\"}";
                    }
                    break;

                case SubjectType.Question:
                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Owner)) == null)
                    {
                        userIds = new List <Guid>()
                        {
                        };
                        Guid?id = QAController.get_question_asker_id(applicationId, info.RefItemID.Value);
                        if (id.HasValue)
                        {
                            userIds.Add(id.Value);
                        }
                    }
                    foreach (Guid _uid in userIds)
                    {
                        users.Add(new Pair(_uid, UserStatus.Owner));
                    }

                    Question question = QAController.get_question(applicationId, info.RefItemID.Value, null);
                    if (question != null)
                    {
                        info.SubjectName = question.Title;
                        info.Description = question.Description;
                    }
                    break;

                case SubjectType.Post:
                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Owner)) == null)
                    {
                        userIds = new List <Guid>();
                        Guid?id = SharingController.get_post_sender_id(applicationId, info.RefItemID.Value);
                        if (id.HasValue)
                        {
                            userIds.Add(id.Value);
                        }
                    }
                    foreach (Guid _uid in userIds)
                    {
                        users.Add(new Pair(_uid, UserStatus.Owner));
                    }

                    Post post = SharingController.get_post(applicationId, info.RefItemID.Value, null);
                    info.Description = string.IsNullOrEmpty(post.Description) ?
                                       post.OriginalDescription : post.Description;
                    break;

                case SubjectType.Comment:
                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Owner)) == null)
                    {
                        userIds = new List <Guid>();
                        Guid?id = SharingController.get_comment_sender_id(applicationId, info.SubjectID.Value);
                        if (id.HasValue)
                        {
                            userIds.Add(id.Value);
                        }
                    }
                    foreach (Guid _uid in userIds)
                    {
                        users.Add(new Pair(_uid, UserStatus.Owner));
                    }

                    Sharing.Comment comment = SharingController.get_comment(applicationId, info.SubjectID.Value, null);
                    info.RefItemID   = comment.PostID;
                    info.Description = comment.Description;
                    break;
                }
                break;

            case ActionType.Dislike:
                switch (info.SubjectType.Value)
                {
                case SubjectType.Post:
                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Owner)) == null)
                    {
                        userIds = new List <Guid>();
                        Guid?id = SharingController.get_post_sender_id(applicationId, info.RefItemID.Value);
                        if (id.HasValue)
                        {
                            userIds.Add(id.Value);
                        }
                    }
                    foreach (Guid _uid in userIds)
                    {
                        users.Add(new Pair(_uid, UserStatus.Owner));
                    }

                    Post post = SharingController.get_post(applicationId, info.RefItemID.Value, null);
                    info.Description = string.IsNullOrEmpty(post.Description) ? post.OriginalDescription : post.Description;
                    break;

                case SubjectType.Comment:
                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Owner)) == null)
                    {
                        userIds = new List <Guid>();
                        Guid?id = SharingController.get_comment_sender_id(applicationId, info.SubjectID.Value);
                        if (id.HasValue)
                        {
                            userIds.Add(id.Value);
                        }
                    }
                    foreach (Guid _uid in userIds)
                    {
                        users.Add(new Pair(_uid, UserStatus.Owner));
                    }

                    Sharing.Comment comment = SharingController.get_comment(applicationId, info.SubjectID.Value, null);
                    info.RefItemID   = comment.PostID;
                    info.Description = comment.Description;
                    break;
                }
                break;

            case ActionType.Question:
                switch (info.SubjectType.Value)
                {
                case SubjectType.Question:
                    if (info.ReceiverUserIDs != null && info.ReceiverUserIDs.Count > 0)
                    {
                        userIds = info.ReceiverUserIDs;
                        foreach (Guid _uid in userIds)
                        {
                            users.Add(new Pair(_uid, UserStatus.Contributor));
                        }
                    }
                    break;
                }
                break;

            case ActionType.Answer:
                switch (info.SubjectType.Value)
                {
                case SubjectType.Answer:
                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Owner)) == null)
                    {
                        userIds = new List <Guid>();
                        Guid?id = QAController.get_question_asker_id(applicationId, info.RefItemID.Value);
                        if (id.HasValue)
                        {
                            userIds.Add(id.Value);
                        }
                    }
                    foreach (Guid _uid in userIds)
                    {
                        users.Add(new Pair(_uid, UserStatus.Owner));
                    }

                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Fan)) == null)
                    {
                        userIds = GlobalController.get_fan_ids(applicationId, info.RefItemID.Value).ToList();
                    }
                    foreach (Guid _usr in userIds)
                    {
                        users.Add(new Pair(_usr, UserStatus.Fan));
                    }

                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Contributor)) == null)
                    {
                        userIds = QAController.get_answer_sender_ids(applicationId, info.RefItemID.Value).ToList();
                    }
                    foreach (Guid _usr in userIds)
                    {
                        users.Add(new Pair(_usr, UserStatus.Contributor));
                    }

                    info.SubjectName = QAController.get_question(applicationId, info.RefItemID.Value, null).Title;
                    break;
                }
                break;

            case ActionType.Post:
            case ActionType.Share:
                switch (info.SubjectType.Value)
                {
                case SubjectType.Post:
                    foreach (Guid _usr in mentionedUserIds)
                    {
                        users.Add(new Pair(_usr, UserStatus.Mentioned));
                    }

                    Node node = null;

                    bool isNode = info.RefItemID.HasValue &&
                                  CNController.is_node(applicationId, info.RefItemID.Value);

                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Director)) != null)
                    {
                        foreach (Guid _usr in userIds)
                        {
                            users.Add(new Pair(_usr, UserStatus.Owner));
                        }
                    }

                    if (isNode)
                    {
                        if ((userIds = __get_audience_user_ids(ref info, UserStatus.Owner)) == null)
                        {
                            userIds = CNController.get_node_creators(applicationId, info.RefItemID.Value).Select(
                                u => u.User.UserID.Value).ToList();
                        }
                        foreach (Guid _usr in userIds)
                        {
                            users.Add(new Pair(_usr, UserStatus.Owner));
                        }

                        if ((userIds = __get_audience_user_ids(ref info, UserStatus.Member)) == null)
                        {
                            userIds = CNController.get_members(applicationId, info.RefItemID.Value,
                                                               pending: false, admin: null).Select(u => u.Member.UserID.Value).ToList();
                        }
                        foreach (Guid _usr in userIds)
                        {
                            users.Add(new Pair(_usr, UserStatus.Member));
                        }

                        if ((userIds = __get_audience_user_ids(ref info, UserStatus.Fan)) == null)
                        {
                            userIds = CNController.get_node_fans_user_ids(applicationId, info.RefItemID.Value).ToList();
                        }
                        foreach (Guid _usr in userIds)
                        {
                            users.Add(new Pair(_usr, UserStatus.Fan));
                        }

                        node = CNController.get_node(applicationId, info.RefItemID.Value);
                        if (node != null)
                        {
                            info.SubjectName = node.Name;
                        }
                    }

                    if (RaaiVanConfig.Modules.SMSEMailNotifier(applicationId))
                    {
                        User user = UsersController.get_user(applicationId, info.Sender.UserID.Value);
                        info.ReplacementDic["SenderProfileImageURL"] =
                            DocumentUtilities.get_personal_image_address(applicationId, user.UserID.Value, true);
                        info.ReplacementDic["SenderFullName"] = user.FirstName + " " + user.LastName;
                        info.ReplacementDic["SenderPageURL"]  =
                            PublicConsts.get_complete_url(applicationId, PublicConsts.HomePage) +
                            "/" + user.UserID.Value.ToString();
                        info.ReplacementDic["PostURL"] =
                            PublicConsts.get_complete_url(applicationId, PublicConsts.PostPage) +
                            "/" + info.SubjectID.Value.ToString();
                        info.ReplacementDic["Description"] = info.Description;

                        if (isNode)
                        {
                            info.ReplacementDic["NodePageURL"] =
                                PublicConsts.get_complete_url(applicationId, PublicConsts.NodePage) +
                                "/" + info.RefItemID.Value.ToString();
                            if (node != null)
                            {
                                info.ReplacementDic["NodeName"] = node.Name;
                            }
                        }
                    }

                    break;
                }
                break;

            case ActionType.Comment:
                switch (info.SubjectType.Value)
                {
                case SubjectType.Comment:
                    foreach (Guid _usr in mentionedUserIds)
                    {
                        users.Add(new Pair(_usr, UserStatus.Mentioned));
                    }

                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Owner)) == null)
                    {
                        userIds = new List <Guid>();
                        Guid?id = SharingController.get_post_sender_id(applicationId, info.RefItemID.Value);
                        if (id.HasValue)
                        {
                            userIds.Add(id.Value);
                        }
                    }
                    foreach (Guid _uid in userIds)
                    {
                        users.Add(new Pair(_uid, UserStatus.Owner));
                    }

                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Fan)) == null)
                    {
                        userIds = SharingController.get_post_fan_ids(applicationId, info.RefItemID.Value).ToList();
                    }
                    foreach (Guid _usr in userIds)
                    {
                        users.Add(new Pair(_usr, UserStatus.Fan));
                    }

                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Contributor)) == null)
                    {
                        userIds = SharingController.get_comment_sender_ids(applicationId, info.RefItemID.Value).ToList();
                    }
                    foreach (Guid _usr in userIds)
                    {
                        users.Add(new Pair(_usr, UserStatus.Contributor));
                    }

                    if (RaaiVanConfig.Modules.SMSEMailNotifier(applicationId))
                    {
                        User user = UsersController.get_user(applicationId, info.Sender.UserID.Value);
                        info.ReplacementDic["SenderProfileImageURL"] =
                            DocumentUtilities.get_personal_image_address(applicationId, user.UserID.Value, true);
                        info.ReplacementDic["SenderFullName"] = user.FirstName + " " + user.LastName;
                        info.ReplacementDic["SenderPageURL"]  =
                            PublicConsts.get_complete_url(applicationId, PublicConsts.ProfilePage) +
                            "/" + user.UserID.Value.ToString();
                        info.ReplacementDic["PostURL"] =
                            PublicConsts.get_complete_url(applicationId, PublicConsts.PostPage) +
                            "/" + info.RefItemID.Value.ToString();
                        info.ReplacementDic["Description"] = info.Description;
                    }

                    break;

                case SubjectType.Question:
                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Owner)) == null)
                    {
                        userIds = new List <Guid>();
                        Guid?id = QAController.get_question_asker_id(applicationId, info.RefItemID.Value);
                        if (id.HasValue)
                        {
                            userIds.Add(id.Value);
                        }
                    }
                    foreach (Guid _uid in userIds)
                    {
                        users.Add(new Pair(_uid, UserStatus.Owner));
                    }

                    break;

                case SubjectType.Answer:
                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Owner)) == null)
                    {
                        userIds = new List <Guid>();
                        Guid?commentId = QAController.get_comment_owner_id(applicationId, info.SubjectID.Value);
                        if (commentId.HasValue)
                        {
                            userIds.Add(commentId.Value);
                        }
                    }
                    foreach (Guid _uid in userIds)
                    {
                        users.Add(new Pair(_uid, UserStatus.Owner));
                    }

                    break;
                }
                break;

            case ActionType.Modify:
                switch (info.SubjectType.Value)
                {
                case SubjectType.Wiki:
                    Node node = CNController.get_node(applicationId, info.RefItemID.Value, false);

                    if (node != null && node.NodeID.HasValue)
                    {
                        if ((userIds = __get_audience_user_ids(ref info, UserStatus.Owner)) == null)
                        {
                            userIds = CNController.get_node_creators(applicationId, info.RefItemID.Value).Select(
                                u => u.User.UserID.Value).ToList();
                        }
                        foreach (Guid _usr in userIds)
                        {
                            users.Add(new Pair(_usr, UserStatus.Owner));
                        }

                        if ((userIds = __get_audience_user_ids(ref info, UserStatus.Fan)) == null)
                        {
                            userIds =
                                CNController.get_node_fans_user_ids(applicationId, info.RefItemID.Value).ToList();
                        }
                        foreach (Guid _usr in userIds)
                        {
                            users.Add(new Pair(_usr, UserStatus.Fan));
                        }

                        if ((userIds = __get_audience_user_ids(ref info, UserStatus.Expert)) == null)
                        {
                            userIds = CNController.get_experts(applicationId, info.RefItemID.Value).Select(
                                u => u.User.UserID.Value).ToList();
                        }
                        foreach (Guid _usr in userIds)
                        {
                            users.Add(new Pair(_usr, UserStatus.Expert));
                        }

                        if ((userIds = __get_audience_user_ids(ref info, UserStatus.Member)) == null)
                        {
                            userIds = CNController.get_member_user_ids(applicationId, info.RefItemID.Value);
                        }
                        foreach (Guid _usr in userIds)
                        {
                            users.Add(new Pair(_usr, UserStatus.Member));
                        }

                        if (!users.Exists(u => (UserStatus)u.Second == UserStatus.Owner))
                        {
                            users.Add(new Pair(node.Creator.UserID.Value, UserStatus.Owner));
                        }

                        info.SubjectName = node.Name;
                        info.Info        = "{\"NodeType\":\"" + Base64.encode(node.NodeType) + "\"}";

                        if (RaaiVanConfig.Modules.SMSEMailNotifier(applicationId))
                        {
                            User user = UsersController.get_user(applicationId, info.Sender.UserID.Value);
                            info.ReplacementDic["SenderProfileImageURL"] =
                                DocumentUtilities.get_personal_image_address(applicationId, user.UserID.Value, true);
                            info.ReplacementDic["SenderFullName"] = user.FirstName + " " + user.LastName;
                            info.ReplacementDic["SenderPageURL"]  =
                                PublicConsts.get_complete_url(applicationId, PublicConsts.ProfilePage) +
                                "/" + user.UserID.Value.ToString();
                            info.ReplacementDic["NodeName"]    = node.Name;
                            info.ReplacementDic["NodeType"]    = node.NodeType;
                            info.ReplacementDic["NodePageURL"] =
                                PublicConsts.get_complete_url(applicationId, PublicConsts.NodePage) +
                                "/" + info.RefItemID.Value.ToString();
                            info.ReplacementDic["Description"] = info.Description;
                        }
                    }
                    break;
                }
                break;

            case ActionType.FriendRequest:
                switch (info.SubjectType.Value)
                {
                case SubjectType.User:
                    users.Clear();
                    users.Add(new Pair(info.UserID, UserStatus.Mentioned));

                    info.UserStatus = UserStatus.Mentioned;

                    if (RaaiVanConfig.Modules.SMSEMailNotifier(applicationId))
                    {
                        User user = UsersController.get_user(applicationId, info.RefItemID.Value);
                        info.ReplacementDic["SenderProfileImageURL"] =
                            DocumentUtilities.get_personal_image_address(applicationId, user.UserID.Value, true);
                        info.ReplacementDic["SenderFullName"] = user.FirstName + " " + user.LastName;
                        info.ReplacementDic["SenderPageURL"]  =
                            PublicConsts.get_complete_url(applicationId, PublicConsts.ProfilePage) +
                            "/" + user.UserID.Value.ToString();
                    }

                    break;
                }
                break;

            case ActionType.AcceptFriendRequest:
                switch (info.SubjectType.Value)
                {
                case SubjectType.User:
                    users.Clear();
                    users.Add(new Pair(info.UserID, UserStatus.Mentioned));

                    info.UserStatus = UserStatus.Mentioned;

                    if (RaaiVanConfig.Modules.SMSEMailNotifier(applicationId))
                    {
                        User user = UsersController.get_user(applicationId, info.RefItemID.Value);
                        info.ReplacementDic["SenderProfileImageURL"] =
                            DocumentUtilities.get_personal_image_address(applicationId, user.UserID.Value, true);
                        info.ReplacementDic["SenderFullName"] = user.FirstName + " " + user.LastName;
                        info.ReplacementDic["SenderPageURL"]  =
                            PublicConsts.get_complete_url(applicationId, PublicConsts.ProfilePage) +
                            "/" + user.UserID.Value.ToString();
                    }

                    break;
                }
                break;

            case ActionType.Accept:
                switch (info.SubjectType)
                {
                case SubjectType.Node:
                {
                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Member)) == null &&
                        info.RefItemID.HasValue)
                    {
                        List <Guid> nIds = CNController.get_related_node_ids(applicationId, info.RefItemID.Value,
                                                                             null, null, false, true);

                        List <Guid> creatorIds =
                            CNController.get_node_creators(applicationId, info.RefItemID.Value)
                            .Select(u => u.User.UserID.Value).ToList();

                        userIds = CNController.get_members(applicationId, nIds,
                                                           pending: false, admin: null).Select(u => u.Member.UserID.Value)
                                  .Distinct().Where(x => !creatorIds.Any(a => a == x)).ToList();
                    }
                    foreach (Guid _usr in userIds)
                    {
                        users.Add(new Pair(_usr, UserStatus.Member));
                    }
                }
                break;
                }
                break;

            case ActionType.Publish:
                switch (info.SubjectType)
                {
                case SubjectType.Question:
                    if ((userIds = __get_audience_user_ids(ref info, UserStatus.Owner)) == null)
                    {
                        userIds = new List <Guid>();
                        Guid?id = QAController.get_question_asker_id(applicationId, info.RefItemID.Value);
                        if (id.HasValue)
                        {
                            userIds.Add(id.Value);
                        }
                    }
                    foreach (Guid _uid in userIds)
                    {
                        users.Add(new Pair(_uid, UserStatus.Owner));
                    }

                    break;
                }
                break;
            }

            users = users.Except(users.Where(u => info.Sender.UserID.HasValue && (Guid)u.First == info.Sender.UserID)).ToList();

            DataProvider.SendNotification(applicationId, ref users, info);

            if (RaaiVanConfig.Modules.SMSEMailNotifier(applicationId))
            {
                NotificationController._send_notification_message(applicationId, users, info);
            }
        }