public ActionResult SubmitPaymentRequest(string request)
        {
            var userId = User.Identity.GetUserId();

            if (userId == null)
            {
                return(RedirectToAction("Login", "Account", new { returnUrl = Request.Url.ToString() }));
            }

            var lndClient = new LndRpcClient(
                host: System.Configuration.ConfigurationManager.AppSettings["LnMainnetHost"],
                macaroonAdmin: System.Configuration.ConfigurationManager.AppSettings["LnMainnetMacaroonAdmin"],
                macaroonRead: System.Configuration.ConfigurationManager.AppSettings["LnMainnetMacaroonRead"],
                macaroonInvoice: System.Configuration.ConfigurationManager.AppSettings["LnMainnetMacaroonInvoice"]);

            string ip = GetClientIpAddress(Request);

            try
            {
                var paymentResult = paymentsService.TryWithdrawal(request, userId, ip, lndClient);
                return(Json(paymentResult));
            }
            catch (Exception e)
            {
                MailingService.Send(new UserEmailModel()
                {
                    Destination = "*****@*****.**",
                    Body        = " Exception: " + e.Message + "\r\n Stack: " + e.StackTrace + "\r\n invoice: " + request + "\r\n user: "******"",
                    Name        = "zapread.com Exception",
                    Subject     = "User withdraw error",
                });
                return(Json(new { Result = "Error processing request." }));
            }
        }
Esempio n. 2
0
        public async Task <JsonResult> SendMessage(int id, string content)
        {
            var userId = User.Identity.GetUserId();

            if (userId != null)
            {
                using (var db = new ZapContext())
                {
                    var sender = db.Users
                                 .Where(u => u.AppId == userId).FirstOrDefault();

                    var receiver = db.Users
                                   .Include("Messages")
                                   .Where(u => u.Id == id).FirstOrDefault();

                    if (sender == null)
                    {
                        return(Json(new { Result = "Failure" }));
                    }

                    var msg = new UserMessage()
                    {
                        Content   = content,
                        From      = sender,
                        To        = receiver,
                        IsDeleted = false,
                        IsRead    = false,
                        TimeStamp = DateTime.UtcNow,
                        Title     = "Private message from <a href='" + @Url.Action(actionName: "Index", controllerName: "User", routeValues: new { username = sender.Name }) + "'>" + sender.Name + "</a>",//" + sender.Name,
                    };

                    receiver.Messages.Add(msg);
                    await db.SaveChangesAsync();

                    // Send email
                    if (receiver.Settings == null)
                    {
                        receiver.Settings = new UserSettings();
                    }

                    if (receiver.Settings.NotifyOnPrivateMessage)
                    {
                        string mentionedEmail = UserManager.FindById(receiver.AppId).Email;
                        MailingService.Send(user: "******",
                                            message: new UserEmailModel()
                        {
                            Subject     = "New private message",
                            Body        = "From: " + sender.Name + "<br/> " + content + "<br/><br/><a href='http://www.zapread.com'>zapread.com</a>",
                            Destination = mentionedEmail,
                            Email       = "",
                            Name        = "ZapRead.com Notify"
                        });
                    }

                    return(Json(new { Result = "Success" }));
                }
            }

            return(Json(new { Result = "Failure" }));
        }
Esempio n. 3
0
        public ActionResult SendMail(UserEmailModel model)
        {
            if (!ModelState.IsValid)
            {
                //TODO: Have a proper error screen
                return(RedirectToAction("Index"));
            }

            model.Destination = "*****@*****.**";
            MailingService.Send(model);

            return(RedirectToAction("FeedbackSuccess"));
        }
