예제 #1
0
        public virtual StoryCreateResult Create(IUser byUser, string url, string title, string category, string description, string tags, string userIPAddress, string userAgent, string urlReferer, NameValueCollection serverVariables, Func <IStory, string> buildDetailUrl)
        {
            StoryCreateResult result = ValidateCreate(byUser, url, title, category, description, userIPAddress, userAgent);

            if (result == null)
            {
                if (_contentService.IsRestricted(url))
                {
                    result = new StoryCreateResult {
                        ErrorMessage = "Podany url jest zablokowany."
                    };
                }
            }

            if (result == null)
            {
                using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
                {
                    IStory alreadyExists = _storyRepository.FindByUrl(url);

                    if (alreadyExists != null)
                    {
                        return(new StoryCreateResult {
                            ErrorMessage = "Artykuł z danym url już istnieje.", DetailUrl = buildDetailUrl(alreadyExists)
                        });
                    }

                    ICategory storyCategory = _categoryRepository.FindByUniqueName(category);

                    if (storyCategory == null)
                    {
                        return(new StoryCreateResult {
                            ErrorMessage = "\"{0}\" nie istnieje.".FormatWith(category)
                        });
                    }
                    if (storyCategory.IsActive == false)
                    {
                        return(new StoryCreateResult
                        {
                            ErrorMessage = "\"{0}\" jest tylko do odczytu.".FormatWith(category)
                        });
                    }

                    StoryContent content = _contentService.Get(url);

                    if (content == StoryContent.Empty)
                    {
                        return(new StoryCreateResult {
                            ErrorMessage = "Podany Url wydaje się być wadliwy."
                        });
                    }

                    var splittedTags = tags.NullSafe().Split(',');
                    if (splittedTags.Length == 1) //only one tag
                    {
                        var tag = splittedTags[0].Trim();
                        if (String.Compare(".net", tag, StringComparison.OrdinalIgnoreCase) == 0 || String.Compare("c#", tag, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            return(new StoryCreateResult {
                                ErrorMessage = "Proszę, pomyśl chwilę nad lepszymi tagami."
                            });
                        }
                    }
                    if (splittedTags.Length == 2) //two tags, maybe not .net and C#
                    {
                        var tag1 = splittedTags[0].Trim();
                        var tag2 = splittedTags[1].Trim();
                        if (
                            (String.Compare(".net", tag1, StringComparison.OrdinalIgnoreCase) == 0 && String.Compare("c#", tag2, StringComparison.OrdinalIgnoreCase) == 0) ||
                            (String.Compare(".net", tag1, StringComparison.OrdinalIgnoreCase) == 0 && String.Compare("c#", tag2, StringComparison.OrdinalIgnoreCase) == 0)
                            )
                        {
                            return(new StoryCreateResult {
                                ErrorMessage = "Tagi: .Net i C#. Srsly?"
                            });
                        }
                    }

                    description = _htmlSanitizer.Sanitize(description);

                    if (!_settings.AllowPossibleSpamStorySubmit && ShouldCheckSpamForUser(byUser))
                    {
                        result = EnsureNotSpam <StoryCreateResult>(byUser, userIPAddress, userAgent, url, urlReferer, description, "social news", serverVariables, "Artykuł odrzucony: {0}, {1}".FormatWith(url, byUser), "Twój artykuł wydaje się być spamem.");

                        if (result != null)
                        {
                            return(result);
                        }
                    }

                    // If we are here which means story is not spam
                    IStory story = _factory.CreateStory(storyCategory, byUser, userIPAddress, title.StripHtml(), description, url);

                    _storyRepository.Add(story);

                    // The Initial vote;
                    story.Promote(story.CreatedAt, byUser, userIPAddress);

                    // Capture the thumbnail, might speed up the thumbnail generation process
                    _thumbnail.Capture(story.Url);

                    // Subscribe comments by default
                    story.SubscribeComment(byUser);

                    AddTagsToContainers(tags, new ITagContainer[] { story, byUser });

                    string detailUrl = buildDetailUrl(story);

                    if (_settings.AllowPossibleSpamStorySubmit && _settings.SendMailWhenPossibleSpamStorySubmitted && ShouldCheckSpamForUser(byUser))
                    {
                        unitOfWork.Commit();
                        _spamProtection.IsSpam(CreateSpamCheckContent(byUser, userIPAddress, userAgent, url, urlReferer, description, "social news", serverVariables), (source, isSpam) => _spamPostprocessor.Process(source, isSpam, detailUrl, story));
                    }
                    else
                    {
                        story.Approve(SystemTime.Now());
                        _eventAggregator.GetEvent <StorySubmitEvent>().Publish(new StorySubmitEventArgs(story, detailUrl));
                        unitOfWork.Commit();
                    }

                    result = new StoryCreateResult {
                        NewStory = story, DetailUrl = detailUrl
                    };
                }
            }

            return(result);
        }
예제 #2
0
        private ShortUrlResult Create(string url, string aliasName, string ipAddress, string userName, string apiKey, bool useUserName)
        {
            if (!string.IsNullOrWhiteSpace(url))
            {
                // add the protocol if missing
                if (!url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &&
                    !url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
                {
                    url = "http://" + url;
                }
            }

            ShortUrlResult result = Validation.Validate <ShortUrlResult>(() => string.IsNullOrWhiteSpace(url), "url", TextMessages.UrlCannotBeBlank)
                                    .Or(() => !url.IsWebUrl(), "url", TextMessages.UrlIsNotInValidFormat)
                                    .Or(() => url.StartsWith(urlResolver.ApplicationRoot, StringComparison.OrdinalIgnoreCase), "url", TextMessages.CannotShrinkUrlForItsOwnDomain)
                                    .Or(() => bannedDomainRepository.IsMatching(url), "url", TextMessages.CannotShrinkUrlWhichMatchesWithOurBannedDomains)
                                    .And(() => !string.IsNullOrWhiteSpace(aliasName) && !baseX.IsValid(aliasName), "alias", TextMessages.AliasIsNotValidAliasCanOnlyContainAlphanumericCharacters)
                                    .Or(() => !string.IsNullOrWhiteSpace(aliasName) && reservedAliasRepository.IsMatching(aliasName), "alias", TextMessages.SpecifiedAliasMatchesWithOurReservedAlias.FormatWith(aliasName))
                                    .And(() => string.IsNullOrWhiteSpace(ipAddress), "ipAddress", TextMessages.IpAddressCannotBeBlank)
                                    .Or(() => !ipAddress.IsIPAddress(), "ipAddress", TextMessages.IpAddressIsNotInValidFormat)
                                    .Result();

            if (result.RuleViolations.IsEmpty())
            {
                User user;

                result = useUserName ?
                         ValidateUserName <ShortUrlResult>(userName, out user) :
                         ValidateApiKey <ShortUrlResult>(apiKey, out user);

                if (result.RuleViolations.IsEmpty())
                {
                    string   hash     = url.ToLower(Culture.Invariant).Hash();
                    ShortUrl shortUrl = shortUrlRepository.GetByHash(hash);

                    if (shortUrl == null)
                    {
                        // Url does not exist, create it, if url is not valid then we
                        // will have the error in the rule violations.
                        ExternalContent externalContent = GetContent(url, out result);

                        if (result.RuleViolations.IsEmpty())
                        {
                            shortUrl = new ShortUrl {
                                Title = externalContent.Title, Url = url, Domain = new Uri(url, UriKind.Absolute).Host.ToLower(Culture.Current), Hash = hash
                            };

                            thumbnail.Capture(url);

                            shortUrlRepository.Add(shortUrl);
                        }
                    }

                    if (result.RuleViolations.IsEmpty() && (shortUrl != null))
                    {
                        if (string.IsNullOrWhiteSpace(aliasName))
                        {
                            if (shortUrl.Id == 0)
                            {
                                result = CreateWithoutAlias(shortUrl, ipAddress, user, !useUserName);
                            }
                            else
                            {
                                if (user == null)
                                {
                                    // Check for existing alias which does not have an associated user;
                                    Alias alias = shortUrl.Aliases.FirstOrDefault(a => a.User == null);

                                    result = alias != null ?
                                             new ShortUrlResult(CreateShortUrlDTO(alias)) :
                                             CreateWithoutAlias(shortUrl, ipAddress, user, !useUserName);
                                }
                                else
                                {
                                    Alias alias = shortUrl.Aliases.FirstOrDefault(a => (a.User != null) && (a.User.Id == user.Id));

                                    result = alias != null?
                                             CreateWithAlias(shortUrl, alias.Name, ipAddress, user, !useUserName) :
                                                 CreateWithoutAlias(shortUrl, ipAddress, user, !useUserName);
                                }
                            }
                        }
                        else
                        {
                            result = CreateWithAlias(shortUrl, aliasName, ipAddress, user, !useUserName);
                        }

                        if (result.RuleViolations.IsEmpty())
                        {
                            unitOfWork.Commit();
                        }
                    }
                }
            }

            return(result);
        }
예제 #3
0
        public virtual StoryCreateResult Create(IUser byUser, string url, string title, string category, string description, string tags, string userIPAddress, string userAgent, string urlReferer, NameValueCollection serverVariables, Func <IStory, string> buildDetailUrl)
        {
            StoryCreateResult result = ValidateCreate(byUser, url, title, category, description, userIPAddress, userAgent);

            if (result == null)
            {
                using (IUnitOfWork unitOfWork = UnitOfWork.Get())
                {
                    IStory alreadyExists = _storyRepository.FindByUrl(url);

                    if (alreadyExists != null)
                    {
                        return(new StoryCreateResult {
                            ErrorMessage = "Wpis z takim samym url ju¿ istnieje.", DetailUrl = buildDetailUrl(alreadyExists)
                        });
                    }

                    ICategory storyCategory = _categoryRepository.FindByUniqueName(category);

                    if (storyCategory == null)
                    {
                        return(new StoryCreateResult {
                            ErrorMessage = "\"{0}\" - kategoria nie istnieje.".FormatWith(category)
                        });
                    }

                    StoryContent content = _contentService.Get(url);

                    if (content == StoryContent.Empty)
                    {
                        return(new StoryCreateResult {
                            ErrorMessage = "Podany url wydaje siê nie byæ poprawnym."
                        });
                    }

                    description = _htmlSanitizer.Sanitize(description);

                    if (!_settings.AllowPossibleSpamStorySubmit && ShouldCheckSpamForUser(byUser))
                    {
                        result = EnsureNotSpam <StoryCreateResult>(byUser, userIPAddress, userAgent, url, urlReferer, description, "social news", serverVariables, "Spamowy wpis odrzucony : {0}, {1}".FormatWith(url, byUser), "Twój wpis wydaje siê byæ spamem.");

                        if (result != null)
                        {
                            return(result);
                        }
                    }

                    // If we are here which means story is not spam
                    IStory story = _factory.CreateStory(storyCategory, byUser, userIPAddress, title.StripHtml(), description, url);

                    _storyRepository.Add(story);

                    // The Initial vote;
                    story.Promote(story.CreatedAt, byUser, userIPAddress);

                    // Capture the thumbnail, might speed up the thumbnail generation process
                    _thumbnail.Capture(story.Url);

                    // Subscribe comments by default
                    story.SubscribeComment(byUser);

                    AddTagsToContainers(tags, new ITagContainer[] { story, byUser });

                    _userScoreService.StorySubmitted(byUser);

                    string detailUrl = buildDetailUrl(story);

                    if (_settings.AllowPossibleSpamStorySubmit && _settings.SendMailWhenPossibleSpamStorySubmitted && ShouldCheckSpamForUser(byUser))
                    {
                        unitOfWork.Commit();
                        _spamProtection.IsSpam(CreateSpamCheckContent(byUser, userIPAddress, userAgent, url, urlReferer, description, "social news", serverVariables),
                                               (source, isSpam) => _spamPostprocessor.Process(source, isSpam, detailUrl, story));
                    }
                    else
                    {
                        story.Approve(SystemTime.Now());
                    }

                    // Ping the Story
                    PingStory(content, story, detailUrl);

                    result = new StoryCreateResult {
                        NewStory = story, DetailUrl = detailUrl
                    };
                }
            }

            return(result);
        }
예제 #4
0
        public virtual StoryCreateResult Create(IUser byUser, string url, string title, string category, string description, string tags, string userIPAddress, string userAgent, string urlReferer, NameValueCollection serverVariables, Func <IStory, string> buildDetailUrl)
        {
            StoryCreateResult result = ValidateCreate(byUser, url, title, category, description, userIPAddress, userAgent);

            if (result == null)
            {
                if (_contentService.IsRestricted(url))
                {
                    result = new StoryCreateResult {
                        ErrorMessage = "Specifed url has match with our banned url list."
                    };
                }
            }

            if (result == null)
            {
                using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
                {
                    IStory alreadyExists = _storyRepository.FindByUrl(url);

                    if (alreadyExists != null)
                    {
                        return(new StoryCreateResult {
                            ErrorMessage = "Story with the same url already exists.", DetailUrl = buildDetailUrl(alreadyExists)
                        });
                    }

                    ICategory storyCategory = _categoryRepository.FindByUniqueName(category);

                    if (storyCategory == null)
                    {
                        return(new StoryCreateResult {
                            ErrorMessage = "\"{0}\" category does not exist.".FormatWith(category)
                        });
                    }

                    StoryContent content = _contentService.Get(url);

                    if (content == StoryContent.Empty)
                    {
                        return(new StoryCreateResult {
                            ErrorMessage = "Specified url appears to be broken."
                        });
                    }

                    description = _htmlSanitizer.Sanitize(description);

                    if (!_settings.AllowPossibleSpamStorySubmit && ShouldCheckSpamForUser(byUser))
                    {
                        result = EnsureNotSpam <StoryCreateResult>(byUser, userIPAddress, userAgent, url, urlReferer, description, "social news", serverVariables, "Spam story rejected : {0}, {1}".FormatWith(url, byUser), "Your story appears to be a spam.");

                        if (result != null)
                        {
                            return(result);
                        }
                    }

                    // If we are here which means story is not spam
                    IStory story = _factory.CreateStory(storyCategory, byUser, userIPAddress, title.StripHtml(), description, url);

                    _storyRepository.Add(story);

                    // The Initial vote;
                    story.Promote(story.CreatedAt, byUser, userIPAddress);

                    // Capture the thumbnail, might speed up the thumbnail generation process
                    _thumbnail.Capture(story.Url);

                    // Subscribe comments by default
                    story.SubscribeComment(byUser);

                    AddTagsToContainers(tags, new ITagContainer[] { story, byUser });

                    string detailUrl = buildDetailUrl(story);

                    if (_settings.AllowPossibleSpamStorySubmit && _settings.SendMailWhenPossibleSpamStorySubmitted && ShouldCheckSpamForUser(byUser))
                    {
                        unitOfWork.Commit();
                        _spamProtection.IsSpam(CreateSpamCheckContent(byUser, userIPAddress, userAgent, url, urlReferer, description, "social news", serverVariables), (source, isSpam) => _spamPostprocessor.Process(source, isSpam, detailUrl, story));
                    }
                    else
                    {
                        story.Approve(SystemTime.Now());
                        _eventAggregator.GetEvent <StorySubmitEvent>().Publish(new StorySubmitEventArgs(story, detailUrl));
                        unitOfWork.Commit();
                    }

                    result = new StoryCreateResult {
                        NewStory = story, DetailUrl = detailUrl
                    };
                }
            }

            return(result);
        }