예제 #1
0
        public async Task <IActionResult> Post(int id)
        {
            var publication = await _publicationManager.Get(id);

            await _publicationManager.IncreaseViewCount(id);

            if (publication == null)
            {
                return(NotFound());
            }

            var categories = await _publicationManager.GetCategories();

            var model = new PublicationViewModel(publication, _settings.WebSiteUrl, categories);

            ViewData["Title"] = model.Title;

            return(View("~/Views/Home/Post.cshtml", model));
        }
예제 #2
0
        public async Task <IActionResult> AddPublication(NewPostRequest request)
        {
            var user = await _userManager.GetBySecretKey(request.Key);

            if (user == null)
            {
                _logger.Write(LogLevel.Warning, $"Somebody tried to login with this key: `{request.Key}`. Text: `{request.Comment}`");

                return(StatusCode((int)HttpStatusCode.Forbidden, "Incorrect security key"));
            }

            var extractor        = new X.Web.MetaExtractor.Extractor();
            var languageAnalyzer = new LanguageAnalyzerService(_settings.CognitiveServicesTextAnalyticsKey, _logger);

            try
            {
                var metadata = await extractor.ExtractAsync(request.Link);

                var existingPublication = await _publicationManager.Get(new Uri(metadata.Url));

                if (existingPublication != null)
                {
                    return(StatusCode((int)HttpStatusCode.Conflict, "Publication with this URL already exist"));
                }

                var languageCode = languageAnalyzer.GetTextLanguage(metadata.Description);
                var languageId   = await _localizationManager.GetLanguageId(languageCode) ?? Language.EnglishId;

                var image = metadata.Images.FirstOrDefault();

                var publication = new DAL.Publication
                {
                    Title       = metadata.Title,
                    Description = metadata.Description,
                    Link        = metadata.Url,
                    Image       = string.IsNullOrWhiteSpace(image) || image.Length > 250 ? string.Empty : image,
                    Type        = "article",
                    DateTime    = DateTime.Now,
                    UserId      = user.Id,
                    CategoryId  = request.CategoryId,
                    Comment     = request.Comment,
                    LanguageId  = languageId
                };

                var player = EmbeddedPlayerFactory.CreatePlayer(request.Link);

                if (player != null)
                {
                    publication.EmbededPlayerCode = await player.GetEmbeddedPlayerUrl(request.Link);
                }

                publication = await _publicationManager.Save(publication);

                if (publication != null)
                {
                    var model = new PublicationViewModel(publication, _settings.WebSiteUrl);

                    //If we can embed main content into site page, so we can share this page.
                    var url = string.IsNullOrEmpty(model.EmbededPlayerCode) ? model.Link : model.ShareUrl;

                    foreach (var crossPostManager in _crossPostManagers)
                    {
                        await crossPostManager.Send(request.CategoryId, request.Comment, url);
                    }

                    return(Created(new Uri(model.ShareUrl), model));
                }

                throw new Exception("Can't save publication to database");
            }
            catch (Exception ex)
            {
                _logger.Write(LogLevel.Error, "Error while creating new publication", ex);

                return(BadRequest(ex.Message));
            }
        }
예제 #3
0
        public async Task <IActionResult> AddPublicaton(NewPostRequest request)
        {
            var user = _userManager.GetBySecretKey(request.Key);

            if (user == null)
            {
                return(StatusCode((int)HttpStatusCode.Forbidden, "Incorrect security key"));
            }

            var extractor        = new X.Web.MetaExtractor.Extractor();
            var languageAnalyzer = new LanguageAnalyzer(_settings.CognitiveServicesTextAnalyticsKey);

            try
            {
                var metadata = await extractor.ExtractAsync(new Uri(request.Link));

                var existingPublication = _publicationManager.Get(new Uri(metadata.Url));

                if (existingPublication != null)
                {
                    return(StatusCode((int)HttpStatusCode.Conflict, "Publication with this URL already exist"));
                }

                var languageCode = languageAnalyzer.GetTextLanguage(metadata.Description);
                var languageId   = _localizationManager.GetLanguageId(languageCode) ?? Language.EnglishId;

                var publication = new DAL.Publication
                {
                    Title       = metadata.Title,
                    Description = metadata.Description,
                    Link        = metadata.Url,
                    Image       = metadata.Images.FirstOrDefault(),
                    Type        = "article",
                    DateTime    = DateTime.Now,
                    UserId      = user.Id,
                    CategoryId  = request.CategoryId,
                    Comment     = request.Comment,
                    LanguageId  = languageId
                };

                if (EmbededPlayer.GetPlayerSoure(request.Link) != null)
                {
                    var player = new EmbededPlayer(request.Link);
                    publication.EmbededPlayerCode = player.Render();
                }

                publication = await _publicationManager.Save(publication);

                if (publication != null)
                {
                    var model = new PublicationViewModel(publication, _settings.WebSiteUrl);

                    //If we can embed main content into site page, so we can share this page.
                    var url = string.IsNullOrEmpty(model.EmbededPlayerCode) ? model.Link : model.ShareUrl;

                    foreach (var crossPostManager in _crossPostManagers)
                    {
                        await crossPostManager.Send(request.CategoryId, request.Comment, url);
                    }

                    return(Created(new Uri(model.ShareUrl), model));
                }


                throw new Exception("Can't save publication to databse");
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }