moosync_edk/
lib.rs

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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Moosync
// Copyright (C) 2024, 2025  Moosync <support@moosync.app>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

pub use extism_pdk::{config, error, http, info, log, warn, HttpRequest, HttpResponse};
use extism_pdk::{plugin_fn, FnResult, Json};
use handler::{
    get_accounts, get_album_songs, get_artist_songs, get_lyrics, get_playback_details,
    get_playlist_content, get_playlist_context_menu, get_playlist_from_url, get_playlists,
    get_provider_scopes, get_recommendations, get_song_context_menu, get_song_from_id,
    get_song_from_url, handle_custom_request, oauth_callback, on_context_menu_action,
    on_player_state_changed, on_playlist_added, on_playlist_removed, on_preferences_changed,
    on_queue_changed, on_seeked, on_song_added, on_song_changed, on_song_removed,
    on_volume_changed, perform_account_login, scrobble, search,
};
use serde_json::Value;

pub use types::{
    entities::*, errors::*, extensions::*, songs::*, ui::extensions::*,
    ui::player_details::PlayerState,
};

pub mod api;
pub mod handler;

extern "C" {
    fn init();
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn entry() -> FnResult<()> {
    unsafe {
        init();
    }
    Ok(())
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn get_provider_scopes_wrapper() -> FnResult<Json<Vec<ExtensionProviderScope>>> {
    let ret = get_provider_scopes()?;
    Ok(Json(ret))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn get_playlists_wrapper() -> FnResult<Json<PlaylistReturnType>> {
    let ret = get_playlists()?;
    Ok(Json(PlaylistReturnType { playlists: ret }))
}

#[tracing::instrument(level = "debug", skip(id))]
#[plugin_fn]
pub fn get_playlist_content_wrapper(
    Json((id, token)): Json<(String, Option<String>)>,
) -> FnResult<Json<SongsWithPageTokenReturnType>> {
    let ret = get_playlist_content(id, token)?;
    Ok(Json(SongsWithPageTokenReturnType {
        songs: ret,
        next_page_token: None,
    }))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn get_playlist_from_url_wrapper(url: String) -> FnResult<Json<PlaylistAndSongsReturnType>> {
    let ret = get_playlist_from_url(url)?;
    Ok(Json(PlaylistAndSongsReturnType {
        playlist: ret,
        songs: None,
    }))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn get_playback_details_wrapper(
    Json(song): Json<Song>,
) -> FnResult<Json<PlaybackDetailsReturnType>> {
    let ret = get_playback_details(song)?;
    Ok(Json(ret))
}

#[tracing::instrument(level = "debug", skip(term))]
#[plugin_fn]
pub fn search_wrapper(term: String) -> FnResult<Json<SearchReturnType>> {
    let ret = search(term)?;
    Ok(Json(SearchReturnType {
        songs: ret.songs,
        playlists: ret.playlists,
        artists: ret.artists,
        albums: ret.albums,
    }))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn get_recommendations_wrapper() -> FnResult<Json<RecommendationsReturnType>> {
    let ret = get_recommendations()?;
    Ok(Json(RecommendationsReturnType { songs: ret }))
}

#[tracing::instrument(level = "debug", skip(url))]
#[plugin_fn]
pub fn get_song_from_url_wrapper(url: String) -> FnResult<Json<SongReturnType>> {
    let ret = get_song_from_url(url)?;
    Ok(Json(SongReturnType { song: ret }))
}

#[tracing::instrument(level = "debug", skip(url))]
#[plugin_fn]
pub fn handle_custom_request_wrapper(url: String) -> FnResult<Json<CustomRequestReturnType>> {
    let ret = handle_custom_request(url)?;
    Ok(Json(ret))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn get_artist_songs_wrapper(
    Json((artist, token)): Json<(QueryableArtist, Option<String>)>,
) -> FnResult<Json<SongsWithPageTokenReturnType>> {
    let ret = get_artist_songs(artist, token)?;
    Ok(Json(SongsWithPageTokenReturnType {
        songs: ret,
        next_page_token: None,
    }))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn get_album_songs_wrapper(
    Json((album, token)): Json<(QueryableAlbum, Option<String>)>,
) -> FnResult<Json<SongsWithPageTokenReturnType>> {
    let ret = get_album_songs(album, token)?;
    Ok(Json(SongsWithPageTokenReturnType {
        songs: ret,
        next_page_token: None,
    }))
}

#[tracing::instrument(level = "debug", skip(id))]
#[plugin_fn]
pub fn get_song_from_id_wrapper(id: String) -> FnResult<Json<SongReturnType>> {
    let ret = get_song_from_id(id)?;
    Ok(Json(SongReturnType { song: ret }))
}

// PlayerEvents trait wrappers
#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn on_queue_changed_wrapper(Json(queue): Json<Value>) -> FnResult<Json<()>> {
    on_queue_changed(queue)?;
    Ok(Json(()))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn on_volume_changed_wrapper() -> FnResult<Json<()>> {
    on_volume_changed()?;
    Ok(Json(()))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn on_player_state_changed_wrapper() -> FnResult<Json<()>> {
    on_player_state_changed()?;
    Ok(Json(()))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn on_song_changed_wrapper() -> FnResult<Json<()>> {
    on_song_changed()?;
    Ok(Json(()))
}

#[tracing::instrument(level = "debug", skip(time))]
#[plugin_fn]
pub fn on_seeked_wrapper(Json(time): Json<f64>) -> FnResult<Json<()>> {
    on_seeked(time)?;
    Ok(Json(()))
}

// PreferenceEvents trait wrapper
#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn on_preferences_changed_wrapper(Json(args): Json<PreferenceArgs>) -> FnResult<Json<()>> {
    on_preferences_changed(args)?;
    Ok(Json(()))
}

// DatabaseEvents trait wrappers
#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn on_song_added_wrapper(Json(song): Json<Song>) -> FnResult<Json<()>> {
    on_song_added(song)?;
    Ok(Json(()))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn on_song_removed_wrapper(Json(song): Json<Song>) -> FnResult<Json<()>> {
    on_song_removed(song)?;
    Ok(Json(()))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn on_playlist_added_wrapper(Json(playlist): Json<QueryablePlaylist>) -> FnResult<Json<()>> {
    on_playlist_added(playlist)?;
    Ok(Json(()))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn on_playlist_removed_wrapper(Json(playlist): Json<QueryablePlaylist>) -> FnResult<Json<()>> {
    on_playlist_removed(playlist)?;
    Ok(Json(()))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn get_accounts_wrapper() -> FnResult<Json<Vec<ExtensionAccountDetail>>> {
    let ret = get_accounts()?;
    Ok(Json(ret))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn perform_account_login_wrapper(Json(args): Json<AccountLoginArgs>) -> FnResult<Json<String>> {
    let ret = perform_account_login(args)?;
    Ok(Json(ret))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn scrobble_wrapper(Json(args): Json<Song>) -> FnResult<Json<()>> {
    scrobble(args)?;
    Ok(Json(()))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn oauth_callback_wrapper(Json(args): Json<String>) -> FnResult<Json<()>> {
    oauth_callback(args)?;
    Ok(Json(()))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn get_song_context_menu_wrapper(
    Json(songs): Json<Vec<Song>>,
) -> FnResult<Json<Vec<ContextMenuReturnType>>> {
    Ok(Json(get_song_context_menu(songs)?))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn get_playlist_context_menu_wrapper(
    Json(playlist): Json<QueryablePlaylist>,
) -> FnResult<Json<Vec<ContextMenuReturnType>>> {
    Ok(Json(get_playlist_context_menu(playlist)?))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn on_context_menu_action_wrapper(action: String) -> FnResult<Json<()>> {
    Ok(Json(on_context_menu_action(action)?))
}

#[tracing::instrument(level = "debug", skip())]
#[plugin_fn]
pub fn get_lyrics_wrapper(Json(song): Json<Song>) -> FnResult<Json<String>> {
    Ok(Json(get_lyrics(song)?))
}