コード例 #1
0
        public JsonResult ToggleFavorite(int id)
        {
            var user = _userRetrievalShim.GetUser(HttpContext);

            if (user == null)
            {
                return(Json(new BasicJsonMessage {
                    Message = Resources.NotLoggedIn, Result = false
                }));
            }
            var topic = _topicService.Get(id);

            if (topic == null)
            {
                return(Json(new BasicJsonMessage {
                    Message = Resources.TopicNotExist, Result = false
                }));
            }
            if (_favoriteTopicService.IsTopicFavorite(user, topic))
            {
                _favoriteTopicService.RemoveFavoriteTopic(user, topic);
                return(Json(new BasicJsonMessage {
                    Data = new { isFavorite = false }, Result = true
                }));
            }
            _favoriteTopicService.AddFavoriteTopic(user, topic);
            return(Json(new BasicJsonMessage {
                Data = new { isFavorite = true }, Result = true
            }));
        }
コード例 #2
0
ファイル: ForumController.cs プロジェクト: popotans/POPForums
        public ActionResult Topic(string id, int page = 1)
        {
            var topic = _topicService.Get(id);

            if (topic == null)
            {
                return(NotFound());
            }
            var forum = _forumService.Get(topic.ForumID);

            if (forum == null)
            {
                throw new Exception(String.Format("TopicID {0} references ForumID {1}, which does not exist.", topic.TopicID, topic.ForumID));
            }

            var user              = _userRetrievalShim.GetUser(HttpContext);
            var adapter           = new ForumAdapterFactory(forum);
            var permissionContext = _forumService.GetPermissionContext(forum, user, topic);

            if (!permissionContext.UserCanView)
            {
                return(Forbid());
            }

            PagerContext pagerContext = null;
            var          isSubscribed = false;
            var          isFavorite   = false;
            DateTime?    lastReadTime = DateTime.UtcNow;

            if (user != null)
            {
                lastReadTime = _lastReadService.GetLastReadTime(user, topic);
                isFavorite   = _favoriteTopicService.IsTopicFavorite(user, topic);
                isSubscribed = _subService.IsTopicSubscribed(user, topic);
                if (isSubscribed)
                {
                    _subService.MarkSubscribedTopicViewed(user, topic);
                }
                if (!adapter.IsAdapterEnabled || (adapter.IsAdapterEnabled && adapter.ForumAdapter.MarkViewedTopicRead))
                {
                    _lastReadService.MarkTopicRead(user, topic);
                }
                if (user.IsInRole(PermanentRoles.Moderator))
                {
                    ViewBag.CategorizedForums = _forumService.GetCategorizedForumContainer();
                }
            }
            List <Post> posts;

            if (forum.IsQAForum)
            {
                posts = _postService.GetPosts(topic, permissionContext.UserCanModerate);
            }
            else
            {
                posts = _postService.GetPosts(topic, permissionContext.UserCanModerate, page, out pagerContext);
            }
            if (posts.Count == 0)
            {
                return(NotFound());
            }
            var signatures = _profileService.GetSignatures(posts);
            var avatars    = _profileService.GetAvatars(posts);
            var votedIDs   = _postService.GetVotedPostIDs(user, posts);
            var container  = ComposeTopicContainer(topic, forum, permissionContext, isSubscribed, posts, pagerContext, isFavorite, signatures, avatars, votedIDs, lastReadTime);

            _topicViewCountService.ProcessView(topic, HttpContext);
            if (adapter.IsAdapterEnabled)
            {
                adapter.ForumAdapter.AdaptTopic(this, container);
                if (String.IsNullOrWhiteSpace(adapter.ForumAdapter.ViewName))
                {
                    return(View(adapter.ForumAdapter.Model));
                }
                return(View(adapter.ForumAdapter.ViewName, adapter.ForumAdapter.Model));
            }
            if (forum.IsQAForum)
            {
                var containerForQA = _forumService.MapTopicContainerForQA(container);
                return(View("TopicQA", containerForQA));
            }
            return(View(container));
        }
