Beispiel #1
0
        public object Get(GetLatestMedia request)
        {
            var user = _userManager.GetUserById(request.UserId);

            if (!request.IsPlayed.HasValue)
            {
                if (user.Configuration.HidePlayedInLatest)
                {
                    request.IsPlayed = false;
                }
            }

            var dtoOptions = GetDtoOptions(_authContext, request);

            var list = _userViewManager.GetLatestItems(new LatestItemsQuery
            {
                GroupItems       = request.GroupItems,
                IncludeItemTypes = ApiEntryPoint.Split(request.IncludeItemTypes, ',', true),
                IsPlayed         = request.IsPlayed,
                Limit            = request.Limit,
                ParentId         = request.ParentId,
                UserId           = request.UserId,
            }, dtoOptions);

            var dtos = list.Select(i =>
            {
                var item       = i.Item2[0];
                var childCount = 0;

                if (i.Item1 != null && (i.Item2.Count > 1 || i.Item1 is MusicAlbum))
                {
                    item       = i.Item1;
                    childCount = i.Item2.Count;
                }

                var dto = _dtoService.GetBaseItemDto(item, dtoOptions, user);

                dto.ChildCount = childCount;

                return(dto);
            });

            return(ToOptimizedResult(dtos.ToArray()));
        }
Beispiel #2
0
        public object Get(GetLatestMedia request)
        {
            var user = _userManager.GetUserById(request.UserId);

            if (!request.IsPlayed.HasValue)
            {
                if (user.Configuration.HidePlayedInLatest)
                {
                    request.IsPlayed = false;
                }
            }

            var list = _userViewManager.GetLatestItems(new LatestItemsQuery
            {
                GroupItems       = request.GroupItems,
                IncludeItemTypes = (request.IncludeItemTypes ?? string.Empty).Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray(),
                IsPlayed         = request.IsPlayed,
                Limit            = request.Limit,
                ParentId         = request.ParentId,
                UserId           = request.UserId
            });

            var options = GetDtoOptions(request);

            var dtos = list.Select(i =>
            {
                var item       = i.Item2[0];
                var childCount = 0;

                if (i.Item1 != null && i.Item2.Count > 0)
                {
                    item       = i.Item1;
                    childCount = i.Item2.Count;
                }

                var dto = _dtoService.GetBaseItemDto(item, options, user);

                dto.ChildCount = childCount;

                return(dto);
            });

            return(ToOptimizedResult(dtos.ToList()));
        }
        public object Get(GetLatestMedia request)
        {
            var user = _userManager.GetUserById(request.UserId);

            // Avoid implicitly captured closure
            var libraryItems = GetAllLibraryItems(request.UserId, _userManager, _libraryManager, request.ParentId)
                               .OrderByDescending(i => i.DateCreated)
                               .Where(i => i.LocationType != LocationType.Virtual);


            //if (request.IsFolder.HasValue)
            //{
            //var val = request.IsFolder.Value;
            libraryItems = libraryItems.Where(f => f.IsFolder == false);
            //}

            if (!string.IsNullOrEmpty(request.IncludeItemTypes))
            {
                var vals = request.IncludeItemTypes.Split(',');
                libraryItems = libraryItems.Where(f => vals.Contains(f.GetType().Name, StringComparer.OrdinalIgnoreCase));
            }

            var currentUser = user;

            if (request.IsPlayed.HasValue)
            {
                var takeLimit = request.Limit * 20;

                var val = request.IsPlayed.Value;
                libraryItems = libraryItems.Where(f => f.IsPlayed(currentUser) == val)
                               .Take(takeLimit);
            }

            // Avoid implicitly captured closure
            var items = libraryItems
                        .ToList();

            var list = new List <Tuple <BaseItem, List <BaseItem> > >();

            foreach (var item in items)
            {
                // Only grab the index container for media
                var container = item.IsFolder || !request.GroupItems ? null : item.LatestItemsIndexContainer;

                if (container == null)
                {
                    list.Add(new Tuple <BaseItem, List <BaseItem> >(null, new List <BaseItem> {
                        item
                    }));
                }
                else
                {
                    var current = list.FirstOrDefault(i => i.Item1 != null && i.Item1.Id == container.Id);

                    if (current != null)
                    {
                        current.Item2.Add(item);
                    }
                    else
                    {
                        list.Add(new Tuple <BaseItem, List <BaseItem> >(container, new List <BaseItem> {
                            item
                        }));
                    }
                }

                if (list.Count >= request.Limit)
                {
                    break;
                }
            }

            var fields = request.GetItemFields().ToList();

            var dtos = list.Select(i =>
            {
                var item       = i.Item2[0];
                var childCount = 0;

                if (i.Item1 != null && i.Item2.Count > 0)
                {
                    item       = i.Item1;
                    childCount = i.Item2.Count;
                }

                var dto = _dtoService.GetBaseItemDto(item, fields, user);

                dto.ChildCount = childCount;

                return(dto);
            });

            return(ToOptimizedResult(dtos.ToList()));
        }