Skip to content

Commit

Permalink
refactor: rename api model
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonflylee committed Jul 4, 2024
1 parent aa2256b commit 1ea4b97
Show file tree
Hide file tree
Showing 22 changed files with 100 additions and 98 deletions.
6 changes: 3 additions & 3 deletions app/include/activity/player_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class VideoView;

class PlayerView : public brls::Box {
public:
PlayerView(const jellyfin::MediaItem& item);
PlayerView(const jellyfin::Item& item);
~PlayerView();

void setSeries(const std::string& seriesId);
Expand All @@ -33,8 +33,8 @@ class PlayerView : public brls::Box {
/// @brief DirectPlay, Transcode
std::string playMethod;
std::string playSessionId;
jellyfin::MediaSource stream;
std::vector<jellyfin::MediaEpisode> episodes;
jellyfin::Source stream;
std::vector<jellyfin::Episode> episodes;

MPVEvent::Subscription eventSubscribeID;
brls::VoidEvent::Subscription exitSubscribeID;
Expand Down
6 changes: 3 additions & 3 deletions app/include/api/jellyfin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace jellyfin {

using OnError = std::function<void(const std::string&)>;

template <typename Then, typename... Args>
inline void getJSON(Then then, OnError error, std::string_view fmt, Args&&... args) {
template <typename Result, typename... Args>
inline void getJSON(const std::function<void(Result)>& then, OnError error, std::string_view fmt, Args&&... args) {
std::string url = fmt::format(fmt::runtime(fmt), std::forward<Args>(args)...);
brls::async([then, error, url]() {
auto& c = AppConfig::instance();
Expand All @@ -24,7 +24,7 @@ inline void getJSON(Then then, OnError error, std::string_view fmt, Args&&... ar
try {
auto resp = HTTP::get(c.getUrl() + url, header, HTTP::Timeout{});
if (resp.empty()) return;
nlohmann::json j = nlohmann::json::parse(resp);
auto j = nlohmann::json::parse(resp).get<Result>();
brls::sync(std::bind(std::move(then), std::move(j)));
} catch (const std::exception& ex) {
if (error) brls::sync(std::bind(error, ex.what()));
Expand Down
70 changes: 36 additions & 34 deletions app/include/api/jellyfin/media.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct MediaChapter {
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(MediaChapter, Name, StartPositionTicks);

struct MediaItem {
struct Item {
std::string Id;
std::string Name;
std::string Type;
Expand All @@ -91,36 +91,36 @@ struct MediaItem {
std::vector<MediaChapter> Chapters;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
MediaItem, Id, Name, Type, ImageTags, IsFolder, ProductionYear, CommunityRating, RunTimeTicks, UserData, Chapters);
Item, Id, Name, Type, ImageTags, IsFolder, ProductionYear, CommunityRating, RunTimeTicks, UserData, Chapters);

struct MediaCollection : public MediaItem {
struct Collection : public Item {
std::string CollectionType;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(MediaCollection, Id, Name, Type, ImageTags, IsFolder, CollectionType);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Collection, Id, Name, Type, ImageTags, IsFolder, CollectionType);

struct MedisSeries : public MediaItem {
struct Series : public Item {
std::string OriginalTitle;
std::string Overview;
std::string OfficialRating;
std::vector<std::string> Genres;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(MedisSeries, Id, Name, Type, ImageTags, IsFolder, ProductionYear,
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Series, Id, Name, Type, ImageTags, IsFolder, ProductionYear,
OriginalTitle, Overview, OfficialRating, CommunityRating, Genres, UserData);

struct MediaSeason : public MediaItem {
struct Season : public Item {
long IndexNumber = 0;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(MediaSeason, Id, Name, Type, ImageTags, IsFolder, IndexNumber);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Season, Id, Name, Type, ImageTags, IsFolder, IndexNumber);

struct MediaAttachment {
struct Attachment {
std::string Codec;
std::string Name;
long Index = 0;
std::string DeliveryUrl;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(MediaAttachment, Codec, Name, Index, DeliveryUrl);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Attachment, Codec, Name, Index, DeliveryUrl);

struct MediaStream {
struct Stream {
std::string Codec;
std::string DisplayTitle;
std::string Type;
Expand All @@ -130,9 +130,9 @@ struct MediaStream {
std::string DeliveryUrl;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
MediaStream, Codec, DisplayTitle, Type, Index, IsDefault, IsExternal, DeliveryUrl);
Stream, Codec, DisplayTitle, Type, Index, IsDefault, IsExternal, DeliveryUrl);

struct MediaSource {
struct Source {
std::string Id;
std::string Name;
std::string Container;
Expand All @@ -144,90 +144,92 @@ struct MediaSource {
std::string DirectStreamUrl;
std::string TranscodingUrl;
std::string ETag;
std::vector<MediaStream> MediaStreams;
std::vector<MediaAttachment> MediaAttachments;
std::vector<Stream> MediaStreams;
std::vector<Attachment> MediaAttachments;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(MediaSource, Id, Name, Container, DefaultAudioStreamIndex,
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Source, Id, Name, Container, DefaultAudioStreamIndex,
DefaultSubtitleStreamIndex, SupportsDirectPlay, SupportsTranscoding, DirectStreamUrl, TranscodingUrl, ETag,
MediaStreams, MediaAttachments, Protocol);

struct PlaybackResult {
std::vector<MediaSource> MediaSources;
std::vector<Source> MediaSources;
std::string PlaySessionId;
std::string ErrorCode;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(PlaybackResult, MediaSources, PlaySessionId, ErrorCode);

struct MediaEpisode : public MediaSeason {
struct Episode : public Season {
int ParentIndexNumber = 0;
std::string Overview;
std::string SeriesId;
std::string SeriesName;
std::string SeriesPrimaryImageTag;
std::string ParentBackdropItemId;
std::vector<std::string> ParentBackdropImageTags;
std::vector<MediaSource> MediaSources;
std::vector<Source> MediaSources;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(MediaEpisode, Id, Name, Type, ImageTags, IsFolder, ProductionYear,
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Episode, Id, Name, Type, ImageTags, IsFolder, ProductionYear,
UserData, Chapters, RunTimeTicks, IndexNumber, ParentIndexNumber, Overview, SeriesId, SeriesName,
SeriesPrimaryImageTag, ParentBackdropItemId, ParentBackdropImageTags, MediaSources);

struct MusicAlbum : public MediaItem {
struct Album : public Item {
std::string AlbumArtist;
long RecursiveItemCount = 0;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
MusicAlbum, Id, Name, Type, ImageTags, IsFolder, ProductionYear, AlbumArtist, RunTimeTicks, RecursiveItemCount);
Album, Id, Name, Type, ImageTags, IsFolder, ProductionYear, AlbumArtist, RunTimeTicks, RecursiveItemCount);

struct MusicTrack : public MediaItem {
struct Track : public Item {
long IndexNumber = 0;
long ParentIndexNumber = 0;
std::vector<std::string> Artists;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
MusicTrack, Id, Name, Type, IndexNumber, ParentIndexNumber, RunTimeTicks, ProductionYear, Artists, UserData);
Track, Id, Name, Type, IndexNumber, ParentIndexNumber, RunTimeTicks, ProductionYear, Artists, UserData);

struct MediaPlaylist : public MediaItem {
struct Playlist : public Item {
long IndexNumber = 0;
long ParentIndexNumber = 0;
std::string AlbumId;
std::string AlbumPrimaryImageTag;
std::vector<std::string> Artists;
std::string SeriesName;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(MediaPlaylist, Id, Name, Type, IndexNumber, ParentIndexNumber,
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Playlist, Id, Name, Type, IndexNumber, ParentIndexNumber,
RunTimeTicks, ProductionYear, Chapters, CommunityRating, SeriesName, ImageTags, AlbumId, AlbumPrimaryImageTag,
Artists, UserData);

struct LiveProgram {
struct Program {
std::string Name;
uint64_t RunTimeTicks = 0;
std::string StartDate;
std::string EndDate;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(LiveProgram, Name, RunTimeTicks, StartDate, EndDate);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Program, Name, RunTimeTicks, StartDate, EndDate);

struct LiveChannel : public MediaItem {
struct Channel : public Item {
std::string ChannelType;
LiveProgram CurrentProgram;
Program CurrentProgram;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(LiveChannel, Id, Name, Type, ImageTags, ChannelType, CurrentProgram);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Channel, Id, Name, Type, ImageTags, ChannelType, CurrentProgram);

template <typename T>
struct MediaQueryResult {
struct Result {
std::vector<T> Items;
long TotalRecordCount = 0;
long StartIndex = 0;
};

template <typename T>
inline void to_json(nlohmann::json& nlohmann_json_j, const MediaQueryResult<T>& nlohmann_json_t) {
inline void to_json(nlohmann::json& nlohmann_json_j, const Result<T>& nlohmann_json_t) {
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, Items, TotalRecordCount, StartIndex))
}

template <typename T>
inline void from_json(const nlohmann::json& nlohmann_json_j, MediaQueryResult<T>& nlohmann_json_t) {
inline void from_json(const nlohmann::json& nlohmann_json_j, Result<T>& nlohmann_json_t) {
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, Items, TotalRecordCount, StartIndex))
}

using EpisodeResult = Result<Episode>;

} // namespace jellyfin
2 changes: 1 addition & 1 deletion app/include/tab/music_album.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class RecyclingGrid;

class MusicAlbum : public brls::Box {
public:
MusicAlbum(const jellyfin::MediaItem& item);
MusicAlbum(const jellyfin::Item& item);
~MusicAlbum() override;

private:
Expand Down
2 changes: 1 addition & 1 deletion app/include/tab/playlist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class RecyclingGrid;

class Playlist : public brls::Box {
public:
Playlist(const jellyfin::MediaItem& item);
Playlist(const jellyfin::Item& item);
~Playlist() override;

brls::View* getDefaultFocus() override;
Expand Down
6 changes: 3 additions & 3 deletions app/include/view/music_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ enum RepeatMode {

const std::string& currentId();

void play(const jellyfin::MediaItem& item);
void play(const jellyfin::Item& item);

void load(const std::vector<jellyfin::MusicTrack>& item, size_t index);
void load(const std::vector<jellyfin::Track>& item, size_t index);

private:
BRLS_BIND(brls::Box, btnPrev, "music/prev");
Expand Down Expand Up @@ -60,7 +60,7 @@ enum RepeatMode {
MPVEvent::Subscription eventSubscribeID;
MPVCommandReply::Subscription replySubscribeID;

using MusicList = std::unordered_map<int64_t, jellyfin::MusicTrack>;
using MusicList = std::unordered_map<int64_t, jellyfin::Track>;
int64_t playSession = 0;
std::string itemId;
MusicList playList;
Expand Down
2 changes: 1 addition & 1 deletion app/include/view/player_setting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ButtonClose;

class PlayerSetting : public brls::Box {
public:
PlayerSetting(const jellyfin::MediaSource* src = nullptr);
PlayerSetting(const jellyfin::Source* src = nullptr);
~PlayerSetting() override;

bool isTranslucent() override { return true; }
Expand Down
2 changes: 1 addition & 1 deletion app/include/view/video_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class VideoDataSource : public RecyclingGridDataSource {
public:
using MediaList = std::vector<jellyfin::MediaEpisode>;
using MediaList = std::vector<jellyfin::Episode>;

explicit VideoDataSource(const MediaList& r);

Expand Down
6 changes: 3 additions & 3 deletions app/src/activity/player_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "view/video_view.hpp"
#include "view/video_profile.hpp"

PlayerView::PlayerView(const jellyfin::MediaItem& item) : itemId(item.Id) {
PlayerView::PlayerView(const jellyfin::Item& item) : itemId(item.Id) {
float width = brls::Application::contentWidth;
float height = brls::Application::contentHeight;
view = new VideoView();
Expand Down Expand Up @@ -108,8 +108,8 @@ void PlayerView::setSeries(const std::string& seriesId) {
});

ASYNC_RETAIN
jellyfin::getJSON(
[ASYNC_TOKEN](const jellyfin::MediaQueryResult<jellyfin::MediaEpisode>& r) {
jellyfin::getJSON<jellyfin::Result<jellyfin::Episode>>(
[ASYNC_TOKEN](const jellyfin::Result<jellyfin::Episode>& r) {
ASYNC_RELEASE
int index = -1;
std::vector<std::string> values;
Expand Down
16 changes: 8 additions & 8 deletions app/src/tab/home_tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ void HomeTab::doResume() {
{"startIndex", std::to_string(this->startResume)},
});
ASYNC_RETAIN
jellyfin::getJSON(
[ASYNC_TOKEN](const jellyfin::MediaQueryResult<jellyfin::MediaEpisode>& r) {
jellyfin::getJSON<jellyfin::Result<jellyfin::Episode>>(
[ASYNC_TOKEN](const jellyfin::Result<jellyfin::Episode>& r) {
ASYNC_RELEASE
this->startResume = r.StartIndex + this->pageSize;
if (r.TotalRecordCount == 0) {
Expand Down Expand Up @@ -91,8 +91,8 @@ void HomeTab::doVideoLatest() {
{"limit", std::to_string(this->latestSize)},
});
ASYNC_RETAIN
jellyfin::getJSON(
[ASYNC_TOKEN](const std::vector<jellyfin::MediaEpisode>& r) {
jellyfin::getJSON<std::vector<jellyfin::Episode>>(
[ASYNC_TOKEN](const std::vector<jellyfin::Episode>& r) {
ASYNC_RELEASE
if (r.empty()) {
this->headerVideo->setVisibility(brls::Visibility::GONE);
Expand All @@ -119,8 +119,8 @@ void HomeTab::doMusicLatest() {
{"limit", std::to_string(this->latestSize)},
});
ASYNC_RETAIN
jellyfin::getJSON(
[ASYNC_TOKEN](const std::vector<jellyfin::MediaEpisode>& r) {
jellyfin::getJSON<std::vector<jellyfin::Episode>>(
[ASYNC_TOKEN](const std::vector<jellyfin::Episode>& r) {
ASYNC_RELEASE
if (r.empty()) {
this->headerMusic->setVisibility(brls::Visibility::GONE);
Expand All @@ -147,8 +147,8 @@ void HomeTab::doNextup() {
{"startIndex", std::to_string(this->startNextup)},
});
ASYNC_RETAIN
jellyfin::getJSON(
[ASYNC_TOKEN](const jellyfin::MediaQueryResult<jellyfin::MediaEpisode>& r) {
jellyfin::getJSON<jellyfin::Result<jellyfin::Episode>>(
[ASYNC_TOKEN](const jellyfin::Result<jellyfin::Episode>& r) {
ASYNC_RELEASE
this->startNextup = r.StartIndex + this->pageSize;
if (r.TotalRecordCount == 0) {
Expand Down
6 changes: 3 additions & 3 deletions app/src/tab/live_tv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using namespace brls::literals; // for _i18n

class LiveDataSource : public RecyclingGridDataSource {
public:
using MediaList = std::vector<jellyfin::LiveChannel>;
using MediaList = std::vector<jellyfin::Channel>;

explicit LiveDataSource(const MediaList& r) : list(std::move(r)) {
brls::Logger::debug("LiveDataSource: create {}", r.size());
Expand Down Expand Up @@ -72,8 +72,8 @@ void LiveTV::doRequest() {
HTTP::Form query = {{"userId", AppConfig::instance().getUser().id}};

ASYNC_RETAIN
jellyfin::getJSON(
[ASYNC_TOKEN](const jellyfin::MediaQueryResult<jellyfin::LiveChannel>& r) {
jellyfin::getJSON<jellyfin::Result<jellyfin::Channel>>(
[ASYNC_TOKEN](const jellyfin::Result<jellyfin::Channel>& r) {
ASYNC_RELEASE
if (r.Items.empty())
this->recycler->setEmpty();
Expand Down
6 changes: 3 additions & 3 deletions app/src/tab/media_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ brls::View* MediaCollection::getDefaultFocus() { return this->recycler; }

void MediaCollection::doPreferences() {
ASYNC_RETAIN
jellyfin::getJSON(
jellyfin::getJSON<jellyfin::DisplayPreferences>(
[ASYNC_TOKEN](const jellyfin::DisplayPreferences& r) {
ASYNC_RELEASE
this->prefId = std::move(r.Id);
Expand Down Expand Up @@ -138,8 +138,8 @@ void MediaCollection::doRequest() {
}

ASYNC_RETAIN
jellyfin::getJSON(
[ASYNC_TOKEN](const jellyfin::MediaQueryResult<jellyfin::MediaEpisode>& r) {
jellyfin::getJSON<jellyfin::Result<jellyfin::Episode>>(
[ASYNC_TOKEN](const jellyfin::Result<jellyfin::Episode>& r) {
ASYNC_RELEASE
this->startIndex = r.StartIndex + this->pageSize;
if (r.TotalRecordCount == 0) {
Expand Down
Loading

0 comments on commit 1ea4b97

Please sign in to comment.