コード例 #3
0
ファイル: ForumController.cs プロジェクト: iomeone/POPForums
        public async Task <ActionResult> Topic(string id, int pageNumber = 1)
        {
            var topic = await _topicService.Get(id);

            if (topic == null)
            {
                return(NotFound());
            }
            var forum = await _forumService.Get(topic.ForumID);

            if (forum == null)
            {
                throw new Exception($"TopicID {topic.TopicID} references ForumID {topic.ForumID}, which does not exist.");
            }

            var user              = _userRetrievalShim.GetUser();
            var adapter           = new ForumAdapterFactory(forum);
            var permissionContext = await _forumPermissionService.GetPermissionContext(forum, user, topic);

            if (!permissionContext.UserCanView)
            {
                return(NotFound());
            }

            PagerContext pagerContext = null;
            var          isSubscribed = false;
            var          isFavorite   = false;
            DateTime?    lastReadTime = DateTime.UtcNow;

            if (user != null)
            {
                lastReadTime = await _lastReadService.GetLastReadTime(user, topic);

                isFavorite = await _favoriteTopicService.IsTopicFavorite(user, topic);

                isSubscribed = await _subService.IsTopicSubscribed(user, topic);

                if (isSubscribed)
                {
                    await _subService.MarkSubscribedTopicViewed(user, topic);
                }
                if (!adapter.IsAdapterEnabled || (adapter.IsAdapterEnabled && adapter.ForumAdapter.MarkViewedTopicRead))
                {
                    await _lastReadService.MarkTopicRead(user, topic);
                }
                if (user.IsInRole(PermanentRoles.Moderator))
                {
                    var categorizedForums = await _forumService.GetCategorizedForumContainer();

                    var categorizedForumSelectList = new List <SelectListItem>();
                    foreach (var uncategorizedForum in categorizedForums.UncategorizedForums)
                    {
                        categorizedForumSelectList.Add(new SelectListItem {
                            Value = uncategorizedForum.ForumID.ToString(), Text = uncategorizedForum.Title, Selected = forum.ForumID == uncategorizedForum.ForumID
                        });
                    }
                    foreach (var categoryPair in categorizedForums.CategoryDictionary)
                    {
                        var group = new SelectListGroup {
                            Name = categoryPair.Key.Title
                        };
                        foreach (var categorizedForum in categoryPair.Value)
                        {
                            categorizedForumSelectList.Add(new SelectListItem {
                                Value = categorizedForum.ForumID.ToString(), Text = categorizedForum.Title, Selected = forum.ForumID == categorizedForum.ForumID, Group = group
                            });
                        }
                    }
                    ViewBag.CategorizedForums = categorizedForumSelectList;
                }
            }
            List <Post> posts;

            if (forum.IsQAForum)
            {
                posts = await _postService.GetPosts(topic, permissionContext.UserCanModerate);
            }
            else
            {
                (posts, pagerContext) = await _postService.GetPosts(topic, permissionContext.UserCanModerate, pageNumber);
            }
            if (posts.Count == 0)
            {
                return(NotFound());
            }
            var signatures = await _profileService.GetSignatures(posts);

            var avatars = await _profileService.GetAvatars(posts);

            var votedIDs = await _postService.GetVotedPostIDs(user, posts);

            var container = ComposeTopicContainer(topic, forum, permissionContext, isSubscribed, posts, pagerContext, isFavorite, signatures, avatars, votedIDs, lastReadTime);
            await _topicViewCountService.ProcessView(topic);

            await _topicViewLogService.LogView(user?.UserID, topic.TopicID);

            if (adapter.IsAdapterEnabled)
            {
                adapter.ForumAdapter.AdaptTopic(this, container);
                if (string.IsNullOrWhiteSpace(adapter.ForumAdapter.ViewName))
                {
                    return(View(adapter.ForumAdapter.Model));
                }
                return(View(adapter.ForumAdapter.ViewName, adapter.ForumAdapter.Model));
            }
            if (forum.IsQAForum)
            {
                var containerForQA = _forumService.MapTopicContainerForQA(container);
                return(View("TopicQA", containerForQA));
            }
            return(View(container));
        }