Esempio n. 4
0
        public ActionResult GetSpendingSum(string days)
        {
            double amount      = 0.0;
            int    numDays     = Convert.ToInt32(days);
            double totalAmount = 0.0;
            string userId      = "?";

            try
            {
                // Get the logged in user ID
                userId = User.Identity.GetUserId();

                using (var db = new ZapContext())
                {
                    var userTxns = db.Users
                                   .Include(i => i.SpendingEvents)
                                   .Where(u => u.AppId == userId)
                                   .SelectMany(u => u.SpendingEvents);

                    // need to ensure that tx.Amount is not null
                    var sum = userTxns
                              .Where(tx => DbFunctions.DiffDays(tx.TimeStamp, DateTime.Now) <= numDays) // Filter for time
                              .Sum(tx => (double?)tx.Amount) ?? 0;

                    totalAmount = userTxns
                                  .Sum(tx => (double?)tx.Amount) ?? 0;

                    amount = sum;
                }
            }
            catch (Exception e)
            {
                MailingService.Send(new UserEmailModel()
                {
                    Destination = "*****@*****.**",
                    Body        = " Exception: " + e.Message + "\r\n Stack: " + e.StackTrace + "\r\n method: GetSpendingSum" + "\r\n user: "******"",
                    Name        = "zapread.com Exception",
                    Subject     = "Account Controller error",
                });

                // If we have an exception, it is possible a user is trying to abuse the system.  Return 0 to be uninformative.
                amount = 0.0;
            }

            string value = amount.ToString("0.##");
            string total = totalAmount.ToString("0.##");

            return(Json(new { value, total }, JsonRequestBehavior.AllowGet));
        }
Esempio n. 5
0
        public ActionResult SendFeedback(string msg, string loc)
        {
            String uid = "";

            uid = User.Identity.GetUserId();

            UserEmailModel message = new UserEmailModel();

            message.Email       = "";
            message.Name        = "ZapRead Feedback";
            message.Subject     = "ZapRead Feedback";
            message.Body        = msg + Environment.NewLine + " Location: " + loc + Environment.NewLine + Environment.NewLine + " User: "******"*****@*****.**";
            MailingService.Send(message);

            return(Json(new { result = "success" }));
        }
Esempio n. 6
0
        /* Monetary aspects */

        public async Task <JsonResult> UserBalance()
        {
            string userId = "?";

            try
            {
                userId = User.Identity.GetUserId();

                if (userId == null)
                {
                    return(Json(new { balance = 0 }));
                }

                using (var db = new ZapContext())
                {
                    await EnsureUserExists(userId, db);

                    var user = db.Users
                               .Include(usr => usr.Funds)
                               .FirstOrDefault(u => u.AppId == userId);

                    return(Json(new { balance = Math.Floor(user.Funds.Balance) }));
                }
            }
            catch (Exception e)
            {
                MailingService.Send(new UserEmailModel()
                {
                    Destination = "*****@*****.**",
                    Body        = " Exception: " + e.Message + "\r\n Stack: " + e.StackTrace + "\r\n method: UserBalance" + "\r\n user: "******"",
                    Name        = "zapread.com Exception",
                    Subject     = "Account Controller error",
                });

                return(Json(new { balance = 0 }));
            }
        }
