示例#1
0
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // Get logged in user
            var uid   = User.GetNumericId();
            var uname = User.GetUsername();

            // Return if not logged in
            if (uid == null || uname == null)
            {
                return(Unauthorized());
            }

            // Create array of hashtags
            var tags = Input.Tags?
                       .Split(',')
                       .Where(t => !string.IsNullOrWhiteSpace(t))
                       .ToList()
                       .Select(t => t.Trim(' ', '#', ',').Friendlify())
                       .Distinct()
                       .ToArray();

            var post = new Blogpost
            {
                Title          = Input.Title.Trim(),
                Slug           = Input.Title.Trim().Friendlify(),
                Body           = Input.Body.Trim(),
                AuthorId       = (long)uid,
                CommentsThread = new CommentsThread(),
                WordCount      = Input.Body.Trim().Split(' ', '\t', '\n').Length,
                Hashtags       = tags ?? Array.Empty <string>()
            };

            if (Input.StoryMinimalId.HasValue)
            {
                post.AttachedStoryId = Input.StoryMinimalId;
            }
            else if (Input.ChapterMinimalId.HasValue)
            {
                post.AttachedChapterId = Input.ChapterMinimalId;
            }

            await _context.Blogposts.AddAsync(post);

            await _context.SaveChangesAsync();

            var notificationRecipients = await _context.Users
                                         .Where(u => u.Following.Any(a => a.Id == uid))
                                         .Select(u => u.Id)
                                         .ToListAsync();

            await _notificationsRepo.Create(ENotificationEvent.FollowedAuthorNewBlogpost,
                                            notificationRecipients,
                                            "/Blog/Post",
                                            new { post.Id, post.Slug });

            return(RedirectToPage("/User/Blog", new { name = uname }));
        }
示例#2
0
        public async Task <IActionResult> OnPostAsync()
        {
            Ratings = await _context.Ratings.ToListAsync();

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // Get logged in user
            var uid = User.GetNumericId();

            // Return if not logged in
            if (uid == null)
            {
                return(Unauthorized());
            }

            var rating = await _context.Ratings.FindAsync(Input.Rating);

            var tags = await _context.Tags
                       .Where(t => Input.Tags.Contains(t.Id))
                       .ToListAsync();

            // Add story
            var story = new Story
            {
                AuthorId    = (long)uid,
                Title       = Input.Title,
                Slug        = Input.Title.Friendlify(),
                Description = Input.Description,
                Hook        = Input.Hook,
                Rating      = rating,
                Tags        = tags
            };

            await _context.Stories.AddAsync(story);

            await _context.SaveChangesAsync();

            // Upload cover
            if (Input.Cover != null && Input.Cover.Length > 0)
            {
                var file = await _uploader.Upload(
                    Input.Cover,
                    "covers",
                    $"{story.Id}-{story.Slug}",
                    _config.StoryCoverWidth,
                    _config.StoryCoverHeight
                    );

                story.CoverId = file.FileId;
                story.Cover   = file.Path;
                // Final save
                await _context.SaveChangesAsync();
            }

            // Get a list of users that should receive notifications
            var notificationRecipients = await _context.Users
                                         .Where(u => u.Following.Any(a => a.Id == uid))
                                         .Select(u => u.Id)
                                         .ToListAsync();

            // Notify
            await _notificationsRepo.Create(ENotificationEvent.FollowedAuthorNewStory,
                                            notificationRecipients,
                                            "/Story",
                                            new { story.Id, story.Slug });

            return(RedirectToPage("../Story", new { id = story.Id, slug = story.Slug }));
        }
示例#3
0
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync(long id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // Get the story to insert a chapter into. Include user in the search to check ownership.
            var story = await _context.Stories
                        .Where(s => s.Id == id)
                        .Where(s => s.AuthorId == User.GetNumericId())
                        .Include(s => s.Chapters)
                        .FirstOrDefaultAsync();

            // Back to index if the story is null or author isn't the logged in user
            if (story == null)
            {
                return(RedirectToPage("../Index"));
            }

            // Get the order number of the latest chapter
            var latestChapter = story.Chapters
                                .OrderByDescending(c => c.Order)
                                .Select(c => c.Order)
                                .FirstOrDefault();

            // Construct new chapter
            var chapter = new Chapter
            {
                Title          = Input.Title.Trim(),
                Body           = Input.Body.Trim(),
                StartNotes     = Input.StartNotes?.Trim(),
                EndNotes       = Input.EndNotes?.Trim(),
                Slug           = Input.Title.Trim().Friendlify(),
                Order          = latestChapter + 1,
                CommentsThread = new CommentsThread(),
                WordCount      = Input.Body.Trim().Split(' ', '\t', '\n').Length
            };

            // Recalculate words and chapters in the story
            story.WordCount    = story.Chapters.Sum(c => c.WordCount) + chapter.WordCount;
            story.ChapterCount = story.Chapters.Count + 1;

            // Create the chapter and add it to the story
            story.Chapters.Add(chapter);

            await _context.SaveChangesAsync();

            // Get a list of users that should receive a notifications
            var notificationRecipients = await _context.Shelves
                                         .Where(s => s.TrackUpdates)
                                         .Where(s => s.Stories.Any(st => st.Id == story.Id))
                                         .Select(s => s.OwnerId)
                                         .ToListAsync();

            // Notify
            await _notificationsRepo.Create(ENotificationEvent.WatchedStoryUpdated,
                                            notificationRecipients,
                                            "/Chapter",
                                            new { chapter.Id, chapter.Slug });

            return(RedirectToPage("../Chapter", new { id = chapter.Id, slug = chapter.Slug }));
        }
示例#4
0
        public async Task <ActionResult <CommentDto> > PostComment(PostData data)
        {
            var uid = User?.GetNumericId();

            if (uid is null)
            {
                return(Unauthorized());
            }

            var(body, threadId) = data;

            var comment = new Comment
            {
                AuthorId = (long)uid,
                Body     = body
            };

            var thread = await _context.CommentThreads
                         .Where(ct => ct.Id == threadId)
                         .Include(ct => ct.Comments)
                         .FirstOrDefaultAsync();

            if (thread is null)
            {
                return(NotFound());
            }
            if (thread.LockDate is not null)
            {
                return(Unauthorized());
            }

            thread.Comments.Add(comment);
            thread.CommentsCount = thread.Comments.Count;

            await _context.SaveChangesAsync();

            if (thread.UserId != uid)
            {
                // Create notification
                var subscribers = await _context.CommentsThreadSubscribers
                                  .Where(cts => cts.CommentsThreadId == thread.Id)
                                  .Select(cts => cts.OgmaUserId)
                                  .ToListAsync();

                var redirection = await _redirector.RedirectToComment(comment.Id);

                if (redirection is not null)
                {
                    await _notificationsRepo.Create(ENotificationEvent.WatchedThreadNewComment,
                                                    subscribers,
                                                    redirection.Url,
                                                    redirection.Params,
                                                    redirection.Fragment,
                                                    comment.Body.Truncate(50)
                                                    );
                }

                await _context.SaveChangesAsync();
            }

            var dto = _mapper.Map <Comment, CommentDto>(comment);

            return(CreatedAtAction("GetComment", new { id = comment.Id }, dto));
        }