コード例 #4
0
        public ViewResult Topic(string id, int page = 1)
        {
            var topic = _topicService.Get(id);

            if (topic == null)
            {
                return(this.NotFound("NotFound", null));
            }
            var forum = _forumService.Get(topic.ForumID);

            if (forum == null)
            {
                throw new Exception(String.Format("TopicID {0} references ForumID {1}, which does not exist.", topic.TopicID, topic.ForumID));
            }

            var adapter           = new ForumAdapterFactory(forum);
            var permissionContext = _forumService.GetPermissionContext(forum, this.CurrentUser(), topic);

            if (!permissionContext.UserCanView)
            {
                return(this.Forbidden("Forbidden", null));
            }
            //var category = _categoryService.Get(forum.CategoryID??0);
            //if (category.ClosureDate.Date < DateTime.Now.Date)
            //{
            //    topic.NotReply = true;
            //}
            var closureDate = _categoryService.GetValue();

            if (DateTime.Now.Date > closureDate.ClosureDate.Date)
            {
                topic.NotPostAll = true;
            }
            if (DateTime.Now.Date > closureDate.FinalClosureDate.Date)
            {
                topic.NotReply = true;
            }
            PagerContext pagerContext = null;
            var          isSubscribed = false;
            var          isFavorite   = false;
            var          user         = this.CurrentUser();

            if (user != null)
            {
                isFavorite   = _favoriteTopicService.IsTopicFavorite(user, topic);
                isSubscribed = _subService.IsTopicSubscribed(user, topic);
                if (isSubscribed)
                {
                    _subService.MarkSubscribedTopicViewed(user, topic);
                }
                if (!adapter.IsAdapterEnabled || (adapter.IsAdapterEnabled && adapter.ForumAdapter.MarkViewedTopicRead))
                {
                    _lastReadService.MarkTopicRead(user, topic);
                }
                if (user.IsInRole(PermanentRoles.Moderator))
                {
                    ViewBag.CategorizedForums = _forumService.GetCategorizedForumContainer();
                }
            }
            List <Post> posts;

            if (forum.IsQAForum)
            {
                posts = _postService.GetPosts(topic, permissionContext.UserCanModerate);
            }
            else
            {
                if (this.CurrentUser().UserType > 1) // manager, staff
                {
                    posts = _postService.GetPosts(topic, permissionContext.UserCanModerate, page, out pagerContext);
                }
                else //student
                {
                    posts = _postService.GetPosts(topic, permissionContext.UserCanModerate, page, out pagerContext).Where(a => a.UserType == 1).ToList();
                }
            }

            if (posts.Count == 0)
            {
                return(this.NotFound("NotFound", null));
            }
            var signatures = _profileService.GetSignatures(posts);
            var avatars    = _profileService.GetAvatars(posts);
            var votedIDs   = _postService.GetVotedPostIDs(this.CurrentUser(), posts);


            var container = ComposeTopicContainer(topic, forum, permissionContext, isSubscribed, posts, pagerContext, isFavorite, signatures, avatars, votedIDs);

            _topicViewCountService.ProcessView(topic, HttpContext);
            if (adapter.IsAdapterEnabled)
            {
                adapter.ForumAdapter.AdaptTopic(this, container);
                if (String.IsNullOrWhiteSpace(adapter.ForumAdapter.ViewName))
                {
                    return(View(adapter.ForumAdapter.Model));
                }
                return(View(adapter.ForumAdapter.ViewName, adapter.ForumAdapter.Model));
            }
            if (forum.IsQAForum)
            {
                var containerForQA = _forumService.MapTopicContainerForQA(container);
                return(View("TopicQA", containerForQA));
            }
            return(View(container));
        }