Esempio n. 7
0
        public async Task <ActionResult> TipUser(int id, int?amount, int?tx)
        {
            using (var db = new ZapContext())
            {
                var receiver = await db.Users
                               .Include(usr => usr.Funds)
                               .Include(usr => usr.EarningEvents)
                               .Include(usr => usr.Settings)
                               .Where(u => u.Id == id).FirstOrDefaultAsync();

                if (receiver == null)
                {
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(Json(new { Result = "Failure", Message = "User not found." }));
                }

                if (tx == null)
                {
                    var userId = User.Identity.GetUserId();
                    if (userId == null)
                    {
                        return(Json(new { Result = "Failure", Message = "User not found." }));
                    }

                    var user = await db.Users
                               .Include(usr => usr.Funds)
                               .Include(usr => usr.SpendingEvents)
                               .Where(u => u.AppId == userId).FirstOrDefaultAsync();

                    // Ensure user has the funds available.
                    if (user.Funds.Balance < amount)
                    {
                        Response.StatusCode = (int)HttpStatusCode.Forbidden;
                        return(Json(new { Result = "Failure", Message = "Not enough funds." }));
                    }

                    // All requirements are met - make payment
                    user.Funds.Balance     -= amount.Value;
                    receiver.Funds.Balance += amount.Value;

                    // Add Earning Event

                    var ea = new EarningEvent()
                    {
                        Amount     = amount.Value,
                        OriginType = 2,
                        TimeStamp  = DateTime.UtcNow,
                        Type       = 0,
                    };

                    receiver.EarningEvents.Add(ea);

                    var spendingEvent = new SpendingEvent()
                    {
                        Amount    = amount.Value,
                        TimeStamp = DateTime.UtcNow,
                    };

                    user.SpendingEvents.Add(spendingEvent);

                    // Notify receiver
                    var alert = new UserAlert()
                    {
                        TimeStamp = DateTime.Now,
                        Title     = "You received a tip!",
                        Content   = "From: <a href='" + @Url.Action(actionName: "Index", controllerName: "User", routeValues: new { username = user.Name }) + "'>" + user.Name + "</a><br/> Amount: " + amount.ToString() + " Satoshi.",
                        IsDeleted = false,
                        IsRead    = false,
                        To        = receiver,
                    };

                    receiver.Alerts.Add(alert);
                    await db.SaveChangesAsync();

                    try
                    {
                        if (receiver.Settings == null)
                        {
                            receiver.Settings = new UserSettings();
                        }

                        if (receiver.Settings.NotifyOnReceivedTip)
                        {
                            string receiverEmail = UserManager.FindById(receiver.AppId).Email;
                            MailingService.Send(user: "******",
                                                message: new UserEmailModel()
                            {
                                Subject     = "You received a tip!",
                                Body        = "From: " + user.Name + "<br/> Amount: " + amount.ToString() + " Satoshi.<br/><br/><a href='http://www.zapread.com'>zapread.com</a>",
                                Destination = receiverEmail,
                                Email       = "",
                                Name        = "ZapRead.com Notify"
                            });
                        }
                    }
                    catch (Exception e)
                    {
                        // Send an email.
                        MailingService.Send(new UserEmailModel()
                        {
                            Destination = "*****@*****.**",
                            Body        = " Exception: " + e.Message + "\r\n Stack: " + e.StackTrace + "\r\n user: "******"",
                            Name        = "zapread.com Exception",
                            Subject     = "Send NotifyOnReceivedTip error.",
                        });
                    }

                    return(Json(new { Result = "Success" }));
                }
                else
                {
                    // Anonymous tip

                    var vtx = await db.LightningTransactions.FirstOrDefaultAsync(txn => txn.Id == tx);

                    if (vtx == null || vtx.IsSpent == true)
                    {
                        Response.StatusCode = (int)HttpStatusCode.Forbidden;
                        return(Json(new { Result = "Failure", Message = "Transaction not found" }));
                    }

                    vtx.IsSpent = true;

                    receiver.Funds.Balance += amount.Value;// vtx.Amount;//amount;

                    // Notify receiver
                    var alert = new UserAlert()
                    {
                        TimeStamp = DateTime.Now,
                        Title     = "You received a tip!",
                        Content   = "From: anonymous <br/> Amount: " + amount.ToString() + " Satoshi.",
                        IsDeleted = false,
                        IsRead    = false,
                        To        = receiver,
                    };

                    receiver.Alerts.Add(alert);
                    await db.SaveChangesAsync();

                    if (receiver.Settings == null)
                    {
                        receiver.Settings = new UserSettings();
                    }

                    if (receiver.Settings.NotifyOnReceivedTip)
                    {
                        string receiverEmail = UserManager.FindById(receiver.AppId).Email;
                        MailingService.Send(user: "******",
                                            message: new UserEmailModel()
                        {
                            Subject     = "You received a tip!",
                            Body        = "From: anonymous <br/> Amount: " + amount.ToString() + " Satoshi.<br/><br/><a href='http://www.zapread.com'>zapread.com</a>",
                            Destination = receiverEmail,
                            Email       = "",
                            Name        = "ZapRead.com Notify"
                        });
                    }
                    return(Json(new { Result = "Success" }));
                }
            }
        }
