public async Task <PublicationViewModel> CreatePublication(NewPostRequest request, User user)
    {
        var extractor = new X.Web.MetaExtractor.Extractor();

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

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

        if (existingPublication != null)
        {
            throw new DuplicateNameException("Publication with this URL already exist");
        }

        var languageCode = _languageAnalyzer.GetTextLanguage(metadata.Description);
        var languageId   = await _localizationService.GetLanguageId(languageCode) ?? Core.Language.EnglishId;

        var player     = EmbeddedPlayerFactory.CreatePlayer(request.Link);
        var playerCode = player != null ? await player.GetEmbeddedPlayerUrl(request.Link) : null;

        var publication = await _publicationService.CreatePublication(
            metadata,
            user.Id,
            languageId,
            playerCode,
            request.CategoryId,
            request.Comment);

        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.EmbeddedPlayerCode) ? model.RedirectUrl : model.ShareUrl;

            var services = await GetServices(publication);

            foreach (var service in services)
            {
                await service.Send(request.Comment, url, GetTags(request));
            }

            return(model);
        }

        throw new Exception("Can't save publication to database");
    }
Exemplo n.º 2
0
        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));
        }