Example #1
0
        public static bool EditArticle(ref ProviderArticle anArticle, ProviderMember aMember, MessageInfo info)
        {
            _log.Info("Populating article from e-mail");
            bool returnValue = false;
            MailAddress senderAddress = new MailAddress(info.Envelope.Sender[0].Address);

            // Now parse the data from the e-mail
            EmailArticle anEmailArticle = new EmailArticle(info, _emailClient);

            if(anEmailArticle.IsHelp && !anEmailArticle.IsSubjectHelp)
            {
                // If we got the help command in the body but not in the subject then send the e-mail.
                // it's important that we check the subject help because otherwise we will send the help e-mail twice.
                _log.Info("Found the [help] command in the e-mail so respond with the help e-mail.");
                EmailManager.Instance.SendHelpEmail(senderAddress, aMember);
            }

            if (anEmailArticle.IsSubjectHelp)
            {
                // If we got help in the subject then ONLY send the help e-mail. Since the subject can't be an article title or id.
                _log.Info("This was a subject help request so send the help e-mail but do nothing else.");
                EmailManager.Instance.SendHelpEmail(senderAddress, aMember);
            }
            else
            {
                if(anEmailArticle.IsBlankEmail)
                {
                    _log.Info("E-mail was completely blank");
                }
                else if (anEmailArticle.Delete)
                {
                    if (anArticle.IsNew)
                    {
                        _log.Info("Attempted to delete a new article, which makes no sense so ignore this e-mail.");
                        MailMessage aMailMessage = new MailMessage("donotreply@insideword",
                                                                    senderAddress.Address,
                                                                    "insideword - article " + info.Envelope.Subject + " rejected",
                                                                    "DO NOT REPLY TO THIS E-MAIL<br />"
                                                                    + "The delete command you sent us in an e-mail was ignored since you tried to delete an article that doesn't exist. Be sure that you used the correct article id number in the subject.");
                        aMailMessage.IsBodyHtml = true;
                        EmailManager.Instance.DefaultSmtp.Send(aMailMessage);
                    }
                    else
                    {
                        _log.Info("Deleting article.");
                        string title = anArticle.Title;
                        long id = anArticle.Id.Value;
                        string result;
                        if (anArticle.Delete())
                        {
                            result = "Successfully deleted article " + title + " with id " + id;
                        }
                        else
                        {
                            result = "Failed to delete article " + title + " with id " + id;
                        }
                        _log.Info(result);
                        MailMessage aMailMessage = new MailMessage("donotreply@insideword",
                                                                    senderAddress.Address,
                                                                    "insideword - article " + info.Envelope.Subject + " rejected",
                                                                    "DO NOT REPLY TO THIS E-MAIL<br />"
                                                                    + result + "<br />");
                        aMailMessage.IsBodyHtml = true;
                        EmailManager.Instance.DefaultSmtp.Send(aMailMessage);
                        returnValue = false;
                    }
                }
                else
                {
                    if (anArticle.IsNew)
                    {
                        anArticle.CreateDate = DateTime.UtcNow;

                        // only update the member id when we first create the article otherwise an admin could end up taking control when editing it.
                        anArticle.MemberId = aMember.Id.Value;
                    }
                    anArticle.EditDate = DateTime.UtcNow;

                    if (anArticle.IsNew || !string.IsNullOrWhiteSpace(anEmailArticle.Title))
                    {
                        anArticle.Title = anEmailArticle.Title;
                    }

                    if (anEmailArticle.CategoryId.HasValue)
                    {
                        anArticle.AddCategory(anEmailArticle.CategoryId.Value);
                    }

                    if (anEmailArticle.Blurb != null)
                    {
                        if (string.IsNullOrWhiteSpace(anEmailArticle.Blurb))
                        {
                            anArticle.Blurb = null;
                        }
                        else
                        {
                            anArticle.Blurb = anEmailArticle.Blurb;
                        }
                    }

                    // don't overwrite unless we have some content.
                    if (!string.IsNullOrWhiteSpace(anEmailArticle.Text))
                    {
                        if (anArticle.IsNew || !anEmailArticle.Append)
                        {
                            anArticle.RawText = anEmailArticle.Text;
                        }
                        else
                        {
                            anArticle.RawText += anEmailArticle.Text;
                        }
                    }

                    if (anEmailArticle.IsPublished.HasValue)
                    {
                        // if the e-mail specifically set the publish status then set it in the article
                        anArticle.IsPublished = anEmailArticle.IsPublished.Value && anEmailArticle.CategoryId.HasValue;
                    }
                    else if (anArticle.IsNew)
                    {
                        // if the article is new but no status was set in the e-mail then set the status based on the following
                        anArticle.IsPublished = anEmailArticle.CategoryId.HasValue;
                    }
                    else
                    {
                        // don't change the status of the article
                    }
                    anArticle.Save();
                    returnValue = true;
                }

                if (anEmailArticle.IsReadArticle)
                {
                    if (anEmailArticle.ReadArticle.HasValue)
                    {
                        _log.Info("Request to send back article " + anEmailArticle.ReadArticle.Value);
                        try
                        {
                            ProviderArticle aDifferentArticle = new ProviderArticle(anEmailArticle.ReadArticle.Value);
                            if (aDifferentArticle.IsPublished || aMember.CanEdit(aDifferentArticle))
                            {
                                EmailManager.Instance.SendArticleEmail(senderAddress, anArticle, aMember);
                            }
                            else
                            {
                                throw new Exception("Member doesn't have the rights to request article " + anEmailArticle.ReadArticle.Value);
                            }
                        }
                        catch (Exception caughtException)
                        {
                            _log.Info("Failed to send back article " + anEmailArticle.ReadArticle.Value, caughtException);
                            MailMessage aMailMessage = new MailMessage("donotreply@insideword",
                                                                        senderAddress.Address,
                                                                        "insideword - article request " + anEmailArticle.ReadArticle.Value + " failed",
                                                                        "DO NOT REPLY TO THIS E-MAIL<br />"
                                                                        + "Failed to return the article with id " + anEmailArticle.ReadArticle.Value + "<br />"
                                                                        + "Check to make sure the article id is a valid id.<br />");
                            aMailMessage.IsBodyHtml = true;
                            EmailManager.Instance.DefaultSmtp.Send(aMailMessage);
                        }
                    }
                    else
                    {
                        _log.Info("Sending back current article");
                        EmailManager.Instance.SendArticleEmail(senderAddress, anArticle, aMember);
                    }
                }
            }

            return returnValue;
        }
