Ejemplo n.º 1
0
        public async Task <IActionResult> Search(int threadId, string query = "")
        {
            try
            {
                if (User.Identity.IsAuthenticated)
                {
                    ViewBag.CurrentAdmin = _db.Admin.First(a => a.Email == User.Identity.Name);
                }

                var postsFiles = new Dictionary <Post, List <File> >();

                if (!string.IsNullOrWhiteSpace(query) || !string.IsNullOrEmpty(query))
                {
                    if (ModelState.IsValid)
                    {
                        try
                        {
                            var posts = _db.Post.Where(p =>
                                                       p.ThreadId == threadId && (p.Comment.Contains(query) || p.Subject.Contains(query)))
                                        .Include(p => p.File).Take(50).ToList();

                            foreach (var post in posts)
                            {
                                post.Comment = PostFormatter.GetFormattedPostText(post);
                                postsFiles.Add(post, post.File.ToList());
                            }
                        }
                        catch (InvalidOperationException)
                        {
                            return(NotFound());
                        }
                    }
                }

                ViewBag.Thread     = _db.Thread.Include(t => t.Board).First(t => t.ThreadId == threadId);
                ViewBag.Board      = ViewBag.Thread.Board;
                ViewBag.PostsFiles = postsFiles;
            }
            catch (InvalidOperationException)
            {
                return(NotFound());
            }
            catch (Exception e)
            {
                await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                  LoggingInformationKind.Error);

                return(StatusCode(500));
            }

            return(View());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Report(int postId)
        {
            var ipHash = _md5.ComputeHash(HttpContext.Connection.RemoteIpAddress.GetAddressBytes())
                         .GetString();

            var anon = new Anon(ipHash, IpCheck.UserIsBanned(_db, ipHash));

            ViewBag.UserIpHash = anon.IpHash;

            ViewBag.UserIsBanned = false;

            if (anon.IsBanned)
            {
                ViewBag.UserIsBanned = anon.IsBanned;
                var ban = _db.Ban.First(b => b.AnonIpHash == anon.IpHash);
                ViewBag.BanReason = ban.Reason;
                ViewBag.BanEnd    = DateTimeOffset.FromUnixTimeSeconds(ban.Term).ToLocalTime();
            }

            try
            {
                var post = _db.Post.Include(p => p.File)
                           .Include(p => p.Admin)
                           .First(p => p.PostId == postId) ?? throw new Exception();

                ViewBag.Board = _collection.Boards.First(brd => brd.BoardId == post.BoardId);

                ViewBag.Thread = _db.Thread.First(t => t.ThreadId == post.ThreadId);

                post.Comment = PostFormatter.GetFormattedPostText(post);

                ViewBag.PostFiles = new KeyValuePair <Post, List <File> >(post, post.File.ToList());

                return(View());
            }
            catch (Exception e)
            {
                await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                  LoggingInformationKind.Error);

                return(NotFound());
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Thread(int id)
        {
            var ipHash = _md5.ComputeHash(HttpContext.Connection.RemoteIpAddress.GetAddressBytes())
                         .GetString();

            var anon = new Anon(ipHash, IpCheck.UserIsBanned(_db, ipHash));

            ViewBag.UserIpHash = anon.IpHash;

            ViewBag.UserIsBanned = false;

            ViewBag.Id = 0;

            if (anon.IsBanned)
            {
                ViewBag.UserIsBanned = anon.IsBanned;
                var ban = _db.Ban.First(b => b.AnonIpHash == anon.IpHash);
                ViewBag.BanReason = ban.Reason;
                ViewBag.BanEnd    = DateTimeOffset.FromUnixTimeSeconds(ban.Term).ToLocalTime();
            }

            if (User.Identity.IsAuthenticated)
            {
                ViewBag.CurrentAdmin = _db.Admin.First(a => a.Email == User.Identity.Name);
            }

            try
            {
                using (_db)
                {
                    try
                    {
                        var thread = _db.Thread.Where(t => t.ThreadId == id)
                                     .Include(t => t.Board)
                                     .ToList()[0];

                        var posts = _db.Post.Where(p => p.ThreadId == id)
                                    .Include(p => p.File)
                                    .Include(p => p.Admin)
                                    .ToList();

                        var postsFiles = new Dictionary <Post, List <File> >();

                        ViewBag.PinnedPost = new KeyValuePair <Post, List <File> >(null, null);

                        foreach (var post in posts)
                        {
                            post.Comment = PostFormatter.GetFormattedPostText(post);
                            if (post.IsPinned)
                            {
                                ViewBag.PinnedPost = new KeyValuePair <Post, List <File> >(post, post.File.ToList());
                                continue;
                            }

                            postsFiles.Add(post, post.File.ToList());
                        }

                        ViewBag.Board      = thread.Board;
                        ViewBag.Thread     = thread;
                        ViewBag.UserIsOp   = IpCheck.UserIsOp(thread, anon.IpHash);
                        ViewBag.PostsFiles = postsFiles;
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        return(NotFound());
                    }
                    catch (Exception e)
                    {
                        await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                          LoggingInformationKind.Error);
                    }
                }
            }
            catch (InvalidOperationException)
            {
                return(NotFound());
            }
            catch (Exception e)
            {
                await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                  LoggingInformationKind.Error);
            }

            return(View());
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Board(string prefix, int page = 1)
        {
            var ipHash = _md5.ComputeHash(HttpContext.Connection.RemoteIpAddress.GetAddressBytes())
                         .GetString();

            var anon = new Anon(ipHash, IpCheck.UserIsBanned(_db, ipHash));

            ViewBag.UserIpHash = anon.IpHash;

            ViewBag.UserIsBanned = false;

            if (anon.IsBanned)
            {
                ViewBag.UserIsBanned = anon.IsBanned;
                var ban = _db.Ban.First(b => b.AnonIpHash == anon.IpHash);
                ViewBag.BanReason = ban.Reason;
                ViewBag.BanEnd    = DateTimeOffset.FromUnixTimeSeconds(ban.Term).ToLocalTime();
            }

            ViewBag.Id = 0;

            ViewBag.Page = page;

            try
            {
                ViewBag.Board = _collection.Boards.First(brd => brd.Prefix == prefix);

                var allThreads = new List <KeyValuePair <Thread, List <KeyValuePair <Post, List <File> > > > >();

                var boards = _db.Board.Where(b => b.Prefix == prefix).Include(b => b.Thread).ToList();

                Board board;
                if (boards.Count > 0)
                {
                    board             = boards[0];
                    ViewBag.FileLimit = board.FileLimit;
                }
                else
                {
                    return(NotFound());
                }

                ViewBag.ThreadCount = board.Thread.Count;

                foreach (var thrd in board.Thread.OrderByDescending(t => t.BumpInUnixTime))
                {
                    var thread = _db.Thread.Include(t => t.Post).First(t => t.ThreadId == thrd.ThreadId);

                    if (thread != null)
                    {
                        var postFiles = new List <KeyValuePair <Post, List <File> > >();

                        if (thread.Post.Count >= 4)
                        {
                            var posts = new List <Post> {
                                thread.Post.ToArray()[0]
                            };

                            posts.AddRange(thread.Post.ToList().OrderByDescending(p => p.TimeInUnixSeconds).Take(3));

                            foreach (var post in posts)
                            {
                                var p = _db.Post
                                        .Include(pp => pp.File)
                                        .Include(pp => pp.Admin)
                                        .First(pp => pp.PostId == post.PostId);

                                postFiles.Add(new KeyValuePair <Post, List <File> >(p, p.File.ToList()));
                            }

                            allThreads.Add(
                                new KeyValuePair <Thread, List <KeyValuePair <Post, List <File> > > >(thread, postFiles));
                        }
                        else if (thread.Post.Count > 0 && thread.Post.Count <= 3)
                        {
                            var p = _db.Post
                                    .Include(pp => pp.File)
                                    .Include(pp => pp.Admin)
                                    .First(pp => pp.ThreadId == thread.ThreadId);
                            postFiles.Add(new KeyValuePair <Post, List <File> >(p, p.File.ToList()));

                            allThreads.Add(
                                new KeyValuePair <Thread, List <KeyValuePair <Post, List <File> > > >(thread, postFiles));
                        }
                    }
                }

                var pageInfo = new PageInfo(page, Constants.BOARD_PAGE_SIZE, allThreads.Count);

                var pageThreads = new List <KeyValuePair <Thread, List <KeyValuePair <Post, List <File> > > > >();

                for (var i = (page - 1) * pageInfo.PageSize; i < page * pageInfo.PageSize; i++)
                {
                    try
                    {
                        pageThreads.Add(allThreads[i]);
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        break;
                    }
                    catch (Exception e)
                    {
                        await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                          LoggingInformationKind.Error);

                        return(StatusCode(500));
                    }
                }

                foreach (var pt in pageThreads)
                {
                    foreach (var pf in pt.Value)
                    {
                        pf.Key.Comment = PostFormatter.GetFormattedPostText(pf.Key);
                    }
                }

                ViewBag.PageInfo = pageInfo;

                ViewBag.BoardViewModel = pageThreads;
            }
            catch (InvalidOperationException e)
            {
                await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                  LoggingInformationKind.Error);

                return(NotFound());
            }
            catch (Exception e)
            {
                await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                  LoggingInformationKind.Error);

                return(StatusCode(500));
            }

            return(View());
        }