Esempio n. 8
0
        public ActionResult SubmitPaymentRequest(string withdrawId)//string request)
        {
            var userAppId = User.Identity.GetUserId();

            if (userAppId == null)
            {
                Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return(Json(new { success = false, message = "User not authorized." }));
            }

            using (var db = new ZapContext())
            {
                var wguid = new Guid(withdrawId);
                var lntx  = db.LightningTransactions
                            .Where(tx => tx.WithdrawId == wguid)
                            .Where(tx => tx.User.AppId == userAppId)
                            .AsNoTracking()
                            .FirstOrDefault();

                if (lntx == null)
                {
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(Json(new { success = false, message = "Invalid withdraw request." }));
                }

                if (lntx.IsSettled || lntx.IsIgnored || lntx.IsLimbo || lntx.IsDeposit || lntx.IsError)
                {
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(Json(new { success = false, message = "Invalid withdraw request." }));
                }

                // Verify lntx invoice is unique.  This is a layer to protect against spam attacks
                var numTxsWithSamePayreq = db.LightningTransactions
                                           .Where(tx => tx.PaymentRequest == lntx.PaymentRequest)
                                           .Count();

                if (numTxsWithSamePayreq > 1)
                {
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(Json(new { success = false, message = "Invalid withdraw request." }));
                }

                // Get interface to LND
                LndRpcClient lndClient = GetLndClient();

                // This is used for DoS or other attack detection
                string ip = GetClientIpAddress(Request);

                try
                {
                    // Submit Payment Request
                    var paymentResult = PaymentsService.TryWithdrawal(lntx, userAppId, ip, lndClient);
                    return(Json(paymentResult));
                }
                catch (RestException e)
                {
                    // The request to LND threw an exception
                    MailingService.Send(new UserEmailModel()
                    {
                        Destination = System.Configuration.ConfigurationManager.AppSettings["ExceptionReportEmail"],
                        Body        = " Exception: " + e.Message + "\r\n Stack: " + e.StackTrace + "\r\n invoice: " + lntx.PaymentRequest
                                      + "\r\n user: "******"\r\n error Content: " + e.Content
                                      + "\r\n HTTP Status: " + e.StatusDescription,
                        Email   = "",
                        Name    = "zapread.com Exception",
                        Subject = "User withdraw error 1",
                    });
                    return(Json(new { Result = "Error processing request." }));
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    MailingService.Send(new UserEmailModel()
                    {
                        Destination = System.Configuration.ConfigurationManager.AppSettings["ExceptionReportEmail"],
                        Body        = " Exception: " + e.Message + "\r\n Stack: " + e.StackTrace + "\r\n invoice: " + lntx.PaymentRequest + "\r\n user: "******"",
                        Name        = "zapread.com Exception",
                        Subject     = "User withdraw error 1b",
                    });
                    return(Json(new { Result = "Error processing request." }));
                }
            }
        }
Esempio n. 9
0
        public async Task <JsonResult> SubmitNewPost(NewPostMsg p)
        {
            var userId = User.Identity.GetUserId();

            using (var db = new ZapContext())
            {
                var user = db.Users.Where(u => u.AppId == userId).First();

                // Cleanup post HTML
                HtmlDocument postDocument = new HtmlDocument();
                postDocument.LoadHtml(p.Content);
                var postImages = postDocument.DocumentNode.SelectNodes("//img/@src");
                if (postImages != null)
                {
                    foreach (var item in postImages)
                    {
                        // ensure images have the img-fluid class
                        if (!item.HasClass("img-fluid"))
                        {
                            item.AddClass("img-fluid");
                        }
                    }
                }
                string contentStr = postDocument.DocumentNode.OuterHtml;

                var postGroup = db.Groups.FirstOrDefault(g => g.GroupId == p.GroupId);

                Post post = null;

                if (p.PostId > 0)
                {
                    // Updated post
                    post = db.Posts.Where(pst => pst.PostId == p.PostId).FirstOrDefault();

                    post.PostTitle = p.Title;
                    post.Group     = postGroup;
                    post.Content   = contentStr;
                    if (post.IsDraft) // Post was or is draft - set timestamp.
                    {
                        post.TimeStamp = DateTime.UtcNow;
                    }
                    else // Post has been published, don't update timestamp, update edit timestamp.
                    {
                        post.TimeStampEdited = DateTime.UtcNow;
                    }
                    post.IsDraft = p.IsDraft;
                    await db.SaveChangesAsync();

                    return(Json(new { result = "success", postId = post.PostId }));
                }
                else
                {
                    // New post
                    post = new Post()
                    {
                        Content     = contentStr,
                        UserId      = user,
                        TotalEarned = 0,
                        IsDeleted   = false,
                        Score       = 1,
                        Group       = postGroup,
                        TimeStamp   = DateTime.UtcNow,
                        VotesUp     = new List <User>()
                        {
                            user
                        },
                        PostTitle = p.Title,
                        IsDraft   = p.IsDraft,
                    };

                    db.Posts.Add(post);
                    await db.SaveChangesAsync();
                }

                if (p.IsDraft)
                {
                    // Don't send any alerts
                    return(Json(new { result = "success", postId = post.PostId }));
                }

                // Send alerts to users subscribed to group

                var subusers = db.Users
                               .Include("Alerts")
                               .Where(u => u.Groups.Select(g => g.GroupId).Contains(postGroup.GroupId));

                foreach (var u in subusers)
                {
                    // Add Alert
                    var alert = new UserAlert()
                    {
                        TimeStamp = DateTime.Now,
                        Title     = "New post in subscribed group <a href='" + Url.Action(actionName: "GroupDetail", controllerName: "Group", routeValues: new { id = postGroup.GroupId }) + "'>" + postGroup.GroupName + "</a>",
                        Content   = "",// "<a href='" + Url.Action(actionName:"Detail", controllerName: "Post", routeValues: new { post.PostId }) + "'>" + (post.PostTitle != null ? post.PostTitle : "Post") + "</a>",
                        IsDeleted = false,
                        IsRead    = false,
                        To        = u,
                        PostLink  = post,
                    };

                    u.Alerts.Add(alert);
                }

                // Send alerts to users subscribed to users

                var followUsers = db.Users
                                  .Include("Alerts")
                                  .Include("Settings")
                                  .Where(u => u.Following.Select(usr => usr.Id).Contains(user.Id));

                foreach (var u in followUsers)
                {
                    // Add Alert
                    var alert = new UserAlert()
                    {
                        TimeStamp = DateTime.Now,
                        Title     = "New post by user you are following: <a href='" + @Url.Action(actionName: "Index", controllerName: "User", routeValues: new { username = user.Name }) + "'>" + user.Name + "</a>",
                        Content   = "",//post.PostTitle,
                        IsDeleted = false,
                        IsRead    = false,
                        To        = u,
                        PostLink  = post,
                    };

                    u.Alerts.Add(alert);

                    if (u.Settings == null)
                    {
                        u.Settings = new UserSettings();
                    }

                    if (u.Settings.NotifyOnNewPostSubscribedUser)
                    {
                        // send email
                        var doc = new HtmlDocument();
                        doc.LoadHtml(post.Content);
                        var baseUri = new Uri("https://www.zapread.com/");
                        var imgs    = doc.DocumentNode.SelectNodes("//img/@src");
                        if (imgs != null)
                        {
                            foreach (var item in imgs)
                            {
                                // TODO: check if external url
                                item.SetAttributeValue("src", new Uri(baseUri, item.GetAttributeValue("src", "")).AbsoluteUri);
                            }
                        }
                        string postContent = doc.DocumentNode.OuterHtml;

                        string followerEmail = UserManager.FindById(u.AppId).Email;
                        MailingService.Send(user: "******",
                                            message: new UserEmailModel()
                        {
                            Subject     = "New post by user you are following: " + user.Name,
                            Body        = "<a href='http://www.zapread.com/Post/Detail/" + post.PostId.ToString() + "'>" + post.PostTitle + "</a><br/><br/>" + postContent + "<br/><br/>" + "<a href='http://www.zapread.com'>zapread.com</a><br/><br/>Log in and go to your user settings to unsubscribe from these emails.", //, // "From: " + user.Name + "<br/> " + comment.Text + "<br/><br/>Go to <a href='http://www.zapread.com/Post/Detail/" + post.PostId.ToString() + "'>post</a> at <a href='http://www.zapread.com'>zapread.com</a>",
                            Destination = followerEmail,
                            Email       = "",
                            Name        = "ZapRead.com Notify"
                        });
                    }
                }

                await db.SaveChangesAsync();

                return(Json(new { result = "success", postId = post.PostId }));
            }
        }
