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)); } }