Esempio n. 1
0
        public ActionResult Index(string id, ActivitiesQueryInput activityInput, PagingInput pagingInput)
        {
            string userId = VerbosifyId<User>(id);

            if (!_permissionManager.DoesExist<User>(userId))
            {
                return HttpNotFound();
            }

            var userResult = _documentSession
                .Query<All_Users.Result, All_Users>()
                .AsProjection<All_Users.Result>()
                .Where(x => x.UserId == userId)
                .First();

            dynamic viewModel = new ExpandoObject();
            viewModel.User = _userViewModelQuery.BuildUser(userId);
            //viewModel.Activities = _activityViewModelQuery.BuildGroupActivityList(userResult.User.UserProject.Id, activityInput, pagingInput);
            viewModel.Activities = _activityViewModelQuery.BuildUserActivityList(userResult.User.Id, activityInput, pagingInput);
            //viewModel.IsMember = _userContext.IsUserAuthenticated() ? _userContext.HasGroupPermission<Project>(PermissionNames.CreateObservation, projectId) : false;
            viewModel.SightingCountDescription = "Sighting" + (userResult.SightingCount == 1 ? string.Empty : "s");
            viewModel.ProjectCountDescription = "Project" + (userResult.Projects.Count() == 1 ? string.Empty : "s");
            viewModel.OrganisationCountDescription = "Organisation" + (userResult.Organisations.Count() == 1 ? string.Empty : "s");
            viewModel.ShowActivities = true;

            return RestfulResult(
                viewModel,
                "users",
                "index");
        }
        public ActionResult Index(string id, ActivitiesQueryInput activityInput, PagingInput pagingInput)
        {
            string organisationId = VerbosifyId<Organisation>(id);

            if (!_permissionManager.DoesExist<Organisation>(organisationId))
            {
                return HttpNotFound();
            }

            var organisationResult = _documentSession
                .Query<All_Groups.Result, All_Groups>()
                .AsProjection<All_Groups.Result>()
                .First(x => x.GroupId == organisationId);

            dynamic viewModel = new ExpandoObject();
            viewModel.Organisation = _organisationViewModelQuery.BuildOrganisation(organisationId);
            viewModel.Activities = _activityViewModelQuery.BuildGroupActivityList(organisationId, activityInput, pagingInput);
            viewModel.IsMember = _userContext.IsUserAuthenticated() ? _userContext.HasGroupPermission<Organisation>(PermissionNames.CreateObservation, organisationId) : false;
            viewModel.UserCountDescription = "Member" + (organisationResult.UserCount == 1 ? string.Empty : "s");
            viewModel.PostCountDescription = "Post" + (organisationResult.PostCount == 1 ? string.Empty : "s");
            viewModel.ShowActivities = true;

            return RestfulResult(
                viewModel,
                "organisations",
                "index");
        }
        public object BuildGroupActivityList(string groupId, ActivitiesQueryInput activityInput, PagingInput pagingInput)
        {
            Check.RequireNotNullOrWhitespace(groupId, "groupId");
            Check.RequireNotNull(activityInput, "activityInput");
            Check.RequireNotNull(pagingInput, "pagingInput");

            var query = _documentSession
                .Query<All_Activities.Result>("All/Activities")
                .Where(x => x.GroupIds.Any(y => y == groupId) && x.Type.In(_activityTypes));

            return Execute(query, activityInput, pagingInput);
        }
        private object Execute(IRavenQueryable<All_Activities.Result> query, ActivitiesQueryInput activityInput, PagingInput pagingInput)
        {
            var profiler = MiniProfiler.Current;

            if (activityInput.NewerThan.HasValue)
            {
                query = query.Where(x => x.CreatedDateTime > activityInput.NewerThan.Value);
            }
            else if (activityInput.OlderThan.HasValue)
            {
                query = query.Where(x => x.CreatedDateTime < activityInput.OlderThan.Value);
            }

            RavenQueryStatistics stats;

            List<dynamic> activities;

            using (profiler.Step("Get top 10 activity list RavenDB query"))
            {
                activities = query
                    .Statistics(out stats)
                    .OrderByDescending(x => x.CreatedDateTime)
                    .Skip(pagingInput.GetSkipIndex())
                    .Take(pagingInput.GetPageSize())
                    .As<dynamic>()
                    .ToList();
            }

            var contributionIds = activities
                .SelectMany(x => new[] { x.ContributionId, x.SubContributionId })
                .Where(x => !string.IsNullOrWhiteSpace(x))
                .Cast<string>()
                .Distinct()
                .ToList();

            List<All_Contributions.Result> contributions;

            using (profiler.Step("Get the 10 contributions RavenDB query"))
            {
                contributions = _documentSession
                    .Query<All_Contributions.Result, All_Contributions>()
                    .AsProjection<All_Contributions.Result>()
                    .Include(x => x.Contribution.User.Id)
                    .Where(x => x.ParentContributionId.In(contributionIds))
                    .ToList();
            }

            User authenticatedUser = null;

            using (profiler.Step("Get authenticated user RavenDB query"))
            {
                if (_userContext.IsUserAuthenticated())
                {
                    authenticatedUser = _documentSession.Load<User>(_userContext.GetAuthenticatedUserId());
                }
            }

            using (profiler.Step("Build activity list view model"))
            {
                return new PagedList<object>()
                {
                    Page = pagingInput.Page,
                    PageSize = pagingInput.PageSize,
                    TotalResultCount = stats.TotalResults,
                    PagedListItems = activities.Select(x =>
                    {
                        dynamic activity = new ExpandoObject();

                        activity.Id = x.Id;
                        activity.Type = x.Type;
                        activity.CreatedDateTime = x.CreatedDateTime;
                        activity.CreatedDateTimeOrder = x.CreatedDateTimeOrder;
                        activity.Description = x.Description;

                        var user = _documentSession.Load<User>((string)x.User.Id);
                        activity.User = _userViewFactory.Make(user, null);
                        activity.Groups = x.Groups;
                        activity.ContributionId = x.ContributionId;
                        activity.SubContributionId = x.SubContributionId;

                        if (x.Type == "sightingadded")
                        {
                            var result = contributions.FirstOrDefault(y => y.ParentContributionId == x.ContributionId && (y.ParentContributionType == "observation" || y.ParentContributionType == "record") && y.SubContributionType == null);

                            if (result == null)
                            {
                                activity.DeletedActivityItem = MakeDeletedActivityItem("sighting");
                            }
                            else
                            {
                                activity.ObservationAdded = new
                                {
                                    Observation = _sightingViewFactory.Make(result.Contribution as Sighting, result.User, result.Groups, authenticatedUser)
                                };
                            }

                            //if (x.RecordAdded != null)
                            //{
                            //    activity.RecordAdded = new
                            //        {
                            //            Record = sighting
                            //        };
                            //}
                        }

                        if (x.Type == "identificationadded")
                        {
                            var result = contributions.FirstOrDefault(y => y.ParentContributionId == x.ContributionId && y.SubContributionId == x.SubContributionId && y.SubContributionType == "identification");

                            if (result == null)
                            {
                                activity.DeletedActivityItem = MakeDeletedActivityItem("identification");
                            }
                            else
                            {
                                activity.IdentificationAdded = new
                                {
                                    Sighting = _sightingViewFactory.Make(result.ParentContribution as Sighting, result.User, result.Groups, authenticatedUser),
                                    Identification = _identificationViewFactory.Make(result.ParentContribution as Sighting, result.Contribution as IdentificationNew, result.User, authenticatedUser)
                                };
                            }
                        }

                        if (x.Type == "sightingnoteadded")
                        {
                            var result = contributions.FirstOrDefault(y => y.ParentContributionId == x.ContributionId && y.SubContributionId == x.SubContributionId && y.SubContributionType == "note");

                            if (result == null)
                            {
                                activity.DeletedActivityItem = MakeDeletedActivityItem("note");
                            }
                            else
                            {
                                activity.SightingNoteAdded = new
                                {
                                    Sighting = _sightingViewFactory.Make(result.ParentContribution as Sighting, result.User, result.Groups, authenticatedUser),
                                    SightingNote = _sightingNoteViewFactory.Make(result.ParentContribution as Sighting, result.Contribution as SightingNote, result.User, authenticatedUser)
                                };
                            }
                        }

                        if (x.Type == "postadded")
                        {
                            var result = contributions.FirstOrDefault(y => y.ParentContributionId == x.ContributionId && y.ParentContributionType == "post");

                            if (result == null)
                            {
                                activity.DeletedActivityItem = MakeDeletedActivityItem("news item");
                            }
                            else
                            {
                                activity.PostAdded = new
                                {
                                    Post = _postViewFactory.Make(result.Contribution as Post, result.User, result.Groups.First(), authenticatedUser)
                                };
                            }
                        }

                        return activity;
                    })
                };
            }
        }
        public object BuildUserActivityList(string userId, ActivitiesQueryInput activityInput, PagingInput pagingInput)
        {
            Check.RequireNotNullOrWhitespace(userId, "userId");
            Check.RequireNotNull(activityInput, "activityInput");
            Check.RequireNotNull(pagingInput, "pagingInput");

            var query = _documentSession
                .Query<All_Activities.Result>("All/Activities")
                .Where(x => x.UserId == userId && x.Type.In(_activityTypes));

            return Execute(query, activityInput, pagingInput);
        }
        public object BuildNotificationActivityList(string userId, ActivitiesQueryInput activityInput, PagingInput pagingInput)
        {
            Check.RequireNotNullOrWhitespace(userId, "userId");
            Check.RequireNotNull(activityInput, "activityInput");
            Check.RequireNotNull(pagingInput, "pagingInput");

            var groupIds = _documentSession
                .Load<User>(userId)
                .Memberships.Select(x => x.Group.Id);

            var query = _documentSession
                .Query<All_Activities.Result>("All/Activities")
                .Where(x => x.GroupIds.Any(y => y.In(groupIds)));

            return Execute(query, activityInput, pagingInput);
        }
