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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use std::ops::Range;

use serde::Deserialize;
use url::Url;

use crate::types::streams::StreamItemState;
use crate::{
    models::{
        addon_details::Selected as AddonDetailsSelected,
        catalog_with_filters::Selected as CatalogWithFiltersSelected,
        catalogs_with_extra::Selected as CatalogsWithExtraSelected,
        installed_addons_with_filters::Selected as InstalledAddonsWithFiltersSelected,
        library_by_type::Selected as LibraryByTypeSelected,
        library_with_filters::Selected as LibraryWithFiltersSelected,
        meta_details::Selected as MetaDetailsSelected,
        player::{Selected as PlayerSelected, VideoParams},
    },
    types::{
        addon::Descriptor,
        api::AuthRequest,
        library::LibraryItemId,
        profile::Settings as ProfileSettings,
        resource::{MetaItemId, MetaItemPreview, Video},
        streaming_server::{
            Settings as StreamingServerSettings,
            StatisticsRequest as StreamingServerStatisticsRequest,
        },
    },
};

#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "action", content = "args")]
pub enum ActionCtx {
    Authenticate(AuthRequest),
    Logout,
    InstallAddon(Descriptor),
    InstallTraktAddon,
    LogoutTrakt,
    UpgradeAddon(Descriptor),
    UninstallAddon(Descriptor),
    UpdateSettings(ProfileSettings),
    AddToLibrary(MetaItemPreview),
    RemoveFromLibrary(String),
    RewindLibraryItem(String),
    LibraryItemMarkAsWatched {
        id: LibraryItemId,
        is_watched: bool,
    },
    /// If boolean is set to `true` it will disable notifications for the LibraryItem.
    ToggleLibraryItemNotifications(LibraryItemId, bool),
    /// Dismiss all Notification for a given [`MetaItemId`].
    DismissNotificationItem(MetaItemId),
    ClearSearchHistory,
    PushUserToAPI,
    PullUserFromAPI,
    PushAddonsToAPI,
    PullAddonsFromAPI,
    SyncLibraryWithAPI,
    /// Pull notifications for all [`LibraryItem`]s that we should pull notifications for.
    ///
    /// **Warning:** The action will **always** trigger requests to the addons.
    ///
    /// See `LibraryItem::should_pull_notifications()`
    ///
    /// [`LibraryItem`]: crate::types::library::LibraryItem
    PullNotifications,
    /// Make request to api to get events modal and notification
    GetEvents,
    /// Dismiss an event by id, either a Modal or Notification
    DismissEvent(String),
}

#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "action", content = "args")]
pub enum ActionCatalogWithFilters {
    LoadNextPage,
}

#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "action", content = "args")]
pub enum ActionCatalogsWithExtra {
    LoadRange(Range<usize>),
    LoadNextPage(usize),
}

#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "action", content = "args")]
pub enum ActionLibraryByType {
    LoadNextPage(usize),
}

#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "action", content = "args")]
pub enum ActionLibraryWithFilters {
    LoadNextPage,
}

#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "action", content = "args")]
pub enum ActionMetaDetails {
    /// Marks the [`LibraryItem`] as watched.
    ///
    /// Applicable when you have single-video (e.g. a movie) and multi-video (e.g. a movie series) item.
    ///
    /// [`LibraryItem`]: crate::types::library::LibraryItem
    MarkAsWatched(bool),
    /// Marks the given [`Video`] of the [`LibraryItem`] as watched.
    ///
    /// Applicable only when you have a multi-video (e.g. movie series) item.
    ///
    /// [`LibraryItem`]: crate::types::library::LibraryItem
    MarkVideoAsWatched(Video, bool),
}

#[derive(Clone, Deserialize, Debug)]
#[serde(untagged)]
pub enum CreateTorrentArgs {
    File(Vec<u8>),
    Magnet(Url),
}

#[derive(Clone, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PlayOnDeviceArgs {
    pub device: String,
    pub source: String,
    pub time: Option<u64>,
}

#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "action", content = "args")]
pub enum ActionStreamingServer {
    Reload,
    UpdateSettings(StreamingServerSettings),
    CreateTorrent(CreateTorrentArgs),
    GetStatistics(StreamingServerStatisticsRequest),
    PlayOnDevice(PlayOnDeviceArgs),
}

#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "action", content = "args")]
pub enum ActionLink {
    ReadData,
}

#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "action", content = "args")]
pub enum ActionPlayer {
    #[serde(rename_all = "camelCase")]
    VideoParamsChanged {
        video_params: Option<VideoParams>,
    },
    StreamStateChanged {
        state: StreamItemState,
    },
    TimeChanged {
        time: u64,
        duration: u64,
        device: String,
    },
    PausedChanged {
        paused: bool,
    },
    /// User has clicked on the next video button.
    NextVideo,
    /// Video player has ended.
    ///
    /// 2 scenarios are possible:
    /// - We've watched a movie to the last second
    /// - We've watched a movie series to the last second
    Ended,
}

#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "model", content = "args")]
pub enum ActionLoad {
    AddonDetails(AddonDetailsSelected),
    CatalogWithFilters(Option<CatalogWithFiltersSelected>),
    CatalogsWithExtra(CatalogsWithExtraSelected),
    DataExport,
    InstalledAddonsWithFilters(InstalledAddonsWithFiltersSelected),
    LibraryWithFilters(LibraryWithFiltersSelected),
    LibraryByType(LibraryByTypeSelected),
    /// Loads the data required for Local search
    LocalSearch,
    MetaDetails(MetaDetailsSelected),
    Player(Box<PlayerSelected>),
    Link,
}

#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "action", content = "args")]
pub enum ActionSearch {
    /// Request for Search queries
    #[serde(rename_all = "camelCase")]
    Search {
        search_query: String,
        max_results: usize,
    },
}

/// Action messages
///
/// Those messages are meant to be dispatched only by the users of the
/// `stremio-core` crate and handled by the `stremio-core` crate.
#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "action", content = "args")]
pub enum Action {
    Ctx(ActionCtx),
    Link(ActionLink),
    CatalogWithFilters(ActionCatalogWithFilters),
    CatalogsWithExtra(ActionCatalogsWithExtra),
    LibraryByType(ActionLibraryByType),
    LibraryWithFilters(ActionLibraryWithFilters),
    MetaDetails(ActionMetaDetails),
    StreamingServer(ActionStreamingServer),
    Player(ActionPlayer),
    Load(ActionLoad),
    Search(ActionSearch),
    Unload,
}