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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use url::Url;

use crate::models::common::ResourceLoadable;
use crate::models::ctx::CtxError;
use crate::models::link::LinkError;
use crate::models::local_search::Searchable;
use crate::models::streaming_server::PlaybackDevice;
use crate::runtime::EnvError;
use crate::types::addon::{Descriptor, Manifest, ResourceRequest, ResourceResponse};
use crate::types::api::{
    APIRequest, AuthRequest, DataExportResponse, DatastoreRequest, GetModalResponse,
    GetNotificationResponse, LinkCodeResponse, LinkDataResponse, SeekLogRequest, SkipGapsRequest,
    SkipGapsResponse, SuccessResponse,
};
use crate::types::library::{LibraryBucket, LibraryItem, LibraryItemId};
use crate::types::profile::{Auth, AuthKey, Profile, User};
use crate::types::streaming_server::{
    DeviceInfo, GetHTTPSResponse, NetworkInfo, SettingsResponse, Statistics, StatisticsRequest,
};
use crate::types::streams::StreamItemState;
use crate::types::{
    resource::{MetaItem, Stream},
    torrent::InfoHash,
};

pub type CtxStorageResponse = (
    Option<Profile>,
    Option<LibraryBucket>,
    Option<LibraryBucket>,
);

#[derive(Debug)]
pub struct CtxAuthResponse {
    pub auth: Auth,
    pub addons_result: Result<Vec<Descriptor>, CtxError>,
    pub library_items_result: Result<Vec<LibraryItem>, CtxError>,
}

pub type LibraryPlanResponse = (Vec<String>, Vec<String>);

//
// Those messages are meant to be dispatched and handled only inside stremio-core crate
//
#[derive(Debug)]
pub enum Internal {
    /// Result for authenticate to API.
    CtxAuthResult(AuthRequest, Result<CtxAuthResponse, CtxError>),
    /// Result for pull addons from API.
    AddonsAPIResult(APIRequest, Result<Vec<Descriptor>, CtxError>),
    /// Result for pull user from API.
    UserAPIResult(APIRequest, Result<User, CtxError>),
    /// Result for library sync plan with API.
    LibrarySyncPlanResult(DatastoreRequest, Result<LibraryPlanResponse, CtxError>),
    /// Result for pull library items from API.
    LibraryPullResult(DatastoreRequest, Result<Vec<LibraryItem>, CtxError>),
    /// Dispatched when expired session is detected
    Logout,
    /// Internal event dispatched on user action or login
    /// to install the addon if it's not present
    InstallTraktAddon,
    /// Dispatched when addons needs to be installed.
    InstallAddon(Descriptor),
    /// Dispatched when addons needs to be uninstalled.
    UninstallAddon(Descriptor),
    UninstallTraktAddon,
    /// Dispatched when a new stream is loaded into the Player.
    StreamLoaded {
        stream: Stream,
        stream_request: Option<ResourceRequest>,
        meta_item: ResourceLoadable<MetaItem>,
    },
    /// Dispatched when stream item's state has changed
    StreamStateChanged {
        state: StreamItemState,
        stream_request: Option<ResourceRequest>,
        meta_request: Option<ResourceRequest>,
    },
    /// Dispatched when requesting search on catalogs.
    CatalogsWithExtraSearch {
        query: String,
    },
    /// Dispatched when library item needs to be updated in the memory, storage and API.
    UpdateLibraryItem(LibraryItem),
    /// Dispatched when some of auth, addons or settings changed.
    ProfileChanged,
    /// Dispatched when library changes with a flag if its already persisted.
    LibraryChanged(bool),
    /// Dispatched when streams bucket changes with a flag if its already persisted.
    StreamsChanged(bool),
    /// Search history has changed.
    SearchHistoryChanged,
    /// User notifications have changed
    NotificationsChanged,
    /// Pulling of notifications triggered either by the user (with an action) or
    /// internally in core.
    PullNotifications,
    /// Dismiss all Notifications for a given [`MetaItemId`].
    ///
    /// [`MetaItemId`]: crate::types::resource::MetaItemId
    DismissNotificationItem(LibraryItemId),
    /// Result for loading link code.
    LinkCodeResult(Result<LinkCodeResponse, LinkError>),
    /// Result for loading link data.
    LinkDataResult(String, Result<LinkDataResponse, LinkError>),
    /// Result for loading streaming server settings.
    StreamingServerSettingsResult(Url, Result<SettingsResponse, EnvError>),
    /// Result for loading streaming server base url.
    StreamingServerBaseURLResult(Url, Result<Url, EnvError>),
    // Result for loading streaming server playback devices.
    StreamingServerPlaybackDevicesResult(Url, Result<Vec<PlaybackDevice>, EnvError>),
    // Result for network info.
    StreamingServerNetworkInfoResult(Url, Result<NetworkInfo, EnvError>),
    // Result for device info.
    StreamingServerDeviceInfoResult(Url, Result<DeviceInfo, EnvError>),
    /// Result for updating streaming server settings.
    StreamingServerUpdateSettingsResult(Url, Result<(), EnvError>),
    /// Result for creating a torrent.
    StreamingServerCreateTorrentResult(InfoHash, Result<(), EnvError>),
    /// Result for playing on device.
    StreamingServerPlayOnDeviceResult(String, Result<(), EnvError>),
    // Result for get https endpoint request
    StreamingServerGetHTTPSResult(Url, Result<GetHTTPSResponse, EnvError>),
    /// Result for streaming server statistics.
    ///
    /// Server will return None (or `null`) in response for [`Statistics`]`,
    /// when stream has been fully loaded up to 100%
    StreamingServerStatisticsResult(
        (Url, StatisticsRequest),
        Result<Option<Statistics>, EnvError>,
    ),
    /// Result for fetching resource from addons.
    ResourceRequestResult(ResourceRequest, Box<Result<ResourceResponse, EnvError>>),
    /// Result for fetching manifest from addon.
    ManifestRequestResult(Url, Result<Manifest, EnvError>),
    /// TODO: write some obvious comment about what it is
    NotificationsRequestResult(ResourceRequest, Box<Result<ResourceResponse, EnvError>>),
    /// Result for requesting a `dataExport` of user data.
    DataExportResult(AuthKey, Result<DataExportResponse, CtxError>),
    /// Result for submitting SeekLogs request for a played stream.
    ///
    /// Applicable only to movie series and torrents.
    SeekLogsResult(SeekLogRequest, Result<SuccessResponse, CtxError>),
    /// Retrieve the skip gaps for skipping intro and outro.
    SkipGapsResult(SkipGapsRequest, Result<SkipGapsResponse, CtxError>),
    /// The result of querying the data for LocalSearch
    LoadLocalSearchResult(Url, Result<Vec<Searchable>, EnvError>),
    /// Result for getModal request
    GetModalResult(APIRequest, Result<Option<GetModalResponse>, CtxError>),
    /// Result for getNotification request
    GetNotificationResult(
        APIRequest,
        Result<Option<GetNotificationResponse>, CtxError>,
    ),
    /// When dismissed events changed
    DismissedEventsChanged,
}