Esempio n. 10
0
        public async Task <ActionResult> AddComment(NewComment c)
        {
            // Check for empty comment

            if (c.CommentContent.Replace(" ", "") == "<p><br></p>")
            {
                return(Json(new
                {
                    success = false,
                    message = "Error: Empty comment.",
                    c.PostId,
                    c.IsReply,
                    c.CommentId,
                }));
            }

            var userId = User.Identity.GetUserId();

            using (var db = new ZapContext())
            {
                await EnsureUserExists(userId, db);

                var user = db.Users
                           .Include(usr => usr.Settings)
                           .Where(u => u.AppId == userId).First();

                var post = db.Posts
                           .Include(pst => pst.UserId)
                           .Include(pst => pst.UserId.Settings)
                           .FirstOrDefault(p => p.PostId == c.PostId);

                if (post == null)
                {
                    return(this.Json(new
                    {
                        HTMLString = "",
                        c.PostId,
                        Success = false,
                        c.CommentId
                    }));
                }
                Comment parent = null;
                if (c.IsReply)
                {
                    parent = db.Comments.Include(cmt => cmt.Post).FirstOrDefault(cmt => cmt.CommentId == c.CommentId);
                }

                Comment comment = new Comment()
                {
                    //CommentId = 1,
                    Parent      = parent,
                    IsReply     = c.IsReply,
                    UserId      = user,
                    Text        = c.CommentContent,
                    TimeStamp   = DateTime.UtcNow,
                    Post        = post,
                    Score       = 0,
                    TotalEarned = 0.0,
                    VotesUp     = new List <User>(),
                    VotesDown   = new List <User>(),
                };

                var postOwner = post.UserId;
                if (postOwner.Settings == null)
                {
                    postOwner.Settings = new UserSettings();
                }

                User commentOwner = null;

                if (c.IsReply)
                {
                    commentOwner = db.Comments.FirstOrDefault(cmt => cmt.CommentId == c.CommentId).UserId;
                    if (commentOwner.Settings == null)
                    {
                        commentOwner.Settings = new UserSettings();
                    }
                }

                db.Comments.Add(comment);
                if (!c.IsReply)
                {
                    post.Comments.Add(comment);
                }
                await db.SaveChangesAsync();

                // Find user mentions

                var doc = new HtmlDocument();
                doc.LoadHtml(c.CommentContent);
                var spans = doc.DocumentNode.SelectNodes("//span");

                try
                {
                    if (spans != null)
                    {
                        foreach (var s in spans)
                        {
                            if (s.Attributes.Count(a => a.Name == "class") > 0)
                            {
                                var cls = s.Attributes.FirstOrDefault(a => a.Name == "class");
                                if (cls.Value.Contains("userhint"))
                                {
                                    var username      = s.InnerHtml.Replace("@", "");
                                    var mentioneduser = db.Users
                                                        .Include(usr => usr.Settings)
                                                        .FirstOrDefault(u => u.Name == username);

                                    if (mentioneduser != null)
                                    {
                                        // Add Message

                                        var message = new UserMessage()
                                        {
                                            TimeStamp   = DateTime.Now,
                                            Title       = "You were mentioned in a comment by <a href='" + @Url.Action(actionName: "Index", controllerName: "User", routeValues: new { username = user.Name }) + "'>" + user.Name + "</a>",
                                            Content     = c.CommentContent,
                                            CommentLink = comment,
                                            IsDeleted   = false,
                                            IsRead      = false,
                                            To          = mentioneduser,
                                            PostLink    = post,
                                            From        = user,
                                        };

                                        mentioneduser.Messages.Add(message);

                                        await db.SaveChangesAsync();

                                        // Send Email
                                        if (mentioneduser.Settings == null)
                                        {
                                            mentioneduser.Settings = new UserSettings();
                                        }

                                        if (mentioneduser.Settings.NotifyOnMentioned)
                                        {
                                            var cdoc = new HtmlDocument();
                                            cdoc.LoadHtml(c.CommentContent);
                                            var baseUri = new Uri("https://www.zapread.com/");
                                            var imgs    = cdoc.DocumentNode.SelectNodes("//img/@src");
                                            if (imgs != null)
                                            {
                                                foreach (var item in imgs)
                                                {
                                                    item.SetAttributeValue("src", new Uri(baseUri, item.GetAttributeValue("src", "")).AbsoluteUri);
                                                }
                                            }
                                            string commentContent = cdoc.DocumentNode.OuterHtml;

                                            string mentionedEmail = UserManager.FindById(mentioneduser.AppId).Email;
                                            MailingService.Send(user: "******",
                                                                message: new UserEmailModel()
                                            {
                                                Subject     = "New mention in comment",
                                                Body        = "From: " + user.Name + "<br/> " + commentContent + "<br/><br/>Go to <a href='http://www.zapread.com/Post/Detail/" + post.PostId.ToString() + "'>post</a> at <a href='http://www.zapread.com'>zapread.com</a>",
                                                Destination = mentionedEmail,
                                                Email       = "",
                                                Name        = "ZapRead.com Notify"
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    MailingService.Send(new UserEmailModel()
                    {
                        Destination = "*****@*****.**",
                        Body        = " Exception: " + e.Message + "\r\n Stack: " + e.StackTrace + "\r\n comment: " + c.CommentContent + "\r\n user: "******"",
                        Name        = "zapread.com Exception",
                        Subject     = "User comment error",
                    });
                }

                // Only send messages if not own post.
                if (!c.IsReply && (postOwner.AppId != user.AppId))
                {
                    // Add Alert
                    if (postOwner.Settings == null)
                    {
                        postOwner.Settings = new UserSettings();
                    }

                    if (postOwner.Settings.AlertOnOwnPostCommented)
                    {
                        var alert = new UserAlert()
                        {
                            TimeStamp   = DateTime.Now,
                            Title       = "New comment on your post: <a href=" + @Url.Action("Detail", "Post", new { id = post.PostId }) + ">" + post.PostTitle + "</a>",
                            Content     = "From: <a href='" + @Url.Action(actionName: "Index", controllerName: "User", routeValues: new { username = user.Name }) + "'>" + user.Name + "</a>",//< br/> " + c.CommentContent,
                            CommentLink = comment,
                            IsDeleted   = false,
                            IsRead      = false,
                            To          = postOwner,
                            PostLink    = post,
                        };
                        postOwner.Alerts.Add(alert);
                        await db.SaveChangesAsync();
                    }

                    // Send Email
                    if (postOwner.Settings.NotifyOnOwnPostCommented)
                    {
                        var cdoc = new HtmlDocument();
                        cdoc.LoadHtml(comment.Text);
                        var baseUri = new Uri("https://www.zapread.com/");
                        var imgs    = cdoc.DocumentNode.SelectNodes("//img/@src");
                        if (imgs != null)
                        {
                            foreach (var item in imgs)
                            {
                                item.SetAttributeValue("src", new Uri(baseUri, item.GetAttributeValue("src", "")).AbsoluteUri);
                            }
                        }
                        string commentContent = cdoc.DocumentNode.OuterHtml;

                        string ownerEmail = UserManager.FindById(postOwner.AppId).Email;
                        MailingService.Send(user: "******",
                                            message: new UserEmailModel()
                        {
                            Subject     = "New comment on your post: " + post.PostTitle,
                            Body        = "From: " + user.Name + "<br/> " + commentContent + "<br/><br/>Go to <a href='http://www.zapread.com/Post/Detail/" + post.PostId.ToString() + "'>post</a> at <a href='http://www.zapread.com'>zapread.com</a>",
                            Destination = ownerEmail,
                            Email       = "",
                            Name        = "ZapRead.com Notify"
                        });
                    }
                }

                if (c.IsReply && commentOwner.AppId != user.AppId)
                {
                    var message = new UserMessage()
                    {
                        TimeStamp   = DateTime.Now,
                        Title       = "New reply to your comment in post: <a href='" + Url.Action(actionName: "Detail", controllerName: "Post", routeValues: new { post.PostId }) + "'>" + (post.PostTitle != null ? post.PostTitle : "Post") + "</a>",
                        Content     = "From: <a href='" + @Url.Action(actionName: "Index", controllerName: "User", routeValues: new { username = user.Name }) + "'>" + user.Name + "</a>" + "<br/> " + c.CommentContent,
                        CommentLink = comment,
                        IsDeleted   = false,
                        IsRead      = false,
                        To          = commentOwner,
                        PostLink    = post,
                        From        = user,
                    };

                    commentOwner.Messages.Add(message);
                    await db.SaveChangesAsync();

                    if (commentOwner.Settings == null)
                    {
                        commentOwner.Settings = new UserSettings();
                    }

                    if (commentOwner.Settings.NotifyOnOwnCommentReplied)
                    {
                        var cdoc = new HtmlDocument();
                        cdoc.LoadHtml(comment.Text);
                        var baseUri = new Uri("https://www.zapread.com/");
                        var imgs    = cdoc.DocumentNode.SelectNodes("//img/@src");
                        if (imgs != null)
                        {
                            foreach (var item in imgs)
                            {
                                item.SetAttributeValue("src", new Uri(baseUri, item.GetAttributeValue("src", "")).AbsoluteUri);
                            }
                        }
                        string commentContent = cdoc.DocumentNode.OuterHtml;

                        string ownerEmail = UserManager.FindById(commentOwner.AppId).Email;
                        MailingService.Send(user: "******",
                                            message: new UserEmailModel()
                        {
                            Subject = "New reply to your comment in post: " + post.PostTitle,
                            Body    = "From: <a href='http://www.zapread.com/user/" + user.Name.ToString() + "'>" + user.Name + "</a>"
                                      + "<br/> " + commentContent
                                      + "<br/><br/>Go to <a href='http://www.zapread.com/Post/Detail/" + post.PostId.ToString() + "'>" + (post.PostTitle != null ? post.PostTitle : "Post") + "</a> at <a href='http://www.zapread.com'>zapread.com</a>",
                            Destination = ownerEmail,
                            Email       = "",
                            Name        = "ZapRead.com Notify"
                        });
                    }
                }

                string CommentHTMLString = RenderPartialViewToString("_PartialCommentRender", new PostCommentsViewModel()
                {
                    Comment = comment, Comments = new List <Comment>()
                });

                return(this.Json(new
                {
                    HTMLString = CommentHTMLString,
                    c.PostId,
                    success = true,
                    IsReply = c.IsReply,
                    c.CommentId,
                }));
            }
        }