public PublicationViewModel( DAL.Publication publication, string websiteUrl, IReadOnlyCollection <DAL.Category> categories = null) { _websiteUrl = websiteUrl; Id = publication.Id; Title = publication.Title; Description = publication.Description; Image = publication.Image; Link = publication.Link; DateTime = publication.DateTime; Type = publication.Type; Content = publication.Content; EmbededPlayerCode = publication.EmbededPlayerCode; ViewsCount = publication.Views; if (publication.CategoryId.HasValue && categories != null && categories.Any()) { var categoryName = categories .Where(o => o.Id == publication.CategoryId) .Select(o => o.Name) .FirstOrDefault(); Category = new CategoryViewModel { Id = publication.CategoryId.Value, Name = categoryName }; } }
public PublicationViewModel( DAL.Publication publication, Uri websiteUrl, IReadOnlyCollection <DAL.Category> categories = null) { _websiteUrl = websiteUrl; Id = publication.Id; Title = publication.Title; Description = publication.Description; Image = publication.Image; Url = new Uri(publication.Link); DateTime = publication.DateTime; Type = publication.Type; Content = publication.Content; EmbededPlayerCode = publication.EmbededPlayerCode; ViewsCount = publication.Views; if (categories != null && categories.Any()) { var categoryName = categories .Where(o => o.Id == publication.CategoryId) .Select(o => o.Name) .FirstOrDefault(); Category = new CategoryViewModel { Id = publication.CategoryId, Name = categoryName, CssClass = $"category-{publication.CategoryId}" }; } }
private RssItem CreateRssItem(DAL.Publication publication) { var p = new PublicationViewModel(publication, _settings.WebSiteUrl); return(new RssItem { Description = p.Description, Link = new RssUrl(p.ShareUrl), PubDate = p.DateTime, Title = p.Title, Guid = new RssGuid { IsPermaLink = true, Value = p.ShareUrl }, Source = new RssSource { Url = new RssUrl(p.ShareUrl) } }); }
public async Task <IActionResult> AddPublicaton(NewPostRequest request) { DAL.User user = _userManager.GetBySecretKey(request.Key); if (user == null) { return(StatusCode((int)HttpStatusCode.Forbidden)); } var extractor = new X.Web.MetaExtractor.Extractor(); var metadata = await extractor.Extract(new Uri(request.Link)); var publication = new DAL.Publication { Title = metadata.Title, Description = metadata.Description, Link = metadata.Url, Image = metadata.Image.FirstOrDefault(), Type = metadata.Type, DateTime = DateTime.Now, UserId = user.Id, CategoryId = request.CategoryId, Comment = request.Comment }; publication = await _publicationManager.Save(publication); if (publication != null) { var model = new PublicationViewModel(publication, Settings.Current.WebSiteUrl); await _telegramManager.Send(request.CategoryId, request.Comment, request.Link); return(Created(new Uri($"{Core.Settings.Current.WebSiteUrl}post/{publication.Id}"), model)); } return(StatusCode((int)HttpStatusCode.BadRequest)); }
public async Task <IActionResult> AddPublicaton(NewPostRequest request) { var user = _userManager.GetBySecretKey(request.Key); if (user == null) { return(StatusCode((int)HttpStatusCode.Forbidden)); } var extractor = new X.Web.MetaExtractor.Extractor(); var languageAnalyzer = new LanguageAnalyzer(Settings.Current.CognitiveServicesTextAnalyticsKey); try { var metadata = await extractor.Extract(new Uri(request.Link)); 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.Image.FirstOrDefault(), Type = metadata.Type, 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.Current.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; await _crossPostManager.Send(request.CategoryId, request.Comment, url); return(Created(new Uri(model.ShareUrl), model)); } else { throw new Exception("Can't save publication to databse"); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
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)); } }