aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Providers/MetadataResult.cs
blob: 58a0fa2a9a8d2f4bb43ba3d0a8da2444ab27137b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#nullable disable

#pragma warning disable CA1002, CA2227, CS1591

using System;
using System.Collections.Generic;
using System.Globalization;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Entities;

namespace MediaBrowser.Controller.Providers
{
    public class MetadataResult<T>
    {
        // Images aren't always used so the allocation is a waste a lot of the time
        private List<LocalImageInfo> _images;
        private List<(string Url, ImageType Type)> _remoteImages;

        public MetadataResult()
        {
            ResultLanguage = "en";
        }

        public List<LocalImageInfo> Images
        {
            get => _images ??= new List<LocalImageInfo>();
            set => _images = value;
        }

        public List<(string Url, ImageType Type)> RemoteImages
        {
            get => _remoteImages ??= new List<(string Url, ImageType Type)>();
            set => _remoteImages = value;
        }

        public List<UserItemData> UserDataList { get; set; }

        public List<PersonInfo> People { get; set; }

        public bool HasMetadata { get; set; }

        public T Item { get; set; }

        public string ResultLanguage { get; set; }

        public string Provider { get; set; }

        public bool QueriedById { get; set; }

        public void AddPerson(PersonInfo p)
        {
            People ??= new List<PersonInfo>();

            PeopleHelper.AddPerson(People, p);
        }

        /// <summary>
        /// Not only does this clear, but initializes the list so that services can differentiate between a null list and zero people.
        /// </summary>
        public void ResetPeople()
        {
            if (People == null)
            {
                People = new List<PersonInfo>();
            }
            else
            {
                People.Clear();
            }
        }

        public UserItemData GetOrAddUserData(string userId)
        {
            UserDataList ??= new List<UserItemData>();

            UserItemData userData = null;

            foreach (var i in UserDataList)
            {
                if (string.Equals(userId, i.UserId.ToString("N", CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase))
                {
                    userData = i;
                }
            }

            if (userData == null)
            {
                userData = new UserItemData()
                {
                    UserId = new Guid(userId)
                };

                UserDataList.Add(userData);
            }

            return userData;
        }
    }
}