Esempio n. 7
0
        public ActionResult PrivateIndex(ActivitiesQueryInput activityInput, PagingInput pagingInput)
        {
            var profiler = MiniProfiler.Current;

            using (profiler.Step("Check if user is authenticated"))
            {
                if (!_userContext.IsUserAuthenticated())
                {
                    return RedirectToAction("PublicIndex");
                }
            }

            dynamic viewModel = new ExpandoObject();

            using (profiler.Step("Build private index view model"))
            {
                var userResult = _documentSession
                    .Query<All_Users.Result, All_Users>()
                    .AsProjection<All_Users.Result>()
                    .Where(x => x.UserId == _userContext.GetAuthenticatedUserId())
                    .First();

                viewModel.User = _userViewModelQuery.BuildUser(_userContext.GetAuthenticatedUserId());
                using (profiler.Step("Build timeline items (ActivityViewModelQuery.BuildHomeActivityList)"))
                {
                    viewModel.Activities = _activityViewModelQuery.BuildHomeActivityList(_userContext.GetAuthenticatedUserId(), activityInput, pagingInput);
                }
                viewModel.ShowUserWelcome = userResult.User.CallsToAction.Contains("user-welcome");
                viewModel.ShowActivities = true;
            }

            return RestfulResult(
                viewModel,
                "home",
                "privateindex");
        }