jellyfin-server/Emby.Dlna/ContentDirectory/ControlHandler.cs

1273 lines
53 KiB
C#
Raw Normal View History

#nullable disable
using System;
2016-10-29 22:22:20 +00:00
using System.Collections.Generic;
using System.Globalization;
2020-01-28 12:19:25 +00:00
using System.IO;
2016-10-29 22:22:20 +00:00
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml;
2019-01-13 19:16:19 +00:00
using Emby.Dlna.Didl;
using Emby.Dlna.Service;
2020-05-20 17:07:53 +00:00
using Jellyfin.Data.Entities;
2020-07-01 01:44:41 +00:00
using Jellyfin.Data.Enums;
2019-01-13 19:16:19 +00:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Drawing;
2017-05-21 07:25:49 +00:00
using MediaBrowser.Controller.Dto;
2019-01-13 19:16:19 +00:00
using MediaBrowser.Controller.Entities;
2016-12-20 07:31:30 +00:00
using MediaBrowser.Controller.Entities.Audio;
2019-01-13 19:16:19 +00:00
using MediaBrowser.Controller.Library;
2016-10-29 22:22:20 +00:00
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.TV;
2019-01-13 19:16:19 +00:00
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Dto;
2019-01-13 19:16:19 +00:00
using MediaBrowser.Model.Entities;
2016-10-29 22:22:20 +00:00
using MediaBrowser.Model.Globalization;
2019-01-13 19:16:19 +00:00
using MediaBrowser.Model.Querying;
using Microsoft.Extensions.Logging;
2020-05-20 17:07:53 +00:00
using Genre = MediaBrowser.Controller.Entities.Genre;
2016-10-29 22:22:20 +00:00
2016-10-29 22:34:54 +00:00
namespace Emby.Dlna.ContentDirectory
2016-10-29 22:22:20 +00:00
{
2020-09-13 13:31:12 +00:00
/// <summary>
/// Defines the <see cref="ControlHandler" />.
/// </summary>
2016-10-29 22:22:20 +00:00
public class ControlHandler : BaseControlHandler
{
2020-08-20 19:04:57 +00:00
private const string NsDc = "http://purl.org/dc/elements/1.1/";
private const string NsDidl = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/";
private const string NsDlna = "urn:schemas-dlna-org:metadata-1-0/";
private const string NsUpnp = "urn:schemas-upnp-org:metadata-1-0/upnp/";
2016-10-29 22:22:20 +00:00
private readonly ILibraryManager _libraryManager;
private readonly IUserDataManager _userDataManager;
2020-05-20 17:07:53 +00:00
private readonly User _user;
2016-10-29 22:22:20 +00:00
private readonly IUserViewManager _userViewManager;
private readonly ITVSeriesManager _tvSeriesManager;
2016-10-29 22:22:20 +00:00
private readonly int _systemUpdateId;
private readonly DidlBuilder _didlBuilder;
private readonly DeviceProfile _profile;
2020-09-13 13:31:12 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="ControlHandler"/> class.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/> for use with the <see cref="ControlHandler"/> instance.</param>
/// <param name="libraryManager">The <see cref="ILibraryManager"/> for use with the <see cref="ControlHandler"/> instance.</param>
/// <param name="profile">The <see cref="DeviceProfile"/> for use with the <see cref="ControlHandler"/> instance.</param>
/// <param name="serverAddress">The server address to use in this instance> for use with the <see cref="ControlHandler"/> instance.</param>
/// <param name="accessToken">The <see cref="string"/> for use with the <see cref="ControlHandler"/> instance.</param>
/// <param name="imageProcessor">The <see cref="IImageProcessor"/> for use with the <see cref="ControlHandler"/> instance.</param>
/// <param name="userDataManager">The <see cref="IUserDataManager"/> for use with the <see cref="ControlHandler"/> instance.</param>
/// <param name="user">The <see cref="User"/> for use with the <see cref="ControlHandler"/> instance.</param>
/// <param name="systemUpdateId">The system id for use with the <see cref="ControlHandler"/> instance.</param>
/// <param name="config">The <see cref="IServerConfigurationManager"/> for use with the <see cref="ControlHandler"/> instance.</param>
/// <param name="localization">The <see cref="ILocalizationManager"/> for use with the <see cref="ControlHandler"/> instance.</param>
/// <param name="mediaSourceManager">The <see cref="IMediaSourceManager"/> for use with the <see cref="ControlHandler"/> instance.</param>
/// <param name="userViewManager">The <see cref="IUserViewManager"/> for use with the <see cref="ControlHandler"/> instance.</param>
/// <param name="mediaEncoder">The <see cref="IMediaEncoder"/> for use with the <see cref="ControlHandler"/> instance.</param>
/// <param name="tvSeriesManager">The <see cref="ITVSeriesManager"/> for use with the <see cref="ControlHandler"/> instance.</param>
public ControlHandler(
ILogger logger,
ILibraryManager libraryManager,
DeviceProfile profile,
string serverAddress,
string accessToken,
IImageProcessor imageProcessor,
IUserDataManager userDataManager,
2020-05-20 17:07:53 +00:00
User user,
2020-01-28 20:54:51 +00:00
int systemUpdateId,
IServerConfigurationManager config,
ILocalizationManager localization,
IMediaSourceManager mediaSourceManager,
IUserViewManager userViewManager,
IMediaEncoder mediaEncoder,
ITVSeriesManager tvSeriesManager)
: base(config, logger)
2016-10-29 22:22:20 +00:00
{
_libraryManager = libraryManager;
_userDataManager = userDataManager;
_user = user;
_systemUpdateId = systemUpdateId;
_userViewManager = userViewManager;
_tvSeriesManager = tvSeriesManager;
2016-10-29 22:22:20 +00:00
_profile = profile;
2020-04-02 14:49:58 +00:00
_didlBuilder = new DidlBuilder(
profile,
user,
imageProcessor,
serverAddress,
accessToken,
userDataManager,
localization,
mediaSourceManager,
Logger,
mediaEncoder,
libraryManager);
2016-10-29 22:22:20 +00:00
}
/// <inheritdoc />
2021-04-01 17:16:00 +00:00
protected override void WriteResult(string methodName, IReadOnlyDictionary<string, string> methodParams, XmlWriter xmlWriter)
2016-10-29 22:22:20 +00:00
{
2020-09-13 13:31:12 +00:00
if (xmlWriter == null)
{
throw new ArgumentNullException(nameof(xmlWriter));
}
if (methodParams == null)
{
throw new ArgumentNullException(nameof(methodParams));
}
const string DeviceId = "test";
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "GetSearchCapabilities", StringComparison.OrdinalIgnoreCase))
{
HandleGetSearchCapabilities(xmlWriter);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "GetSortCapabilities", StringComparison.OrdinalIgnoreCase))
{
HandleGetSortCapabilities(xmlWriter);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "GetSortExtensionCapabilities", StringComparison.OrdinalIgnoreCase))
{
HandleGetSortExtensionCapabilities(xmlWriter);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "GetSystemUpdateID", StringComparison.OrdinalIgnoreCase))
{
HandleGetSystemUpdateID(xmlWriter);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "Browse", StringComparison.OrdinalIgnoreCase))
{
HandleBrowse(xmlWriter, methodParams, DeviceId);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "X_GetFeatureList", StringComparison.OrdinalIgnoreCase))
{
HandleXGetFeatureList(xmlWriter);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "GetFeatureList", StringComparison.OrdinalIgnoreCase))
{
HandleGetFeatureList(xmlWriter);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "X_SetBookmark", StringComparison.OrdinalIgnoreCase))
{
HandleXSetBookmark(methodParams);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "Search", StringComparison.OrdinalIgnoreCase))
{
HandleSearch(xmlWriter, methodParams, DeviceId);
return;
}
2016-10-29 22:22:20 +00:00
2016-12-14 20:58:55 +00:00
if (string.Equals(methodName, "X_BrowseByLetter", StringComparison.OrdinalIgnoreCase))
{
HandleXBrowseByLetter(xmlWriter, methodParams, DeviceId);
return;
}
2016-12-14 20:58:55 +00:00
2016-10-29 22:22:20 +00:00
throw new ResourceNotFoundException("Unexpected control request name: " + methodName);
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Adds a "XSetBookmark" element to the xml document.
/// </summary>
2021-04-01 17:16:00 +00:00
/// <param name="sparams">The method parameters.</param>
private void HandleXSetBookmark(IReadOnlyDictionary<string, string> sparams)
2016-10-29 22:22:20 +00:00
{
var id = sparams["ObjectID"];
2020-04-02 14:49:58 +00:00
var serverItem = GetItemFromObjectId(id);
2016-10-29 22:22:20 +00:00
var item = serverItem.Item;
2020-01-26 16:37:47 +00:00
var newbookmark = int.Parse(sparams["PosSecond"], CultureInfo.InvariantCulture);
2016-10-29 22:22:20 +00:00
var userdata = _userDataManager.GetUserData(_user, item);
2016-10-29 22:22:20 +00:00
userdata.PlaybackPositionTicks = TimeSpan.FromSeconds(newbookmark).Ticks;
2020-08-20 19:04:57 +00:00
_userDataManager.SaveUserData(
_user,
item,
userdata,
UserDataSaveReason.TogglePlayed,
2016-10-29 22:22:20 +00:00
CancellationToken.None);
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Adds the "SearchCaps" element to the xml document.
/// </summary>
/// <param name="xmlWriter">The <see cref="XmlWriter"/>.</param>
private static void HandleGetSearchCapabilities(XmlWriter xmlWriter)
2016-10-29 22:22:20 +00:00
{
xmlWriter.WriteElementString(
"SearchCaps",
"res@resolution,res@size,res@duration,dc:title,dc:creator,upnp:actor,upnp:artist,upnp:genre,upnp:album,dc:date,upnp:class,@id,@refID,@protocolInfo,upnp:author,dc:description,pv:avKeywords");
2016-10-29 22:22:20 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Adds the "SortCaps" element to the xml document.
/// </summary>
/// <param name="xmlWriter">The <see cref="XmlWriter"/>.</param>
private static void HandleGetSortCapabilities(XmlWriter xmlWriter)
2016-10-29 22:22:20 +00:00
{
xmlWriter.WriteElementString(
"SortCaps",
"res@duration,res@size,res@bitrate,dc:date,dc:title,dc:size,upnp:album,upnp:artist,upnp:albumArtist,upnp:episodeNumber,upnp:genre,upnp:originalTrackNumber,upnp:rating");
2016-10-29 22:22:20 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Adds the "SortExtensionCaps" element to the xml document.
/// </summary>
/// <param name="xmlWriter">The <see cref="XmlWriter"/>.</param>
private static void HandleGetSortExtensionCapabilities(XmlWriter xmlWriter)
2016-10-29 22:22:20 +00:00
{
xmlWriter.WriteElementString(
"SortExtensionCaps",
"res@duration,res@size,res@bitrate,dc:date,dc:title,dc:size,upnp:album,upnp:artist,upnp:albumArtist,upnp:episodeNumber,upnp:genre,upnp:originalTrackNumber,upnp:rating");
2016-10-29 22:22:20 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Adds the "Id" element to the xml document.
/// </summary>
/// <param name="xmlWriter">The <see cref="XmlWriter"/>.</param>
private void HandleGetSystemUpdateID(XmlWriter xmlWriter)
2016-10-29 22:22:20 +00:00
{
xmlWriter.WriteElementString("Id", _systemUpdateId.ToString(CultureInfo.InvariantCulture));
2016-10-29 22:22:20 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Adds the "FeatureList" element to the xml document.
/// </summary>
/// <param name="xmlWriter">The <see cref="XmlWriter"/>.</param>
private static void HandleGetFeatureList(XmlWriter xmlWriter)
2016-10-29 22:22:20 +00:00
{
xmlWriter.WriteElementString("FeatureList", WriteFeatureListXml());
2016-10-29 22:22:20 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Adds the "FeatureList" element to the xml document.
/// </summary>
/// <param name="xmlWriter">The <see cref="XmlWriter"/>.</param>
private static void HandleXGetFeatureList(XmlWriter xmlWriter)
=> HandleGetFeatureList(xmlWriter);
2016-10-29 22:22:20 +00:00
2020-09-13 13:31:12 +00:00
/// <summary>
/// Builds a static feature list.
/// </summary>
/// <returns>The xml feature list.</returns>
private static string WriteFeatureListXml()
2016-10-29 22:22:20 +00:00
{
2021-07-05 22:01:33 +00:00
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<Features xmlns=\"urn:schemas-upnp-org:av:avs\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:schemas-upnp-org:av:avs http://www.upnp.org/schemas/av/avs.xsd\">"
+ "<Feature name=\"samsung.com_BASICVIEW\" version=\"1\">"
2021-11-08 08:24:33 +00:00
+ "<container id=\"0\" type=\"object.item.imageItem\"/>"
+ "<container id=\"0\" type=\"object.item.audioItem\"/>"
+ "<container id=\"0\" type=\"object.item.videoItem\"/>"
2021-07-05 22:01:33 +00:00
+ "</Feature>"
+ "</Features>";
2016-10-29 22:22:20 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Builds the "Browse" xml response.
/// </summary>
/// <param name="xmlWriter">The <see cref="XmlWriter"/>.</param>
2021-04-01 17:16:00 +00:00
/// <param name="sparams">The method parameters.</param>
2020-09-13 13:31:12 +00:00
/// <param name="deviceId">The device Id to use.</param>
2021-04-01 17:16:00 +00:00
private void HandleBrowse(XmlWriter xmlWriter, IReadOnlyDictionary<string, string> sparams, string deviceId)
2016-10-29 22:22:20 +00:00
{
var id = sparams["ObjectID"];
var flag = sparams["BrowseFlag"];
2021-04-01 17:16:00 +00:00
var filter = new Filter(sparams.GetValueOrDefault("Filter", "*"));
var sortCriteria = new SortCriteria(sparams.GetValueOrDefault("SortCriteria", string.Empty));
2016-10-29 22:22:20 +00:00
var provided = 0;
// Default to null instead of 0
// Upnp inspector sends 0 as requestedCount when it wants everything
int? requestedCount = null;
int? start = 0;
if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out var requestedVal) && requestedVal > 0)
2016-10-29 22:22:20 +00:00
{
requestedCount = requestedVal;
}
if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out var startVal) && startVal > 0)
2016-10-29 22:22:20 +00:00
{
start = startVal;
}
int totalCount;
2020-01-28 12:19:25 +00:00
2021-11-09 18:40:36 +00:00
var settings = new XmlWriterSettings
{
Encoding = Encoding.UTF8,
CloseOutput = false,
OmitXmlDeclaration = true,
ConformanceLevel = ConformanceLevel.Fragment
};
2020-01-28 12:19:25 +00:00
using (StringWriter builder = new StringWriterWithEncoding(Encoding.UTF8))
2021-11-09 18:40:36 +00:00
using (var writer = XmlWriter.Create(builder, settings))
2016-10-29 22:22:20 +00:00
{
2021-11-09 18:40:36 +00:00
writer.WriteStartElement(string.Empty, "DIDL-Lite", NsDidl);
2016-10-29 22:22:20 +00:00
2021-11-09 18:40:36 +00:00
writer.WriteAttributeString("xmlns", "dc", null, NsDc);
writer.WriteAttributeString("xmlns", "dlna", null, NsDlna);
writer.WriteAttributeString("xmlns", "upnp", null, NsUpnp);
2016-10-29 22:22:20 +00:00
2021-11-09 18:40:36 +00:00
DidlBuilder.WriteXmlRootAttributes(_profile, writer);
2016-10-29 22:22:20 +00:00
2021-11-09 18:40:36 +00:00
var serverItem = GetItemFromObjectId(id);
var item = serverItem.Item;
2020-01-28 12:19:25 +00:00
2021-11-09 18:40:36 +00:00
if (string.Equals(flag, "BrowseMetadata", StringComparison.Ordinal))
{
totalCount = 1;
2016-10-29 22:22:20 +00:00
2021-11-09 18:40:36 +00:00
if (item.IsDisplayedAsFolder || serverItem.StubType.HasValue)
2020-01-28 12:19:25 +00:00
{
2021-11-09 18:40:36 +00:00
var childrenResult = GetUserItems(item, serverItem.StubType, _user, sortCriteria, start, requestedCount);
2020-01-29 16:30:27 +00:00
2021-11-09 18:40:36 +00:00
_didlBuilder.WriteFolderElement(writer, item, serverItem.StubType, null, childrenResult.TotalRecordCount, filter, id);
2020-01-28 12:19:25 +00:00
}
2020-01-29 16:30:27 +00:00
else
{
2021-11-09 18:40:36 +00:00
_didlBuilder.WriteItemElement(writer, item, _user, null, null, deviceId, filter);
}
provided++;
}
else
{
var childrenResult = GetUserItems(item, serverItem.StubType, _user, sortCriteria, start, requestedCount);
totalCount = childrenResult.TotalRecordCount;
provided = childrenResult.Items.Count;
2020-01-29 16:30:27 +00:00
2021-11-09 18:40:36 +00:00
foreach (var i in childrenResult.Items)
{
var childItem = i.Item;
var displayStubType = i.StubType;
if (childItem.IsDisplayedAsFolder || displayStubType.HasValue)
{
var childCount = GetUserItems(childItem, displayStubType, _user, sortCriteria, null, 0)
.TotalRecordCount;
2020-01-28 12:19:25 +00:00
2021-11-09 18:40:36 +00:00
_didlBuilder.WriteFolderElement(writer, childItem, displayStubType, item, childCount, filter);
}
else
2020-01-29 16:30:27 +00:00
{
2021-11-09 18:40:36 +00:00
_didlBuilder.WriteItemElement(writer, childItem, _user, item, serverItem.StubType, deviceId, filter);
2020-01-29 16:30:27 +00:00
}
}
}
2020-01-28 12:19:25 +00:00
2021-11-09 18:40:36 +00:00
writer.WriteFullEndElement();
writer.Flush();
2020-01-28 12:19:25 +00:00
xmlWriter.WriteElementString("Result", builder.ToString());
}
2016-10-29 22:22:20 +00:00
2020-01-26 16:37:47 +00:00
xmlWriter.WriteElementString("NumberReturned", provided.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteElementString("TotalMatches", totalCount.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteElementString("UpdateID", _systemUpdateId.ToString(CultureInfo.InvariantCulture));
2016-10-29 22:22:20 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Builds the response to the "X_BrowseByLetter request.
/// </summary>
/// <param name="xmlWriter">The <see cref="XmlWriter"/>.</param>
2021-04-01 17:16:00 +00:00
/// <param name="sparams">The method parameters.</param>
2020-09-13 13:31:12 +00:00
/// <param name="deviceId">The device id.</param>
2021-04-01 17:16:00 +00:00
private void HandleXBrowseByLetter(XmlWriter xmlWriter, IReadOnlyDictionary<string, string> sparams, string deviceId)
2016-12-14 20:58:55 +00:00
{
// TODO: Implement this method
HandleSearch(xmlWriter, sparams, deviceId);
2016-12-14 20:58:55 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Builds a response to the "Search" request.
/// </summary>
/// <param name="xmlWriter">The xmlWriter<see cref="XmlWriter"/>.</param>
2021-04-01 17:16:00 +00:00
/// <param name="sparams">The method parameters.</param>
2020-09-13 13:31:12 +00:00
/// <param name="deviceId">The deviceId<see cref="string"/>.</param>
2021-04-01 17:16:00 +00:00
private void HandleSearch(XmlWriter xmlWriter, IReadOnlyDictionary<string, string> sparams, string deviceId)
2016-10-29 22:22:20 +00:00
{
2021-04-01 17:16:00 +00:00
var searchCriteria = new SearchCriteria(sparams.GetValueOrDefault("SearchCriteria", string.Empty));
var sortCriteria = new SortCriteria(sparams.GetValueOrDefault("SortCriteria", string.Empty));
var filter = new Filter(sparams.GetValueOrDefault("Filter", "*"));
2016-10-29 22:22:20 +00:00
// sort example: dc:title, dc:date
// Default to null instead of 0
// Upnp inspector sends 0 as requestedCount when it wants everything
int? requestedCount = null;
int? start = 0;
if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out var requestedVal) && requestedVal > 0)
2016-10-29 22:22:20 +00:00
{
requestedCount = requestedVal;
}
if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out var startVal) && startVal > 0)
2016-10-29 22:22:20 +00:00
{
start = startVal;
}
2020-01-28 12:19:25 +00:00
QueryResult<BaseItem> childrenResult;
var settings = new XmlWriterSettings
{
Encoding = Encoding.UTF8,
CloseOutput = false,
OmitXmlDeclaration = true,
ConformanceLevel = ConformanceLevel.Fragment
};
2016-10-29 22:22:20 +00:00
2020-01-28 12:19:25 +00:00
using (StringWriter builder = new StringWriterWithEncoding(Encoding.UTF8))
using (var writer = XmlWriter.Create(builder, settings))
2020-01-28 12:19:25 +00:00
{
writer.WriteStartElement(string.Empty, "DIDL-Lite", NsDidl);
writer.WriteAttributeString("xmlns", "dc", null, NsDc);
writer.WriteAttributeString("xmlns", "dlna", null, NsDlna);
writer.WriteAttributeString("xmlns", "upnp", null, NsUpnp);
2016-10-29 22:22:20 +00:00
DidlBuilder.WriteXmlRootAttributes(_profile, writer);
2016-10-29 22:22:20 +00:00
var serverItem = GetItemFromObjectId(sparams["ContainerID"]);
2018-09-12 17:26:21 +00:00
var item = serverItem.Item;
2016-11-04 08:31:05 +00:00
childrenResult = GetChildrenSorted(item, _user, searchCriteria, sortCriteria, start, requestedCount);
foreach (var i in childrenResult.Items)
{
if (i.IsDisplayedAsFolder)
2020-01-28 12:19:25 +00:00
{
var childCount = GetChildrenSorted(i, _user, searchCriteria, sortCriteria, null, 0)
.TotalRecordCount;
2020-01-29 16:30:27 +00:00
_didlBuilder.WriteFolderElement(writer, i, null, item, childCount, filter);
}
else
{
_didlBuilder.WriteItemElement(writer, i, _user, item, serverItem.StubType, deviceId, filter);
2020-01-28 12:19:25 +00:00
}
2020-01-29 16:30:27 +00:00
}
2020-01-28 12:19:25 +00:00
writer.WriteFullEndElement();
writer.Flush();
2020-01-28 12:19:25 +00:00
xmlWriter.WriteElementString("Result", builder.ToString());
}
2016-10-29 22:22:20 +00:00
2020-01-26 16:37:47 +00:00
xmlWriter.WriteElementString("NumberReturned", childrenResult.Items.Count.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteElementString("TotalMatches", childrenResult.TotalRecordCount.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteElementString("UpdateID", _systemUpdateId.ToString(CultureInfo.InvariantCulture));
2016-10-29 22:22:20 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the child items meeting the criteria.
/// </summary>
/// <param name="item">The <see cref="BaseItem"/>.</param>
/// <param name="user">The <see cref="User"/>.</param>
/// <param name="search">The <see cref="SearchCriteria"/>.</param>
/// <param name="sort">The <see cref="SortCriteria"/>.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="limit">The maximum number to return.</param>
/// <returns>The <see cref="QueryResult{BaseItem}"/>.</returns>
private static QueryResult<BaseItem> GetChildrenSorted(BaseItem item, User user, SearchCriteria search, SortCriteria sort, int? startIndex, int? limit)
2016-10-29 22:22:20 +00:00
{
var folder = (Folder)item;
string[] mediaTypes = Array.Empty<string>();
2016-10-29 22:22:20 +00:00
bool? isFolder = null;
2021-11-09 18:44:21 +00:00
switch (search.SearchType)
2016-10-29 22:22:20 +00:00
{
2021-11-09 18:44:21 +00:00
case SearchType.Audio:
mediaTypes = new[] { MediaType.Audio };
isFolder = false;
break;
case SearchType.Video:
mediaTypes = new[] { MediaType.Video };
isFolder = false;
break;
case SearchType.Image:
mediaTypes = new[] { MediaType.Photo };
isFolder = false;
break;
case SearchType.Playlist:
case SearchType.MusicAlbum:
isFolder = true;
break;
2016-10-29 22:22:20 +00:00
}
return folder.GetItems(new InternalItemsQuery
{
Limit = limit,
StartIndex = startIndex,
2021-11-09 18:33:15 +00:00
OrderBy = GetOrderBy(sort, folder.IsPreSorted),
2016-10-29 22:22:20 +00:00
User = user,
Recursive = true,
IsMissing = false,
2021-12-12 02:31:30 +00:00
ExcludeItemTypes = new[] { BaseItemKind.Book },
2016-10-29 22:22:20 +00:00
IsFolder = isFolder,
MediaTypes = mediaTypes,
2017-05-21 07:25:49 +00:00
DtoOptions = GetDtoOptions()
2016-10-29 22:22:20 +00:00
});
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns a new DtoOptions object.
/// </summary>
/// <returns>The <see cref="DtoOptions"/>.</returns>
private static DtoOptions GetDtoOptions()
2017-05-21 07:25:49 +00:00
{
return new DtoOptions(true);
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the User items meeting the criteria.
/// </summary>
/// <param name="item">The <see cref="BaseItem"/>.</param>
/// <param name="stubType">The <see cref="StubType"/>.</param>
/// <param name="user">The <see cref="User"/>.</param>
/// <param name="sort">The <see cref="SortCriteria"/>.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="limit">The maximum number to return.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetUserItems(BaseItem item, StubType? stubType, User user, SortCriteria sort, int? startIndex, int? limit)
2016-10-29 22:22:20 +00:00
{
switch (item)
{
case MusicGenre:
return GetMusicGenreItems(item, user, sort, startIndex, limit);
case MusicArtist:
return GetMusicArtistItems(item, user, sort, startIndex, limit);
case Genre:
return GetGenreItems(item, user, sort, startIndex, limit);
2016-12-20 19:59:25 +00:00
}
if (stubType != StubType.Folder && item is IHasCollectionType collectionFolder)
2016-12-20 19:59:25 +00:00
{
var collectionType = collectionFolder.CollectionType;
if (string.Equals(CollectionType.Music, collectionType, StringComparison.OrdinalIgnoreCase))
2017-11-20 17:38:36 +00:00
{
return GetMusicFolders(item, user, stubType, sort, startIndex, limit);
}
if (string.Equals(CollectionType.Movies, collectionType, StringComparison.OrdinalIgnoreCase))
2017-11-20 17:38:36 +00:00
{
return GetMovieFolders(item, user, stubType, sort, startIndex, limit);
}
if (string.Equals(CollectionType.TvShows, collectionType, StringComparison.OrdinalIgnoreCase))
2017-11-20 17:38:36 +00:00
{
return GetTvFolders(item, user, stubType, sort, startIndex, limit);
}
if (string.Equals(CollectionType.Folders, collectionType, StringComparison.OrdinalIgnoreCase))
2017-11-20 17:38:36 +00:00
{
2020-04-02 14:49:58 +00:00
return GetFolders(user, startIndex, limit);
2017-11-20 17:38:36 +00:00
}
if (string.Equals(CollectionType.LiveTv, collectionType, StringComparison.OrdinalIgnoreCase))
2018-09-12 17:26:21 +00:00
{
2020-04-02 14:49:58 +00:00
return GetLiveTvChannels(user, sort, startIndex, limit);
2018-09-12 17:26:21 +00:00
}
}
2017-07-22 23:00:48 +00:00
if (stubType.HasValue && stubType.Value != StubType.Folder)
2016-10-29 22:22:20 +00:00
{
// TODO should this be doing something?
return new QueryResult<ServerItem>();
2016-10-29 22:22:20 +00:00
}
2016-12-20 07:31:30 +00:00
var folder = (Folder)item;
2018-09-12 17:26:21 +00:00
var query = new InternalItemsQuery(user)
2016-10-29 22:22:20 +00:00
{
Limit = limit,
StartIndex = startIndex,
2017-08-13 02:09:07 +00:00
IsVirtualItem = false,
2021-12-12 02:31:30 +00:00
ExcludeItemTypes = new[] { BaseItemKind.Book },
2017-05-21 07:25:49 +00:00
IsPlaceHolder = false,
DtoOptions = GetDtoOptions(),
OrderBy = GetOrderBy(sort, folder.IsPreSorted)
2016-12-20 07:31:30 +00:00
};
2017-05-26 06:48:54 +00:00
var queryResult = folder.GetItems(query);
2016-10-29 22:22:20 +00:00
2016-12-20 07:31:30 +00:00
return ToResult(queryResult);
}
2016-10-29 22:22:20 +00:00
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the Live Tv Channels meeting the criteria.
/// </summary>
/// <param name="user">The <see cref="User"/>.</param>
/// <param name="sort">The <see cref="SortCriteria"/>.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="limit">The maximum number to return.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetLiveTvChannels(User user, SortCriteria sort, int? startIndex, int? limit)
2018-09-12 17:26:21 +00:00
{
var query = new InternalItemsQuery(user)
{
StartIndex = startIndex,
Limit = limit,
2021-12-12 02:31:30 +00:00
IncludeItemTypes = new[] { BaseItemKind.LiveTvChannel },
OrderBy = GetOrderBy(sort, false)
2018-09-12 17:26:21 +00:00
};
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the music folders meeting the criteria.
/// </summary>
/// <param name="item">The <see cref="BaseItem"/>.</param>
/// <param name="user">The <see cref="User"/>.</param>
/// <param name="stubType">The <see cref="StubType"/>.</param>
/// <param name="sort">The <see cref="SortCriteria"/>.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="limit">The maximum number to return.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMusicFolders(BaseItem item, User user, StubType? stubType, SortCriteria sort, int? startIndex, int? limit)
2017-07-22 23:00:48 +00:00
{
var query = new InternalItemsQuery(user)
{
StartIndex = startIndex,
Limit = limit,
OrderBy = GetOrderBy(sort, false)
2017-07-22 23:00:48 +00:00
};
switch (stubType)
{
case StubType.Latest:
2021-12-12 02:31:30 +00:00
return GetLatest(item, query, BaseItemKind.Audio);
case StubType.Playlists:
return GetMusicPlaylists(query);
case StubType.Albums:
2021-12-12 02:31:30 +00:00
return GetChildrenOfItem(item, query, BaseItemKind.MusicAlbum);
case StubType.Artists:
return GetMusicArtists(item, query);
case StubType.AlbumArtists:
return GetMusicAlbumArtists(item, query);
case StubType.FavoriteAlbums:
2021-12-12 02:31:30 +00:00
return GetChildrenOfItem(item, query, BaseItemKind.MusicAlbum, true);
case StubType.FavoriteArtists:
return GetFavoriteArtists(item, query);
case StubType.FavoriteSongs:
2021-12-12 02:31:30 +00:00
return GetChildrenOfItem(item, query, BaseItemKind.Audio, true);
case StubType.Songs:
2021-12-12 02:31:30 +00:00
return GetChildrenOfItem(item, query, BaseItemKind.Audio);
case StubType.Genres:
return GetMusicGenres(item, query);
2017-07-22 23:00:48 +00:00
}
2021-11-09 14:57:39 +00:00
var serverItems = new ServerItem[]
2017-07-22 23:00:48 +00:00
{
2021-12-24 17:28:27 +00:00
new(item, StubType.Latest),
new(item, StubType.Playlists),
new(item, StubType.Albums),
new(item, StubType.AlbumArtists),
new(item, StubType.Artists),
new(item, StubType.Songs),
new(item, StubType.Genres),
new(item, StubType.FavoriteArtists),
new(item, StubType.FavoriteAlbums),
new(item, StubType.FavoriteSongs)
2020-09-13 13:31:12 +00:00
};
2017-07-22 23:00:48 +00:00
2021-11-09 14:57:39 +00:00
if (limit < serverItems.Length)
2021-11-08 08:24:33 +00:00
{
2021-11-09 14:57:39 +00:00
serverItems = serverItems[..limit.Value];
2021-11-08 08:24:33 +00:00
}
2017-07-22 23:00:48 +00:00
return new QueryResult<ServerItem>
{
2021-11-09 14:57:39 +00:00
Items = serverItems,
TotalRecordCount = serverItems.Length
2017-07-22 23:00:48 +00:00
};
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the movie folders meeting the criteria.
/// </summary>
/// <param name="item">The <see cref="BaseItem"/>.</param>
/// <param name="user">The <see cref="User"/>.</param>
/// <param name="stubType">The <see cref="StubType"/>.</param>
/// <param name="sort">The <see cref="SortCriteria"/>.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="limit">The maximum number to return.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMovieFolders(BaseItem item, User user, StubType? stubType, SortCriteria sort, int? startIndex, int? limit)
{
var query = new InternalItemsQuery(user)
{
StartIndex = startIndex,
Limit = limit,
OrderBy = GetOrderBy(sort, false)
};
switch (stubType)
{
case StubType.ContinueWatching:
return GetMovieContinueWatching(item, query);
case StubType.Latest:
2021-12-12 02:31:30 +00:00
return GetLatest(item, query, BaseItemKind.Movie);
case StubType.Movies:
2021-12-12 02:31:30 +00:00
return GetChildrenOfItem(item, query, BaseItemKind.Movie);
case StubType.Collections:
return GetMovieCollections(query);
case StubType.Favorites:
2021-12-12 02:31:30 +00:00
return GetChildrenOfItem(item, query, BaseItemKind.Movie, true);
case StubType.Genres:
return GetGenres(item, query);
}
var array = new ServerItem[]
{
2021-12-24 17:28:27 +00:00
new(item, StubType.ContinueWatching),
new(item, StubType.Latest),
new(item, StubType.Movies),
new(item, StubType.Collections),
new(item, StubType.Favorites),
new(item, StubType.Genres)
2020-04-02 14:49:58 +00:00
};
if (limit < array.Length)
2021-11-08 08:24:33 +00:00
{
array = array[..limit.Value];
2021-11-08 08:24:33 +00:00
}
return new QueryResult<ServerItem>
{
2020-04-02 14:49:58 +00:00
Items = array,
TotalRecordCount = array.Length
};
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the folders meeting the criteria.
/// </summary>
/// <param name="user">The <see cref="User"/>.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="limit">The maximum number to return.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetFolders(User user, int? startIndex, int? limit)
2017-11-20 17:38:36 +00:00
{
var folders = _libraryManager.GetUserRootFolder().GetChildren(user, true);
var totalRecordCount = folders.Count;
// Handle paging
var items = folders
2017-11-20 17:38:36 +00:00
.OrderBy(i => i.SortName)
.Skip(startIndex ?? 0)
.Take(limit ?? int.MaxValue)
.Select(i => new ServerItem(i, StubType.Folder))
2017-11-20 17:38:36 +00:00
.ToArray();
return new QueryResult<ServerItem>
{
Items = items,
TotalRecordCount = totalRecordCount
};
2017-11-20 17:38:36 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the TV folders meeting the criteria.
/// </summary>
/// <param name="item">The <see cref="BaseItem"/>.</param>
/// <param name="user">The <see cref="User"/>.</param>
/// <param name="stubType">The <see cref="StubType"/>.</param>
/// <param name="sort">The <see cref="SortCriteria"/>.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="limit">The maximum number to return.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetTvFolders(BaseItem item, User user, StubType? stubType, SortCriteria sort, int? startIndex, int? limit)
{
var query = new InternalItemsQuery(user)
{
StartIndex = startIndex,
Limit = limit,
OrderBy = GetOrderBy(sort, false)
};
switch (stubType)
{
case StubType.ContinueWatching:
return GetMovieContinueWatching(item, query);
case StubType.NextUp:
return GetNextUp(item, query);
case StubType.Latest:
2021-12-12 02:31:30 +00:00
return GetLatest(item, query, BaseItemKind.Episode);
case StubType.Series:
2021-12-12 02:31:30 +00:00
return GetChildrenOfItem(item, query, BaseItemKind.Series);
case StubType.FavoriteSeries:
2021-12-12 02:31:30 +00:00
return GetChildrenOfItem(item, query, BaseItemKind.Series, true);
case StubType.FavoriteEpisodes:
2021-12-12 02:31:30 +00:00
return GetChildrenOfItem(item, query, BaseItemKind.Episode, true);
case StubType.Genres:
return GetGenres(item, query);
}
2021-11-09 14:57:39 +00:00
var serverItems = new ServerItem[]
{
2021-12-24 17:28:27 +00:00
new(item, StubType.ContinueWatching),
new(item, StubType.NextUp),
new(item, StubType.Latest),
new(item, StubType.Series),
new(item, StubType.FavoriteSeries),
new(item, StubType.FavoriteEpisodes),
new(item, StubType.Genres)
2020-09-13 13:31:12 +00:00
};
2021-11-09 14:57:39 +00:00
if (limit < serverItems.Length)
2021-11-08 08:24:33 +00:00
{
2021-11-09 14:57:39 +00:00
serverItems = serverItems[..limit.Value];
2021-11-08 08:24:33 +00:00
}
return new QueryResult<ServerItem>
{
2021-11-09 14:57:39 +00:00
Items = serverItems,
TotalRecordCount = serverItems.Length
};
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the Movies that are part watched that meet the criteria.
/// </summary>
/// <param name="parent">The <see cref="BaseItem"/>.</param>
/// <param name="query">The <see cref="InternalItemsQuery"/>.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
private QueryResult<ServerItem> GetMovieContinueWatching(BaseItem parent, InternalItemsQuery query)
{
query.Recursive = true;
query.Parent = parent;
2019-10-20 14:08:40 +00:00
query.OrderBy = new[]
{
2019-10-20 14:08:40 +00:00
(ItemSortBy.DatePlayed, SortOrder.Descending),
(ItemSortBy.SortName, SortOrder.Ascending)
};
query.IsResumable = true;
query.Limit ??= 10;
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the Movie collections meeting the criteria.
/// </summary>
/// <param name="query">The see cref="InternalItemsQuery"/>.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
private QueryResult<ServerItem> GetMovieCollections(InternalItemsQuery query)
{
query.Recursive = true;
2021-12-12 02:31:30 +00:00
query.IncludeItemTypes = new[] { BaseItemKind.BoxSet };
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the children that meet the criteria.
2020-09-13 13:31:12 +00:00
/// </summary>
/// <param name="parent">The <see cref="BaseItem"/>.</param>
/// <param name="query">The <see cref="InternalItemsQuery"/>.</param>
/// <param name="itemType">The item type.</param>
/// <param name="isFavorite">A value indicating whether to only fetch favorite items.</param>
2020-09-13 13:31:12 +00:00
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
2021-12-12 02:31:30 +00:00
private QueryResult<ServerItem> GetChildrenOfItem(BaseItem parent, InternalItemsQuery query, BaseItemKind itemType, bool isFavorite = false)
2017-07-22 23:00:48 +00:00
{
query.Recursive = true;
query.Parent = parent;
query.IsFavorite = isFavorite;
query.IncludeItemTypes = new[] { itemType };
2017-07-22 23:00:48 +00:00
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the genres meeting the criteria.
/// The GetGenres.
/// </summary>
/// <param name="parent">The <see cref="BaseItem"/>.</param>
/// <param name="query">The <see cref="InternalItemsQuery"/>.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
private QueryResult<ServerItem> GetGenres(BaseItem parent, InternalItemsQuery query)
{
// Don't sort
query.OrderBy = Array.Empty<(string, SortOrder)>();
query.AncestorIds = new[] { parent.Id };
var genresResult = _libraryManager.GetGenres(query);
return ToResult(genresResult);
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the music genres meeting the criteria.
/// </summary>
/// <param name="parent">The <see cref="BaseItem"/>.</param>
/// <param name="query">The <see cref="InternalItemsQuery"/>.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
private QueryResult<ServerItem> GetMusicGenres(BaseItem parent, InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
// Don't sort
query.OrderBy = Array.Empty<(string, SortOrder)>();
query.AncestorIds = new[] { parent.Id };
var genresResult = _libraryManager.GetMusicGenres(query);
2017-07-22 23:00:48 +00:00
return ToResult(genresResult);
2017-07-22 23:00:48 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the music albums by artist that meet the criteria.
/// </summary>
/// <param name="parent">The <see cref="BaseItem"/>.</param>
/// <param name="query">The <see cref="InternalItemsQuery"/>.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
private QueryResult<ServerItem> GetMusicAlbumArtists(BaseItem parent, InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
// Don't sort
query.OrderBy = Array.Empty<(string, SortOrder)>();
query.AncestorIds = new[] { parent.Id };
var artists = _libraryManager.GetAlbumArtists(query);
2017-07-22 23:00:48 +00:00
return ToResult(artists);
2017-07-22 23:00:48 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the music artists meeting the criteria.
/// </summary>
/// <param name="parent">The <see cref="BaseItem"/>.</param>
/// <param name="query">The <see cref="InternalItemsQuery"/>.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
private QueryResult<ServerItem> GetMusicArtists(BaseItem parent, InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
// Don't sort
query.OrderBy = Array.Empty<(string, SortOrder)>();
query.AncestorIds = new[] { parent.Id };
var artists = _libraryManager.GetArtists(query);
return ToResult(artists);
2017-07-22 23:00:48 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the artists tagged as favourite that meet the criteria.
/// </summary>
/// <param name="parent">The <see cref="BaseItem"/>.</param>
/// <param name="query">The <see cref="InternalItemsQuery"/>.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
private QueryResult<ServerItem> GetFavoriteArtists(BaseItem parent, InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
// Don't sort
query.OrderBy = Array.Empty<(string, SortOrder)>();
query.AncestorIds = new[] { parent.Id };
query.IsFavorite = true;
var artists = _libraryManager.GetArtists(query);
return ToResult(artists);
2017-07-22 23:00:48 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the music playlists meeting the criteria.
/// </summary>
/// <param name="query">The query<see cref="InternalItemsQuery"/>.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
private QueryResult<ServerItem> GetMusicPlaylists(InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
query.Parent = null;
2021-12-12 02:31:30 +00:00
query.IncludeItemTypes = new[] { BaseItemKind.Playlist };
2017-07-22 23:00:48 +00:00
query.Recursive = true;
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the next up item meeting the criteria.
/// </summary>
/// <param name="parent">The <see cref="BaseItem"/>.</param>
/// <param name="query">The <see cref="InternalItemsQuery"/>.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
2020-04-02 14:49:58 +00:00
private QueryResult<ServerItem> GetNextUp(BaseItem parent, InternalItemsQuery query)
{
2019-10-20 14:08:40 +00:00
query.OrderBy = Array.Empty<(string, SortOrder)>();
2020-08-20 19:04:57 +00:00
var result = _tvSeriesManager.GetNextUp(
new NextUpQuery
{
Limit = query.Limit,
StartIndex = query.StartIndex,
2021-11-09 18:19:48 +00:00
// User cannot be null here as the caller has set it
UserId = query.User!.Id
2020-08-20 19:04:57 +00:00
},
new[] { parent },
query.DtoOptions);
return ToResult(result);
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the latest items of [itemType] meeting the criteria.
2020-09-13 13:31:12 +00:00
/// </summary>
/// <param name="parent">The <see cref="BaseItem"/>.</param>
/// <param name="query">The <see cref="InternalItemsQuery"/>.</param>
/// <param name="itemType">The item type.</param>
2020-09-13 13:31:12 +00:00
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
2021-12-12 02:31:30 +00:00
private QueryResult<ServerItem> GetLatest(BaseItem parent, InternalItemsQuery query, BaseItemKind itemType)
{
2019-10-20 14:08:40 +00:00
query.OrderBy = Array.Empty<(string, SortOrder)>();
2020-05-13 02:10:35 +00:00
var items = _userViewManager.GetLatestItems(
new LatestItemsQuery
2020-08-20 19:04:57 +00:00
{
2021-11-09 18:19:48 +00:00
// User cannot be null here as the caller has set it
UserId = query.User!.Id,
2021-11-08 08:24:33 +00:00
Limit = query.Limit ?? 50,
IncludeItemTypes = new[] { itemType },
2020-08-20 19:04:57 +00:00
ParentId = parent?.Id ?? Guid.Empty,
GroupItems = true
},
query.DtoOptions).Select(i => i.Item1 ?? i.Item2.FirstOrDefault()).Where(i => i != null).ToArray();
return ToResult(items);
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns music artist items that meet the criteria.
/// </summary>
/// <param name="item">The <see cref="BaseItem"/>.</param>
/// <param name="user">The <see cref="User"/>.</param>
/// <param name="sort">The <see cref="SortCriteria"/>.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="limit">The maximum number to return.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
private QueryResult<ServerItem> GetMusicArtistItems(BaseItem item, User user, SortCriteria sort, int? startIndex, int? limit)
2016-12-20 19:59:25 +00:00
{
var query = new InternalItemsQuery(user)
{
Recursive = true,
2018-09-12 17:26:21 +00:00
ArtistIds = new[] { item.Id },
2021-12-12 02:31:30 +00:00
IncludeItemTypes = new[] { BaseItemKind.MusicAlbum },
2016-12-20 19:59:25 +00:00
Limit = limit,
2017-05-21 07:25:49 +00:00
StartIndex = startIndex,
DtoOptions = GetDtoOptions(),
OrderBy = GetOrderBy(sort, false)
2016-12-20 19:59:25 +00:00
};
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the genre items meeting the criteria.
/// </summary>
/// <param name="item">The <see cref="BaseItem"/>.</param>
/// <param name="user">The <see cref="User"/>.</param>
/// <param name="sort">The <see cref="SortCriteria"/>.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="limit">The maximum number to return.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
private QueryResult<ServerItem> GetGenreItems(BaseItem item, User user, SortCriteria sort, int? startIndex, int? limit)
2017-08-19 19:43:35 +00:00
{
var query = new InternalItemsQuery(user)
{
Recursive = true,
2018-09-12 17:26:21 +00:00
GenreIds = new[] { item.Id },
2020-05-13 02:10:35 +00:00
IncludeItemTypes = new[]
{
2021-12-12 02:31:30 +00:00
BaseItemKind.Movie,
BaseItemKind.Series
2020-05-13 02:10:35 +00:00
},
2017-08-19 19:43:35 +00:00
Limit = limit,
StartIndex = startIndex,
DtoOptions = GetDtoOptions(),
OrderBy = GetOrderBy(sort, false)
2017-08-19 19:43:35 +00:00
};
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Returns the music genre items meeting the criteria.
/// </summary>
/// <param name="item">The <see cref="BaseItem"/>.</param>
/// <param name="user">The <see cref="User"/>.</param>
/// <param name="sort">The <see cref="SortCriteria"/>.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="limit">The maximum number to return.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
private QueryResult<ServerItem> GetMusicGenreItems(BaseItem item, User user, SortCriteria sort, int? startIndex, int? limit)
2016-12-20 07:31:30 +00:00
{
var query = new InternalItemsQuery(user)
{
Recursive = true,
2018-09-12 17:26:21 +00:00
GenreIds = new[] { item.Id },
2021-12-12 02:31:30 +00:00
IncludeItemTypes = new[] { BaseItemKind.MusicAlbum },
2016-12-20 07:31:30 +00:00
Limit = limit,
2017-05-21 07:25:49 +00:00
StartIndex = startIndex,
DtoOptions = GetDtoOptions(),
OrderBy = GetOrderBy(sort, false)
2016-12-20 07:31:30 +00:00
};
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Converts <see cref="IReadOnlyCollection{BaseItem}"/> into a <see cref="QueryResult{ServerItem}"/>.
2020-09-13 13:31:12 +00:00
/// </summary>
/// <param name="result">An array of <see cref="BaseItem"/>.</param>
/// <returns>A <see cref="QueryResult{ServerItem}"/>.</returns>
private static QueryResult<ServerItem> ToResult(IReadOnlyCollection<BaseItem> result)
2017-07-22 23:00:48 +00:00
{
var serverItems = result
.Select(i => new ServerItem(i, null))
2018-12-28 15:48:26 +00:00
.ToArray();
2017-07-22 23:00:48 +00:00
return new QueryResult<ServerItem>
{
TotalRecordCount = result.Count,
2017-07-22 23:00:48 +00:00
Items = serverItems
};
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Converts a <see cref="QueryResult{BaseItem}"/> to a <see cref="QueryResult{ServerItem}"/>.
/// </summary>
/// <param name="result">A <see cref="QueryResult{BaseItem}"/>.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
private static QueryResult<ServerItem> ToResult(QueryResult<BaseItem> result)
2016-12-20 07:31:30 +00:00
{
2021-11-09 21:45:34 +00:00
var length = result.Items.Count;
var serverItems = new ServerItem[length];
for (var i = 0; i < length; i++)
2021-11-09 18:22:16 +00:00
{
serverItems[i] = new ServerItem(result.Items[i], null);
}
return new QueryResult<ServerItem>
{
TotalRecordCount = result.TotalRecordCount,
Items = serverItems
};
}
/// <summary>
/// Converts a query result to a <see cref="QueryResult{ServerItem}"/>.
/// </summary>
/// <param name="result">A <see cref="QueryResult{BaseItem}"/>.</param>
/// <returns>The <see cref="QueryResult{ServerItem}"/>.</returns>
2022-01-07 10:48:59 +00:00
private static QueryResult<ServerItem> ToResult(QueryResult<(BaseItem Item, ItemCounts ItemCounts)> result)
{
2021-11-09 21:45:34 +00:00
var length = result.Items.Count;
var serverItems = new ServerItem[length];
for (var i = 0; i < length; i++)
2021-11-09 18:22:16 +00:00
{
2022-01-07 10:48:59 +00:00
serverItems[i] = new ServerItem(result.Items[i].Item, null);
2021-11-09 18:22:16 +00:00
}
2016-10-29 22:22:20 +00:00
return new QueryResult<ServerItem>
{
2016-12-20 07:31:30 +00:00
TotalRecordCount = result.TotalRecordCount,
2016-10-29 22:22:20 +00:00
Items = serverItems
};
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Gets the sorting method on a query.
2020-09-13 13:31:12 +00:00
/// </summary>
/// <param name="sort">The <see cref="SortCriteria"/>.</param>
/// <param name="isPreSorted">True if pre-sorted.</param>
2022-01-07 10:48:59 +00:00
private static (string SortName, SortOrder SortOrder)[] GetOrderBy(SortCriteria sort, bool isPreSorted)
2016-12-20 07:31:30 +00:00
{
return isPreSorted ? Array.Empty<(string, SortOrder)>() : new[] { (ItemSortBy.SortName, sort.SortOrder) };
2016-10-29 22:22:20 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
2020-11-18 13:23:45 +00:00
/// Retrieves the ServerItem id.
2020-09-13 13:31:12 +00:00
/// </summary>
/// <param name="id">The id<see cref="string"/>.</param>
/// <returns>The <see cref="ServerItem"/>.</returns>
2020-04-02 14:49:58 +00:00
private ServerItem GetItemFromObjectId(string id)
2016-10-29 22:22:20 +00:00
{
return DidlBuilder.IsIdRoot(id)
? new ServerItem(_libraryManager.GetUserRootFolder(), null)
2020-04-02 14:49:58 +00:00
: ParseItemId(id);
2016-10-29 22:22:20 +00:00
}
2020-09-13 13:31:12 +00:00
/// <summary>
/// Parses the item id into a <see cref="ServerItem"/>.
/// </summary>
/// <param name="id">The <see cref="string"/>.</param>
/// <returns>The corresponding <see cref="ServerItem"/>.</returns>
2020-04-02 14:49:58 +00:00
private ServerItem ParseItemId(string id)
2016-10-29 22:22:20 +00:00
{
StubType? stubType = null;
// After using PlayTo, MediaMonkey sends a request to the server trying to get item info
const string ParamsSrch = "Params=";
var paramsIndex = id.IndexOf(ParamsSrch, StringComparison.OrdinalIgnoreCase);
2016-10-29 22:22:20 +00:00
if (paramsIndex != -1)
{
2021-11-09 18:44:21 +00:00
id = id[(paramsIndex + ParamsSrch.Length)..];
2016-10-29 22:22:20 +00:00
var parts = id.Split(';');
id = parts[23];
}
2021-11-09 14:57:39 +00:00
var dividerIndex = id.IndexOf('_', StringComparison.Ordinal);
if (dividerIndex != -1 && Enum.TryParse<StubType>(id.AsSpan(0, dividerIndex), true, out var parsedStubType))
2016-10-29 22:22:20 +00:00
{
id = id[(dividerIndex + 1)..];
stubType = parsedStubType;
2017-07-22 23:00:48 +00:00
}
2016-10-29 22:22:20 +00:00
if (Guid.TryParse(id, out var itemId))
2016-10-29 22:22:20 +00:00
{
var item = _libraryManager.GetItemById(itemId);
return new ServerItem(item, stubType);
2016-10-29 22:22:20 +00:00
}
2020-08-31 20:20:19 +00:00
Logger.LogError("Error parsing item Id: {Id}. Returning user root folder.", id);
2016-10-29 22:22:20 +00:00
return new ServerItem(_libraryManager.GetUserRootFolder(), null);
2016-10-29 22:22:20 +00:00
}
}
}