예제 #1
0
        /// <summary>
        /// Add the answer to Database
        /// </summary>
        /// <param name="data">Answer</param>
        public static void AddAnswer(Answer data, int questionID, int userid)
        {
            using (var db = new QaAContext())
            {
                data.Date = DateTime.Now;
                data.UserId = userid;
                data.QuestionId = questionID;
                db.Answers.Add(data);
                db.SaveChanges();

                HttpContext.Current.Cache.UpdateCache("AnsweredQuestions" + userid, data);
                HttpContext.Current.Cache.UpdateCache("AnswersToQuestion"+questionID, data);
                HttpContext.Current.Cache.UpdateCache("AnswersToQuestion" + questionID+"User"+userid, data);

                //Send mails to the subscripted users
                var question = db.Questions.Where(q => q.Id == questionID).SingleOrDefault();
                IUserMailer usermailer = new UserMailer();
                var tagsOfQuestion = question.QuestionHasTags.Select(s => s.TagId).ToList();
                var tos = db.UserProfiles.Where(up => (up.Subscriptions.Any(u => tagsOfQuestion.Any(x => x == u.TagId)) && up.Email!= null && up.IsVerified==true)).ToList();

                foreach (var user in tos)
                {
                    usermailer.NewAnswer(question, user, data).SendAsync();
                }
            }
        }
예제 #2
0
 /// <summary>
 /// Answer edit
 /// </summary>
 /// <param name="data">Answer's data</param>
 public static void EditAnswer(Answer data)
 {
     using (var db = new QaAContext())
     {
         var q = from a in db.Answers where a.Id == data.Id select a;
         var ans = q.SingleOrDefault();
         ans.Content = data.Content;
         db.SaveChanges();
         HttpContext.Current.Cache.UpdateCache("AnswersToQuestion" + data.QuestionId, data);
         HttpContext.Current.Cache.UpdateCache("AnswersToQuestion" + data.QuestionId + "User" + data.UserId, data);
     }
 }
예제 #3
0
        public virtual MvcMailMessage NewAnswer(Question question, UserProfile to, Answer answer)
        {
            string subject;
            if (EmailConfig.IsReceivingEmailSetted)
            {
                var hash = UserManager.SentEmailHash(question.Id, to.UserId);
                subject = Resources.Global.NewAnswerSubject+" - Id:" + hash;
            }
            else
            {
                subject = Resources.Global.NewAnswerSubject;
            }

            ViewBag.User = to;
            ViewBag.Question = question;
            ViewBag.Answer = answer;

            return Populate(x =>
            {
                x.Subject = subject;
                x.ViewName = "NewAnswer";
                x.To.Add(to.Email);
            });
        }
        public ActionResult Parse(string mandrill_events)
        {
            //Get the email message from the mandrill
            var events = JsonConvert.DeserializeObject<IEnumerable<Mandrill.MailEvent>>(mandrill_events);
            string message=events.FirstOrDefault().Msg.Html;
            string email=events.FirstOrDefault().Msg.FromEmail;

            //Store the answer which will in the site
            string answerContent;

            if (message == null || message.Length == 0)
            {
                //Yahoo emails process
                message = events.FirstOrDefault().Msg.Text;
                if (message == null || message.Length == 0)
                {
                    UserMailer.FailedEmailAnswer(email);
                    return new HttpStatusCodeResult(HttpStatusCode.OK);
                }
                answerContent = Regex.Split(message, "-------------------------------------------").FirstOrDefault();
            }
            else
            {
                //Gmail and hotmail process
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(message);
                var firstdiv = doc.DocumentNode.SelectSingleNode("//div");
                if (firstdiv == null)
                {
                    UserMailer.FailedEmailAnswer(email);
                    return new HttpStatusCodeResult(HttpStatusCode.OK);
                }
                answerContent = firstdiv.InnerHtml;

                //Hotmail
                answerContent = Regex.Split(answerContent, "<hr id=\"stopSpelling\">").FirstOrDefault();
            }

            if (answerContent == null || answerContent.Length == 0)
            {
                UserMailer.FailedEmailAnswer(email);
                return new HttpStatusCodeResult(HttpStatusCode.OK);
            }

            //Get the question and user id
            var subject=events.FirstOrDefault().Msg.Subject;
            string hash=Regex.Split(subject, "Id:").LastOrDefault();
            if (hash == null || hash.Length == 0)
            {
                UserMailer.FailedEmailAnswer(email);
                return new HttpStatusCodeResult(HttpStatusCode.OK);
            }
            EmailIdentifier ei = UserManager.GetEmailDatasByHash(hash);
            if (ei == null)
            {
                UserMailer.FailedEmailAnswer(email);
                return new HttpStatusCodeResult(HttpStatusCode.OK);
            }

            //Check answer length
            if (answerContent.Length > 25000 || answerContent.Length < 25)
            {
                UserMailer.FailedEmailAnswer(email);
                return new HttpStatusCodeResult(HttpStatusCode.OK);
            }

            //Create the answer
            var ans = new Answer();
            ans.Content = answerContent;
            AnswerManager.AddAnswer(ans, ei.QuestionId, ei.UserId);
            return new HttpStatusCodeResult(HttpStatusCode.OK);
        }
예제 #5
0
 public ActionResult EditAnswer(Answer data, string AnsId)
 {
     data.Id = Int32.Parse(AnsId);
     data.UserId = WebSecurity.CurrentUserId;
     if (ModelState.IsValid)
     {
         AnswerManager.EditAnswer(data);
     }
     return RedirectToAction("Index");
 }