Ejemplo n.º 1
0
        public void AddMessageComment(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.ComboMsgID != 0)
                newComment.ComboMsgID = comment.ComboMsgID;
            else
            {
                _response.ErrorCode = 31;
                _response.ErrorMsg = "Can't insert comment. No Msg id .";
                _response.bool_result = false;
                SetContentResult(_response);
                return;
            }

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

            /**************************/
            // save notification and push it to device
            ComboMsg post = new ComboMsg();
            post.LoadByPrimaryKey(comment.ComboMsgID);
            ComboUser creator = new ComboUser();
            ComboUser commentor = new ComboUser();
            creator.LoadByPrimaryKey(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
                    {
                        ComboMsgID = Convert.ToInt32(row["ComboMsgID"]),
                        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()
                    };
                }).ToList();

                ComboNotification notification = new ComboNotification();
                notification.AddNew();
                notification.ComboUserID = post.ComboUserID;
                notification.NotificationType = (int)Combo.Models.NotificationType.COMMENT_ON_MESSAGE; // 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_MESSAGE);
                bool notify = false;
                if (settings.RowCount == 0)
                    notify = true;
                else
                    notify = settings.CanGetNotification(creator.ComboUserID, commentor.ComboUserID, (int)Combo.Models.NotificationType.COMMENT_ON_MESSAGE);
                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);
                }
            }
            /**************************/

            JavaScriptSerializer js = new JavaScriptSerializer();
            Models.Attachment[] att = js.Deserialize<Models.Attachment[]>(js.Serialize(comment.Attachments));

            ComboMsgAttachment attachment = new ComboMsgAttachment();
            foreach (Models.Attachment item in att)
            {
                attachment.AddNew();
                attachment.AttachmentID = item.AttachmentID;
                attachment.ComboMsgID = newComment.ComboMsgID;

            }
            attachment.Save();

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

            SetContentResult(_response);
            return;
        }
Ejemplo n.º 2
0
        public void AddMessage(Models.ComboMessage msg)
        {
            Models.ComboResponse _response = new Models.ComboResponse();
            _response.bool_result = true;
            _response.ErrorCode = 0;
            _response.ErrorMsg = "";

            if (string.IsNullOrEmpty(msg.ToIds))
            {
                _response.ErrorCode = 30;
                _response.ErrorMsg = "Can't add Msg. No to id .";
                _response.bool_result = false;
                SetContentResult(_response);
                return;
            }

            if (msg.ToIds.Split(',').Length == 0)
            {
                _response.ErrorCode = 30;
                _response.ErrorMsg = "Can't add Msg. No to idsor error in format .";
                _response.bool_result = false;
                SetContentResult(_response);
                return;
            }

            BLL.ComboMsg newMsg = new ComboMsg();
            newMsg.AddNew();

            if (msg.ComboUserID != 0)
                newMsg.ComboUserID = msg.ComboUserID;
            else
            {
                _response.ErrorCode = 30;
                _response.ErrorMsg = "Can't add Msg. No user id .";
                _response.bool_result = false;
                SetContentResult(_response);
                return;
            }

            newMsg.MsgText = msg.MsgText.Replace("\n", " "); ;
            newMsg.MsgDate = DateTime.UtcNow;
            newMsg.IsRead = false;
            newMsg.Save();

            string[] ToIds = msg.ToIds.Split(',');
            ComboUserMsg tomsg = new ComboUserMsg();
            foreach (string item in ToIds)
            {
                tomsg.AddNew();
                tomsg.ComboUserID = Convert.ToInt32(item);
                tomsg.ComboMsgID = newMsg.ComboMsgID;
            }
            tomsg.Save();

            /**************************/
            // save notification and push it to device
            ComboUser creator = new ComboUser();
            ComboUser commentor = new ComboUser();
            creator.GetUserByUserId(newMsg.ComboUserID);

            List<Models.ComboMessage> amsg = newMsg.DefaultView.Table.AsEnumerable().Select(row =>
            {
                return new Models.ComboMessage
                {
                    ComboMsgID = Convert.ToInt32(row["ComboMsgID"]),
                    ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                    ComboUserName = creator.UserName,
                    ComboUserDisplayName = creator.DisplayName,
                    MsgText = newMsg.MsgText,
                    MsgDate = newMsg.MsgDate.Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                    ProfilePic = creator.GetColumn("ProfilePic").ToString()
                };
            }).ToList();

            foreach (string item in ToIds)
            {
                ComboNotification notification = new ComboNotification();
                notification.AddNew();
                notification.ComboUserID = Convert.ToInt32(item);
                notification.NotificationType = (int)Combo.Models.NotificationType.RECEIVE_MSG; // new Msg recieved
                notification.NotificationDate = DateTime.UtcNow;
                notification.NotificationBody = Newtonsoft.Json.JsonConvert.SerializeObject(amsg);
                notification.IsRead = false;
                notification.Save();

                commentor.GetUserByUserId(Convert.ToInt32(item));

                NotificationUserSettings settings = new NotificationUserSettings();
                settings.LoadByPrimaryKey(Convert.ToInt32(item), (int)Combo.Models.NotificationType.RECEIVE_MSG);
                bool notify = false;
                if (settings.RowCount == 0)
                    notify = true;
                else
                    notify = settings.CanGetNotification(commentor.ComboUserID, creator.ComboUserID, (int)Combo.Models.NotificationType.FOLLOW_FIREND);
                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), commentor.DeviceID);
                }
            }
            /**************************/

            JavaScriptSerializer js = new JavaScriptSerializer();
            Models.Attachment[] att = js.Deserialize<Models.Attachment[]>(js.Serialize(msg.Attachments));

            ComboMsgAttachment attachment = new ComboMsgAttachment();
            foreach (Models.Attachment item in att)
            {
                attachment.AddNew();
                attachment.AttachmentID = item.AttachmentID;
                attachment.ComboMsgID = newMsg.ComboMsgID;

            }
            attachment.Save();

            msg.ComboMsgID = newMsg.ComboMsgID;
            msg.MsgDate = newMsg.MsgDate.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
            _response.Entity = new Models.ComboMessage[] { msg };

            SetContentResult(_response);
            return;
        }