Example #2
0
        public static bool Save(ArticleEditorVM model, ProviderArticle anArticle, ProviderCurrentMember currentMember, ref List<string> errorList)
        {
            bool returnValue = false;

            ProviderMember owningMember = GetArticleOwner(model, anArticle, currentMember);

            // IsNowClaimedArticle indicates if an article's state changed from unclaimed to now claimed
            bool IsNowClaimedArticle = anArticle.MemberId == null && owningMember.Id.HasValue;
            anArticle.MemberId = owningMember.Id;

            if (AssociatePhotos(model.ArticleBody, anArticle, ref errorList))
            {
                if (owningMember.IsActive && !currentMember.IsLoggedOn)
                {
                    // The owner is an active member but the current member is not logged in?! We're not sure if the member was lazy
                    // and didn't bother to login or if this is a malicious person, so we will set IsPublished to false and treat
                    // the article as a draft just to be safe.
                    anArticle.IsPublished = false;
                }
                else if (model.SaveState == ArticleEditorVM.SaveStates.Published)
                {
                    anArticle.IsPublished = true;
                }
                else if (model.SaveState == ArticleEditorVM.SaveStates.DraftAndContinue ||
                         model.SaveState == ArticleEditorVM.SaveStates.DraftAndPreview)
                {
                    anArticle.IsPublished = false;
                }
                else
                {
                    // defensive programming
                    anArticle.IsPublished = false;
                }

                anArticle.Title = model.Title;
                anArticle.Blurb = model.Blurb;
                anArticle.RawText = model.ArticleBody;

                if (anArticle.IsNew)
                {
                    anArticle.CreateDate = DateTime.UtcNow;
                }
                anArticle.EditDate = DateTime.UtcNow;

                // remove previous categories before adding new ones
                anArticle.RemoveAllCategories();
                anArticle.AddCategory(model.ArticleCategoryId);

                // important that we fetch this info before we save because then the article is no longer new.
                bool isNew = anArticle.IsNew;

                anArticle.Save();

                // if the current member is not logged on then save it in their workspace
                if (!currentMember.IsLoggedOn)
                {
                    currentMember.CurrentWorkSpace.ArticleIdList.Add(anArticle.Id.Value);
                }

                // Send out an e-mail for the article only if it was claimed for the first time through e-mail
                // and the member is not active
                if (IsNowClaimedArticle && !string.IsNullOrEmpty(model.ArticleEmail) && !currentMember.IsActive)
                {
                    EmailManager.Instance.SendEditArticleEmail(new MailAddress(model.ArticleEmail), anArticle, owningMember);
                }
                returnValue = true;
            }

            return returnValue;
        }