Пример #1
0
        public void AddComment(Models.ComboComment comment)
        {
            Models.ComboResponse _response = new Models.ComboResponse();
            _response.bool_result = true;
            _response.ErrorCode = 0;
            _response.ErrorMsg = "";

            BLL.ComboComment newComment = new ComboComment();
            newComment.AddNew();

            if (comment.ComboUserID != 0)
                newComment.ComboUserID = comment.ComboUserID;
            else
            {
                _response.ErrorCode = 30;
                _response.ErrorMsg = "Can't insert commnet. No user id .";
                _response.bool_result = false;
                SetContentResult(_response);
                return;
            }

            if (comment.ComboPostID != 0)
                newComment.ComboPostID = comment.ComboPostID;
            else
            {
                _response.ErrorCode = 31;
                _response.ErrorMsg = "Can't insert comment. No post id .";
                _response.bool_result = false;
                SetContentResult(_response);
                return;
            }

            newComment.CommentText = comment.CommentText.Replace("\n", " "); ;
            newComment.CommentDate = DateTime.UtcNow;

            newComment.Save();

            JavaScriptSerializer js = new JavaScriptSerializer();
            Models.Attachment[] att = js.Deserialize<Models.Attachment[]>(js.Serialize(comment.Attachments));
            if (att != null)
            {
                ComboCommentAttachment attachment = new ComboCommentAttachment();
                foreach (Models.Attachment item in att)
                {
                    attachment.AddNew();
                    attachment.AttachmentID = item.AttachmentID;
                    attachment.ComboCommnetID = newComment.ComboCommentID;

                }
                attachment.Save();
            }

            ComboCommentAttachment notificationAtt = new ComboCommentAttachment ();
            notificationAtt.GetCommentAttachmentsByCommentID(newComment.ComboCommentID);

            Models.CommentUserTag[] userTags = js.Deserialize<Models.CommentUserTag[]>(js.Serialize(comment.UserTags));
            if (userTags != null)
            {
                CommentUserTag usertag = new CommentUserTag();
                foreach (Models.CommentUserTag item in userTags)
                {
                    usertag.AddNew();
                    usertag.ComboUserID = item.ComboUserID;
                    usertag.ComboCommentID = newComment.ComboCommentID;
                    usertag.Offset = item.Offset;

                    /**************************/
                    // save notification and push it to device
                    ComboUser commentcreator = new ComboUser();
                    ComboUser tagged = new ComboUser();
                    commentcreator.GetUserByUserId(newComment.ComboUserID);
                    tagged.GetUserByUserId(item.ComboUserID);
                    List<Models.CommentUserTag> postTag = new List<Models.CommentUserTag>();
                    postTag.Add(new Models.CommentUserTag
                    {
                        ComboCommentID = newComment.ComboCommentID,
                        ComboUserID = tagged.ComboUserID,
                        UserName = tagged.UserName,
                        Offset = item.Offset,
                        ComboPostID = newComment.ComboPostID,
                        CreatorUserID = commentcreator.ComboUserID,
                        CreatorUserName= commentcreator.UserName,
                        CreatorProfilePic = commentcreator.GetColumn("ProfilePic").ToString(),
                        CommentText = newComment.CommentText,
                        Attachments = notificationAtt.DefaultView.Table.AsEnumerable().Select(r =>
                        {
                            return new Models.Attachment
                            {
                                AttachmentID = Convert.ToInt32(r["AttachmentID"]),
                                Path = r["Path"].ToString(),
                                AttachmentTypeID = Convert.ToInt32(r["AttachmentTypeID"]),
                                ThumbsPath = r["ThumbsPath"].ToString()
                            };
                        }).ToList(),
                    });

                    ComboNotification notification = new ComboNotification();
                    notification.AddNew();
                    notification.ComboUserID = tagged.ComboUserID;
                    notification.NotificationType = (int)Combo.Models.NotificationType.TAG_USER_IN_POST; // tag user to post
                    notification.NotificationDate = DateTime.UtcNow;
                    notification.NotificationBody = Newtonsoft.Json.JsonConvert.SerializeObject(postTag);
                    notification.IsRead = false;
                    notification.Save();

                    NotificationUserSettings settings = new NotificationUserSettings();
                    settings.LoadByPrimaryKey(tagged.ComboUserID, (int)Combo.Models.NotificationType.TAG_USER_IN_POST);
                    bool notify = false;
                    if (settings.RowCount == 0)
                        notify = true;
                    else
                        notify = settings.CanGetNotification(tagged.ComboUserID, commentcreator.ComboUserID, (int)Combo.Models.NotificationType.TAG_USER_IN_POST);
                    if (notify)
                    {
                        List<Models.ComboNotification> notificationJson = notification.DefaultView.Table.AsEnumerable().Select(row =>
                        {
                            return new Models.ComboNotification
                            {
                                ComboNotificationID = Convert.ToInt32(row["ComboNotificationID"]),
                                ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                                IsRead = Convert.ToBoolean(row["IsRead"]),
                                NotificationBody = row["NotificationBody"].ToString(),
                                NotificationDate = Convert.ToDateTime(row["NotificationDate"].ToString()).Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                                NotificationType = Convert.ToInt32(row["NotificationType"])
                            };
                        }).ToList();

                        SendGCMNotification(Newtonsoft.Json.JsonConvert.SerializeObject(notificationJson), tagged.DeviceID);
                    }
                    /**************************/

                }
                usertag.Save();
            }

            Models.CommentHashTag[] hashTags = js.Deserialize<Models.CommentHashTag[]>(js.Serialize(comment.HashTags));
            if (hashTags != null)
            {
                CommentHashTag commentthashtag = new CommentHashTag();

                foreach (Models.CommentHashTag item in hashTags)
                {
                    HashTag currenttag = new HashTag();

                    if (!currenttag.GetHashTagByName(item.TagName))
                    {
                        currenttag.AddNew();
                        currenttag.Name = item.TagName;
                        currenttag.Save();
                    }

                    commentthashtag.AddNew();
                    commentthashtag.HashTagID = currenttag.HashTagID;
                    commentthashtag.ComboCommentID = newComment.ComboCommentID;
                    commentthashtag.Offset = item.Offset;
                }
                commentthashtag.Save();
            }

            /**************************/
            // save notification and push it to device
            ComboPost post = new ComboPost();
            post.LoadByPrimaryKey(comment.ComboPostID);
            ComboUser creator = new ComboUser();
            ComboUser commentor = new ComboUser();
            ComboPostAttachment postatt = new ComboPostAttachment();
            postatt.GetPostAttachmentsByPostID(comment.ComboPostID);
            creator.GetUserByUserId(post.ComboUserID);
            commentor.GetUserByUserId(comment.ComboUserID);
            if (creator.ComboUserID != commentor.ComboUserID)
            {
                List<Models.ComboComment> acomment = newComment.DefaultView.Table.AsEnumerable().Select(row =>
                {
                    return new Models.ComboComment
                    {
                        ComboPostID = Convert.ToInt32(row["ComboPostID"]),
                        ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                        ComboUserName = commentor.UserName,
                        ComboDisplayName = commentor.DisplayName,
                        CommentText = newComment.CommentText,
                        CommentDate = newComment.CommentDate.Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                        ProfilePic = commentor.GetColumn("ProfilePic").ToString(),
                        PostAttachemnts = postatt.DefaultView.Table.AsEnumerable().Select(r =>
                        {
                            return new Models.Attachment
                            {
                                AttachmentID = Convert.ToInt32(r["AttachmentID"]),
                                Path = r["Path"].ToString(),
                                AttachmentTypeID = Convert.ToInt32(r["AttachmentTypeID"]),
                                ThumbsPath = r["ThumbsPath"].ToString()
                            };
                        }).ToList(),
                    };
                }).ToList();

                ComboNotification notification = new ComboNotification();
                notification.AddNew();
                notification.ComboUserID = post.ComboUserID;
                notification.NotificationType = (int)Combo.Models.NotificationType.COMMENT_ON_POST; // add comment to post
                notification.NotificationDate = DateTime.UtcNow;
                notification.NotificationBody = Newtonsoft.Json.JsonConvert.SerializeObject(acomment);
                notification.IsRead = false;
                notification.Save();

                NotificationUserSettings settings = new NotificationUserSettings();
                settings.LoadByPrimaryKey(creator.ComboUserID, (int)Combo.Models.NotificationType.COMMENT_ON_POST);
                bool notify = false;
                if (settings.RowCount == 0)
                    notify = true;
                else
                    notify = settings.CanGetNotification(creator.ComboUserID, commentor.ComboUserID, (int)Combo.Models.NotificationType.COMMENT_ON_POST);
                if (notify)
                {
                    List<Models.ComboNotification> notificationJson = notification.DefaultView.Table.AsEnumerable().Select(row =>
                    {
                        return new Models.ComboNotification
                        {
                            ComboNotificationID = Convert.ToInt32(row["ComboNotificationID"]),
                            ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                            IsRead = Convert.ToBoolean(row["IsRead"]),
                            NotificationBody = row["NotificationBody"].ToString(),
                            NotificationDate = Convert.ToDateTime(row["NotificationDate"].ToString()).Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                            NotificationType = Convert.ToInt32(row["NotificationType"])
                        };
                    }).ToList();

                    SendGCMNotification(Newtonsoft.Json.JsonConvert.SerializeObject(notificationJson), creator.DeviceID);
                }
            }
            /**************************/

            comment.ComboCommentID = newComment.ComboCommentID;
            comment.CommentDate = newComment.CommentDate.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
            _response.Entity = new Models.ComboComment[] { comment };

            SetContentResult(_response);
            return;
        }