Merge branch 'develop' for release v3.26.0

This commit is contained in:
emeric
2021-05-22 21:19:18 +02:00
111 changed files with 5133 additions and 3090 deletions
+3 -3
View File
@@ -17,18 +17,18 @@ extraction:
- libtag1-dev
- libpam0g-dev
after_prepare:
- export WT_VERSION=4.4.0
- export WT_VERSION=4.5.0
- export WT_INSTALL_PREFIX=${LGTM_WORKSPACE}/wt-${WT_VERSION}
- pushd ${LGTM_WORKSPACE}
- git clone https://github.com/emweb/wt.git ${LGTM_WORKSPACE}/wt
- pushd ${LGTM_WORKSPACE}/wt
- git checkout ${WT_VERSION}
- cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${WT_INSTALL_PREFIX} -DENABLE_LIBWTTEST=OFF
- cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${WT_INSTALL_PREFIX} -DBUILD_EXAMPLES=OFF -DENABLE_LIBWTTEST=OFF -DCONNECTOR_FCGI=OFF
- make install
- popd
configure:
command:
- export WT_VERSION=4.4.0
- export WT_VERSION=4.5.0
- export WT_INSTALL_PREFIX=${LGTM_WORKSPACE}/wt-${WT_VERSION}
- cmake -DCMAKE_PREFIX_PATH=${WT_INSTALL_PREFIX} -DCMAKE_BUILD_TYPE=Release
+3 -2
View File
@@ -19,6 +19,7 @@ addons:
- libtag1-dev
- libpam0g-dev
- libgraphicsmagick++1-dev
- libgtest-dev
compiler:
- clang
- gcc
@@ -28,7 +29,7 @@ before_install:
git clone https://github.com/emweb/wt.git wt
cd wt
git checkout ${WT_VERSION}
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${WT_INSTALL_PREFIX} -DENABLE_LIBWTTEST=OFF
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${WT_INSTALL_PREFIX} -DBUILD_EXAMPLES=OFF -DENABLE_LIBWTTEST=OFF -DCONNECTOR_FCGI=OFF
make install
cd ..
fi
@@ -39,7 +40,7 @@ cache:
env:
global:
- MAKEFLAGS="-j 2"
- WT_VERSION=4.4.0
- WT_VERSION=4.5.0
- WT_INSTALL_PREFIX=${HOME}/wt-${WT_VERSION}
jobs:
- BUILD_TYPE=Release USE_PAM=ON IMAGE_LIBRARY=STB
+1
View File
@@ -19,6 +19,7 @@ find_package(PkgConfig REQUIRED)
pkg_check_modules(Taglib REQUIRED IMPORTED_TARGET taglib)
pkg_check_modules(Config++ REQUIRED IMPORTED_TARGET libconfig++)
pkg_check_modules(GraphicsMagick++ IMPORTED_TARGET GraphicsMagick++)
find_package(GTest REQUIRED)
# WT
if (NOT Wt_FOUND)
+1 -1
View File
@@ -44,7 +44,7 @@ __Notes__:
* a C++17 compiler is needed
* ffmpeg version 4 minimum is required
```sh
apt-get install g++ cmake libboost-program-options-dev libboost-system-dev libavutil-dev libavformat-dev libstb-dev libconfig++-dev ffmpeg libtag1-dev libpam0g-dev
apt-get install g++ cmake libboost-program-options-dev libboost-system-dev libavutil-dev libavformat-dev libstb-dev libconfig++-dev ffmpeg libtag1-dev libpam0g-dev libgtest-dev
```
__Notes__:
* libpam0g-dev is optional (only for using PAM authentication)
+16
View File
@@ -56,6 +56,22 @@ The Subsonic API is enabled by default.
__Note__: since _LMS_ may store hashed and salted passwords or may forward authentication requests to external services, it cannot handle the __token authentication__ method defined from version 1.13.0.
## About tags
_LMS_ relies exclusively on tags to organize your music collection.
### Filtering
You can specify the tags you want to be used to filter your collection. By default, `GENRE`, `ALBUMGROUPING`, `MOOD` and `ALBUMMOOD` tags are used.
In the administration panel, you can set whatever tags you want, even custom tags.
### Multiple album artists
_LMS_ requires the `ALBUMARTISTS` and `ALBUMARTISTSSORT` tags to properly handle multiple album artists on the same album. As they are a custom tags, you may need to setup your favorite tagger to add them.
__Note__: if you use [Picard](https://picard.musicbrainz.org/), add the following script to include these tags:
```
$setmulti(albumartists,%_albumartists%)
$setmulti(albumartistssort,%_albumartists_sort%)
```
## Keyboard shortcuts
* Play/pause: <kbd>Space</bbd>
* Previous track: <kbd>Ctrl</kbd> + <kbd>Left</kbd>
-1
View File
@@ -12,7 +12,6 @@
</div>
</div>
${artists}
${loading-indicator class="Lms-horizontal-center Lms-loading-indicator"}
</message>
<message id="Lms.Explore.Artists.template.entry">
-1
View File
@@ -155,7 +155,6 @@
<!--Explore:Search-->
<message id="Lms.Explore.Search.search-placeholder">Search...</message>
<message id="Lms.Explore.Search.search">Search results</message>
<!--Player-->
<message id="Lms.Player.transcoding-active">Transcoding active</message>
-1
View File
@@ -155,7 +155,6 @@
<!--Explore:Search-->
<message id="Lms.Explore.Search.search-placeholder">Rechercher...</message>
<message id="Lms.Explore.Search.search">Résultats de la recherche</message>
<!--Player-->
<message id="Lms.Player.transcoding-active">Transcodage actif</message>
-1
View File
@@ -154,7 +154,6 @@
<!--Explore:Search-->
<message id="Lms.Explore.Search.search-placeholder">Ricerca...</message>
<message id="Lms.Explore.Search.search">Risultati di ricerca</message>
<!--Player-->
<message id="Lms.Player.transcoding-active">Transcodifica attiva</message>
-1
View File
@@ -154,7 +154,6 @@
<!--Explore:Search-->
<message id="Lms.Explore.Search.search-placeholder">搜索中...</message>
<message id="Lms.Explore.Search.search">搜索结果</message>
<!--Player-->
<message id="Lms.Player.transcoding-active">转码有效</message>
+4 -5
View File
@@ -8,14 +8,13 @@
<message id="Lms.PlayQueue.template.radio-btn"><i class="fa fa-fw fa-rss"></i></message>
<message id="Lms.PlayQueue.template">
<div class="Lms-header">
${clear-btn class="Lms-playqueue-btn Lms-btn"}${shuffle-btn class="Lms-playqueue-btn Lms-btn"}${repeat-btn class="Lms-playqueue-btn Lms-btn"}${radio-btn class="Lms-playqueue-btn Lms-btn"}
<h4><small>${nb-tracks}</small></h4>
</div>
<div class="row">
<div class="col-lg-8">
<div class="Lms-header">
${clear-btn class="Lms-playqueue-btn Lms-btn"}${shuffle-btn class="Lms-playqueue-btn Lms-btn"}${repeat-btn class="Lms-playqueue-btn Lms-btn"}${radio-btn class="Lms-playqueue-btn Lms-btn"}
<h4><small>${nb-tracks}</small></h4>
</div>
${entries}
${loading-indicator class="Lms-horizontal-center Lms-loading-indicator"}
</div>
</div>
</message>
+7 -2
View File
@@ -14,7 +14,8 @@
<div class="col-md-8">
<div class="Lms-header">
<h3><div class="Lms-releasename text-primary">${name}</div></h3>
${<if-has-artist>}<h4>${artist class="Lms-btn Lms-artistname text-muted"}</h4>${</if-has-artist>}
${<if-has-release-artists>}${artists}${</if-has-release-artists>}
${<if-has-various-release-artists>}<div class="Lms-artistname text-muted">${tr:Lms.Explore.various-artists}</div>${</if-has-various-release-artists>}
<div class="text-muted"><small>${<if-has-year>}${year}${</if-has-year>}${<if-has-orig-year>} (${orig-year})${</if-has-orig-year>}</small></div>
${clusters}
${play-btn class="Lms-explore-btn Lms-btn"}${more-btn class="Lms-explore-btn Lms-btn"}
@@ -52,7 +53,11 @@
</div>
</message>
<message id="Lms.Explore.Release.template.disc-entry">
<message id="Lms.Explore.Release.template.entry-release-artist">
<h4>${artist class="Lms-btn Lms-artistname text-muted"}</h4>
</message>
<message id="Lms.Explore.Release.template.entry-disc">
<h4>${disc-title}</h4>
${tracks}
</message>
+10 -5
View File
@@ -7,10 +7,7 @@
${mode class="nav nav-pills Lms-explore-mode-container"}
${play-btn class="Lms-explore-btn Lms-btn"}${more-btn class="Lms-explore-btn Lms-btn"}
</div>
<div class="row">
${releases}
</div>
${loading-indicator class="Lms-horizontal-center Lms-loading-indicator"}
${releases}
</message>
<message id="Lms.Explore.Releases.template.entry-grid">
@@ -21,10 +18,18 @@
</div>
<div class="Lms-explore-entry-grid-text-container">
<h5>${release-name class="Lms-btn Lms-releasename text-primary"}</h5>
<div class="Lms-explore-entry-separator"><h6>${<if-has-year>}<small><span class="text-muted">${year}</span></small>${</if-has-year>}${<if-has-artist>}${artist-name class="Lms-btn Lms-artistname text-muted"}${</if-has-artist>}</h6></div>
<div class="Lms-explore-entry-separator"><h6>${<if-has-year>}<small><span class="text-muted">${year}</span></small>${</if-has-year>}${<if-has-artist>}${artist-name class="Lms-btn Lms-artistname text-muted"}${</if-has-artist>}${<if-has-various-artists>}<div class="Lms-artistname text-muted">${tr:Lms.Explore.various-artists}</div>${</if-has-various-artists>}</h6></div>
</div>
</div>
</div>
</message>
<message id="Lms.Explore.Releases.template.container">
<div class="row">
${elements}
</div>
${loading-indicator class="Lms-horizontal-center Lms-loading-indicator"}
</message>
</messages>
+2 -29
View File
@@ -4,36 +4,9 @@
<message id="Lms.Explore.Search.template">
<div class="Lms-header">
<h2>${tr:Lms.Explore.Search.search}</h2>
</div>
<div class="row">
${<if-artists>}
<div class="col-lg-8">
<div class="well well-sm">
<h3>${tr:Lms.Explore.artists}</h3>
${artists}
</div>
</div>
${</if-artists>}
${<if-releases>}
<div class="col-lg-8">
<div class="well well-sm">
<h3>${tr:Lms.Explore.releases}</h3>
<div class="row">
${releases}
</div>
</div>
</div>
${</if-releases>}
${<if-tracks>}
<div class="col-lg-8">
<div class="well well-sm">
<h3>${tr:Lms.Explore.tracks}</h3>
${tracks}
</div>
</div>
${</if-tracks>}
${mode class="nav nav-pills Lms-explore-mode-container"}
</div>
${stack}
</message>
</messages>
+6 -1
View File
@@ -18,8 +18,8 @@
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li>${artists class="Lms-btn"}</li>
<li>${releases class="Lms-btn"}</li>
<li>${artists class="Lms-btn"}</li>
<li>${tracks class="Lms-btn"}</li>
<li>${playqueue class="Lms-btn"}</li>
</ul>
@@ -61,4 +61,9 @@
<span class="sr-only">${tr:Lms.loading}</span>
</message>
<message id="Lms.infinite-scrolling-container">
${elements}
${loading-indicator class="Lms-horizontal-center Lms-loading-indicator"}
</message>
</messages>
+13 -9
View File
@@ -4,16 +4,11 @@
<!--FORMS message blocks-->
<message id="Lms.Explore.Tracks.template">
<div class="row">
<div class="col-lg-8">
<div class="Lms-header">
${mode class="nav nav-pills Lms-explore-mode-container"}
${play-btn class="Lms-explore-btn Lms-btn"}${more-btn class="Lms-explore-btn Lms-btn"}
</div>
${tracks}
${loading-indicator class="Lms-horizontal-center Lms-loading-indicator"}
</div>
<div class="Lms-header">
${mode class="nav nav-pills Lms-explore-mode-container"}
${play-btn class="Lms-explore-btn Lms-btn"}${more-btn class="Lms-explore-btn Lms-btn"}
</div>
${tracks}
</message>
<message id="Lms.Explore.Tracks.template.entry">
@@ -45,4 +40,13 @@
<small>${artist class="Lms-btn Lms-artistname text-muted"}</small>
</message>
<message id="Lms.Explore.Tracks.template.container">
<div class="row">
<div class="col-lg-8">
${elements}
${loading-indicator class="Lms-horizontal-center Lms-loading-indicator"}
</div>
</div>
</message>
</messages>
+6 -2
View File
@@ -35,10 +35,14 @@ deploy-path = "/";
http-server-thread-count = 0;
# ListenBrainz root API
listenbrainz-api-url = "https://api.listenbrainz.org/1/";
listenbrainz-api-base-url = "https://api.listenbrainz.org";
# How many listens to retrieve when syncing (0 disables sync)
listenbrainz-max-sync-listen-count = 1000;
# How often to resync listens (0 disables sync)
listenbrainz-sync-listens-period-hours = 1;
# Acousticbrainz root API
acousticbrainz-api-url = "https://acousticbrainz.org/api/v1/";
acousticbrainz-api-base-url = "https://acousticbrainz.org";
# Authentication
# Available backends: "internal", "PAM", "http-headers"
+20 -20
View File
@@ -1,12 +1,8 @@
FROM alpine:3.10 AS build
FROM alpine:3.13 AS build
WORKDIR /tmp/workdir
ARG MAKEFLAGS="-j2"
ARG FFMPEG_VERSION=4.1.4
ARG WT_VERSION=4.5.0
ARG STB_VERSION=b42009b3b9d4ca35bc703f5310eedc74f584be58
ARG LMS_VERSION=v3.6.3
ARG PREFIX="/tmp/install"
@@ -22,7 +18,6 @@ ARG BUILD_PACKAGES=" \
g++ \
make \
openjpeg-dev \
jasper-dev \
libpng-dev \
nasm \
yasm \
@@ -36,19 +31,13 @@ ARG BUILD_PACKAGES=" \
openssl-dev \
boost-dev \
libconfig-dev \
taglib-dev"
taglib-dev \
gtest-dev"
RUN apk add --no-cache --update ${BUILD_PACKAGES}
# STB
RUN \
DIR=/tmp/stb && mkdir -p ${DIR} && cd ${DIR} && \
curl -sLO https://github.com/nothings/stb/archive/${STB_VERSION}.tar.gz && \
tar -x --strip-components=1 -f ${STB_VERSION}.tar.gz && \
mkdir -p ${PREFIX}/include/stb && \
cp ./*.h ${PREFIX}/include/stb
# ffmpeg
ARG FFMPEG_VERSION=4.1.4
RUN \
DIR=/tmp/ffmpeg && mkdir -p ${DIR} && cd ${DIR} && \
curl -sLO https://ffmpeg.org/releases/ffmpeg-${FFMPEG_VERSION}.tar.bz2 && \
@@ -90,6 +79,7 @@ RUN \
make distclean
# WT
ARG WT_VERSION=4.5.0
RUN \
DIR=/tmp/wt && mkdir -p ${DIR} && cd ${DIR} && \
curl -sLO https://github.com/emweb/wt/archive/${WT_VERSION}.tar.gz && \
@@ -97,11 +87,21 @@ RUN \
RUN \
DIR=/tmp/wt && mkdir -p ${DIR} && cd ${DIR} && \
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${PREFIX} -DBUILD_EXAMPLES=NO -DENABLE_LIBWTTEST=OFF && \
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${PREFIX} -DBUILD_EXAMPLES=OFF -DENABLE_LIBWTTEST=OFF -DCONNECTOR_FCGI=OFF && \
make && \
make install
# STB
ARG STB_VERSION=b42009b3b9d4ca35bc703f5310eedc74f584be58
RUN \
DIR=/tmp/stb && mkdir -p ${DIR} && cd ${DIR} && \
curl -sLO https://github.com/nothings/stb/archive/${STB_VERSION}.tar.gz && \
tar -x --strip-components=1 -f ${STB_VERSION}.tar.gz && \
mkdir -p ${PREFIX}/include/stb && \
cp ./*.h ${PREFIX}/include/stb
# LMS
ARG LMS_VERSION=v3.25.2
RUN \
DIR=/tmp/lms && mkdir -p ${DIR} && cd ${DIR} && \
curl -sL https://github.com/epoupon/lms/archive/${LMS_VERSION}.tar.gz -o ${LMS_VERSION}.tar.gz && \
@@ -110,7 +110,7 @@ RUN \
RUN \
DIR=/tmp/lms && mkdir -p ${DIR} && cd ${DIR} && \
PKG_CONFIG_PATH=/tmp/install/lib/pkgconfig CXXFLAGS="-I${PREFIX}/include" LDFLAGS="-L${PREFIX}/lib -Wl,--rpath-link=${PREFIX}/lib" cmake . -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${PREFIX} -DCMAKE_PREFIX_PATH=${PREFIX} && \
make && \
LD_LIBRARY_PATH=${PREFIX}/lib VERBOSE=1 make && \
LD_LIBRARY_PATH=${PREFIX}/lib make test && \
make install && \
mkdir -p ${PREFIX}/etc/ && \
@@ -134,14 +134,14 @@ RUN \
rm -rf /tmp/fakeroot/share/man
## Release Stage
FROM alpine:3.10 AS release
FROM alpine:3.13 AS release
LABEL maintainer="Emeric Poupon <itmfr@yahoo.fr>"
LABEL version=${LMS_VERSION}
ARG RUNTIME_PACKAGES=" \
openssl \
libssl1.1 \
libcrypto1.1 \
openjpeg \
jasper-libs \
libpng \
libogg \
opus \
+1 -1
View File
@@ -251,7 +251,7 @@ guessMediaFileFormat(const std::filesystem::path& file)
if (formats.size() > 1)
LMS_LOG(AV, INFO) << "File '" << file.string() << "' reported several formats: '" << format->name << "'";
std::vector<std::string> mimeTypes;
std::vector<std::string_view> mimeTypes;
if (format->mime_type)
mimeTypes = StringUtils::splitString(format->mime_type, ",");
+1
View File
@@ -13,6 +13,7 @@ add_library(lmsdatabase SHARED
impl/Track.cpp
impl/TrackBookmark.cpp
impl/User.cpp
impl/Utils.cpp
)
target_include_directories(lmsdatabase INTERFACE
+9 -9
View File
@@ -27,7 +27,7 @@
#include "database/User.hpp"
#include "utils/Logger.hpp"
#include "SqlQuery.hpp"
#include "Utils.hpp"
namespace Database
{
@@ -83,7 +83,7 @@ Wt::Dbo::Query<T>
createQuery(Session& session,
const std::string& queryStr,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords,
const std::vector<std::string_view>& keywords,
std::optional<TrackArtistLinkType> linkType)
{
session.checkSharedLocked();
@@ -100,16 +100,16 @@ createQuery(Session& session,
std::vector<std::string> clauses;
std::vector<std::string> sortClauses;
for (const std::string& keyword : keywords)
for (std::string_view keyword : keywords)
{
clauses.push_back("a.name LIKE ?");
query.bind("%%" + keyword + "%%");
clauses.push_back("a.name LIKE ? ESCAPE '" ESCAPE_CHAR_STR "'");
query.bind("%" + escapeLikeKeyword(keyword) + "%");
}
for (const std::string& keyword : keywords)
for (std::string_view keyword : keywords)
{
sortClauses.push_back("a.sort_name LIKE ?");
query.bind("%%" + keyword + "%%");
sortClauses.push_back("a.sort_name LIKE ? ESCAPE '" ESCAPE_CHAR_STR "'");
query.bind("%" + escapeLikeKeyword(keyword) + "%");
}
query.where("(" + StringUtils::joinStrings(clauses, " AND ") + ") OR (" + StringUtils::joinStrings(sortClauses, " AND ") + ")");
@@ -267,7 +267,7 @@ Artist::getByClusters(Session& session, const std::set<IdType>& clusters, SortMe
std::vector<Artist::pointer>
Artist::getByFilter(Session& session,
const std::set<IdType>& clusters,
const std::vector<std::string>& keywords,
const std::vector<std::string_view>& keywords,
std::optional<TrackArtistLinkType> linkType,
SortMethod sortMethod,
std::optional<Range> range,
+5 -4
View File
@@ -28,6 +28,7 @@
#include "database/User.hpp"
#include "utils/Logger.hpp"
#include "SqlQuery.hpp"
#include "Utils.hpp"
namespace Database
{
@@ -38,14 +39,14 @@ Wt::Dbo::Query<T>
createQuery(Session& session,
const std::string& queryStr,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords)
const std::vector<std::string_view>& keywords)
{
auto query {session.getDboSession().query<T>(queryStr)};
query.join("track t ON t.release_id = r.id");
for (const std::string& keyword : keywords)
query.where("r.name LIKE ?").bind("%%" + keyword + "%%");
for (std::string_view keyword : keywords)
query.where("r.name LIKE ? ESCAPE '" ESCAPE_CHAR_STR "'").bind("%" + escapeLikeKeyword(keyword) + "%");
if (!clusterIds.empty())
{
@@ -300,7 +301,7 @@ Release::getByClusters(Session& session, const std::set<IdType>& clusters)
std::vector<Release::pointer>
Release::getByFilter(Session& session,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords,
const std::vector<std::string_view>& keywords,
std::optional<Range> range,
bool& moreResults)
{
+24 -8
View File
@@ -30,6 +30,8 @@
#include "utils/Logger.hpp"
#include "SqlQuery.hpp"
#include "StringViewTraits.hpp"
#include "Utils.hpp"
namespace Database {
@@ -39,14 +41,14 @@ Wt::Dbo::Query<T>
createQuery(Session& session,
const std::string& queryStr,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords)
const std::vector<std::string_view>& keywords)
{
session.checkSharedLocked();
auto query {session.getDboSession().query<T>(queryStr)};
for (const std::string& keyword : keywords)
query.where("t.name LIKE ?").bind("%%" + keyword + "%%");
for (std::string_view keyword : keywords)
query.where("t.name LIKE ? ESCAPE '" ESCAPE_CHAR_STR "'").bind("%" + escapeLikeKeyword(keyword) + "%");
if (!clusterIds.empty())
{
@@ -150,13 +152,15 @@ Track::getById(Session& session, IdType id)
.where("id = ?").bind(id);
}
Track::pointer
Track::getByMBID(Session& session, const UUID& mbid)
std::vector<Track::pointer>
Track::getByRecordingMBID(Session& session, const UUID& mbid)
{
session.checkSharedLocked();
return session.getDboSession().find<Track>()
.where("mbid = ?").bind(std::string {mbid.getAsString()});
Wt::Dbo::collection<Track::pointer> res = session.getDboSession().find<Track>()
.where("recording_mbid = ?").bind(std::string {mbid.getAsString()});
return std::vector<Track::pointer>(res.begin(), res.end());
}
Track::pointer
@@ -332,7 +336,7 @@ Track::hasTrackFeatures() const
std::vector<Track::pointer>
Track::getByFilter(Session& session,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords,
const std::vector<std::string_view>& keywords,
std::optional<Range> range,
bool& moreResults)
{
@@ -354,6 +358,18 @@ Track::getByFilter(Session& session,
return res;
}
std::vector<Track::pointer>
Track::getByNameAndReleaseName(Session& session, std::string_view trackName, std::string_view releaseName)
{
session.checkSharedLocked();
Wt::Dbo::collection<pointer> collection = session.getDboSession().query<Track::pointer>("SELECT t from track t")
.join("release r ON t.release_id = r.id")
.where("t.name = ?").bind(trackName)
.where("r.name = ?").bind(releaseName);
return std::vector<pointer>(collection.begin(), collection.end());
}
std::vector<Track::pointer>
Track::getSimilarTracks(Session& session,
const std::unordered_set<IdType>& tracks,
+12
View File
@@ -148,6 +148,18 @@ TrackList::getEntries(std::optional<std::size_t> offset, std::optional<std::size
return std::vector<Wt::Dbo::ptr<TrackListEntry>>(entries.begin(), entries.end());
}
Wt::Dbo::ptr<TrackListEntry>
TrackList::getEntryByTrackAndDateTime(Wt::Dbo::ptr<Track> track, const Wt::WDateTime& dateTime) const
{
assert(session());
assert(IdIsValid(self()->id()));
return session()->find<TrackListEntry>()
.where("tracklist_id = ?").bind(self().id())
.where("track_id = ?").bind(track.id())
.where("date_time = ?").bind(Wt::WDateTime::fromTime_t(dateTime.toTime_t()));
}
static
Wt::Dbo::Query<Artist::pointer>
createArtistsQuery(Wt::Dbo::Session& session, const std::string& queryStr, IdType tracklistId, const std::set<IdType>& clusterIds, std::optional<TrackArtistLinkType> linkType)
+9
View File
@@ -84,6 +84,15 @@ User::getAll(Session& session)
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<IdType>
User::getAllIds(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<IdType> res = session.getDboSession().query<IdType>("SELECT id FROM user");
return std::vector<IdType>(res.begin(), res.end());
}
User::pointer
User::getDemo(Session& session)
{
+33
View File
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Utils.hpp"
#include "utils/String.hpp"
namespace Database
{
std::string
escapeLikeKeyword(std::string_view keyword)
{
return StringUtils::escapeString(keyword, "%_", escapeChar);
}
} // namespace Database
+33
View File
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <string_view>
#include <vector>
namespace Database
{
#define ESCAPE_CHAR_STR "\\"
static constexpr char escapeChar {'\\'};
std::string escapeLikeKeyword(std::string_view keywords);
} // namespace Database
@@ -21,6 +21,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <unordered_set>
#include <vector>
@@ -69,7 +70,7 @@ class Artist : public Wt::Dbo::Dbo<Artist>
);
static std::vector<pointer> getByFilter(Session& session,
const std::set<IdType>& clusters, // if non empty, at least one artist that belongs to these clusters
const std::vector<std::string>& keywords, // if non empty, name must match all of these keywords (name + sort name fields)
const std::vector<std::string_view>& keywords, // if non empty, name must match all of these keywords (name + sort name fields)
std::optional<TrackArtistLinkType> linkType, // if set, only artists that have produced at least one track with this link type
SortMethod sortMethod,
std::optional<Range> range,
@@ -66,7 +66,7 @@ class Release : public Wt::Dbo::Dbo<Release>
static std::vector<pointer> getByClusters(Session& session, const std::set<IdType>& clusters);
static std::vector<pointer> getByFilter(Session& session,
const std::set<IdType>& clusters, // if non empty, at least one release that belongs to these clusters
const std::vector<std::string>& keywords, // if non empty, name must match all of these keywords
const std::vector<std::string_view>& keywords, // if non empty, name must match all of these keywords
std::optional<Range> range,
bool& moreExpected);
static std::vector<IdType> getAllIdsWithClusters(Session& session, std::optional<std::size_t> limit = {});
+18 -15
View File
@@ -23,6 +23,7 @@
#include <filesystem>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_set>
#include <vector>
@@ -61,7 +62,7 @@ class Track : public Wt::Dbo::Dbo<Track>
static std::size_t getCount(Session& session);
static pointer getByPath(Session& session, const std::filesystem::path& p);
static pointer getById(Session& session, IdType id);
static pointer getByMBID(Session& session, const UUID& MBID);
static std::vector<pointer> getByRecordingMBID(Session& session, const UUID& MBID);
static std::vector<pointer> getSimilarTracks(Session& session,
const std::unordered_set<IdType>& trackIds,
std::optional<std::size_t> offset = {},
@@ -70,9 +71,10 @@ class Track : public Wt::Dbo::Dbo<Track>
const std::set<IdType>& clusters); // tracks that belong to these clusters
static std::vector<pointer> getByFilter(Session& session,
const std::set<IdType>& clusters, // if non empty, tracks that belong to these clusters
const std::vector<std::string>& keywords, // if non empty, name must match all of these keywords
const std::vector<std::string_view>& keywords, // if non empty, name must match all of these keywords
std::optional<Range> range,
bool& moreExpected);
static std::vector<pointer> getByNameAndReleaseName(Session& session, std::string_view trackName, std::string_view releaseName);
static std::vector<pointer> getAll(Session& session, std::optional<std::size_t> limit = std::nullopt);
static std::vector<pointer> getAllRandom(Session& session, const std::set<IdType>& clusters, std::optional<std::size_t> limit = std::nullopt);
@@ -127,6 +129,7 @@ class Track : public Wt::Dbo::Dbo<Track>
std::string getName() const { return _name; }
std::filesystem::path getPath() const { return _filePath; }
std::chrono::milliseconds getDuration() const { return _duration; }
const Wt::WDateTime& getLastWritten() const { return _fileLastWrite; }
std::optional<int> getYear() const;
std::optional<int> getOriginalYear() const;
Wt::WDateTime getLastWriteTime() const { return _fileLastWrite; }
@@ -188,28 +191,28 @@ class Track : public Wt::Dbo::Dbo<Track>
static const std::size_t _maxCopyrightLength = 128;
static const std::size_t _maxCopyrightURLLength = 128;
int _scanVersion {};
int _trackNumber {};
int _discNumber {};
std::string _discSubtitle;
int _totalTrack {};
int _totalDisc {};
int _scanVersion {};
int _trackNumber {};
int _discNumber {};
std::string _discSubtitle;
int _totalTrack {};
int _totalDisc {};
std::string _name;
std::string _artistName;
std::string _releaseName;
std::chrono::duration<int, std::milli> _duration;
int _year {};
int _originalYear {};
std::chrono::duration<int, std::milli> _duration {};
int _year {};
int _originalYear {};
std::string _filePath;
Wt::WDateTime _fileLastWrite;
Wt::WDateTime _fileAdded;
Wt::WDateTime _fileLastWrite;
Wt::WDateTime _fileAdded;
bool _hasCover {};
std::string _trackMBID;
std::string _recordingMBID;
std::string _copyright;
std::string _copyrightURL;
std::optional<float> _trackReplayGain;
std::optional<float> _releaseReplayGain;
std::optional<float> _trackReplayGain;
std::optional<float> _releaseReplayGain;
Wt::Dbo::ptr<Release> _release;
Wt::Dbo::collection<Wt::Dbo::ptr<TrackArtistLink>> _trackArtistLinks;
@@ -84,6 +84,9 @@ class TrackList : public Wt::Dbo::Dbo<TrackList>
std::size_t getCount() const;
Wt::Dbo::ptr<TrackListEntry> getEntry(std::size_t pos) const;
std::vector<Wt::Dbo::ptr<TrackListEntry>> getEntries(std::optional<std::size_t> offset = {}, std::optional<std::size_t> size = {}) const;
Wt::Dbo::ptr<TrackListEntry> getEntryByTrackAndDateTime(Wt::Dbo::ptr<Track> track, const Wt::WDateTime& dateTime) const;
// Get track bya
std::vector<Wt::Dbo::ptr<Artist>> getArtistsReverse(const std::set<IdType>& clusterIds, std::optional<TrackArtistLinkType> linkType, std::optional<Range> range, bool& moreResults) const;
std::vector<Wt::Dbo::ptr<Release>> getReleasesReverse(const std::set<IdType>& clusterIds, std::optional<Range> range, bool& moreResults) const;
@@ -139,6 +139,7 @@ class User : public Wt::Dbo::Dbo<User>
static pointer getById(Session& session, IdType id);
static pointer getByLoginName(Session& session, std::string_view loginName);
static std::vector<pointer> getAll(Session& session);
static std::vector<IdType> getAllIds(Session& session);
static pointer getDemo(Session& session);
static std::size_t getCount(Session& session);
+13 -9
View File
@@ -49,10 +49,10 @@ findFirstValueOfAs(const Av::IAudioFile::MetadataMap& metadataMap, std::initiali
if (!str)
return std::nullopt;
std::vector<std::string> strUuids = StringUtils::splitString(*str, "/");
const std::vector<std::string_view> strUuids {StringUtils::splitString(*str, "/")};
std::vector<UUID> res;
for (const std::string& strUuid : strUuids)
for (std::string_view strUuid : strUuids)
{
std::optional<UUID> uuid {UUID::fromString(strUuid)};
if (!uuid)
@@ -101,7 +101,7 @@ getArtists(const Av::IAudioFile::MetadataMap& metadataMap)
{
std::vector<Artist> artists;
std::vector<std::string> artistNames;
std::vector<std::string_view> artistNames;
if (metadataMap.find("ARTISTS") != metadataMap.end())
{
artistNames = StringUtils::splitString(metadataMap.find("ARTISTS")->second, "/;");
@@ -161,8 +161,7 @@ AvFormatParser::parse(const std::filesystem::path& p, bool debug)
else if (tag == "TRACK")
{
// Expecting 'Number/Total'
std::vector<std::string> strings {StringUtils::splitString(value, "/") };
const std::vector<std::string_view> strings {StringUtils::splitString(value, "/") };
if (strings.size() > 0)
{
track.trackNumber = StringUtils::readAs<std::size_t>(strings[0]);
@@ -174,8 +173,7 @@ AvFormatParser::parse(const std::filesystem::path& p, bool debug)
else if (tag == "DISC")
{
// Expecting 'Number/Total'
std::vector<std::string> strings {StringUtils::splitString(value, "/")};
const std::vector<std::string_view> strings {StringUtils::splitString(value, "/")};
if (strings.size() > 0)
{
track.discNumber = StringUtils::readAs<std::size_t>(strings[0]);
@@ -217,10 +215,16 @@ AvFormatParser::parse(const std::filesystem::path& p, bool debug)
}
else if (_clusterTypeNames.find(tag) != _clusterTypeNames.end())
{
std::vector<std::string> clusterNames {StringUtils::splitString(value, "/,;")};
const std::vector<std::string_view> clusterNames {StringUtils::splitString(value, "/,;")};
if (!clusterNames.empty())
track.clusters[tag] = std::set<std::string>{clusterNames.begin(), clusterNames.end()};
{
std::set<std::string> values;
std::transform(std::cbegin(clusterNames), std::cend(clusterNames),
std::inserter(values, std::begin(values)),
[](std::string_view clusterName) { return std::string {clusterName}; });
track.clusters[tag] = std::move(values);
}
}
}
+11 -12
View File
@@ -78,12 +78,12 @@ getPropertyValuesAs(const TagLib::PropertyMap& properties, const std::string& ke
static
std::vector<std::string>
splitAndTrimString(const std::string& str, const std::string& delimiters)
splitAndTrimString(const std::string& str, std::string_view delimiters)
{
std::vector<std::string> res;
std::vector<std::string> strings {StringUtils::splitString(str, delimiters)};
for (const std::string& s : strings)
std::vector<std::string_view> strings {StringUtils::splitString(str, delimiters)};
for (std::string_view s : strings)
res.emplace_back(StringUtils::stringTrim(s));
return res;
@@ -203,8 +203,7 @@ TagLibParser::processTag(Track& track, const std::string& tag, const TagLib::Str
else if (tag == "DISCNUMBER")
{
// Expecting 'Number/Total'
std::vector<std::string> strings {StringUtils::splitString(value, "/")};
std::vector<std::string_view> strings {StringUtils::splitString(value, "/")};
if (!strings.empty())
{
track.discNumber = StringUtils::readAs<std::size_t>(strings[0]);
@@ -397,13 +396,13 @@ TagLibParser::parse(const std::filesystem::path& p, bool debug)
track.album = getAlbum(properties);
track.artists = getArtists(properties, {"ARTISTS", "ARTIST"}, {"ARTISTSORT"}, {"MUSICBRAINZ_ARTISTID", "MUSICBRAINZ ARTIST ID"});
track.albumArtists = getArtists(properties, {"ALBUMARTIST"}, {"ALBUMARTISTSORT"}, {"MUSICBRAINZ_ALBUMARTISTID", "MUSICBRAINZ ALBUM ARTIST ID"});
track.conductorArtists = getArtists(properties, {"CONDUCTOR"}, {""}, {});
track.composerArtists = getArtists(properties, {"COMPOSER"}, {"COMPOSERSORT"}, {});
track.lyricistArtists = getArtists(properties, {"LYRICIST"}, {"LYRICISTSORT"}, {});
track.mixerArtists = getArtists(properties, {"MIXER"}, {""}, {});
track.producerArtists = getArtists(properties, {"PRODUCER"}, {""}, {});
track.remixerArtists = getArtists(properties, {"REMIXER", "ModifiedBy"}, {""}, {});
track.albumArtists = getArtists(properties, {"ALBUMARTISTS", "ALBUMARTIST"}, {"ALBUMARTISTSSORT", "ALBUMARTISTSORT"}, {"MUSICBRAINZ_ALBUMARTISTID", "MUSICBRAINZ ALBUM ARTIST ID"});
track.conductorArtists = getArtists(properties, {"CONDUCTORS", "CONDUCTOR"}, {"CONDUCTORSSORT", "CONDUCTORSORT"}, {});
track.composerArtists = getArtists(properties, {"COMPOSERS", "COMPOSER"}, {"COMPOSERSSORT", "COMPOSERSORT"}, {});
track.lyricistArtists = getArtists(properties, {"LYRICISTS", "LYRICIST"}, {"LYRICISTSSORT", "LYRICISTSORT"}, {});
track.mixerArtists = getArtists(properties, {"MIXERS", "MIXER"}, {"MIXERSSORT", "MIXERSORT"}, {});
track.producerArtists = getArtists(properties, {"PRODUCERS", "PRODUCER"}, {"PRODUCERSSORT", "PRODUCERSORT"}, {});
track.remixerArtists = getArtists(properties, {"REMIXERS", "REMIXER", "ModifiedBy"}, {"REMIXERSSORT", "REMIXERSORT"}, {});
return track;
}
@@ -38,9 +38,9 @@ static
std::string
getJsonData(const UUID& mbid)
{
static constexpr std::string_view defaultAPIURL {"https://acousticbrainz.org/api/v1/"};
static constexpr std::string_view defaultAPIURL {"https://acousticbrainz.org"};
const std::string url {std::string {Service<IConfig>::get()->getString("acousticbrainz-api-url", defaultAPIURL)} + std::string {mbid.getAsString()} + "/low-level"};
const std::string url {std::string {Service<IConfig>::get()->getString("acousticbrainz-api-base-url", defaultAPIURL)} + "/api/v1/" + std::string {mbid.getAsString()} + "/low-level"};
boost::asio::io_service ioService;
+7 -1
View File
@@ -2,6 +2,9 @@
add_library(lmsscrobbling SHARED
impl/internal/InternalScrobbler.cpp
impl/listenbrainz/ListenBrainzScrobbler.cpp
impl/listenbrainz/ListensSynchronizer.cpp
impl/listenbrainz/SendQueue.cpp
impl/listenbrainz/Utils.cpp
impl/Scrobbling.cpp
)
@@ -14,9 +17,12 @@ target_include_directories(lmsscrobbling PRIVATE
impl
)
target_link_libraries(lmsscrobbling PRIVATE
lmsutils
)
target_link_libraries(lmsscrobbling PUBLIC
lmsdatabase
lmsutils
)
install(TARGETS lmsscrobbling DESTINATION lib)
+1 -1
View File
@@ -46,7 +46,7 @@ namespace Scrobbling
virtual void listenStarted(const Listen& listen) = 0;
virtual void listenFinished(const Listen& listen, std::optional<std::chrono::seconds> duration) = 0;
virtual void addListen(const Listen& listen, const Wt::WDateTime& timePoint) = 0;
virtual void addTimedListen(const TimedListen& listen) = 0;
virtual Wt::Dbo::ptr<Database::TrackList> getListensTrackList(Database::Session& session, Wt::Dbo::ptr<Database::User> user) = 0;
};
+9 -9
View File
@@ -30,37 +30,37 @@
namespace Scrobbling
{
std::unique_ptr<IScrobbling>
createScrobbling(Database::Db& db)
createScrobbling(boost::asio::io_context& ioContext, Database::Db& db)
{
return std::make_unique<Scrobbling>(db);
return std::make_unique<Scrobbling>(ioContext, db);
}
Scrobbling::Scrobbling(Database::Db& db)
Scrobbling::Scrobbling(boost::asio::io_context& ioContext, Database::Db& db)
: _db {db}
{
_scrobblers.emplace(Database::Scrobbler::Internal, std::make_unique<InternalScrobbler>(_db));
_scrobblers.emplace(Database::Scrobbler::ListenBrainz, std::make_unique<ListenBrainzScrobbler>(_db));
_scrobblers.emplace(Database::Scrobbler::ListenBrainz, std::make_unique<ListenBrainz::Scrobbler>(ioContext, _db));
}
void
Scrobbling::listenStarted(const Listen& listen)
{
if (auto scrobbler {getUserScrobbler(listen.userId)})
if (std::optional<Database::Scrobbler> scrobbler {getUserScrobbler(listen.userId)})
_scrobblers[*scrobbler]->listenStarted(listen);
}
void
Scrobbling::listenFinished(const Listen& listen, std::optional<std::chrono::seconds> duration)
{
if (auto scrobbler {getUserScrobbler(listen.userId)})
if (std::optional<Database::Scrobbler> scrobbler {getUserScrobbler(listen.userId)})
_scrobblers[*scrobbler]->listenFinished(listen, duration);
}
void
Scrobbling::addListen(const Listen& listen, Wt::WDateTime timePoint)
Scrobbling::addTimedListen(const TimedListen& listen)
{
if (auto scrobbler {getUserScrobbler(listen.userId)})
_scrobblers[*scrobbler]->addListen(listen, timePoint);
if (std::optional<Database::Scrobbler> scrobbler {getUserScrobbler(listen.userId)})
_scrobblers[*scrobbler]->addTimedListen(listen);
}
std::optional<Database::Scrobbler>
+2 -2
View File
@@ -31,12 +31,12 @@ namespace Scrobbling
class Scrobbling : public IScrobbling
{
public:
Scrobbling(Database::Db& db);
Scrobbling(boost::asio::io_context& ioContext, Database::Db& db);
private:
void listenStarted(const Listen& listen) override;
void listenFinished(const Listen& listen, std::optional<std::chrono::seconds> duration) override;
void addListen(const Listen& listen, Wt::WDateTime timePoint) override;
void addTimedListen(const TimedListen& listen) override;
std::vector<Wt::Dbo::ptr<Database::Artist>> getRecentArtists(Database::Session& session,
Wt::Dbo::ptr<Database::User> user,
@@ -47,11 +47,11 @@ namespace Scrobbling
if (duration && *duration < std::chrono::seconds {5})
return;
addListen(listen, Wt::WDateTime::currentDateTime());
addTimedListen({listen, Wt::WDateTime::currentDateTime()});
}
void
InternalScrobbler::addListen(const Listen& listen, const Wt::WDateTime& timePoint)
InternalScrobbler::addTimedListen(const TimedListen& listen)
{
Database::Session& session {_db.getTLSSession()};
@@ -69,7 +69,7 @@ namespace Scrobbling
if (!track)
return;
Database::TrackListEntry::create(session, track, getListensTrackList(session, user), timePoint);
Database::TrackListEntry::create(session, track, getListensTrackList(session, user), listen.listenedAt);
}
Wt::Dbo::ptr<Database::TrackList>
@@ -32,7 +32,7 @@ namespace Scrobbling
void listenStarted(const Listen& listen) override;
void listenFinished(const Listen& listen, std::optional<std::chrono::seconds> duration) override;
void addListen(const Listen& listen, const Wt::WDateTime& timePoint) override;
void addTimedListen(const TimedListen& listen) override;
Wt::Dbo::ptr<Database::TrackList> getListensTrackList(Database::Session& session, Wt::Dbo::ptr<Database::User> user) override;
@@ -34,41 +34,12 @@
#include "utils/IConfig.hpp"
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
#include "Utils.hpp"
#define LOG(sev) LMS_LOG(SCROBBLING, sev) << "[listenbrainz] - "
namespace StringUtils
{
template<>
std::optional<std::chrono::seconds>
readAs(const std::string& str)
{
std::optional<std::chrono::seconds> res;
if (const std::optional<std::size_t> value {StringUtils::readAs<std::size_t>(str)})
res = std::chrono::seconds {*value};
return res;
}
}
namespace
{
std::optional<UUID>
getListenBrainzToken(Database::Session& session, Database::IdType userId)
{
auto transaction {session.createSharedTransaction()};
const Database::User::pointer user {Database::User::getById(session, userId)};
if (!user)
return std::nullopt;
if (user->getScrobbler() != Database::Scrobbler::ListenBrainz)
return std::nullopt;
return user->getListenBrainzToken();
}
bool
canBeScrobbled(Database::Session& session, Database::IdType trackId, std::chrono::seconds duration)
{
@@ -164,252 +135,105 @@ namespace
res = Wt::Json::serialize(root);
return res;
}
template <typename T>
std::optional<T>
headerReadAs(const Wt::Http::Message& msg, std::string_view headerName)
{
std::optional<T> res;
if (const std::string* headerValue {msg.getHeader(std::string {headerName})})
res = StringUtils::readAs<T>(*headerValue);
return res;
}
}
namespace Scrobbling
namespace Scrobbling::ListenBrainz
{
static const std::string historyTracklistName {"__scrobbler_listenbrainz_history__"};
ListenBrainzScrobbler::ListenBrainzScrobbler(Database::Db& db)
: _apiEndpoint {Service<IConfig>::get()->getString("listenbrainz-api-url", "https://api.listenbrainz.org/1/")}
Scrobbler::Scrobbler(boost::asio::io_context& ioContext, Database::Db& db)
: _ioContext {ioContext}
, _db {db}
, _sendQueue {_ioContext, Service<IConfig>::get()->getString("listenbrainz-api-base-url", "https://api.listenbrainz.org")}
, _listensSynchronizer {_ioContext, db, _sendQueue}
{
LOG(INFO) << "Starting ListenBrainz scrobbler... API endpoint = '" << _apiEndpoint << "'";
_client.done().connect([this](Wt::AsioWrapper::error_code ec, const Wt::Http::Message& msg)
{
onClientDone(ec, msg);
});
_ioService.setThreadCount(1);
_ioService.start();
LOG(INFO) << "Starting ListenBrainz scrobbler... API endpoint = '" << _sendQueue.getAPIBaseURL();
}
ListenBrainzScrobbler::~ListenBrainzScrobbler()
Scrobbler::~Scrobbler()
{
_ioService.stop();
LOG(INFO) << "Stopped ListenBrainz scrobbler";
LOG(INFO) << "Stopped ListenBrainz scrobbler!";
}
void
ListenBrainzScrobbler::listenStarted(const Listen& listen)
Scrobbler::listenStarted(const Listen& listen)
{
_ioService.post([=]
{
enqueListen(listen, Wt::WDateTime {});
});
enqueListen(listen, Wt::WDateTime {});
}
void
ListenBrainzScrobbler::listenFinished(const Listen& listen, std::optional<std::chrono::seconds> duration)
Scrobbler::listenFinished(const Listen& listen, std::optional<std::chrono::seconds> duration)
{
if (duration && !canBeScrobbled(_db.getTLSSession(), listen.trackId, *duration))
return;
Listen timedListen {listen};
const Listen timedListen {listen};
const Wt::WDateTime now {Wt::WDateTime::currentDateTime()};
_ioService.post([=]
{
enqueListen(timedListen, now);
});
enqueListen(timedListen, now);
}
void
ListenBrainzScrobbler::addListen(const Listen& listen, const Wt::WDateTime& timePoint)
Scrobbler::addTimedListen(const TimedListen& listen)
{
assert(timePoint.isValid());
_ioService.post([=]
{
enqueListen(listen, timePoint);
});
assert(listen.listenedAt.isValid());
enqueListen(listen, listen.listenedAt);
}
Wt::Dbo::ptr<Database::TrackList>
ListenBrainzScrobbler::getListensTrackList(Database::Session& session, Wt::Dbo::ptr<Database::User> user)
Database::TrackList::pointer
Scrobbler::getListensTrackList(Database::Session& session, Database::User::pointer user)
{
return Database::TrackList::get(session, historyTracklistName, Database::TrackList::Type::Internal, user);
return Utils::getListensTrackList(session, user);
}
void
ListenBrainzScrobbler::enqueListen(const Listen& listen, const Wt::WDateTime& timePoint)
Scrobbler::enqueListen(const Listen& listen, const Wt::WDateTime& timePoint)
{
if (!timePoint.isValid())
{
// If we are currently throttled, just replace the entry if it has no timePoint
// in order to only report the newest track listened to
// If not throttled, just search past the next current first message as it is being sent
const std::size_t offset {_state == State::Throttled ? std::size_t {0} : std::size_t {1}};
if (_sendQueue.size() > offset)
{
_sendQueue.erase(std::remove_if(std::next(std::begin(_sendQueue), offset), std::end(_sendQueue),
[&](const QueuedListen& queuedListen) { return queuedListen.listen.userId == listen.userId && !queuedListen.timePoint.isValid(); }), std::end(_sendQueue));
}
}
_sendQueue.emplace_back(QueuedListen {listen, timePoint});
LOG(DEBUG) << "listen queue size = " << _sendQueue.size();
if (_state == State::Idle)
sendNextQueuedListen();
}
void
ListenBrainzScrobbler::sendNextQueuedListen()
{
assert(_state == State::Idle);
while (!_sendQueue.empty())
{
if (sendListen(_sendQueue.front().listen, _sendQueue.front().timePoint))
{
_state = State::Sending;
break;
}
_sendQueue.pop_front();
}
}
bool
ListenBrainzScrobbler::sendListen(const Listen& listen, const Wt::WDateTime& timePoint)
{
Database::Session& session {_db.getTLSSession()};
const std::optional<UUID> listenBrainzToken {getListenBrainzToken(session, listen.userId)};
if (!listenBrainzToken)
return false;
std::string payload {listenToJsonString(session, listen, timePoint, timePoint.isValid() ? "single" : "playing_now")};
if (payload.empty())
{
LOG(DEBUG) << "Cannot convert listen to json: skipping";
return false;
}
// now send this
Wt::Http::Message message;
message.addHeader("Authorization", "Token " + std::string {listenBrainzToken->getAsString()});
message.addHeader("Content-Type", "application/json");
message.addBodyText(payload);
const std::string endPoint {_apiEndpoint + "submit-listens"};
if (!_client.post(endPoint, message))
{
LOG(ERROR) << "Cannot post to '" << endPoint << "': invalid scheme or URL?";
return false;
}
LOG(DEBUG) << "Listen POST done to '" << endPoint << "'";
return true;
}
void
ListenBrainzScrobbler::onClientDone(Wt::AsioWrapper::error_code ec, const Wt::Http::Message& msg)
{
assert(!_sendQueue.empty());
QueuedListen& queuedListen {_sendQueue.front()};
_state = State::Idle;
LOG(DEBUG) << "POST done. status = " << msg.status() << ", msg = '" << msg.body() << "'";
if (ec)
{
LOG(ERROR) << "Retry " << queuedListen.retryCount << ", client error: '" << ec.message() << "'";
// may be a network error, try again later
if (++queuedListen.retryCount > _maxRetryCount)
_sendQueue.pop_front();
throttle(_defaultRetryWaitDuration);
std::optional<SendQueue::RequestData> requestData {createSubmitListenRequestData(listen, timePoint)};
if (!requestData)
return;
}
bool mustThrottle{};
switch (msg.status())
SendQueue::Request submitListen {std::move(*requestData)};
if (timePoint.isValid())
{
case 429:
mustThrottle = true;
break;
case 200:
if (queuedListen.timePoint.isValid())
cacheListen(queuedListen.listen, queuedListen.timePoint);
_sendQueue.pop_front();
break;
default:
LOG(ERROR) << "Submit error: '" << msg.body() << "'";
_sendQueue.pop_front();
break;
}
const auto remainingCount {headerReadAs<std::size_t>(msg, "X-RateLimit-Remaining")};
LOG(DEBUG) << "Remaining messages = " << (remainingCount ? *remainingCount : 0);
if (mustThrottle || (remainingCount && *remainingCount == 0))
{
const auto waitDuration {headerReadAs<std::chrono::seconds>(msg, "X-RateLimit-Reset-In")};
throttle(waitDuration.value_or(_defaultRetryWaitDuration));
submitListen.setPriority(SendQueue::Request::Priority::Normal);
submitListen.setOnSuccessFunc([=](std::string_view)
{
_listensSynchronizer.saveListen(TimedListen {listen, timePoint});
});
}
else
{
sendNextQueuedListen();
// We want "listen now" to appear as soon as possible
submitListen.setPriority(SendQueue::Request::Priority::High);
}
_sendQueue.enqueueRequest(std::move(submitListen));
}
void
ListenBrainzScrobbler::throttle(std::chrono::seconds requestedDuration)
{
assert(_state == State::Idle);
const std::chrono::seconds duration {clamp(requestedDuration, _minRetryWaitDuration, _maxRetryWaitDuration)};
LOG(DEBUG) << "Throttling for " << duration.count() << " seconds";
_ioService.schedule(duration, [this]
{
_state = State::Idle;
sendNextQueuedListen();
});
_state = State::Throttled;
}
void
ListenBrainzScrobbler::cacheListen(const Listen& listen, const Wt::WDateTime& timePoint)
std::optional<SendQueue::RequestData>
Scrobbler::createSubmitListenRequestData(const Listen& listen, const Wt::WDateTime& timePoint)
{
Database::Session& session {_db.getTLSSession()};
auto transaction {session.createUniqueTransaction()};
const std::optional<UUID> listenBrainzToken {Utils::getListenBrainzToken(session, listen.userId)};
if (!listenBrainzToken)
return std::nullopt;
const Database::User::pointer user {Database::User::getById(session, listen.userId)};
if (!user)
return;
SendQueue::RequestData requestData;
requestData.endpoint = "/1/submit-listens";
requestData.type = SendQueue::RequestData::Type::POST;
const Database::Track::pointer track {Database::Track::getById(session, listen.trackId)};
if (!track)
return;
std::string bodyText {listenToJsonString(session, listen, timePoint, timePoint.isValid() ? "single" : "playing_now")};
if (bodyText.empty())
{
LOG(DEBUG) << "Cannot convert listen to json: skipping";
return std::nullopt;
}
Database::TrackList::pointer tracklist {getListensTrackList(session, user)};
if (!tracklist)
tracklist = Database::TrackList::create(session, historyTracklistName, Database::TrackList::Type::Internal, false, user);
requestData.message.addBodyText(bodyText);
requestData.message.addHeader("Authorization", "Token " + std::string {listenBrainzToken->getAsString()});
requestData.message.addHeader("Content-Type", "application/json");
Database::TrackListEntry::create(session, track, getListensTrackList(session, user), timePoint);
return requestData;
}
} // Scrobbling
} // namespace Scrobbling::ListenBrainz
@@ -19,12 +19,12 @@
#pragma once
#include <deque>
#include <Wt/Http/Client.h>
#include <Wt/WIOService.h>
#include <optional>
#include <boost/asio/io_context.hpp>
#include "IScrobbler.hpp"
#include "ListensSynchronizer.hpp"
#include "SendQueue.hpp"
namespace Database
{
@@ -33,59 +33,33 @@ namespace Database
class TrackList;
}
namespace Scrobbling
namespace Scrobbling::ListenBrainz
{
class ListenBrainzScrobbler final : public IScrobbler
class Scrobbler final : public IScrobbler
{
public:
ListenBrainzScrobbler(Database::Db& db);
~ListenBrainzScrobbler();
Scrobbler(boost::asio::io_context& ioContext, Database::Db& db);
~Scrobbler();
ListenBrainzScrobbler(const ListenBrainzScrobbler&) = delete;
ListenBrainzScrobbler(const ListenBrainzScrobbler&&) = delete;
ListenBrainzScrobbler& operator=(const ListenBrainzScrobbler&) = delete;
ListenBrainzScrobbler& operator=(const ListenBrainzScrobbler&&) = delete;
Scrobbler(const Scrobbler&) = delete;
Scrobbler(const Scrobbler&&) = delete;
Scrobbler& operator=(const Scrobbler&) = delete;
Scrobbler& operator=(const Scrobbler&&) = delete;
private:
void listenStarted(const Listen& listen) override;
void listenFinished(const Listen& listen, std::optional<std::chrono::seconds> duration) override;
void addListen(const Listen& listen, const Wt::WDateTime& timePoint) override;
void addTimedListen(const TimedListen& listen) override;
Wt::Dbo::ptr<Database::TrackList> getListensTrackList(Database::Session& session, Wt::Dbo::ptr<Database::User> user) override;
// Submit listens
void enqueListen(const Listen& listen, const Wt::WDateTime& timePoint);
void sendNextQueuedListen();
bool sendListen(const Listen& listen, const Wt::WDateTime& timePoint);
void onClientDone(Wt::AsioWrapper::error_code ec, const Wt::Http::Message& msg);
void throttle(std::chrono::seconds duration);
void cacheListen(const Listen& listen, const Wt::WDateTime& timePoint);
enum class State
{
Idle,
Throttled,
Sending,
};
State _state {State::Idle};
const std::string _apiEndpoint;
const std::size_t _maxRetryCount {2};
const std::chrono::seconds _defaultRetryWaitDuration {30};
const std::chrono::seconds _minRetryWaitDuration {1};
const std::chrono::seconds _maxRetryWaitDuration {300};
std::optional<SendQueue::RequestData> createSubmitListenRequestData(const Listen& listen, const Wt::WDateTime& timePoint);
boost::asio::io_context& _ioContext;
Database::Db& _db;
Wt::WIOService _ioService;
Wt::Http::Client _client {_ioService};
struct QueuedListen
{
Listen listen;
Wt::WDateTime timePoint;
std::size_t retryCount {};
};
std::deque<QueuedListen> _sendQueue;
SendQueue _sendQueue;
ListensSynchronizer _listensSynchronizer;
};
} // Scrobbling
} // Scrobbling::ListenBrainz
@@ -0,0 +1,524 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ListenBrainzScrobbler.hpp"
#include <boost/asio/bind_executor.hpp>
#include <Wt/Json/Array.h>
#include <Wt/Json/Object.h>
#include <Wt/Json/Value.h>
#include <Wt/Json/Serializer.h>
#include "database/Artist.hpp"
#include "database/Db.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackList.hpp"
#include "database/User.hpp"
#include "scrobbling/Exception.hpp"
#include "utils/IConfig.hpp"
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
#include "Utils.hpp"
#define LOG(sev) LMS_LOG(SCROBBLING, sev) << "[listenbrainz Synchronizer] - "
namespace
{
using namespace Scrobbling::ListenBrainz;
SendQueue::RequestData
createValidateTokenRequestData(std::string_view authToken)
{
SendQueue::RequestData requestData;
requestData.type = SendQueue::RequestData::Type::GET;
requestData.endpoint = "/1/validate-token";
requestData.headers = { {"Authorization", "Token " + std::string {authToken}} };
return requestData;
}
std::string
parseValidateToken(std::string_view msgBody)
{
std::string listenBrainzUserName;
Wt::Json::ParseError error;
Wt::Json::Object root;
if (!Wt::Json::parse(std::string {msgBody}, root, error))
{
LOG(ERROR) << "Cannot parse 'validate-token' result: " << error.what();
return listenBrainzUserName;
}
if (!root.get("valid").orIfNull(false))
{
LOG(INFO) << "Invalid listenbrainz user";
return listenBrainzUserName;
}
listenBrainzUserName = root.get("user_name").orIfNull("");
return listenBrainzUserName;
}
SendQueue::RequestData
createListenCountRequestData(std::string_view listenBrainzUserName)
{
LOG(DEBUG) << "Getting listen count for listenbrainz user '" << listenBrainzUserName << "'";
SendQueue::RequestData requestData;
requestData.type = SendQueue::RequestData::Type::GET;
requestData.endpoint = "/1/user/" + std::string {listenBrainzUserName} + "/listen-count";
return requestData;
}
std::optional<std::size_t>
parseListenCount(std::string_view msgBody)
{
try
{
Wt::Json::Object root;
Wt::Json::parse(std::string {msgBody}, root);
const Wt::Json::Object& payload {static_cast<const Wt::Json::Object&>(root.get("payload"))};
return static_cast<int>(payload.get("count"));
}
catch (const Wt::WException& e)
{
LOG(ERROR) << "Cannot parse listen count response: " << e.what();
return std::nullopt;
}
}
SendQueue::RequestData
createGetListensRequestData(std::string_view listenBrainzUserName, const Wt::WDateTime& maxDateTime)
{
LOG(DEBUG) << "Getting listens for listenbrainz user '" << listenBrainzUserName << "' with max_ts = " << maxDateTime.toString();
SendQueue::RequestData requestData;
requestData.type = SendQueue::RequestData::Type::GET;
requestData.endpoint = "/1/user/" + std::string {listenBrainzUserName} + "/listens?max_ts=" + std::to_string(maxDateTime.toTime_t());
return requestData;
}
Database::Track::pointer
tryMatchListen(Database::Session& session, const Wt::Json::Object& metadata)
{
Database::Track::pointer track;
// first try to get the associated track using MBIDs, and then fallback on names
if (metadata.type("additional_info") == Wt::Json::Type::Object)
{
const Wt::Json::Object& additionalInfo = metadata.get("additional_info");
if (std::optional<UUID> recordingMBID {UUID::fromString(additionalInfo.get("recording_mbid").orIfNull(""))})
{
const auto tracks {Database::Track::getByRecordingMBID(session, *recordingMBID)};
// if duplicated files, do not record it (let the user correct its database)
if (tracks.size() == 1)
track = tracks.front();
}
}
if (track)
return track;
// these fields are mandatory
const std::string trackName {static_cast<std::string>(metadata.get("track_name"))};
const std::string releaseName {static_cast<std::string>(metadata.get("release_name"))};
auto tracks {Database::Track::getByNameAndReleaseName(session, trackName, releaseName)};
if (tracks.size() > 1)
{
tracks.erase(std::remove_if(std::begin(tracks), std::end(tracks),
[&](const Database::Track::pointer track)
{
if (std::string artistName {metadata.get("artist_name").orIfNull("")}; !artistName.empty())
{
const auto& artists {track->getArtists({Database::TrackArtistLinkType::Artist})};
if (std::none_of(std::begin(artists), std::end(artists), [&](const Database::Artist::pointer& artist) { return artist->getName() == artistName; }))
return true;
}
if (metadata.type("additional_info") == Wt::Json::Type::Object)
{
const Wt::Json::Object& additionalInfo = metadata.get("additional_info");
if (track->getTrackNumber())
{
int otherTrackNumber {additionalInfo.get("tracknumber").orIfNull(-1)};
if (otherTrackNumber > 0 && static_cast<std::size_t>(otherTrackNumber) != *track->getTrackNumber())
return true;
}
if (auto releaseMBID {track->getRelease()->getMBID()})
{
if (std::optional<UUID> otherReleaseMBID {UUID::fromString(additionalInfo.get("release_mbid").orIfNull(""))})
{
if (otherReleaseMBID->getAsString() != releaseMBID->getAsString())
return true;
}
}
}
return false;
}), std::end(tracks));
}
if (tracks.size() == 1)
track = tracks.front();
return track;
}
struct ParseGetListensResult
{
Wt::WDateTime oldestEntry;
std::size_t listenCount{};
std::vector<Scrobbling::TimedListen> matchedListens;
};
ParseGetListensResult
parseGetListens(Database::Session& session, std::string_view msgBody, Database::IdType userId)
{
ParseGetListensResult result;
try
{
Wt::Json::Object root;
Wt::Json::parse(std::string {msgBody}, root);
const Wt::Json::Object& payload = root.get("payload");
const Wt::Json::Array& listens = payload.get("listens");
LOG(DEBUG) << "Got " << listens.size() << " listens";
if (listens.empty())
return result;
auto transaction {session.createSharedTransaction()};
for (const Wt::Json::Value& value : listens)
{
const Wt::Json::Object& listen = value;
const Wt::WDateTime listenedAt {Wt::WDateTime::fromTime_t(static_cast<int>(listen.get("listened_at")))};
const Wt::Json::Object& metadata = listen.get("track_metadata");
if (!listenedAt.isValid())
{
LOG(ERROR) << "bad listened_at field!";
continue;
}
result.listenCount++;
if (!result.oldestEntry.isValid())
result.oldestEntry = listenedAt;
else if (listenedAt < result.oldestEntry)
result.oldestEntry = listenedAt;
if (const Database::Track::pointer track {tryMatchListen(session, metadata)})
result.matchedListens.emplace_back(Scrobbling::TimedListen {userId, track.id(), listenedAt});
}
}
catch (const Wt::WException& error)
{
LOG(ERROR) << "Cannot parse 'get-listens' result: " << error.what();
}
return result;
}
}
namespace Scrobbling::ListenBrainz
{
ListensSynchronizer::ListensSynchronizer(boost::asio::io_context& ioContext, Database::Db& db, SendQueue& sendQueue)
: _ioContext {ioContext}
, _db {db}
, _sendQueue {sendQueue}
, _maxSyncListenCount {Service<IConfig>::get()->getULong("listenbrainz-max-sync-listen-count", 1000)}
, _syncListensPeriod {Service<IConfig>::get()->getULong("listenbrainz-sync-listens-period-hours", 1)}
{
LOG(INFO) << "Starting Listens synchronizer, maxSyncListenCount = " << _maxSyncListenCount << ", _syncListensPeriod = " << _syncListensPeriod.count() << " hours";
scheduleGetListens(std::chrono::seconds {30});
}
void
ListensSynchronizer::saveListen(const TimedListen& listen)
{
_strand.dispatch([=]
{
Database::Session& session {_db.getTLSSession()};
auto transaction {session.createUniqueTransaction()};
const Database::User::pointer user {Database::User::getById(session, listen.userId)};
if (!user)
return;
const Database::Track::pointer track {Database::Track::getById(session, listen.trackId)};
if (!track)
return;
Database::TrackListEntry::create(session, track, Utils::getOrCreateListensTrackList(session, user), listen.listenedAt);
UserContext& context {getUserContext(listen.userId)};
if (context.listenCount)
(*context.listenCount)++;
});
}
ListensSynchronizer::UserContext&
ListensSynchronizer::getUserContext(Database::IdType userId)
{
auto itContext {_userContexts.find(userId)};
if (itContext == std::cend(_userContexts))
{
auto [itNewContext, inserted] {_userContexts.emplace(userId, userId)};
itContext = itNewContext;
}
return itContext->second;
}
bool
ListensSynchronizer::isFetching() const
{
return std::any_of(std::cbegin(_userContexts), std::cend(_userContexts), [](const auto& contextEntry)
{
const auto& [userId, context] {contextEntry};
return context.fetching;
});
}
void
ListensSynchronizer::scheduleGetListens(std::chrono::seconds fromNow)
{
if (_syncListensPeriod.count() == 0 || _maxSyncListenCount == 0)
return;
LOG(DEBUG) << "Scheduled sync in " << fromNow.count() << " seconds...";
_getListensTimer.expires_after(fromNow);
_getListensTimer.async_wait(boost::asio::bind_executor(_strand, [this] (const boost::system::error_code& ec)
{
if (ec == boost::asio::error::operation_aborted)
{
LOG(DEBUG) << "getListens aborted";
return;
}
else if (ec)
{
throw Exception {"GetListens timer failure: " + std::string {ec.message()} };
}
startGetListens();
}));
}
void
ListensSynchronizer::startGetListens()
{
LOG(DEBUG) << "GetListens started!!!";
assert(!isFetching());
std::vector<Database::IdType> userIds;
{
Database::Session& session {_db.getTLSSession()};
auto transaction {session.createSharedTransaction()};
userIds = Database::User::getAllIds(_db.getTLSSession());
}
for (const Database::IdType userId : userIds)
{
if (Utils::getListenBrainzToken(_db.getTLSSession(), userId))
startGetListens(getUserContext(userId));
}
if (!isFetching())
scheduleGetListens(_syncListensPeriod);
}
void
ListensSynchronizer::startGetListens(UserContext& context)
{
context.fetching = true;
context.listenBrainzUserName = "";
context.maxDateTime = {};
context.fetchedListenCount = 0;
context.matchedListenCount = 0;
context.importedListenCount = 0;
enqueValidateToken(context);
}
void
ListensSynchronizer::onGetListensEnded(UserContext& context)
{
_strand.dispatch([this, &context]
{
LOG(DEBUG) << "Fetch done for user " << context.userId << ", fetched: " << context.fetchedListenCount << ", matched: " << context.matchedListenCount << ", imported: " << context.importedListenCount;
context.fetching = false;
if (!isFetching())
scheduleGetListens(_syncListensPeriod);
});
}
void
ListensSynchronizer::enqueValidateToken(UserContext& context)
{
assert(context.listenBrainzUserName.empty());
std::optional<SendQueue::RequestData> requestData {createValidateTokenRequestData(context.userId)};
if (!requestData)
{
onGetListensEnded(context);
return;
}
SendQueue::Request validateTokenRequest {std::move(*requestData)};
validateTokenRequest.setOnSuccessFunc([this, &context] (std::string_view msgBody)
{
context.listenBrainzUserName = parseValidateToken(msgBody);
if (context.listenBrainzUserName.empty())
{
onGetListensEnded(context);
return;
}
enqueGetListenCount(context);
});
validateTokenRequest.setOnFailureFunc([this, &context]
{
onGetListensEnded(context);
});
validateTokenRequest.setPriority(SendQueue::Request::Priority::Low);
_sendQueue.enqueueRequest(std::move(validateTokenRequest));
}
void
ListensSynchronizer::enqueGetListenCount(UserContext& context)
{
assert(!context.listenBrainzUserName.empty());
SendQueue::Request getListenCountRequest {createListenCountRequestData(context.listenBrainzUserName)};
getListenCountRequest.setOnSuccessFunc([=, &context] (std::string_view msgBody)
{
const auto listenCount = parseListenCount(msgBody);
if (listenCount)
LOG(DEBUG) << "Listen count for listenbrainz user '" << context.listenBrainzUserName << "' = " << *listenCount;
bool needSync {listenCount && (!context.listenCount || *context.listenCount != *listenCount)};
context.listenCount = listenCount;
if (!needSync)
{
onGetListensEnded(context);
return;
}
context.maxDateTime = Wt::WDateTime::currentDateTime();
enqueGetListens(context);
});
getListenCountRequest.setOnFailureFunc([this, &context]
{
onGetListensEnded(context);
});
getListenCountRequest.setPriority(SendQueue::Request::Priority::Low);
_sendQueue.enqueueRequest(std::move(getListenCountRequest));
}
void
ListensSynchronizer::enqueGetListens(UserContext& context)
{
assert(!context.listenBrainzUserName.empty());
SendQueue::Request getListensRequest {::createGetListensRequestData(context.listenBrainzUserName, context.maxDateTime)};
getListensRequest.setOnSuccessFunc([=, &context] (std::string_view msgBody)
{
processGetListensResponse(msgBody, context);
if (context.fetchedListenCount >= _maxSyncListenCount || !context.maxDateTime.isValid())
{
onGetListensEnded(context);
return;
}
enqueGetListens(context);
});
getListensRequest.setOnFailureFunc([=, &context]
{
onGetListensEnded(context);
});
getListensRequest.setPriority(SendQueue::Request::Priority::Low);
_sendQueue.enqueueRequest(std::move(getListensRequest));
}
std::optional<SendQueue::RequestData>
ListensSynchronizer::createValidateTokenRequestData(Database::IdType userId)
{
Database::Session& session {_db.getTLSSession()};
const std::optional<UUID> listenBrainzToken {Utils::getListenBrainzToken(session, userId)};
if (!listenBrainzToken)
return std::nullopt;
return ::createValidateTokenRequestData(listenBrainzToken->getAsString());
}
void
ListensSynchronizer::processGetListensResponse(std::string_view msgBody, UserContext& context)
{
Database::Session& session {_db.getTLSSession()};
const ParseGetListensResult parseResult {parseGetListens(session, msgBody, context.userId)};
context.fetchedListenCount += parseResult.listenCount;
context.matchedListenCount += parseResult.matchedListens.size();
context.maxDateTime = parseResult.oldestEntry;
if (parseResult.matchedListens.empty())
return;
auto transaction {session.createUniqueTransaction()};
Database::User::pointer user {Database::User::getById(session, context.userId)};
if (!user)
return;
Database::TrackList::pointer tracklist {Utils::getOrCreateListensTrackList(session, user)};
for (const TimedListen& listen : parseResult.matchedListens)
{
const Database::Track::pointer track {Database::Track::getById(session, listen.trackId)};
if (!track)
continue;
if (!tracklist->getEntryByTrackAndDateTime(track, listen.listenedAt))
{
context.importedListenCount++;
Database::TrackListEntry::create(session, track, tracklist, listen.listenedAt);
}
}
}
} // namespace Scrobbling::ListenBrainz
@@ -0,0 +1,97 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <optional>
#include <unordered_map>
#include <boost/asio/io_context.hpp>
#include <boost/asio/io_context_strand.hpp>
#include <boost/asio/steady_timer.hpp>
#include <Wt/Dbo/Dbo.h>
#include "database/Types.hpp"
#include "scrobbling/Listen.hpp"
#include "SendQueue.hpp"
namespace Database
{
class Db;
class Session;
class TrackList;
class User;
}
namespace Scrobbling::ListenBrainz
{
class ListensSynchronizer
{
public:
ListensSynchronizer(boost::asio::io_context& ioContext, Database::Db& db, SendQueue& sendQueue);
void saveListen(const TimedListen& listen);
private:
struct UserContext
{
UserContext(Database::IdType id) : userId {id} {}
UserContext(const UserContext&) = delete;
UserContext(UserContext&&) = delete;
UserContext& operator=(const UserContext&) = delete;
UserContext& operator=(UserContext&&) = delete;
const Database::IdType userId;
bool fetching {};
std::optional<std::size_t> listenCount {};
// resetted at each fetch
std::string listenBrainzUserName; // need to be resolved first
Wt::WDateTime maxDateTime;
std::size_t fetchedListenCount{};
std::size_t matchedListenCount{};
std::size_t importedListenCount{};
};
UserContext& getUserContext(Database::IdType userId);
bool isFetching() const;
void scheduleGetListens(std::chrono::seconds fromNow);
void startGetListens();
void startGetListens(UserContext& context);
void onGetListensEnded(UserContext& context);
void enqueValidateToken(UserContext& context);
void enqueGetListenCount(UserContext& context);
void enqueGetListens(UserContext& context);
std::optional<SendQueue::RequestData> createValidateTokenRequestData(Database::IdType userId);
std::optional<SendQueue::RequestData> createGetListensRequestData(std::string_view listenBrainzUserName, const Wt::WDateTime& maxDateTime);
void processGetListensResponse(std::string_view body, UserContext& context);
boost::asio::io_context& _ioContext;
boost::asio::io_context::strand _strand {_ioContext};
Database::Db& _db;
SendQueue& _sendQueue;
boost::asio::steady_timer _getListensTimer {_ioContext};
std::unordered_map<Database::IdType, UserContext> _userContexts;
const std::size_t _maxSyncListenCount;
const std::chrono::hours _syncListensPeriod;
};
} // Scrobbling::ListenBrainz
@@ -0,0 +1,242 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SendQueue.hpp"
#include <boost/asio/bind_executor.hpp>
#include "scrobbling/Exception.hpp"
#include "utils/Logger.hpp"
#include "utils/String.hpp"
#define LOG(sev) LMS_LOG(SCROBBLING, sev) << "[listenbrainz SendQueue] - "
namespace StringUtils
{
template<>
std::optional<std::chrono::seconds>
readAs(std::string_view str)
{
std::optional<std::chrono::seconds> res;
if (const std::optional<std::size_t> value {StringUtils::readAs<std::size_t>(str)})
res = std::chrono::seconds {*value};
return res;
}
}
namespace
{
template <typename T>
std::optional<T>
headerReadAs(const Wt::Http::Message& msg, std::string_view headerName)
{
std::optional<T> res;
if (const std::string* headerValue {msg.getHeader(std::string {headerName})})
res = StringUtils::readAs<T>(*headerValue);
return res;
}
}
namespace Scrobbling::ListenBrainz
{
SendQueue::SendQueue(boost::asio::io_context& ioContext, std::string_view apiBaseURL)
: _ioContext {ioContext}
, _apiBaseURL {apiBaseURL}
{
_client.done().connect([this](Wt::AsioWrapper::error_code ec, const Wt::Http::Message& msg)
{
_strand.dispatch([=, msg = std::move(msg)]
{
onClientDone(ec, msg);
});
});
}
SendQueue::~SendQueue()
{
_client.abort();
}
void
SendQueue::enqueueRequest(Request request)
{
_strand.dispatch([this, request = std::move(request)]()
{
_sendQueue[request._priority].emplace_back(std::move(request));
if (_state == State::Idle)
sendNextQueuedRequest();
});
}
void
SendQueue::sendNextQueuedRequest()
{
assert(_state == State::Idle);
for (auto& [prio, requests] : _sendQueue)
{
LOG(DEBUG) << "Processing prio " << static_cast<int>(prio) << ", request count = " << requests.size();
while (!requests.empty())
{
Request request {std::move(requests.front())};
requests.pop_front();
if (!sendRequest(request._requestData))
continue;
_state = State::Sending;
_currentRequest = std::move(request);
return;
}
}
}
bool
SendQueue::sendRequest(const RequestData& requestData)
{
const std::string url {_apiBaseURL + requestData.endpoint};
LOG(DEBUG) << "Sending request type " << (requestData.type == RequestData::Type::GET ? "GET" : "POST") << " to url '" << url << "'";
bool res{};
switch (requestData.type)
{
case RequestData::Type::GET:
res = _client.get(url, requestData.headers);
break;
case RequestData::Type::POST:
res = _client.post(url, requestData.message);
break;
}
if (!res)
LOG(ERROR) << "Send failed, bad url or unsupported scheme?";
return res;
}
void
SendQueue::onClientDone(Wt::AsioWrapper::error_code ec, const Wt::Http::Message& msg)
{
if (ec == boost::asio::error::operation_aborted)
{
LOG(DEBUG) << "SendQueue: client aborted";
return;
}
assert(_currentRequest);
Request request {std::move(*_currentRequest)};
_state = State::Idle;
LOG(DEBUG) << "Client done. status = " << msg.status();
if (ec)
onClientDoneError(std::move(request), ec);
else
onClientDoneSuccess(std::move(request), msg);
}
void
SendQueue::onClientDoneError(Request request, Wt::AsioWrapper::error_code ec)
{
LOG(ERROR) << "Retry " << request._retryCount << ", client error: '" << ec.message() << "'";
// may be a network error, try again later
throttle(_defaultRetryWaitDuration);
if (request._retryCount++ < _maxRetryCount)
{
_sendQueue[request._priority].emplace_front(std::move(request));
}
else
{
LOG(ERROR) << "Too many retries, giving up operation and throttle";
if (request._onFailureFunc)
request._onFailureFunc();
}
}
void
SendQueue::onClientDoneSuccess(Request request, const Wt::Http::Message& msg)
{
bool mustThrottle{};
if (msg.status() == 429)
{
_sendQueue[request._priority].emplace_front(std::move(request));
mustThrottle = true;
}
const auto remainingCount {headerReadAs<std::size_t>(msg, "X-RateLimit-Remaining")};
LOG(DEBUG) << "Remaining messages = " << (remainingCount ? *remainingCount : 0);
if (mustThrottle || (remainingCount && *remainingCount == 0))
{
const auto waitDuration {headerReadAs<std::chrono::seconds>(msg, "X-RateLimit-Reset-In")};
throttle(waitDuration.value_or(_defaultRetryWaitDuration));
}
if (!mustThrottle)
{
if (msg.status() == 200)
{
if (request._onSuccessFunc)
request._onSuccessFunc(msg.body());
}
else
{
LOG(ERROR) << "Send error: '" << msg.body() << "'";
if (request._onFailureFunc)
request._onFailureFunc();
}
}
if (_state == State::Idle)
sendNextQueuedRequest();
}
void
SendQueue::throttle(std::chrono::seconds requestedDuration)
{
assert(_state == State::Idle);
const std::chrono::seconds duration {clamp(requestedDuration, _minRetryWaitDuration, _maxRetryWaitDuration)};
LOG(DEBUG) << "Throttling for " << duration.count() << " seconds";
_throttleTimer.expires_after(duration);
_throttleTimer.async_wait([this](const boost::system::error_code& ec)
{
if (ec == boost::asio::error::operation_aborted)
{
LOG(DEBUG) << "SendQueue: throttle aborted";
return;
}
else if (ec)
{
throw Exception {"Throttle timer failure: " + std::string {ec.message()} };
}
_state = State::Idle;
sendNextQueuedRequest();
});
_state = State::Throttled;
}
} // namespace Scrobbling::ListenBrainz
@@ -0,0 +1,119 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <deque>
#include <boost/asio/io_context.hpp>
#include <boost/asio/io_context_strand.hpp>
#include <boost/asio/steady_timer.hpp>
#include <Wt/Http/Client.h>
namespace Scrobbling::ListenBrainz
{
class SendQueue
{
public:
SendQueue(boost::asio::io_context& ioContext, std::string_view apiBaseURL);
~SendQueue();
SendQueue(const SendQueue&) = delete;
SendQueue(const SendQueue&&) = delete;
SendQueue& operator=(const SendQueue&) = delete;
SendQueue& operator=(const SendQueue&&) = delete;
// generic queue operations
struct RequestData
{
enum class Type
{
GET,
POST,
};
Type type;
std::string endpoint; // relative URL to the base API
std::vector<Wt::Http::Message::Header> headers; // used by GET
Wt::Http::Message message; // used by POST
};
class Request
{
public:
enum class Priority
{
High,
Normal,
Low,
};
Request(RequestData requestData) : _requestData {std::move(requestData)} {}
using OnSuccessFunc = std::function<void(std::string_view msgBody)>;
using OnFailureFunc = std::function<void()>;
void setOnSuccessFunc(OnSuccessFunc onSuccessFunc) { _onSuccessFunc = onSuccessFunc; }
void setOnFailureFunc(OnFailureFunc onFailureFunc) { _onFailureFunc = onFailureFunc; }
void setPriority(Priority priority) { _priority = priority; }
private:
friend class SendQueue;
RequestData _requestData;
Priority _priority {Priority::Normal};
std::size_t _retryCount {};
OnSuccessFunc _onSuccessFunc;
OnFailureFunc _onFailureFunc;
};
std::string_view getAPIBaseURL() const { return _apiBaseURL; }
void enqueueRequest(Request request);
private:
void sendNextQueuedRequest();
bool sendRequest(const RequestData& request);
void onClientDone(Wt::AsioWrapper::error_code ec, const Wt::Http::Message& msg);
void onClientDoneError(Request request, Wt::AsioWrapper::error_code ec);
void onClientDoneSuccess(Request request, const Wt::Http::Message& msg);
void throttle(std::chrono::seconds duration);
const std::size_t _maxRetryCount {2};
const std::chrono::seconds _defaultRetryWaitDuration {30};
const std::chrono::seconds _minRetryWaitDuration {1};
const std::chrono::seconds _maxRetryWaitDuration {300};
enum class State
{
Idle,
Throttled,
Sending,
};
boost::asio::io_context& _ioContext;
boost::asio::io_context::strand _strand {_ioContext};
boost::asio::steady_timer _throttleTimer {_ioContext};
std::string _apiBaseURL;
State _state {State::Idle};
Wt::Http::Client _client {_ioContext};
std::map<Request::Priority, std::deque<Request>> _sendQueue;
std::optional<Request> _currentRequest;
};
} // namespace Scrobbling::ListenBrainz
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Utils.hpp"
#include <string_view>
#include "database/Session.hpp"
#include "database/TrackList.hpp"
#include "database/User.hpp"
static constexpr std::string_view historyTracklistName {"__scrobbler_listenbrainz_history__"};
namespace Scrobbling::ListenBrainz::Utils
{
std::optional<UUID>
getListenBrainzToken(Database::Session& session, Database::IdType userId)
{
auto transaction {session.createSharedTransaction()};
const Database::User::pointer user {Database::User::getById(session, userId)};
if (!user)
return std::nullopt;
if (user->getScrobbler() != Database::Scrobbler::ListenBrainz)
return std::nullopt;
return user->getListenBrainzToken();
}
Database::TrackList::pointer
getListensTrackList(Database::Session& session, Database::User::pointer user)
{
return Database::TrackList::get(session, historyTracklistName, Database::TrackList::Type::Internal, user);
}
Database::TrackList::pointer
getOrCreateListensTrackList(Database::Session& session, Database::User::pointer user)
{
Database::TrackList::pointer tracklist {getListensTrackList(session, user)};
if (!tracklist)
tracklist = Database::TrackList::create(session, historyTracklistName, Database::TrackList::Type::Internal, false, user);
return tracklist;
}
}
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <Wt/Dbo/ptr.h>
#include "utils/UUID.hpp"
#include "database/Types.hpp"
namespace Database
{
class Session;
class TrackList;
class User;
}
namespace Scrobbling::ListenBrainz::Utils
{
std::optional<UUID> getListenBrainzToken(Database::Session& session, Database::IdType userId);
Wt::Dbo::ptr<Database::TrackList> getOrCreateListensTrackList(Database::Session& session, Wt::Dbo::ptr<Database::User> user);
Wt::Dbo::ptr<Database::TrackList> getListensTrackList(Database::Session& session, Wt::Dbo::ptr<Database::User> user);
}
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "utils/Exception.hpp"
namespace Scrobbling
{
class Exception : public LmsException
{
public:
using LmsException::LmsException;
};
}
@@ -19,6 +19,8 @@
#pragma once
#include <boost/asio/io_service.hpp>
#include <chrono>
#include <memory>
#include <optional>
@@ -51,7 +53,7 @@ namespace Scrobbling
virtual void listenStarted(const Listen& listen) = 0;
virtual void listenFinished(const Listen& listen, std::optional<std::chrono::seconds> playedDuration = std::nullopt) = 0;
virtual void addListen(const Listen& listen, Wt::WDateTime timePoint) = 0;
virtual void addTimedListen(const TimedListen& listen) = 0;
// Stats
// From most recent to oldest
@@ -95,7 +97,7 @@ namespace Scrobbling
bool& moreResults) = 0;
};
std::unique_ptr<IScrobbling> createScrobbling(Database::Db& db);
std::unique_ptr<IScrobbling> createScrobbling(boost::asio::io_service& ioService, Database::Db& db);
} // ns Scrobbling
@@ -19,6 +19,8 @@
#pragma once
#include <Wt/WDateTime.h>
#include "database/Types.hpp"
namespace Scrobbling
@@ -28,5 +30,10 @@ namespace Scrobbling
Database::IdType userId {};
Database::IdType trackId {};
};
struct TimedListen : public Listen
{
Wt::WDateTime listenedAt;
};
} // ns Scrobbling
+2 -2
View File
@@ -23,14 +23,14 @@ namespace StringUtils
{
template<>
std::optional<API::Subsonic::Id>
readAs(const std::string& str)
readAs(std::string_view str)
{
return API::Subsonic::IdFromString(str);
}
template<>
std::optional<bool>
readAs(const std::string& str)
readAs(std::string_view str)
{
if (str == "true")
return true;
+2 -2
View File
@@ -90,10 +90,10 @@ namespace StringUtils
{
template<>
std::optional<API::Subsonic::Id>
readAs(const std::string& str);
readAs(std::string_view str);
template<>
std::optional<bool>
readAs(const std::string& str);
readAs(std::string_view str);
}
+2 -2
View File
@@ -28,12 +28,12 @@ namespace API::Subsonic
{
std::optional<Id>
IdFromString(const std::string& id)
IdFromString(const std::string_view id)
{
if (id == "root")
return Id {Id::Type::Root};
std::vector<std::string> values {StringUtils::splitString(id, "-")};
std::vector<std::string_view> values {StringUtils::splitString(id, "-")};
if (values.size() != 2)
return std::nullopt;
+1 -1
View File
@@ -41,7 +41,7 @@ struct Id
Database::IdType value {};
};
std::optional<Id> IdFromString(const std::string& id);
std::optional<Id> IdFromString(std::string_view id);
std::string IdToString(const Id& id);
} // namespace API::Subsonic
+16 -10
View File
@@ -71,7 +71,7 @@ namespace StringUtils
{
template<>
std::optional<API::Subsonic::ClientVersion>
readAs(const std::string& str)
readAs(std::string_view str)
{
// Expects "X.Y.Z"
const auto numbers {StringUtils::splitString(str, ".")};
@@ -312,6 +312,17 @@ formatToSuffix(AudioFormat format)
return "";
}
static
std::string
dateTimeToCreatedString(const Wt::WDateTime& dateTime)
{
const std::time_t t {dateTime.toTime_t()};
std::tm gmTime;
std::ostringstream oss;
oss << std::put_time(::gmtime_r(&t, &gmTime), "%FT%T");
return oss.str();
}
static
Response::Node
trackToResponseNode(const Track::pointer& track, Session& dbSession, const User::pointer& user)
@@ -365,6 +376,7 @@ trackToResponseNode(const Track::pointer& track, Session& dbSession, const User:
trackResponse.setAttribute("duration", std::chrono::duration_cast<std::chrono::seconds>(track->getDuration()).count());
trackResponse.setAttribute("type", "music");
trackResponse.setAttribute("created", dateTimeToCreatedString(track->getLastWritten()));
if (user->hasStarredTrack(track))
trackResponse.setAttribute("starred", reportedStarredDate);
@@ -415,13 +427,7 @@ releaseToResponseNode(const Release::pointer& release, Session& dbSession, const
albumNode.setAttribute("isDir", true);
}
{
std::time_t t {release->getLastWritten().toTime_t()};
std::tm gmTime;
std::ostringstream oss; oss << std::put_time(::gmtime_r(&t, &gmTime), "%FT%T");
albumNode.setAttribute("created", oss.str());
}
albumNode.setAttribute("created", dateTimeToCreatedString(release->getLastWritten()));
albumNode.setAttribute("id", IdToString({Id::Type::Release, release.id()}));
albumNode.setAttribute("coverArt", IdToString({Id::Type::Release, release.id()}));
auto releaseYear {release->getReleaseYear()};
@@ -1454,7 +1460,7 @@ handleSearchRequestCommon(RequestContext& context, bool id3)
// Mandatory params
std::string query {getMandatoryParameterAs<std::string>(context.parameters, "query")};
std::vector<std::string> keywords {StringUtils::splitString(query, " ")};
std::vector<std::string_view> keywords {StringUtils::splitString(query, " ")};
// Optional params
std::size_t artistCount {getParameterAs<std::size_t>(context.parameters, "artistCount").value_or(20)};
@@ -1679,7 +1685,7 @@ handleScrobble(RequestContext& context)
{
const Database::IdType trackId {ids[i].value};
const unsigned long time {times[i]};
Service<Scrobbling::IScrobbling>::get()->addListen({context.userId, trackId}, Wt::WDateTime::fromTime_t(static_cast<std::time_t>(time / 1000)));
Service<Scrobbling::IScrobbling>::get()->addTimedListen({context.userId, trackId, Wt::WDateTime::fromTime_t(static_cast<std::time_t>(time / 1000))});
}
}
}
+1
View File
@@ -4,6 +4,7 @@ add_library(lmsutils SHARED
impl/ChildProcessManager.cpp
impl/Config.cpp
impl/FileResourceHandler.cpp
impl/IOContextRunner.cpp
impl/Logger.cpp
impl/NetAddress.cpp
impl/Path.cpp
+4 -32
View File
@@ -25,42 +25,14 @@
std::unique_ptr<IChildProcessManager>
createChildProcessManager()
createChildProcessManager(boost::asio::io_context& ioContext)
{
return std::make_unique<ChildProcessManager>();
return std::make_unique<ChildProcessManager>(ioContext);
}
ChildProcessManager::ChildProcessManager()
: _work {boost::asio::make_work_guard(_ioContext)}
ChildProcessManager::ChildProcessManager(boost::asio::io_context& ioContext)
: _ioContext {ioContext}
{
start();
}
ChildProcessManager::~ChildProcessManager()
{
stop();
}
void
ChildProcessManager::start()
{
LMS_LOG(CHILDPROCESS, INFO) << "Starting child process manager...";
_thread = std::make_unique<std::thread>([&]()
{
_ioContext.run();
});
LMS_LOG(CHILDPROCESS, INFO) << "Child process manager started!";
}
void
ChildProcessManager::stop()
{
LMS_LOG(CHILDPROCESS, INFO) << "Stopping child process manager";
_work.reset();
_thread->join();
LMS_LOG(CHILDPROCESS, INFO) << "Stopped child process manager";
}
std::unique_ptr<IChildProcess>
+3 -9
View File
@@ -23,15 +23,14 @@
#include <thread>
#include <boost/asio/io_context.hpp>
#include <boost/asio/executor_work_guard.hpp>
#include "utils/IChildProcessManager.hpp"
class ChildProcessManager : public IChildProcessManager
{
public:
ChildProcessManager();
~ChildProcessManager();
ChildProcessManager(boost::asio::io_context& ioContext);
~ChildProcessManager() = default;
ChildProcessManager(const ChildProcessManager&) = delete;
ChildProcessManager(ChildProcessManager&&) = delete;
@@ -41,12 +40,7 @@ class ChildProcessManager : public IChildProcessManager
private:
std::unique_ptr<IChildProcess> spawnChildProcess(const std::filesystem::path& path, const IChildProcess::Args& args) override;
void start();
void stop();
boost::asio::io_context _ioContext;
std::unique_ptr<std::thread> _thread;
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> _work;
boost::asio::io_context& _ioContext;
};
+63
View File
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utils/IOContextRunner.hpp"
#include <cstdlib>
#include "utils/Logger.hpp"
IOContextRunner::IOContextRunner(boost::asio::io_service& ioService, std::size_t threadCount)
: _ioService {ioService}
, _work {ioService}
{
LMS_LOG(UTILS, INFO) << "Starting IO Context with " << threadCount << " threads...";
for (std::size_t i {}; i < threadCount; ++i)
{
_threads.emplace_back([&]
{
try
{
_ioService.run();
}
catch (const std::exception& e)
{
LMS_LOG(UTILS, FATAL) << "Exception caught in IO context: " << e.what();
std::abort();
}
});
}
}
void
IOContextRunner::stop()
{
LMS_LOG(UTILS, INFO) << "Stopping IO Context";
_work.reset();
_ioService.stop();
LMS_LOG(UTILS, INFO) << "Stopped IO Context";
}
IOContextRunner::~IOContextRunner()
{
stop();
for (std::thread& t : _threads)
t.join();
}
+51 -10
View File
@@ -19,6 +19,7 @@
#include "utils/String.hpp"
#include <algorithm>
#include <iomanip>
#include <unordered_map>
@@ -57,13 +58,13 @@ readList(const std::string& str, const std::string& separators, std::list<std::s
template<>
std::optional<std::string>
readAs(const std::string& str)
readAs(std::string_view str)
{
return str;
return std::string {str};
}
std::vector<std::string>
splitString(const std::string& string, const std::string& separators)
splitStringCopy(std::string_view string, std::string_view separators)
{
std::string str {stringTrim(string, separators)};
@@ -73,6 +74,29 @@ splitString(const std::string& string, const std::string& separators)
return res;
}
std::vector<std::string_view>
splitString(std::string_view str, std::string_view separators)
{
std::vector<std::string_view> res;
std::string_view::size_type strBegin {};
while ((strBegin = str.find_first_not_of(separators, strBegin)) != std::string_view::npos)
{
auto strEnd {str.find_first_of(separators, strBegin + 1)};
if (strEnd == std::string_view::npos)
{
res.push_back(str.substr(strBegin, str.size() - strBegin));
break;
}
res.push_back(str.substr(strBegin, strEnd - strBegin));
strBegin = strEnd + 1;
}
return res;
}
std::string
joinStrings(const std::vector<std::string>& strings, const std::string& delimiter)
{
@@ -80,22 +104,22 @@ joinStrings(const std::vector<std::string>& strings, const std::string& delimite
}
std::string
stringTrim(const std::string& str, const std::string& whitespace)
stringTrim(std::string_view str, std::string_view whitespaces)
{
const auto strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
const auto strBegin = str.find_first_not_of(whitespaces);
if (strBegin == std::string_view::npos)
return ""; // no content
const auto strEnd = str.find_last_not_of(whitespace);
const auto strEnd = str.find_last_not_of(whitespaces);
const auto strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
return std::string {str.substr(strBegin, strRange)};
}
std::string
stringTrimEnd(const std::string& str, const std::string& whitespace)
stringTrimEnd(std::string_view str, std::string_view whitespaces)
{
return str.substr(0, str.find_last_not_of(whitespace)+1);
return std::string {str.substr(0, str.find_last_not_of(whitespaces) + 1)};
}
std::string
@@ -185,6 +209,23 @@ jsEscape(const std::string& str)
return escaped;
}
std::string
escapeString(std::string_view str, std::string_view charsToEscape, char escapeChar)
{
std::string res;
res.reserve(str.size());
for (const char c : str)
{
if (std::any_of(std::cbegin(charsToEscape), std::cend(charsToEscape), [c](char charToEscape) { return c == charToEscape; }))
res += escapeChar;
res += c;
}
return res;
}
bool
stringEndsWith(const std::string& str, const std::string& ending)
{
+1 -1
View File
@@ -27,7 +27,7 @@ namespace StringUtils
{
template <>
std::optional<UUID>
readAs(const std::string& str)
readAs(std::string_view str)
{
return UUID::fromString(str);
}
@@ -20,10 +20,7 @@
#include <filesystem>
#include <memory>
#pragma once
#include <filesystem>
#include <memory>
#include <boost/asio/io_service.hpp>
#include "IChildProcess.hpp"
@@ -35,6 +32,6 @@ class IChildProcessManager
virtual std::unique_ptr<IChildProcess> spawnChildProcess(const std::filesystem::path& path, const IChildProcess::Args& args) = 0;
};
std::unique_ptr<IChildProcessManager> createChildProcessManager();
std::unique_ptr<IChildProcessManager> createChildProcessManager(boost::asio::io_service& ioService);
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <optional>
#include <thread>
#include <boost/asio/io_service.hpp>
class IOContextRunner
{
public:
IOContextRunner(boost::asio::io_service& ioService, std::size_t threadCount);
~IOContextRunner();
IOContextRunner(const IOContextRunner&) = delete;
IOContextRunner(IOContextRunner&&) = delete;
IOContextRunner& operator=(const IOContextRunner&) = delete;
IOContextRunner& operator=(IOContextRunner&&) = delete;
void stop();
private:
boost::asio::io_service& _ioService;
std::optional<boost::asio::io_service::work> _work;
std::vector<std::thread> _threads;
};
+13 -7
View File
@@ -19,6 +19,7 @@
#pragma once
#include <initializer_list>
#include <optional>
#include <string>
#include <string_view>
@@ -31,16 +32,19 @@
namespace StringUtils {
std::vector<std::string>
splitString(const std::string& string, const std::string& separators);
splitStringCopy(std::string_view string, std::string_view separators);
std::vector<std::string_view>
splitString(std::string_view string, std::string_view separators);
std::string
joinStrings(const std::vector<std::string>& strings, const std::string& delimiter);
std::string
stringTrim(const std::string& str, const std::string& whitespaces = " \t");
stringTrim(std::string_view str, std::string_view whitespaces = " \t");
std::string
stringTrimEnd(const std::string& str, const std::string& whitespaces = " \t");
stringTrimEnd(std::string_view str, std::string_view whitespaces = " \t");
std::string
stringToLower(std::string_view str);
@@ -55,11 +59,11 @@ std::string
bufferToString(const std::vector<unsigned char>& data);
template<typename T>
std::optional<T> readAs(const std::string& str)
std::optional<T> readAs(std::string_view str)
{
T res;
std::istringstream iss ( str );
std::istringstream iss {std::string {str}};
iss >> res;
if (iss.fail())
return std::nullopt;
@@ -69,15 +73,17 @@ std::optional<T> readAs(const std::string& str)
template<>
std::optional<std::string>
readAs(const std::string& str);
readAs(std::string_view str);
[[nodiscard]]
std::string
replaceInString(const std::string& str, const std::string& from, const std::string& to);
std::string
jsEscape(const std::string& str);
std::string
escapeString(std::string_view str, std::string_view charsToEscape, char escapeChar);
bool
stringEndsWith(const std::string& str, const std::string& ending);
+1 -1
View File
@@ -42,6 +42,6 @@ namespace StringUtils
{
template <>
std::optional<UUID>
readAs(const std::string& str);
readAs(std::string_view str);
}
+5
View File
@@ -15,21 +15,26 @@ add_executable(lms
ui/admin/UserView.cpp
ui/admin/UsersView.cpp
ui/common/DirectoryValidator.cpp
ui/common/InfiniteScrollingContainer.cpp
ui/common/LoadingIndicator.cpp
ui/common/LoginNameValidator.cpp
ui/common/MandatoryValidator.cpp
ui/common/PasswordValidator.cpp
ui/common/UUIDValidator.cpp
ui/explore/ArtistCollector.cpp
ui/explore/ArtistListHelpers.cpp
ui/explore/ArtistView.cpp
ui/explore/ArtistsView.cpp
ui/explore/DatabaseCollectorBase.cpp
ui/explore/Explore.cpp
ui/explore/Filters.cpp
ui/explore/ReleaseCollector.cpp
ui/explore/ReleaseListHelpers.cpp
ui/explore/ReleasePopup.cpp
ui/explore/ReleasesView.cpp
ui/explore/ReleaseView.cpp
ui/explore/SearchView.cpp
ui/explore/TrackCollector.cpp
ui/explore/TrackListHelpers.cpp
ui/explore/TrackPopup.cpp
ui/explore/TracksView.cpp
+12 -5
View File
@@ -19,6 +19,7 @@
#include <thread>
#include <boost/asio/io_context.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <Wt/WServer.h>
@@ -38,6 +39,7 @@
#include "ui/LmsApplicationManager.hpp"
#include "utils/IChildProcessManager.hpp"
#include "utils/IConfig.hpp"
#include "utils/IOContextRunner.hpp"
#include "utils/Service.hpp"
#include "utils/String.hpp"
#include "utils/WtLogger.hpp"
@@ -212,9 +214,12 @@ int main(int argc, char* argv[])
wtArgv[i] = wtServerArgs[i].c_str();
}
boost::asio::io_context ioContext; // ioContext used to dispatch all the services that are out of the Wt event loop
Wt::WServer server {argv[0]};
server.setServerConfiguration(wtServerArgs.size(), const_cast<char**>(&wtArgv[0]));
IOContextRunner ioContextRunner {ioContext, std::max<unsigned long>(2, std::thread::hardware_concurrency())};
// Initializing a connection pool to the database that will be shared along services
Database::Db database {config->getPath("working-dir") / "lms.db"};
{
@@ -226,7 +231,7 @@ int main(int argc, char* argv[])
UserInterface::LmsApplicationManager appManager;
// Service initialization order is important (reverse-order for deinit)
Service<IChildProcessManager> childProcessManagerService {createChildProcessManager()};
Service<IChildProcessManager> childProcessManagerService {createChildProcessManager(ioContext)};
Service<Auth::IAuthTokenService> authTokenService;
Service<Auth::IPasswordService> authPasswordService;
@@ -251,7 +256,7 @@ int main(int argc, char* argv[])
config->getULong("cover-max-file-size", 10) * 1000 * 1000,
config->getULong("cover-jpeg-quality", 75))};
Service<Recommendation::IEngine> recommendationEngineService {Recommendation::createEngine(database)};
Service<Scanner::IScanner> scannerService {Scanner::createScanner(database, *recommendationEngineService)};
Service<Scanner::IScanner> scannerService {Scanner::createScanner(/*ioContext,*/ database, *recommendationEngineService)};
scannerService->getEvents().scanComplete.connect([&]
{
@@ -260,7 +265,7 @@ int main(int argc, char* argv[])
coverArtService->flushCache();
});
Service<Scrobbling::IScrobbling> scrobblingService {Scrobbling::createScrobbling(database)};
Service<Scrobbling::IScrobbling> scrobblingService {Scrobbling::createScrobbling(ioContext, database)};
API::Subsonic::SubsonicResource subsonicResource {database};
@@ -289,13 +294,15 @@ int main(int argc, char* argv[])
LMS_LOG(MAIN, INFO) << "Quitting...";
res = EXIT_SUCCESS;
}
catch(Wt::WServer::Exception& e)
catch (const Wt::WServer::Exception& e)
{
LMS_LOG(MAIN, FATAL) << "Caught WServer::Exception: " << e.what();
std::cerr << "Caught a WServer::Exception: " << e.what() << std::endl;
res = EXIT_FAILURE;
}
catch(std::exception& e)
catch (const std::exception& e)
{
LMS_LOG(MAIN, FATAL) << "Caught std::exception: " << e.what();
std::cerr << "Caught std::exception: " << e.what() << std::endl;
res = EXIT_FAILURE;
}
+8 -2
View File
@@ -277,7 +277,10 @@ LmsApplication::finalize()
Wt::WLink
LmsApplication::createArtistLink(Database::Artist::pointer artist)
{
return Wt::WLink {Wt::LinkType::InternalPath, "/artist/" + std::to_string(artist.id())};
if (const auto mbid {artist->getMBID()})
return Wt::WLink {Wt::LinkType::InternalPath, "/artist/mbid/" + std::string {mbid->getAsString()}};
else
return Wt::WLink {Wt::LinkType::InternalPath, "/artist/" + std::to_string(artist.id())};
}
std::unique_ptr<Wt::WAnchor>
@@ -298,7 +301,10 @@ LmsApplication::createArtistAnchor(Database::Artist::pointer artist, bool addTex
Wt::WLink
LmsApplication::createReleaseLink(Database::Release::pointer release)
{
return Wt::WLink {Wt::LinkType::InternalPath, "/release/" + std::to_string(release.id())};
if (const auto mbid {release->getMBID()})
return Wt::WLink {Wt::LinkType::InternalPath, "/release/mbid/" + std::string {mbid->getAsString()}};
else
return Wt::WLink {Wt::LinkType::InternalPath, "/release/" + std::to_string(release.id())};
}
std::unique_ptr<Wt::WAnchor>
+3 -3
View File
@@ -34,19 +34,19 @@ class LmsApplicationException : public LmsException
class ArtistNotFoundException : public LmsApplicationException
{
public:
ArtistNotFoundException(Database::IdType artistId) : LmsApplicationException {Wt::WString::tr("Lms.Error.artist-not-found").arg(artistId)} {}
ArtistNotFoundException() : LmsApplicationException {Wt::WString::tr("Lms.Error.artist-not-found")} {}
};
class ReleaseNotFoundException : public LmsApplicationException
{
public:
ReleaseNotFoundException(Database::IdType releaseId) : LmsApplicationException {Wt::WString::tr("Lms.Error.release-not-found").arg(releaseId)} {}
ReleaseNotFoundException() : LmsApplicationException {Wt::WString::tr("Lms.Error.release-not-found")} {}
};
class UserNotFoundException : public LmsApplicationException
{
public:
UserNotFoundException(Database::IdType userId) : LmsApplicationException {Wt::WString::tr("Lms.Error.user-not-found").arg(userId)} {}
UserNotFoundException() : LmsApplicationException {Wt::WString::tr("Lms.Error.user-not-found")} {}
};
class UserNotAllowedException : public LmsApplicationException
+18 -39
View File
@@ -33,7 +33,7 @@
#include "utils/Service.hpp"
#include "utils/String.hpp"
#include "common/LoadingIndicator.hpp"
#include "common/InfiniteScrollingContainer.hpp"
#include "resource/CoverResource.hpp"
#include "resource/DownloadResource.hpp"
#include "LmsApplication.hpp"
@@ -67,8 +67,12 @@ PlayQueue::PlayQueue()
clearTracks();
});
_entriesContainer = bindNew<Wt::WContainerWidget>("entries");
hideLoadingIndicator();
_entriesContainer = bindNew<InfiniteScrollingContainer>("entries");
_entriesContainer->onRequestElements.connect([this]
{
addSome();
updateCurrentTrack(true);
});
Wt::WText* shuffleBtn = bindNew<Wt::WText>("shuffle-btn", Wt::WString::tr("Lms.PlayQueue.template.shuffle-btn"), Wt::TextFormat::XHTML);
setToolTip(*shuffleBtn, Wt::WString::tr("Lms.PlayQueue.shuffle"));
@@ -184,27 +188,6 @@ PlayQueue::updateRadioBtn()
_radioBtn->toggleStyleClass("text-muted", !_radioMode);
}
void
PlayQueue::displayLoadingIndicator()
{
_loadingIndicator = bindWidget<Wt::WTemplate>("loading-indicator", createLoadingIndicator());
_loadingIndicator->scrollVisibilityChanged().connect([this](bool visible)
{
if (!visible)
return;
addSome();
updateCurrentTrack(true);
});
}
void
PlayQueue::hideLoadingIndicator()
{
_loadingIndicator = nullptr;
bindEmpty("loading-indicator");
}
Database::TrackList::pointer
PlayQueue::getTrackList() const
{
@@ -226,7 +209,6 @@ PlayQueue::clearTracks()
getTrackList().modify()->clear();
}
hideLoadingIndicator();
_entriesContainer->clear();
updateInfo();
}
@@ -322,10 +304,10 @@ PlayQueue::updateInfo()
void
PlayQueue::updateCurrentTrack(bool selected)
{
if (!_trackPos || *_trackPos >= static_cast<std::size_t>(_entriesContainer->count()))
if (!_trackPos || *_trackPos >= static_cast<std::size_t>(_entriesContainer->getCount()))
return;
Wt::WTemplate* entry {static_cast<Wt::WTemplate*>(_entriesContainer->widget(*_trackPos))};
Wt::WTemplate* entry {static_cast<Wt::WTemplate*>(_entriesContainer->getWidget(*_trackPos))};
if (entry)
entry->bindString("is-selected", selected ? "Lms-playqueue-selected" : "");
}
@@ -410,14 +392,11 @@ PlayQueue::addSome()
auto tracklist = getTrackList();
auto tracklistEntries = tracklist->getEntries(_entriesContainer->count(), 50);
auto tracklistEntries = tracklist->getEntries(_entriesContainer->getCount(), _batchSize);
for (const Database::TrackListEntry::pointer& tracklistEntry : tracklistEntries)
addEntry(tracklistEntry);
if (static_cast<std::size_t>(_entriesContainer->count()) < tracklist->getCount())
displayLoadingIndicator();
else
hideLoadingIndicator();
_entriesContainer->setHasMore(_entriesContainer->getCount() < tracklist->getCount());
}
void
@@ -474,9 +453,9 @@ PlayQueue::addEntry(const Database::TrackListEntry::pointer& tracklistEntry)
Wt::WText* playBtn {entry->bindNew<Wt::WText>("play-btn", Wt::WString::tr("Lms.PlayQueue.template.play-btn"), Wt::TextFormat::XHTML)};
playBtn->clicked().connect([=]
{
auto pos = _entriesContainer->indexOf(entry);
if (pos >= 0)
loadTrack(pos, true);
const std::optional<std::size_t> pos {_entriesContainer->getIndexOf(*entry)};
if (pos)
loadTrack(*pos, true);
});
Wt::WText* delBtn {entry->bindNew<Wt::WText>("del-btn", Wt::WString::tr("Lms.PlayQueue.template.delete-btn"), Wt::TextFormat::XHTML)};
@@ -492,12 +471,12 @@ PlayQueue::addEntry(const Database::TrackListEntry::pointer& tracklistEntry)
if (_trackPos)
{
auto pos {_entriesContainer->indexOf(entry)};
if (pos > 0 && *_trackPos >= static_cast<std::size_t>(pos))
(*_trackPos)--;
const std::optional<std::size_t> pos {_entriesContainer->getIndexOf(*entry)};
if (pos && *_trackPos >= *pos)
(*_trackPos)--;
}
_entriesContainer->removeWidget(entry);
_entriesContainer->remove(*entry);
updateInfo();
});
+8 -6
View File
@@ -28,11 +28,13 @@
#include "database/Types.hpp"
#include "PlayQueueAction.hpp"
namespace Similarity {
namespace Similarity
{
class Finder;
}
namespace Database {
namespace Database
{
class Track;
class TrackList;
class TrackListEntry;
@@ -40,6 +42,8 @@ namespace Database {
namespace UserInterface {
class InfiniteScrollingContainer;
class PlayQueue : public Wt::WTemplate
{
public:
@@ -72,8 +76,6 @@ class PlayQueue : public Wt::WTemplate
void updateCurrentTrack(bool selected);
void updateRepeatBtn();
void updateRadioBtn();
void displayLoadingIndicator();
void hideLoadingIndicator();
void loadTrack(std::size_t pos, bool play);
void stop();
@@ -83,13 +85,13 @@ class PlayQueue : public Wt::WTemplate
std::optional<float> getReplayGain(std::size_t pos, const Wt::Dbo::ptr<Database::Track>& track) const;
static inline constexpr std::size_t _nbMaxEntries {1000};
static inline constexpr std::size_t _batchSize {12};
bool _repeatAll {};
bool _radioMode {};
bool _mediaPlayerSettingsLoaded {};
Database::IdType _tracklistId {};
Wt::WContainerWidget* _entriesContainer {};
Wt::WTemplate* _loadingIndicator {};
InfiniteScrollingContainer* _entriesContainer {};
Wt::WText* _nbTracks {};
Wt::WText* _repeatBtn {};
Wt::WText* _radioBtn {};
+1 -1
View File
@@ -139,7 +139,7 @@ class DatabaseSettingsModel : public Wt::WFormModel
if (recommendationEngineTypeRow)
scanSettings.modify()->setRecommendationEngineType(_recommendationEngineTypeModel->getValue(*recommendationEngineTypeRow));
auto clusterTypes {StringUtils::splitString(valueText(TagsField).toUTF8(), " ")};
auto clusterTypes {StringUtils::splitStringCopy(valueText(TagsField).toUTF8(), " ")};
scanSettings.modify()->setClusterTypes(LmsApp->getDbSession(), std::set<std::string>(clusterTypes.begin(), clusterTypes.end()));
}
+3 -3
View File
@@ -84,7 +84,7 @@ class UserModel : public Wt::WFormModel
// Update user
Database::User::pointer user {Database::User::getById(LmsApp->getDbSession(), *_userId)};
if (!user)
throw UserNotFoundException {*_userId};
throw UserNotFoundException {};
if (_authPasswordService && !valueText(PasswordField).empty())
_authPasswordService->setPassword(LmsApp->getDbSession(), user.id(), valueText(PasswordField).toUTF8());
@@ -117,7 +117,7 @@ class UserModel : public Wt::WFormModel
const Database::User::pointer user {Database::User::getById(LmsApp->getDbSession(), *_userId)};
if (!user)
throw UserNotFoundException {*_userId};
throw UserNotFoundException {};
else if (user == LmsApp->getUser())
throw UserNotAllowedException {};
}
@@ -215,7 +215,7 @@ UserView::refreshView()
const Database::User::pointer user {Database::User::getById(LmsApp->getDbSession(), *userId)};
if (!user)
throw UserNotFoundException {*userId};
throw UserNotFoundException {};
t->bindString("title", Wt::WString::tr("Lms.Admin.User.user-edit").arg(user->getLoginName()), Wt::TextFormat::Plain);
t->setCondition("if-has-last-login", true);
@@ -0,0 +1,101 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "InfiniteScrollingContainer.hpp"
#include "LoadingIndicator.hpp"
namespace UserInterface
{
InfiniteScrollingContainer::InfiniteScrollingContainer(const Wt::WString& text)
: Wt::WTemplate {text}
, _elements {bindNew<Wt::WContainerWidget>("elements")}
, _loadingIndicator {bindWidget<Wt::WTemplate>("loading-indicator", createLoadingIndicator())}
{
hideLoadingIndicator();
}
void
InfiniteScrollingContainer::clear()
{
_elements->clear();
hideLoadingIndicator();
}
std::size_t
InfiniteScrollingContainer::getCount()
{
return _elements->count();
}
void
InfiniteScrollingContainer::add(std::unique_ptr<Wt::WWidget> result)
{
return _elements->addWidget(std::move(result));
}
void
InfiniteScrollingContainer::setHasMore(bool hasMore)
{
if (hasMore)
displayLoadingIndicator();
else
hideLoadingIndicator();
}
void
InfiniteScrollingContainer::remove(Wt::WWidget& widget)
{
_elements->removeWidget(&widget);
}
Wt::WWidget*
InfiniteScrollingContainer::getWidget(std::size_t pos) const
{
return _elements->widget(pos);
}
std::optional<std::size_t>
InfiniteScrollingContainer::getIndexOf(Wt::WWidget& widget) const
{
return _elements->indexOf(&widget);
}
void
InfiniteScrollingContainer::displayLoadingIndicator()
{
_loadingIndicator = bindWidget<Wt::WTemplate>("loading-indicator", createLoadingIndicator());
_loadingIndicator->scrollVisibilityChanged().connect([this](bool visible)
{
if (!visible)
return;
onRequestElements.emit();
});
}
void
InfiniteScrollingContainer::hideLoadingIndicator()
{
_loadingIndicator = nullptr;
bindEmpty("loading-indicator");
}
}
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <optional>
#include <utility>
#include <Wt/WContainerWidget.h>
#include <Wt/WSignal.h>
#include <Wt/WString.h>
#include <Wt/WTemplate.h>
namespace UserInterface
{
class InfiniteScrollingContainer : public Wt::WTemplate
{
public:
// "text" must contain loading-indicator and "elements"
InfiniteScrollingContainer(const Wt::WString& text = Wt::WString::tr("Lms.infinite-scrolling-container"));
void clear();
std::size_t getCount();
void add(std::unique_ptr<Wt::WWidget> result);
template<typename T, typename... Args>
T* addNew(Args&&... args)
{
return _elements->addNew<T>(std::forward<Args>(args)...);
}
void remove(Wt::WWidget& widget);
Wt::WWidget* getWidget(std::size_t pos) const;
std::optional<std::size_t> getIndexOf(Wt::WWidget& widget) const;
void setHasMore(bool hasMore);
Wt::Signal<> onRequestElements;
private:
void displayLoadingIndicator();
void hideLoadingIndicator();
Wt::WContainerWidget* _elements;
Wt::WTemplate* _loadingIndicator;
};
}
+130
View File
@@ -0,0 +1,130 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ArtistCollector.hpp"
#include "database/Artist.hpp"
#include "database/User.hpp"
#include "database/TrackList.hpp"
#include "scrobbling/IScrobbling.hpp"
#include "utils/Service.hpp"
#include "Filters.hpp"
#include "LmsApplication.hpp"
namespace UserInterface
{
using namespace Database;
std::vector<Wt::Dbo::ptr<Database::Artist>>
ArtistCollector::get(std::optional<Database::Range> range, bool& moreResults)
{
range = getActualRange(range);
std::vector<Artist::pointer> artists;
switch (getMode())
{
case Mode::Random:
artists = getRandomArtists(range, moreResults);
break;
case Mode::Starred:
artists = Artist::getStarred(LmsApp->getDbSession(),
LmsApp->getUser(),
getFilters().getClusterIds(),
_linkType,
Artist::SortMethod::BySortName,
range, moreResults);
break;
case Mode::RecentlyPlayed:
artists = Service<Scrobbling::IScrobbling>::get()->getRecentArtists(LmsApp->getDbSession(), LmsApp->getUser(),
getFilters().getClusterIds(),
_linkType,
range, moreResults);
break;
case Mode::MostPlayed:
artists = Service<Scrobbling::IScrobbling>::get()->getTopArtists(LmsApp->getDbSession(), LmsApp->getUser(),
getFilters().getClusterIds(),
_linkType,
range, moreResults);
break;
case Mode::RecentlyAdded:
artists = Artist::getLastWritten(LmsApp->getDbSession(),
std::nullopt, // after
getFilters().getClusterIds(),
_linkType,
range, moreResults);
break;
case Mode::Search:
artists = Database::Artist::getByFilter(LmsApp->getDbSession(),
getFilters().getClusterIds(),
getSearchKeywords(),
std::nullopt, // no link
Database::Artist::SortMethod::BySortName,
range, moreResults);
break;
case Mode::All:
artists = Artist::getByFilter(LmsApp->getDbSession(),
getFilters().getClusterIds(),
{},
_linkType,
Artist::SortMethod::BySortName,
range, moreResults);
break;
}
if (range && getMaxCount() && (range->offset + range->limit == *getMaxCount()))
moreResults = false;
return artists;
}
std::vector<Database::Artist::pointer>
ArtistCollector::getRandomArtists(std::optional<Range> range, bool& moreResults)
{
std::vector<Artist::pointer> artists;
assert(getMode() == Mode::Random);
if (_randomArtists.empty())
_randomArtists = Artist::getAllIdsRandom(LmsApp->getDbSession(), getFilters().getClusterIds(), _linkType, getMaxCount());
{
auto itBegin {std::cbegin(_randomArtists) + std::min(range ? range->offset : 0, _randomArtists.size())};
auto itEnd {std::cbegin(_randomArtists) + std::min(range ? range->offset + range->limit : _randomArtists.size(), _randomArtists.size())};
for (auto it {itBegin}; it != itEnd; ++it)
{
Artist::pointer artist {Artist::getById(LmsApp->getDbSession(), *it)};
if (artist)
artists.push_back(artist);
}
moreResults = (itEnd != std::cend(_randomArtists));
}
return artists;
}
} // ns UserInterface
+51
View File
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <optional>
#include <vector>
#include "DatabaseCollectorBase.hpp"
#include "database/Types.hpp"
namespace Database
{
class Artist;
}
namespace UserInterface
{
class ArtistCollector : public DatabaseCollectorBase
{
public:
using DatabaseCollectorBase::DatabaseCollectorBase;
std::vector<Wt::Dbo::ptr<Database::Artist>> get(std::optional<Database::Range> range, bool& moreResults);
void reset() { _randomArtists.clear(); }
void setArtistLinkType(std::optional<Database::TrackArtistLinkType> linkType) { _linkType = linkType; }
private:
std::vector<Wt::Dbo::ptr<Database::Artist>> getRandomArtists(std::optional<Range> range, bool& moreResults);
std::vector<Database::IdType> _randomArtists;
std::optional<Database::TrackArtistLinkType> _linkType;
};
} // ns UserInterface
+23 -3
View File
@@ -64,6 +64,26 @@ Artist::Artist(Filters* filters)
refreshView();
}
static
std::optional<IdType>
extractArtistIdFromInternalPath()
{
if (wApp->internalPathMatches("/artist/mbid/"))
{
const auto mbid {UUID::fromString(wApp->internalPathNextPart("/artist/mbid/"))};
if (mbid)
{
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
if (const Database::Artist::pointer artist {Database::Artist::getByMBID(LmsApp->getDbSession(), *mbid)})
return artist.id();
}
return std::nullopt;
}
return StringUtils::readAs<Database::IdType>(wApp->internalPathNextPart("/artist/"));
}
void
Artist::refreshView()
{
@@ -72,9 +92,9 @@ Artist::refreshView()
clear();
const auto artistId {StringUtils::readAs<Database::IdType>(wApp->internalPathNextPart("/artist/"))};
const auto artistId {extractArtistIdFromInternalPath()};
if (!artistId)
throw ArtistNotFoundException {*artistId};
throw ArtistNotFoundException {};
const auto similarArtistIds {Service<Recommendation::IEngine>::get()->getSimilarArtists(LmsApp->getDbSession(),
*artistId,
@@ -85,7 +105,7 @@ Artist::refreshView()
const Database::Artist::pointer artist {Database::Artist::getById(LmsApp->getDbSession(), *artistId)};
if (!artist)
throw ArtistNotFoundException {*artistId};
throw ArtistNotFoundException {};
refreshLinks(artist);
refreshSimilarArtists(similarArtistIds);
+50 -156
View File
@@ -19,23 +19,18 @@
#include "ArtistsView.hpp"
#include <optional>
#include <Wt/WMenu.h>
#include "common/ValueStringModel.hpp"
#include "database/Artist.hpp"
#include "database/Session.hpp"
#include "database/User.hpp"
#include "database/TrackArtistLink.hpp"
#include "database/TrackList.hpp"
#include "scrobbling/IScrobbling.hpp"
#include "utils/EnumSet.hpp"
#include "utils/Logger.hpp"
#include "common/LoadingIndicator.hpp"
#include "common/ValueStringModel.hpp"
#include "common/InfiniteScrollingContainer.hpp"
#include "ArtistListHelpers.hpp"
#include "LmsApplication.hpp"
#include "Filters.hpp"
#include "LmsApplication.hpp"
using namespace Database;
@@ -43,35 +38,39 @@ namespace UserInterface {
using ArtistLinkModel = ValueStringModel<std::optional<TrackArtistLinkType>>;
Artists::Artists(Filters* filters)
: Wt::WTemplate {Wt::WString::tr("Lms.Explore.Artists.template")},
_filters {filters}
Artists::Artists(Filters& filters)
: Wt::WTemplate {Wt::WString::tr("Lms.Explore.Artists.template")}
, _artistCollector {filters, _defaultMode, _maxCount}
{
addFunction("tr", &Wt::WTemplate::Functions::tr);
{
auto* menu {bindNew<Wt::WMenu>("mode")};
auto addItem = [this](Wt::WMenu& menu,const Wt::WString& str, Mode mode)
auto addItem = [this](Wt::WMenu& menu,const Wt::WString& str, ArtistCollector::Mode mode)
{
auto* item {menu.addItem(str)};
item->clicked().connect([this, mode] { refreshView(mode); });
if (mode == defaultMode)
if (mode == _defaultMode)
item->renderSelected(true);
};
addItem(*menu, Wt::WString::tr("Lms.Explore.random"), Mode::Random);
addItem(*menu, Wt::WString::tr("Lms.Explore.starred"), Mode::Starred);
addItem(*menu, Wt::WString::tr("Lms.Explore.recently-played"), Mode::RecentlyPlayed);
addItem(*menu, Wt::WString::tr("Lms.Explore.most-played"), Mode::MostPlayed);
addItem(*menu, Wt::WString::tr("Lms.Explore.recently-added"), Mode::RecentlyAdded);
addItem(*menu, Wt::WString::tr("Lms.Explore.all"), Mode::All);
addItem(*menu, Wt::WString::tr("Lms.Explore.random"), ArtistCollector::Mode::Random);
addItem(*menu, Wt::WString::tr("Lms.Explore.starred"), ArtistCollector::Mode::Starred);
addItem(*menu, Wt::WString::tr("Lms.Explore.recently-played"), ArtistCollector::Mode::RecentlyPlayed);
addItem(*menu, Wt::WString::tr("Lms.Explore.most-played"), ArtistCollector::Mode::MostPlayed);
addItem(*menu, Wt::WString::tr("Lms.Explore.recently-added"), ArtistCollector::Mode::RecentlyAdded);
addItem(*menu, Wt::WString::tr("Lms.Explore.all"), ArtistCollector::Mode::All);
}
_linkType = bindNew<Wt::WComboBox>("link-type");
_linkType->setModel(std::make_shared<ArtistLinkModel>());
_linkType->changed().connect([this] { refreshView(); });
_linkType->changed().connect([this]
{
const std::optional<TrackArtistLinkType> linkType {static_cast<ArtistLinkModel*>(_linkType->model().get())->getValue(_linkType->currentIndex())};
refreshView(linkType);
});
refreshArtistLinkTypes();
LmsApp->getScannerEvents().scanComplete.connect(this, [this](const Scanner::ScanStats& stats)
@@ -80,26 +79,39 @@ Artists::Artists(Filters* filters)
refreshArtistLinkTypes();
});
_container = bindNew<Wt::WContainerWidget>("artists");
hideLoadingIndicator();
_container = bindNew<InfiniteScrollingContainer>("artists");
_container->onRequestElements.connect([this]
{
addSome();
});
refreshView();
filters.updated().connect([this]
{
refreshView();
});
filters->updated().connect([this] { refreshView(); });
refreshView(_artistCollector.getMode());
}
void
Artists::refreshView()
{
_container->clear();
_randomArtists.clear();
_artistCollector.reset();
addSome();
}
void
Artists::refreshView(Mode mode)
Artists::refreshView(ArtistCollector::Mode mode)
{
_mode = mode;
_artistCollector.setMode(mode);
refreshView();
}
void
Artists::refreshView(std::optional<Database::TrackArtistLinkType> linkType)
{
_artistCollector.setArtistLinkType(linkType);
refreshView();
}
@@ -124,7 +136,9 @@ Artists::refreshArtistLinkTypes()
linkTypeModel->clear();
linkTypeModel->add(Wt::WString::tr("Lms.Explore.Artists.linktype-all"), {});
// add default one first (none)
linkTypeModel->add(Wt::WString::tr("Lms.Explore.Artists.linktype-all"), std::nullopt);
addTypeIfUsed(TrackArtistLinkType::Artist, "Lms.Explore.Artists.linktype-artist");
addTypeIfUsed(TrackArtistLinkType::ReleaseArtist, "Lms.Explore.Artists.linktype-releaseartist");
addTypeIfUsed(TrackArtistLinkType::Composer, "Lms.Explore.Artists.linktype-composer");
@@ -134,140 +148,20 @@ Artists::refreshArtistLinkTypes()
addTypeIfUsed(TrackArtistLinkType::Remixer, "Lms.Explore.Artists.linktype-remixer");
}
void
Artists::displayLoadingIndicator()
{
_loadingIndicator = bindWidget<Wt::WTemplate>("loading-indicator", createLoadingIndicator());
_loadingIndicator->scrollVisibilityChanged().connect([this](bool visible)
{
if (!visible)
return;
addSome();
});
}
void
Artists::hideLoadingIndicator()
{
_loadingIndicator = nullptr;
bindEmpty("loading-indicator");
}
std::vector<Artist::pointer>
Artists::getRandomArtists(std::optional<Range> range, bool& moreResults)
{
std::vector<Artist::pointer> artists;
const std::optional<TrackArtistLinkType> linkType {static_cast<ArtistLinkModel*>(_linkType->model().get())->getValue(_linkType->currentIndex())};
if (_randomArtists.empty())
_randomArtists = Artist::getAllIdsRandom(LmsApp->getDbSession(), _filters->getClusterIds(), linkType, maxItemsPerMode[Mode::Random]);
{
auto itBegin {std::cbegin(_randomArtists) + std::min(range ? range->offset : 0, _randomArtists.size())};
auto itEnd {std::cbegin(_randomArtists) + std::min(range ? range->offset + range->limit : _randomArtists.size(), _randomArtists.size())};
for (auto it {itBegin}; it != itEnd; ++it)
{
Artist::pointer artist {Artist::getById(LmsApp->getDbSession(), *it)};
if (artist)
artists.push_back(artist);
}
moreResults = (itEnd != std::cend(_randomArtists));
}
return artists;
}
std::vector<Artist::pointer>
Artists::getArtists(std::optional<Range> range, bool& moreResults)
{
std::vector<Artist::pointer> artists;
const std::optional<TrackArtistLinkType> linkType {static_cast<ArtistLinkModel*>(_linkType->model().get())->getValue(_linkType->currentIndex())};
const std::optional<std::size_t> modeLimit{maxItemsPerMode[_mode]};
if (modeLimit)
{
if (range)
range->limit = std::min(*modeLimit - range->offset, range->limit);
else
range = Range {0, *modeLimit};
}
switch (_mode)
{
case Mode::Random:
artists = getRandomArtists(range, moreResults);
break;
case Mode::Starred:
artists = Artist::getStarred(LmsApp->getDbSession(),
LmsApp->getUser(),
_filters->getClusterIds(),
linkType,
Artist::SortMethod::BySortName,
range, moreResults);
break;
case Mode::RecentlyPlayed:
artists = Service<Scrobbling::IScrobbling>::get()->getRecentArtists(LmsApp->getDbSession(), LmsApp->getUser(),
_filters->getClusterIds(),
linkType,
range, moreResults);
break;
case Mode::MostPlayed:
artists = Service<Scrobbling::IScrobbling>::get()->getTopArtists(LmsApp->getDbSession(), LmsApp->getUser(),
_filters->getClusterIds(),
linkType,
range, moreResults);
break;
case Mode::RecentlyAdded:
artists = Artist::getLastWritten(LmsApp->getDbSession(),
std::nullopt,
_filters->getClusterIds(),
linkType,
range, moreResults);
break;
case Mode::All:
artists = Artist::getByFilter(LmsApp->getDbSession(),
_filters->getClusterIds(),
{},
linkType,
Artist::SortMethod::BySortName,
range, moreResults);
break;
default:
break;
}
if (range && modeLimit && (range->offset + range->limit == *modeLimit))
moreResults = false;
return artists;
}
void
Artists::addSome()
{
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
bool moreResults {};
for (const Artist::pointer& artist : getArtists(Range {static_cast<std::size_t>(_container->count()), batchSize}, moreResults))
{
_container->addWidget(ArtistListHelpers::createEntry(artist));
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
const auto artists {_artistCollector.get(Range {static_cast<std::size_t>(_container->getCount()), _batchSize}, moreResults)};
for (const auto& artist : artists)
_container->add(ArtistListHelpers::createEntry(artist));
}
if (moreResults)
displayLoadingIndicator();
else
hideLoadingIndicator();
_container->setHasMore(moreResults);
}
} // namespace UserInterface
+22 -52
View File
@@ -19,69 +19,39 @@
#pragma once
#include <memory>
#include <optional>
#include <unordered_map>
#include <vector>
#include <Wt/WComboBox.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WTemplate.h>
#include "database/Types.hpp"
#include "ArtistCollector.hpp"
namespace Database
namespace UserInterface
{
class Artist;
}
class Filters;
class InfiniteScrollingContainer;
namespace UserInterface {
class Artists : public Wt::WTemplate
{
public:
Artists(Filters& filters);
class Filters;
private:
void refreshView();
void refreshView(ArtistCollector::Mode mode);
void refreshView(std::optional<Database::TrackArtistLinkType> linkType);
void refreshArtistLinkTypes();
void addSome();
class Artists : public Wt::WTemplate
{
public:
Artists(Filters* filters);
private:
enum class Mode
{
Random,
Starred,
RecentlyPlayed,
RecentlyAdded,
MostPlayed,
All
};
void refreshView();
void refreshView(Mode mode);
void refreshArtistLinkTypes();
void displayLoadingIndicator();
void hideLoadingIndicator();
void addSome();
std::vector<Wt::Dbo::ptr<Database::Artist>> getArtists(std::optional<Database::Range> range, bool& moreResults);
std::vector<Wt::Dbo::ptr<Database::Artist>> getRandomArtists(std::optional<Database::Range> range, bool& moreResults);
static constexpr Mode defaultMode {Mode::Random};
static constexpr std::size_t batchSize {30};
static inline std::unordered_map<Mode, std::optional<std::size_t>> maxItemsPerMode
{
{Mode::Random, batchSize * 4},
{Mode::RecentlyPlayed, batchSize * 4},
{Mode::RecentlyAdded, batchSize * 4},
{Mode::MostPlayed, batchSize * 4},
{Mode::All, batchSize * 50},
};
Mode _mode {defaultMode};
std::vector<Database::IdType> _randomArtists;
Filters* _filters {};
Wt::WTemplate* _loadingIndicator {};
Wt::WContainerWidget* _container {};
Wt::WComboBox* _linkType {};
};
static constexpr std::size_t _batchSize {30};
static constexpr std::size_t _maxCount {256};
InfiniteScrollingContainer* _container {};
ArtistCollector _artistCollector;
Wt::WComboBox* _linkType {};
static constexpr ArtistCollector::Mode _defaultMode {ArtistCollector::Mode::Random};
};
} // namespace UserInterface
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DatabaseCollectorBase.hpp"
#include "utils/String.hpp"
namespace UserInterface
{
DatabaseCollectorBase::DatabaseCollectorBase(Filters& filters, Mode defaultMode, std::optional<std::size_t> maxCount)
: _filters {filters}
, _mode {defaultMode}
, _maxCount {maxCount}
{
}
std::optional<DatabaseCollectorBase::Range>
DatabaseCollectorBase::getActualRange(std::optional<Range> range) const
{
if (std::optional<std::size_t> maxCount {getMaxCount()})
{
if (range)
range->limit = std::min(*maxCount - range->offset, range->limit);
else
range = Range {0, *maxCount};
}
return range;
}
std::optional<std::size_t>
DatabaseCollectorBase::getMaxCount() const
{
return _maxCount;
}
void
DatabaseCollectorBase::setSearch(std::string_view searchText)
{
_searchText = searchText;
_searchKeywords = StringUtils::splitString(_searchText, " ");
}
} // ns UserInterface
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
#include "database/Types.hpp"
namespace UserInterface
{
class Filters;
class DatabaseCollectorBase
{
public:
using Range = Database::Range;
virtual ~DatabaseCollectorBase() {}
enum class Mode
{
Random,
Starred,
RecentlyPlayed,
RecentlyAdded,
MostPlayed,
Search,
All
};
DatabaseCollectorBase(Filters& filters, Mode defaultMode, std::optional<std::size_t> maxCount = std::nullopt);
Mode getMode() const { return _mode; }
void setMode(Mode mode) { _mode = mode; }
void setMaxCount(std::size_t maxCount) { _maxCount = maxCount; }
void setSearch(std::string_view search);
protected:
std::optional<Range> getActualRange(std::optional<Range> range) const;
std::optional<std::size_t> getMaxCount() const;
Filters& getFilters() { return _filters; }
const std::vector<std::string_view>& getSearchKeywords() const { return _searchKeywords; }
private:
Filters& _filters;
std::string _searchText;
std::vector<std::string_view> _searchKeywords;
Mode _mode;
std::optional<std::size_t> _maxCount;
};
} // ns UserInterface
+3 -3
View File
@@ -88,14 +88,14 @@ Explore::Explore(Filters* filters)
Wt::WStackedWidget* contentsStack = bindNew<Wt::WStackedWidget>("contents");
contentsStack->setAttributeValue("style", "overflow-x:visible;overflow-y:visible;");
auto artists = std::make_unique<Artists>(_filters);
auto artists = std::make_unique<Artists>(*_filters);
contentsStack->addWidget(std::move(artists));
auto artist = std::make_unique<Artist>(_filters);
artist->artistsAction.connect(this, &Explore::handleArtistsAction);
contentsStack->addWidget(std::move(artist));
auto releases = std::make_unique<Releases>(_filters);
auto releases = std::make_unique<Releases>(*_filters);
releases->releasesAction.connect(this, &Explore::handleReleasesAction);
contentsStack->addWidget(std::move(releases));
@@ -109,7 +109,7 @@ Explore::Explore(Filters* filters)
_search = search.get();
contentsStack->addWidget(std::move(search));
auto tracks = std::make_unique<Tracks>(_filters);
auto tracks = std::make_unique<Tracks>(*_filters);
tracks->tracksAction.connect(this, &Explore::handleTracksAction);
contentsStack->addWidget(std::move(tracks));
+120
View File
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ReleaseCollector.hpp"
#include <algorithm>
#include "database/Release.hpp"
#include "database/User.hpp"
#include "database/TrackList.hpp"
#include "scrobbling/IScrobbling.hpp"
#include "utils/Service.hpp"
#include "Filters.hpp"
#include "LmsApplication.hpp"
namespace UserInterface
{
using namespace Database;
std::vector<Release::pointer>
ReleaseCollector::get(std::optional<Database::Range> range, bool& moreResults)
{
range = getActualRange(range);
std::vector<Release::pointer> releases;
switch (getMode())
{
case Mode::Random:
releases = getRandomReleases(range, moreResults);
break;
case Mode::Starred:
releases = Release::getStarred(LmsApp->getDbSession(), LmsApp->getUser(), getFilters().getClusterIds(), range, moreResults);
break;
case ReleaseCollector::Mode::RecentlyPlayed:
releases = Service<Scrobbling::IScrobbling>::get()->getRecentReleases(LmsApp->getDbSession(), LmsApp->getUser(), getFilters().getClusterIds(), range, moreResults);
break;
case Mode::MostPlayed:
releases = Service<Scrobbling::IScrobbling>::get()->getTopReleases(LmsApp->getDbSession(), LmsApp->getUser(), getFilters().getClusterIds(), range, moreResults);
break;
case Mode::RecentlyAdded:
releases = Release::getLastWritten(LmsApp->getDbSession(), std::nullopt, getFilters().getClusterIds(), range, moreResults);
break;
case Mode::Search:
releases = Release::getByFilter(LmsApp->getDbSession(), getFilters().getClusterIds(), getSearchKeywords(), range, moreResults);
break;
case Mode::All:
releases = Release::getByFilter(LmsApp->getDbSession(), getFilters().getClusterIds(), {}, range, moreResults);
break;
}
if (range && getMaxCount() && (range->offset + range->limit == *getMaxCount()))
moreResults = false;
return releases;
}
std::vector<Database::IdType>
ReleaseCollector::getAll()
{
bool moreResults;
const auto releases {get(std::nullopt, moreResults)};
std::vector<IdType> res;
res.reserve(releases.size());
std::transform(std::cbegin(releases), std::cend(releases), std::back_inserter(res), [](const Release::pointer& release) { return release.id(); });
return res;
}
std::vector<Database::Release::pointer>
ReleaseCollector::getRandomReleases(std::optional<Range> range, bool& moreResults)
{
std::vector<Release::pointer> releases;
assert(getMode() == Mode::Random);
if (_randomReleases.empty())
_randomReleases = Release::getAllIdsRandom(LmsApp->getDbSession(), getFilters().getClusterIds(), getMaxCount());
{
auto itBegin {std::cbegin(_randomReleases) + std::min(range ? range->offset : 0, _randomReleases.size())};
auto itEnd {std::cbegin(_randomReleases) + std::min(range ? range->offset + range->limit : _randomReleases.size(), _randomReleases.size())};
for (auto it {itBegin}; it != itEnd; ++it)
{
Release::pointer release {Release::getById(LmsApp->getDbSession(), *it)};
if (release)
releases.push_back(release);
}
moreResults = (itEnd != std::cend(_randomReleases));
}
return releases;
}
} // ns UserInterface
+47
View File
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include "DatabaseCollectorBase.hpp"
namespace Database
{
class Release;
}
namespace UserInterface
{
class ReleaseCollector : public DatabaseCollectorBase
{
public:
using DatabaseCollectorBase::DatabaseCollectorBase;
std::vector<Wt::Dbo::ptr<Database::Release>> get(std::optional<Database::Range> range, bool& moreResults);
std::vector<Database::IdType> getAll();
void reset() { _randomReleases.clear(); }
private:
std::vector<Wt::Dbo::ptr<Database::Release>> getRandomReleases(std::optional<Range> range, bool& moreResults);
std::vector<Database::IdType> _randomReleases;
};
} // ns UserInterface
+2 -2
View File
@@ -41,6 +41,7 @@ namespace UserInterface::ReleaseListHelpers
auto entry {std::make_unique<Wt::WTemplate>(Wt::WString::tr(templateKey))};
entry->bindWidget("release-name", LmsApplication::createReleaseAnchor(release));
entry->addFunction("tr", &Wt::WTemplate::Functions::tr);
Wt::WAnchor* anchor = entry->bindWidget("cover", LmsApplication::createReleaseAnchor(release, false));
auto cover = std::make_unique<Wt::WImage>();
@@ -57,8 +58,7 @@ namespace UserInterface::ReleaseListHelpers
if (artists.size() > 1)
{
entry->setCondition("if-has-artist", true);
entry->bindNew<Wt::WText>("artist-name", Wt::WString::tr("Lms.Explore.various-artists"));
entry->setCondition("if-has-various-artists", true);
}
else if (artists.size() == 1 && !isSameArtist)
{
+55 -23
View File
@@ -67,6 +67,27 @@ Release::Release(Filters* filters)
refreshView();
}
static
std::optional<IdType>
extractReleaseIdFromInternalPath()
{
if (wApp->internalPathMatches("/release/mbid/"))
{
const auto mbid {UUID::fromString(wApp->internalPathNextPart("/release/mbid/"))};
if (mbid)
{
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
if (const Database::Release::pointer release {Database::Release::getByMBID(LmsApp->getDbSession(), *mbid)})
return release.id();
}
return std::nullopt;
}
return StringUtils::readAs<Database::IdType>(wApp->internalPathNextPart("/release/"));
}
void
Release::refreshView()
{
@@ -75,9 +96,9 @@ Release::refreshView()
clear();
const auto releaseId {StringUtils::readAs<Database::IdType>(wApp->internalPathNextPart("/release/"))};
const auto releaseId {extractReleaseIdFromInternalPath()};
if (!releaseId)
throw ReleaseNotFoundException {*releaseId};
throw ReleaseNotFoundException {};
auto similarReleasesIds {Service<Recommendation::IEngine>::get()->getSimilarReleases(LmsApp->getDbSession(), *releaseId, 6)};
@@ -85,7 +106,7 @@ Release::refreshView()
const Database::Release::pointer release {Database::Release::getById(LmsApp->getDbSession(), *releaseId)};
if (!release)
throw ReleaseNotFoundException {*releaseId};
throw ReleaseNotFoundException {};
refreshCopyright(release);
refreshLinks(release);
@@ -107,24 +128,7 @@ Release::refreshView()
}
}
{
std::vector<Wt::Dbo::ptr<Database::Artist>> artists;
artists = release->getReleaseArtists();
if (artists.empty())
artists = release->getArtists();
if (artists.size() > 1)
{
setCondition("if-has-artist", true);
bindNew<Wt::WText>("artist", Wt::WString::tr("Lms.Explore.various-artists"));
}
else if (artists.size() == 1)
{
setCondition("if-has-artist", true);
bindWidget("artist", LmsApplication::createArtistAnchor(artists.front()));
}
}
refreshReleaseArtists(release);
{
Wt::WImage* cover {bindNew<Wt::WImage>("cover", Wt::WLink(LmsApp->getCoverResource()->getReleaseUrl(release.id(), CoverResource::Size::Large)))};
@@ -183,8 +187,7 @@ Release::refreshView()
return it->second;
}
Wt::WTemplate* disc {rootContainer->addNew<Wt::WTemplate>(Wt::WString::tr("Lms.Explore.Release.template.disc-entry"))};
disc->addFunction("tr", &Wt::WTemplate::Functions::tr);
Wt::WTemplate* disc {rootContainer->addNew<Wt::WTemplate>(Wt::WString::tr("Lms.Explore.Release.template.entry-disc"))};
if (discSubtitle.empty())
disc->bindNew<Wt::WText>("disc-title", Wt::WString::tr("Lms.Explore.Release.disc").arg(discNumber));
@@ -263,6 +266,35 @@ Release::refreshView()
}
}
void
Release::refreshReleaseArtists(const Database::Release::pointer& release)
{
std::vector<Wt::Dbo::ptr<Database::Artist>> artists;
artists = release->getReleaseArtists();
if (artists.empty())
{
artists = release->getArtists(Database::TrackArtistLinkType::Artist);
if (artists.size() > 1)
{
setCondition("if-has-various-release-artists", true);
return;
}
}
if (!artists.empty())
{
setCondition("if-has-release-artists", true);
Wt::WContainerWidget* artistsContainer {bindNew<Wt::WContainerWidget>("artists")};
for (const auto& artist : artists)
{
Wt::WTemplate* artistTemplate {artistsContainer->addNew<Wt::WTemplate>(Wt::WString::tr("Lms.Explore.Release.template.entry-release-artist"))};
artistTemplate->bindWidget("artist", LmsApplication::createArtistAnchor(artist));
}
}
}
void
Release::refreshCopyright(const Database::Release::pointer& release)
{
+1
View File
@@ -44,6 +44,7 @@ namespace UserInterface
private:
void refreshView();
void refreshReleaseArtists(const Wt::Dbo::ptr<Database::Release>& release);
void refreshCopyright(const Wt::Dbo::ptr<Database::Release>& release);
void refreshLinks(const Wt::Dbo::ptr<Database::Release>& release);
void refreshSimilarReleases(const std::unordered_set<Database::IdType>& similarReleasesId);
+32 -141
View File
@@ -19,23 +19,12 @@
#include "ReleasesView.hpp"
#include <algorithm>
#include <Wt/WAnchor.h>
#include <Wt/WImage.h>
#include <Wt/WMenu.h>
#include <Wt/WPopupMenu.h>
#include <Wt/WText.h>
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/TrackList.hpp"
#include "database/User.hpp"
#include "scrobbling/IScrobbling.hpp"
#include "utils/Logger.hpp"
#include "utils/String.hpp"
#include "common/LoadingIndicator.hpp"
#include "common/InfiniteScrollingContainer.hpp"
#include "ReleaseListHelpers.hpp"
#include "Filters.hpp"
#include "LmsApplication.hpp"
@@ -44,30 +33,30 @@ using namespace Database;
namespace UserInterface {
Releases::Releases(Filters* filters)
: Wt::WTemplate {Wt::WString::tr("Lms.Explore.Releases.template")},
_filters {filters}
Releases::Releases(Filters& filters)
: Wt::WTemplate {Wt::WString::tr("Lms.Explore.Releases.template")}
, _releaseCollector {filters, _defaultMode, _maxCount}
{
addFunction("tr", &Wt::WTemplate::Functions::tr);
{
auto* menu {bindNew<Wt::WMenu>("mode")};
auto addItem = [this](Wt::WMenu& menu,const Wt::WString& str, Mode mode)
auto addItem = [this](Wt::WMenu& menu,const Wt::WString& str, ReleaseCollector::Mode mode)
{
auto* item {menu.addItem(str)};
item->clicked().connect([this, mode] { refreshView(mode); });
if (mode == defaultMode)
if (mode == _defaultMode)
item->renderSelected(true);
};
addItem(*menu, Wt::WString::tr("Lms.Explore.random"), Mode::Random);
addItem(*menu, Wt::WString::tr("Lms.Explore.starred"), Mode::Starred);
addItem(*menu, Wt::WString::tr("Lms.Explore.recently-played"), Mode::RecentlyPlayed);
addItem(*menu, Wt::WString::tr("Lms.Explore.most-played"), Mode::MostPlayed);
addItem(*menu, Wt::WString::tr("Lms.Explore.recently-added"), Mode::RecentlyAdded);
addItem(*menu, Wt::WString::tr("Lms.Explore.all"), Mode::All);
addItem(*menu, Wt::WString::tr("Lms.Explore.random"), ReleaseCollector::Mode::Random);
addItem(*menu, Wt::WString::tr("Lms.Explore.starred"), ReleaseCollector::Mode::Starred);
addItem(*menu, Wt::WString::tr("Lms.Explore.recently-played"), ReleaseCollector::Mode::RecentlyPlayed);
addItem(*menu, Wt::WString::tr("Lms.Explore.most-played"), ReleaseCollector::Mode::MostPlayed);
addItem(*menu, Wt::WString::tr("Lms.Explore.recently-added"), ReleaseCollector::Mode::RecentlyAdded);
addItem(*menu, Wt::WString::tr("Lms.Explore.all"), ReleaseCollector::Mode::All);
}
Wt::WText* playBtn {bindNew<Wt::WText>("play-btn", Wt::WString::tr("Lms.Explore.template.play-btn"), Wt::TextFormat::XHTML)};
@@ -94,153 +83,55 @@ _filters {filters}
popup->popup(moreBtn);
});
_container = bindNew<Wt::WContainerWidget>("releases");
hideLoadingIndicator();
_container = bindNew<InfiniteScrollingContainer>("releases", Wt::WString::tr("Lms.Explore.Releases.template.container"));
_container->onRequestElements.connect([this]
{
addSome();
});
refreshView(defaultMode);
filters.updated().connect([this]
{
refreshView();
});
filters->updated().connect([this] { refreshView(); });
refreshView(_releaseCollector.getMode());
}
void
Releases::refreshView()
{
_container->clear();
_randomReleases.clear();
_releaseCollector.reset();
addSome();
}
void
Releases::refreshView(Mode mode)
Releases::refreshView(ReleaseCollector::Mode mode)
{
_mode = mode;
_releaseCollector.setMode(mode);
refreshView();
}
void
Releases::displayLoadingIndicator()
{
_loadingIndicator = bindWidget<Wt::WTemplate>("loading-indicator", createLoadingIndicator());
_loadingIndicator->scrollVisibilityChanged().connect([this](bool visible)
{
if (!visible)
return;
addSome();
});
}
void
Releases::hideLoadingIndicator()
{
_loadingIndicator = nullptr;
bindEmpty("loading-indicator");
}
void
Releases::addSome()
{
bool moreResults {};
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
const auto releases {getReleases(Range {static_cast<std::size_t>(_container->count()), batchSize}, moreResults)};
for (const Release::pointer& release : releases)
{
_container->addWidget(ReleaseListHelpers::createEntry(release));
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
const auto releases {_releaseCollector.get(Range {static_cast<std::size_t>(_container->getCount()), _batchSize}, moreResults)};
for (const auto& release : releases)
_container->add(ReleaseListHelpers::createEntry(release));
}
if (moreResults)
displayLoadingIndicator();
else
hideLoadingIndicator();
}
std::vector<Database::Release::pointer>
Releases::getRandomReleases(std::optional<Range> range, bool& moreResults)
{
std::vector<Release::pointer> releases;
if (_randomReleases.empty())
_randomReleases = Release::getAllIdsRandom(LmsApp->getDbSession(), _filters->getClusterIds(), maxItemsPerMode[Mode::Random]);
{
auto itBegin {std::cbegin(_randomReleases) + std::min(range ? range->offset : 0, _randomReleases.size())};
auto itEnd {std::cbegin(_randomReleases) + std::min(range ? range->offset + range->limit : _randomReleases.size(), _randomReleases.size())};
for (auto it {itBegin}; it != itEnd; ++it)
{
Release::pointer release {Release::getById(LmsApp->getDbSession(), *it)};
if (release)
releases.push_back(release);
}
moreResults = (itEnd != std::cend(_randomReleases));
}
return releases;
}
std::vector<Database::Release::pointer>
Releases::getReleases(std::optional<Range> range, bool& moreResults)
{
std::vector<Release::pointer> releases;
const std::optional<std::size_t> modeLimit{maxItemsPerMode[_mode]};
if (modeLimit)
{
if (range)
range->limit = std::min(*modeLimit - range->offset, range->limit);
else
range = Range {0, *modeLimit};
}
switch (_mode)
{
case Mode::Random:
releases = getRandomReleases(range, moreResults);
break;
case Mode::Starred:
releases = Release::getStarred(LmsApp->getDbSession(), LmsApp->getUser(), _filters->getClusterIds(), range, moreResults);
break;
case Mode::RecentlyPlayed:
releases = Service<Scrobbling::IScrobbling>::get()->getRecentReleases(LmsApp->getDbSession(), LmsApp->getUser(), _filters->getClusterIds(), range, moreResults);
break;
case Mode::MostPlayed:
releases = Service<Scrobbling::IScrobbling>::get()->getTopReleases(LmsApp->getDbSession(), LmsApp->getUser(), _filters->getClusterIds(), range, moreResults);
break;
case Mode::RecentlyAdded:
releases = Release::getLastWritten(LmsApp->getDbSession(), std::nullopt, _filters->getClusterIds(), range, moreResults);
break;
case Mode::All:
releases = Release::getByFilter(LmsApp->getDbSession(), _filters->getClusterIds(), {}, range, moreResults);
break;
}
if (range && modeLimit && (range->offset + range->limit == *modeLimit))
moreResults = false;
return releases;
_container->setHasMore(moreResults);
}
std::vector<Database::IdType>
Releases::getAllReleases()
{
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
bool moreResults;
const auto releases {getReleases(std::nullopt, moreResults)};
std::vector<IdType> res;
res.reserve(releases.size());
std::transform(std::cbegin(releases), std::cend(releases), std::back_inserter(res), [](const Release::pointer& release) { return release.id(); });
return res;
return _releaseCollector.getAll();
}
} // namespace UserInterface
+21 -50
View File
@@ -19,7 +19,6 @@
#pragma once
#include <optional>
#include <unordered_map>
#include <Wt/WContainerWidget.h>
@@ -27,63 +26,35 @@
#include "database/Types.hpp"
#include "PlayQueueAction.hpp"
#include "ReleaseCollector.hpp"
namespace Database
namespace UserInterface
{
class Release;
}
class Filters;
class InfiniteScrollingContainer;
namespace UserInterface {
class Releases : public Wt::WTemplate
{
public:
Releases(Filters& filters);
class Filters;
PlayQueueActionSignal releasesAction;
class Releases : public Wt::WTemplate
{
public:
Releases(Filters* filters);
private:
PlayQueueActionSignal releasesAction;
void refreshView();
void refreshView(ReleaseCollector::Mode mode);
private:
void addSome();
std::vector<Database::IdType> getAllReleases();
enum class Mode
{
Random,
Starred,
RecentlyPlayed,
RecentlyAdded,
MostPlayed,
All
};
void refreshView();
void refreshView(Mode mode);
void displayLoadingIndicator();
void hideLoadingIndicator();
void addSome();
std::vector<Wt::Dbo::ptr<Database::Release>> getReleases(std::optional<Database::Range> range, bool& moreResults);
std::vector<Wt::Dbo::ptr<Database::Release>> getRandomReleases(std::optional<Database::Range> range, bool& moreResults);
std::vector<Database::IdType> getAllReleases();
static constexpr Mode defaultMode {Mode::Random};
static constexpr std::size_t maxItemsPerLine {6};
static constexpr std::size_t batchSize {maxItemsPerLine * 3};
static inline std::unordered_map<Mode, std::optional<std::size_t>> maxItemsPerMode
{
{Mode::Random, batchSize * 10},
{Mode::RecentlyPlayed, batchSize * 10},
{Mode::RecentlyAdded, batchSize * 10},
{Mode::MostPlayed, batchSize * 10},
{Mode::All, batchSize * 30},
};
Mode _mode {defaultMode};
Filters* _filters {};
std::vector<Database::IdType> _randomReleases;
Wt::WContainerWidget* _container {};
Wt::WTemplate* _loadingIndicator {};
};
static constexpr std::size_t _maxItemsPerLine {6};
static constexpr std::size_t _batchSize {_maxItemsPerLine};
static constexpr std::size_t _maxCount {_maxItemsPerLine * 32};
InfiniteScrollingContainer* _container {};
ReleaseCollector _releaseCollector;
static constexpr ReleaseCollector::Mode _defaultMode {ReleaseCollector::Mode::Random};
};
} // namespace UserInterface
+105 -45
View File
@@ -19,14 +19,19 @@
#include "SearchView.hpp"
#include <functional>
#include <Wt/WAnchor.h>
#include <Wt/WImage.h>
#include <Wt/WStackedWidget.h>
#include "database/Artist.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "common/InfiniteScrollingContainer.hpp"
#include "common/LoadingIndicator.hpp"
#include "ArtistListHelpers.hpp"
#include "Filters.hpp"
#include "LmsApplication.hpp"
@@ -41,95 +46,150 @@ namespace UserInterface
SearchView::SearchView(Filters* filters)
: Wt::WTemplate {Wt::WString::tr("Lms.Explore.Search.template")}
, _filters {filters}
, _releaseCollector {*filters, ReleaseCollector::Mode::Search, getMaxCount(Mode::Release)}
, _artistCollector {*filters, ArtistCollector::Mode::Search, getMaxCount(Mode::Artist)}
, _trackCollector {*filters, TrackCollector::Mode::Search, getMaxCount(Mode::Track)}
{
addFunction("tr", &Wt::WTemplate::Functions::tr);
Wt::WStackedWidget* stack {bindNew<Wt::WStackedWidget>("stack")};
stack->setAttributeValue("style", "overflow-x:visible;overflow-y:visible;");
_menu = bindNew<Wt::WMenu>("mode", stack);
auto addItem = [=](const Wt::WString& str, Mode mode, const Wt::WString& templateStr, std::function<void()> onRequestElementsFunc)
{
assert(modeToIndex(mode) == _results.size());
auto results {std::make_unique<InfiniteScrollingContainer>(templateStr)};
results->onRequestElements.connect(std::move(onRequestElementsFunc));
_results.push_back(results.get());
_menu->addItem(str, std::move(results));
};
// same order as Mode!
addItem(Wt::WString::tr("Lms.Explore.releases"), Mode::Release, Wt::WString::tr("Lms.Explore.Releases.template.container"), [this]{ addSomeReleases(); });
addItem(Wt::WString::tr("Lms.Explore.artists"), Mode::Artist, Wt::WString::tr("Lms.infinite-scrolling-container"), [this]{ addSomeArtists(); });
addItem(Wt::WString::tr("Lms.Explore.tracks"), Mode::Track, Wt::WString::tr("Lms.Explore.Tracks.template.container"), [this]{ addSomeTracks(); });
_filters->updated().connect([=]
{
refreshView();
});
}
std::size_t
SearchView::modeToIndex(Mode mode) const
{
return static_cast<std::size_t>(mode);
}
Wt::WMenuItem&
SearchView::getItemMenu(Mode mode) const
{
return *_menu->itemAt(modeToIndex(mode));
}
InfiniteScrollingContainer&
SearchView::getResultContainer(Mode mode) const
{
return *_results[modeToIndex(mode)];
}
std::size_t
SearchView::getBatchSize(Mode mode) const
{
auto it {_batchSizes.find(mode)};
assert(it != _batchSizes.cend());
return it->second;
}
std::size_t
SearchView::getMaxCount(Mode mode) const
{
auto it {_maxCounts.find(mode)};
assert(it != _maxCounts.cend());
return it->second;
}
void
SearchView::refreshView(const Wt::WString& searchText)
{
_keywords = StringUtils::splitString(searchText.toUTF8(), " ");
_releaseCollector.setSearch(searchText.toUTF8());
_artistCollector.setSearch(searchText.toUTF8());
_trackCollector.setSearch(searchText.toUTF8());
refreshView();
}
void
SearchView::refreshView()
{
clear();
for (InfiniteScrollingContainer* results : _results)
results->clear();
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
searchArtists();
searchReleases();
searchTracks();
addSomeReleases();
addSomeArtists();
addSomeTracks();
}
void
SearchView::searchArtists()
SearchView::addSomeArtists()
{
bool more;
const auto artists {Database::Artist::getByFilter(LmsApp->getDbSession(),
_filters->getClusterIds(),
_keywords,
std::nullopt,
Database::Artist::SortMethod::BySortName,
Database::Range {0, maxEntries}, more)};
InfiniteScrollingContainer& results {getResultContainer(Mode::Artist)};
bool moreResults {};
if (!artists.empty())
{
setCondition("if-artists", true);
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
auto* container {bindNew<Wt::WContainerWidget>("artists")};
for (const Database::Artist::pointer& artist : artists)
container->addWidget(ArtistListHelpers::createEntrySmall(artist));
const Database::Range range {results.getCount(), getBatchSize(Mode::Artist)};
for (const auto& artist : _artistCollector.get(range, moreResults))
results.add(ArtistListHelpers::createEntry(artist));
}
results.setHasMore(moreResults);
getItemMenu(Mode::Artist).setDisabled(results.getCount() == 0);
}
void
SearchView::searchReleases()
SearchView::addSomeReleases()
{
bool more;
const auto releases {Database::Release::getByFilter(LmsApp->getDbSession(),
_filters->getClusterIds(),
_keywords,
Database::Range {0, maxEntries}, more)};
InfiniteScrollingContainer& results {getResultContainer(Mode::Release)};
bool moreResults {};
if (!releases.empty())
{
setCondition("if-releases", true);
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
auto* container {bindNew<Wt::WContainerWidget>("releases")};
for (const Database::Release::pointer& release : releases)
container->addWidget(ReleaseListHelpers::createEntry(release));
const Database::Range range {results.getCount(), getBatchSize(Mode::Release)};
for (const auto& release : _releaseCollector.get(range, moreResults))
results.add(ReleaseListHelpers::createEntry(release));
}
results.setHasMore(moreResults);
getItemMenu(Mode::Release).setDisabled(results.getCount() == 0);
}
void
SearchView::searchTracks()
SearchView::addSomeTracks()
{
bool more;
const auto tracks {Database::Track::getByFilter(LmsApp->getDbSession(),
_filters->getClusterIds(),
_keywords,
Database::Range {0, maxEntries}, more)};
InfiniteScrollingContainer& results {getResultContainer(Mode::Track)};
bool moreResults {};
if (!tracks.empty())
{
setCondition("if-tracks", true);
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
auto* container {bindNew<Wt::WContainerWidget>("tracks")};
for (const Database::Track::pointer& track : tracks)
container->addWidget(TrackListHelpers::createEntry(track, tracksAction));
const Database::Range range {results.getCount(), getBatchSize(Mode::Track)};
for (const auto& track : _trackCollector.get(range, moreResults))
results.add(TrackListHelpers::createEntry(track, tracksAction));
}
results.setHasMore(moreResults);
getItemMenu(Mode::Track).setDisabled(results.getCount() == 0);
}
} // namespace UserInterface
+59 -17
View File
@@ -20,34 +20,76 @@
#pragma once
#include <string>
#include <string_view>
#include <vector>
#include <unordered_map>
#include <Wt/WContainerWidget.h>
#include <Wt/WMenu.h>
#include <Wt/WTemplate.h>
#include "ArtistCollector.hpp"
#include "ReleaseCollector.hpp"
#include "TrackCollector.hpp"
#include "PlayQueueAction.hpp"
namespace UserInterface {
class Filters;
class SearchView : public Wt::WTemplate
namespace UserInterface
{
public:
SearchView(Filters* filters);
PlayQueueActionSignal tracksAction;
class InfiniteScrollingContainer;
class Filters;
void refreshView(const Wt::WString& searchText);
class SearchView : public Wt::WTemplate
{
public:
SearchView(Filters* filters);
private:
void refreshView();
void searchArtists();
void searchReleases();
void searchTracks();
PlayQueueActionSignal tracksAction;
Filters* _filters {};
std::vector<std::string> _keywords;
};
void refreshView(const Wt::WString& searchText);
private:
// same order as in the menu
enum class Mode
{
Release,
Artist,
Track,
};
std::size_t modeToIndex(Mode mode) const;
Wt::WMenuItem& getItemMenu(Mode mode) const;
InfiniteScrollingContainer& getResultContainer(Mode mode) const;
static constexpr Mode _defaultMode {Mode::Release};
static inline std::unordered_map<Mode, std::size_t> _batchSizes
{
{Mode::Artist, 6},
{Mode::Release, 6},
{Mode::Track, 6},
};
static inline std::unordered_map<Mode, std::size_t> _maxCounts
{
{Mode::Artist, 64},
{Mode::Release, 60},
{Mode::Track, 64},
};
std::size_t getBatchSize(Mode mode) const;
std::size_t getMaxCount(Mode mode) const;
void refreshView();
void addSomeReleases();
void addSomeArtists();
void addSomeTracks();
Filters* _filters {};
Wt::WMenu* _menu {};
ReleaseCollector _releaseCollector;
ArtistCollector _artistCollector;
TrackCollector _trackCollector;
std::vector<InfiniteScrollingContainer*> _results;
};
} // namespace UserInterface
+120
View File
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TrackCollector.hpp"
#include <algorithm>
#include "database/Track.hpp"
#include "database/User.hpp"
#include "database/TrackList.hpp"
#include "scrobbling/IScrobbling.hpp"
#include "utils/Service.hpp"
#include "Filters.hpp"
#include "LmsApplication.hpp"
namespace UserInterface
{
using namespace Database;
std::vector<Track::pointer>
TrackCollector::get(std::optional<Database::Range> range, bool& moreResults)
{
range = getActualRange(range);
std::vector<Track::pointer> releases;
switch (getMode())
{
case Mode::Random:
releases = getRandomTracks(range, moreResults);
break;
case Mode::Starred:
releases = Track::getStarred(LmsApp->getDbSession(), LmsApp->getUser(), getFilters().getClusterIds(), range, moreResults);
break;
case TrackCollector::Mode::RecentlyPlayed:
releases = Service<Scrobbling::IScrobbling>::get()->getRecentTracks(LmsApp->getDbSession(), LmsApp->getUser(), getFilters().getClusterIds(), range, moreResults);
break;
case Mode::MostPlayed:
releases = Service<Scrobbling::IScrobbling>::get()->getTopTracks(LmsApp->getDbSession(), LmsApp->getUser(), getFilters().getClusterIds(), range, moreResults);
break;
case Mode::RecentlyAdded:
releases = Track::getLastWritten(LmsApp->getDbSession(), std::nullopt, getFilters().getClusterIds(), range, moreResults);
break;
case Mode::Search:
releases = Track::getByFilter(LmsApp->getDbSession(), getFilters().getClusterIds(), getSearchKeywords(), range, moreResults);
break;
case Mode::All:
releases = Track::getByFilter(LmsApp->getDbSession(), getFilters().getClusterIds(), {}, range, moreResults);
break;
}
if (range && getMaxCount() && (range->offset + range->limit == *getMaxCount()))
moreResults = false;
return releases;
}
std::vector<Database::IdType>
TrackCollector::getAll()
{
bool moreResults;
const auto releases {get(std::nullopt, moreResults)};
std::vector<IdType> res;
res.reserve(releases.size());
std::transform(std::cbegin(releases), std::cend(releases), std::back_inserter(res), [](const Track::pointer& release) { return release.id(); });
return res;
}
std::vector<Database::Track::pointer>
TrackCollector::getRandomTracks(std::optional<Range> range, bool& moreResults)
{
std::vector<Track::pointer> releases;
assert(getMode() == Mode::Random);
if (_randomTracks.empty())
_randomTracks = Track::getAllIdsRandom(LmsApp->getDbSession(), getFilters().getClusterIds(), getMaxCount());
{
auto itBegin {std::cbegin(_randomTracks) + std::min(range ? range->offset : 0, _randomTracks.size())};
auto itEnd {std::cbegin(_randomTracks) + std::min(range ? range->offset + range->limit : _randomTracks.size(), _randomTracks.size())};
for (auto it {itBegin}; it != itEnd; ++it)
{
Track::pointer release {Track::getById(LmsApp->getDbSession(), *it)};
if (release)
releases.push_back(release);
}
moreResults = (itEnd != std::cend(_randomTracks));
}
return releases;
}
} // ns UserInterface
+47
View File
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include "DatabaseCollectorBase.hpp"
namespace Database
{
class Track;
}
namespace UserInterface
{
class TrackCollector : public DatabaseCollectorBase
{
public:
using DatabaseCollectorBase::DatabaseCollectorBase;
std::vector<Wt::Dbo::ptr<Database::Track>> get(std::optional<Database::Range> range, bool& moreResults);
std::vector<Database::IdType> getAll();
void reset() { _randomTracks.clear(); }
private:
std::vector<Wt::Dbo::ptr<Database::Track>> getRandomTracks(std::optional<Range> range, bool& moreResults);
std::vector<Database::IdType> _randomTracks;
};
} // ns UserInterface
+37 -145
View File
@@ -19,57 +19,46 @@
#include "TracksView.hpp"
#include <Wt/WAnchor.h>
#include <Wt/WLineEdit.h>
#include <Wt/WMenu.h>
#include <Wt/WPopupMenu.h>
#include <Wt/WText.h>
#include "database/Artist.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackList.hpp"
#include "database/User.hpp"
#include "scrobbling/IScrobbling.hpp"
#include "utils/Logger.hpp"
#include "utils/String.hpp"
#include "common/LoadingIndicator.hpp"
#include "common/InfiniteScrollingContainer.hpp"
#include "Filters.hpp"
#include "LmsApplication.hpp"
#include "MediaPlayer.hpp"
#include "TrackListHelpers.hpp"
#include "TrackStringUtils.hpp"
using namespace Database;
namespace UserInterface {
Tracks::Tracks(Filters* filters)
Tracks::Tracks(Filters& filters)
: Wt::WTemplate {Wt::WString::tr("Lms.Explore.Tracks.template")},
_filters {filters}
_trackCollector {filters, _defaultMode, _maxCount}
{
addFunction("tr", &Wt::WTemplate::Functions::tr);
{
auto* menu {bindNew<Wt::WMenu>("mode")};
auto addItem = [this](Wt::WMenu& menu,const Wt::WString& str, Mode mode)
auto addItem = [this](Wt::WMenu& menu,const Wt::WString& str, TrackCollector::Mode mode)
{
auto* item {menu.addItem(str)};
item->clicked().connect([this, mode] { refreshView(mode); });
if (mode == defaultMode)
if (mode == _defaultMode)
item->renderSelected(true);
};
addItem(*menu, Wt::WString::tr("Lms.Explore.random"), Mode::Random);
addItem(*menu, Wt::WString::tr("Lms.Explore.starred"), Mode::Starred);
addItem(*menu, Wt::WString::tr("Lms.Explore.recently-played"), Mode::RecentlyPlayed);
addItem(*menu, Wt::WString::tr("Lms.Explore.most-played"), Mode::MostPlayed);
addItem(*menu, Wt::WString::tr("Lms.Explore.recently-added"), Mode::RecentlyAdded);
addItem(*menu, Wt::WString::tr("Lms.Explore.all"), Mode::All);
addItem(*menu, Wt::WString::tr("Lms.Explore.random"), TrackCollector::Mode::Random);
addItem(*menu, Wt::WString::tr("Lms.Explore.starred"), TrackCollector::Mode::Starred);
addItem(*menu, Wt::WString::tr("Lms.Explore.recently-played"), TrackCollector::Mode::RecentlyPlayed);
addItem(*menu, Wt::WString::tr("Lms.Explore.most-played"), TrackCollector::Mode::MostPlayed);
addItem(*menu, Wt::WString::tr("Lms.Explore.recently-added"), TrackCollector::Mode::RecentlyAdded);
addItem(*menu, Wt::WString::tr("Lms.Explore.all"), TrackCollector::Mode::All);
}
Wt::WText* playBtn = bindNew<Wt::WText>("play-btn", Wt::WString::tr("Lms.Explore.template.play-btn"), Wt::TextFormat::XHTML);
@@ -97,155 +86,58 @@ _filters {filters}
popup->popup(moreBtn);
});
_tracksContainer = bindNew<Wt::WContainerWidget>("tracks");
hideLoadingIndicator();
_container = bindNew<InfiniteScrollingContainer>("tracks", Wt::WString::tr("Lms.Explore.Tracks.template.container"));
_container->onRequestElements.connect([this]
{
addSome();
});
filters->updated().connect([this]
filters.updated().connect([this]
{
refreshView();
});
refreshView();
refreshView(_trackCollector.getMode());
}
void
Tracks::refreshView()
{
_tracksContainer->clear();
_randomTracks.clear();
_container->clear();
_trackCollector.reset();
addSome();
}
void
Tracks::refreshView(Mode mode)
Tracks::refreshView(TrackCollector::Mode mode)
{
_mode = mode;
_trackCollector.setMode(mode);
refreshView();
}
void
Tracks::displayLoadingIndicator()
{
_loadingIndicator = bindWidget<Wt::WTemplate>("loading-indicator", createLoadingIndicator());
_loadingIndicator->scrollVisibilityChanged().connect([this](bool visible)
{
if (!visible)
return;
addSome();
});
}
void
Tracks::hideLoadingIndicator()
{
_loadingIndicator = nullptr;
bindEmpty("loading-indicator");
}
std::vector<Database::Track::pointer>
Tracks::getRandomTracks(std::optional<Range> range, bool& moreResults)
{
std::vector<Track::pointer> tracks;
if (_randomTracks.empty())
_randomTracks = Track::getAllIdsRandom(LmsApp->getDbSession(), _filters->getClusterIds(), maxItemsPerMode[Mode::Random]);
{
auto itBegin {std::cbegin(_randomTracks) + std::min(range ? range->offset : 0, _randomTracks.size())};
auto itEnd {std::cbegin(_randomTracks) + std::min(range ? range->offset + range->limit : _randomTracks.size(), _randomTracks.size())};
for (auto it {itBegin}; it != itEnd; ++it)
{
const Track::pointer track {Track::getById(LmsApp->getDbSession(), *it)};
if (track)
tracks.push_back(track);
}
moreResults = (itEnd != std::cend(_randomTracks));
}
return tracks;
}
std::vector<Track::pointer>
Tracks::getTracks(std::optional<Range> range, bool& moreResults)
{
std::vector<Track::pointer> tracks;
const std::optional<std::size_t> modeLimit{maxItemsPerMode[_mode]};
if (modeLimit)
{
if (range)
range->limit = std::min(*modeLimit - range->offset, range->limit);
else
range = Range {0, *modeLimit};
}
switch (_mode)
{
case Mode::Random:
tracks = getRandomTracks(range, moreResults);
break;
case Mode::Starred:
tracks = Track::getStarred(LmsApp->getDbSession(), LmsApp->getUser(), _filters->getClusterIds(), range, moreResults);
break;
case Mode::RecentlyPlayed:
tracks = Service<Scrobbling::IScrobbling>::get()->getRecentTracks(LmsApp->getDbSession(), LmsApp->getUser(), _filters->getClusterIds(), range, moreResults);
break;
case Mode::MostPlayed:
tracks = Service<Scrobbling::IScrobbling>::get()->getTopTracks(LmsApp->getDbSession(), LmsApp->getUser(), _filters->getClusterIds(), range, moreResults);
break;
case Mode::RecentlyAdded:
tracks = Track::getLastWritten(LmsApp->getDbSession(), std::nullopt, _filters->getClusterIds(), range, moreResults);
break;
case Mode::All:
tracks = Track::getByFilter(LmsApp->getDbSession(), _filters->getClusterIds(), {}, range, moreResults);
break;
}
if (range && modeLimit && (range->offset + range->limit == *modeLimit))
moreResults = false;
return tracks;
}
std::vector<Database::IdType>
Tracks::getAllTracks()
{
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
bool moreResults;
const auto tracks {getTracks(std::nullopt, moreResults)};
std::vector<Database::IdType> res;
res.reserve(tracks.size());
std::transform(std::cbegin(tracks), std::cend(tracks), std::back_inserter(res), [](const Database::Track::pointer& track) { return track.id(); });
return res;
}
void
Tracks::addSome()
{
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
bool moreResults {};
bool moreResults;
for (const Track::pointer& track : getTracks(Range {static_cast<std::size_t>(_tracksContainer->count()), batchSize}, moreResults))
{
_tracksContainer->addWidget(TrackListHelpers::createEntry(track, tracksAction));
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
const auto tracks {_trackCollector.get(Range {static_cast<std::size_t>(_container->getCount()), _batchSize}, moreResults)};
for (const auto& track : tracks)
_container->add(TrackListHelpers::createEntry(track, tracksAction));
}
if (moreResults)
displayLoadingIndicator();
else
hideLoadingIndicator();
_container->setHasMore(moreResults);
}
std::vector<Database::IdType>
Tracks::getAllTracks()
{
return _trackCollector.getAll();
}
} // namespace UserInterface
+20 -47
View File
@@ -19,7 +19,6 @@
#pragma once
#include <optional>
#include <unordered_map>
#include <Wt/WContainerWidget.h>
@@ -27,61 +26,35 @@
#include "database/Types.hpp"
#include "PlayQueueAction.hpp"
#include "TrackCollector.hpp"
namespace Database
namespace UserInterface
{
class Track;
}
namespace UserInterface {
class Filters;
class InfiniteScrollingContainer;
class Filters;
class Tracks : public Wt::WTemplate
{
public:
Tracks(Filters* filters);
class Tracks : public Wt::WTemplate
{
public:
Tracks(Filters& filters);
PlayQueueActionSignal tracksAction;
PlayQueueActionSignal tracksAction;
private:
private:
void refreshView();
void refreshView(TrackCollector::Mode mode);
void addSome();
enum class Mode
{
Random,
Starred,
RecentlyPlayed,
RecentlyAdded,
MostPlayed,
All
};
std::vector<Database::IdType> getAllTracks();
void refreshView();
void refreshView(Mode mode);
void displayLoadingIndicator();
void hideLoadingIndicator();
void addSome();
static constexpr TrackCollector::Mode _defaultMode {TrackCollector::Mode::Random};
static constexpr std::size_t _batchSize {6};
static constexpr std::size_t _maxCount {160};
std::vector<Wt::Dbo::ptr<Database::Track>> getRandomTracks(std::optional<Database::Range> range, bool& moreResults);
std::vector<Wt::Dbo::ptr<Database::Track>> getTracks(std::optional<Database::Range> range, bool& moreResults);
std::vector<Database::IdType> getAllTracks();
static constexpr Mode defaultMode {Mode::Random};
static constexpr std::size_t batchSize {20};
static inline std::unordered_map<Mode, std::optional<std::size_t>> maxItemsPerMode
{
{Mode::Random, batchSize * 20},
{Mode::RecentlyPlayed, batchSize * 10},
{Mode::RecentlyAdded, batchSize * 10},
{Mode::MostPlayed, batchSize * 10},
{Mode::All, batchSize * 50},
};
Mode _mode {defaultMode};
Filters* _filters {};
std::vector<Database::IdType> _randomTracks;
Wt::WContainerWidget* _tracksContainer {};
Wt::WTemplate* _loadingIndicator {};
};
InfiniteScrollingContainer* _container {};
TrackCollector _trackCollector;
};
} // namespace UserInterface
@@ -39,7 +39,7 @@ namespace StringUtils
{
template <>
std::optional<Database::AudioFormat>
readAs(const std::string& str)
readAs(std::string_view str)
{
auto encodedFormat {readAs<int>(str)};
+314
View File
@@ -0,0 +1,314 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Common.hpp"
using namespace Database;
TEST_F(DatabaseFixture, SingleArtist)
{
ScopedArtist artist {session, "MyArtist"};
{
auto transaction {session.createSharedTransaction()};
auto artists {Artist::getAll(session, Artist::SortMethod::ByName)};
ASSERT_EQ(artists.size(), 1);
EXPECT_EQ(artists.front().id(), artist.getId());
artists = Artist::getAllOrphans(session);
ASSERT_EQ(artists.size(), 1);
EXPECT_EQ(artists.front().id(), artist.getId());
}
}
TEST_F(DatabaseFixture, SingleTrackSingleArtist)
{
ScopedTrack track {session, "MyTrack"};
ScopedArtist artist {session, "MyArtist"};
{
auto transaction {session.createUniqueTransaction()};
TrackArtistLink::create(session, track.get(), artist.get(), TrackArtistLinkType::Artist);
}
{
auto transaction {session.createSharedTransaction()};
EXPECT_TRUE(Artist::getAllOrphans(session).empty());
}
{
auto transaction {session.createSharedTransaction()};
auto artists {track->getArtists({TrackArtistLinkType::Artist})};
ASSERT_EQ(artists.size(), 1);
EXPECT_EQ(artists.front().id(), artist.getId());
EXPECT_EQ(artist->getReleaseCount(), 0);
ASSERT_EQ(track->getArtistLinks().size(), 1);
auto artistLink {track->getArtistLinks().front()};
EXPECT_EQ(artistLink->getTrack().id(), track.getId());
EXPECT_EQ(artistLink->getArtist().id(), artist.getId());
ASSERT_EQ(track->getArtists({TrackArtistLinkType::Artist}).size(), 1);
EXPECT_TRUE(track->getArtists({TrackArtistLinkType::ReleaseArtist}).empty());
EXPECT_EQ(track->getArtists({}).size(), 1);
}
{
auto transaction {session.createUniqueTransaction()};
auto tracks {artist->getTracks()};
ASSERT_EQ(tracks.size(), 1);
EXPECT_EQ(tracks.front().id(), track.getId());
EXPECT_TRUE(artist->getTracks(TrackArtistLinkType::ReleaseArtist).empty());
EXPECT_EQ(artist->getTracks(TrackArtistLinkType::Artist).size(), 1);
}
}
TEST_F(DatabaseFixture, SingleTrackSingleArtistMultiRoles)
{
ScopedTrack track {session, "MyTrack"};
ScopedArtist artist {session, "MyArtist"};
{
auto transaction {session.createUniqueTransaction()};
TrackArtistLink::create(session, track.get(), artist.get(), TrackArtistLinkType::Artist);
TrackArtistLink::create(session, track.get(), artist.get(), TrackArtistLinkType::ReleaseArtist);
TrackArtistLink::create(session, track.get(), artist.get(), TrackArtistLinkType::Writer);
}
{
auto transaction {session.createSharedTransaction()};
EXPECT_TRUE(Artist::getAllOrphans(session).empty());
}
{
auto transaction {session.createSharedTransaction()};
bool hasMore{};
EXPECT_EQ(Artist::getByFilter(session, {}, {}, std::nullopt, Artist::SortMethod::ByName, std::nullopt, hasMore).size(), 1);
EXPECT_EQ(Artist::getByFilter(session, {}, {}, TrackArtistLinkType::Artist, Artist::SortMethod::ByName, std::nullopt, hasMore).size(), 1);
EXPECT_EQ(Artist::getByFilter(session, {}, {}, TrackArtistLinkType::ReleaseArtist, Artist::SortMethod::ByName, std::nullopt, hasMore).size(), 1);
EXPECT_EQ(Artist::getByFilter(session, {}, {}, TrackArtistLinkType::Writer, Artist::SortMethod::ByName, std::nullopt, hasMore).size(), 1);
EXPECT_TRUE(Artist::getByFilter(session, {}, {}, TrackArtistLinkType::Composer, Artist::SortMethod::ByName, std::nullopt, hasMore).empty());
}
{
auto transaction {session.createSharedTransaction()};
auto artists {track->getArtists({TrackArtistLinkType::Artist})};
ASSERT_EQ(artists.size(), 1);
EXPECT_EQ(artists.front().id(), artist.getId());
artists = track->getArtists({TrackArtistLinkType::ReleaseArtist});
ASSERT_EQ(artists.size(), 1);
EXPECT_EQ(artists.front().id(), artist.getId());
EXPECT_EQ(track->getArtistLinks().size(), 3);
EXPECT_EQ(artist->getTracks().size(), 1);
EXPECT_EQ(artist->getTracks({TrackArtistLinkType::ReleaseArtist}).size(), 1);
EXPECT_EQ(artist->getTracks({TrackArtistLinkType::Artist}).size(), 1);
EXPECT_EQ(artist->getTracks({TrackArtistLinkType::Writer}).size(), 1);
}
}
TEST_F(DatabaseFixture,SingleTrackMultiArtists)
{
ScopedTrack track {session, "track"};
ScopedArtist artist1 {session, "artist1"};
ScopedArtist artist2 {session, "artist2"};
ASSERT_NE(artist1.getId(), artist2.getId());
{
auto transaction {session.createUniqueTransaction()};
TrackArtistLink::create(session, track.get(), artist1.get(), TrackArtistLinkType::Artist);
TrackArtistLink::create(session, track.get(), artist2.get(), TrackArtistLinkType::Artist);
}
{
auto transaction {session.createSharedTransaction()};
EXPECT_TRUE(Artist::getAllOrphans(session).empty());
}
{
auto transaction {session.createSharedTransaction()};
auto artists {track->getArtists({TrackArtistLinkType::Artist})};
ASSERT_EQ(artists.size(), 2);
EXPECT_TRUE((artists[0].id() == artist1.getId() && artists[1].id() == artist2.getId())
|| (artists[0].id() == artist2.getId() && artists[1].id() == artist1.getId()));
EXPECT_EQ(track->getArtists({}).size(), 2);
EXPECT_EQ(track->getArtists({TrackArtistLinkType::Artist}).size(), 2);
EXPECT_TRUE(track->getArtists({TrackArtistLinkType::ReleaseArtist}).empty());
EXPECT_EQ(Artist::getAll(session, Artist::SortMethod::ByName).size(), 2);
EXPECT_EQ(Artist::getAllIds(session).size(), 2);
}
{
auto transaction {session.createUniqueTransaction()};
EXPECT_EQ(artist1->getTracks().front(), track.get());
EXPECT_EQ(artist2->getTracks().front(), track.get());
EXPECT_TRUE(artist1->getTracks(TrackArtistLinkType::ReleaseArtist).empty());
EXPECT_EQ(artist1->getTracks(TrackArtistLinkType::Artist).size(), 1);
EXPECT_TRUE(artist2->getTracks(TrackArtistLinkType::ReleaseArtist).empty());
EXPECT_EQ(artist2->getTracks(TrackArtistLinkType::Artist).size(), 1);
}
}
TEST_F(DatabaseFixture, SingleArtistSearchByName)
{
ScopedArtist artist {session, "AAA"};
ScopedTrack track {session, "MyTrack"}; // filters does not work on orphans
{
auto transaction {session.createUniqueTransaction()};
artist.get().modify()->setSortName("ZZZ");
TrackArtistLink::create(session, track.get(), artist.get(), TrackArtistLinkType::Artist);
}
{
auto transaction {session.createSharedTransaction()};
bool more {};
EXPECT_TRUE(Artist::getByFilter(session, {}, {"N"}, std::nullopt, Artist::SortMethod::ByName, std::nullopt, more).empty());
const auto artistsByAAA {Artist::Artist::getByFilter(session, {}, {"A"}, std::nullopt, Artist::SortMethod::ByName, std::nullopt, more)};
ASSERT_EQ(artistsByAAA.size(), 1);
EXPECT_EQ(artistsByAAA.front().id(), artist.getId());
const auto artistsByZZZ {Artist::Artist::getByFilter(session, {}, {"Z"}, std::nullopt, Artist::SortMethod::ByName, std::nullopt, more)};
ASSERT_EQ(artistsByZZZ.size(), 1);
EXPECT_EQ(artistsByZZZ.front().id(), artist.getId());
EXPECT_TRUE(Artist::getByName(session, "NNN").empty());
}
}
TEST_F(DatabaseFixture, MultipleArtistsSearchByNameEscaped)
{
ScopedArtist artist1 {session, "MyArtist%"};
ScopedArtist artist2 {session, "%MyArtist"};
ScopedArtist artist3 {session, "%_MyArtist"};
ScopedArtist artist4 {session, "MyArtist%foo"};
ScopedArtist artist5 {session, "foo%MyArtist"};
ScopedArtist artist6 {session, "%AMyArtist"};
{
auto transaction {session.createSharedTransaction()};
{
const auto artists {Artist::getByName(session, "MyArtist%")};
ASSERT_TRUE(artists.size() == 1);
EXPECT_EQ(artists.front().id(), artist1.getId());
EXPECT_TRUE(Artist::getByName(session, "MyArtistFoo").empty());
}
{
const auto artists {Artist::getByName(session, "%MyArtist")};
ASSERT_TRUE(artists.size() == 1);
EXPECT_EQ(artists.front().id(), artist2.getId());
EXPECT_TRUE(Artist::getByName(session, "FooMyArtist").empty());
}
{
const auto artists {Artist::getByName(session, "%_MyArtist")};
ASSERT_TRUE(artists.size() == 1);
ASSERT_EQ(artists.front().id(), artist3.getId());
EXPECT_TRUE(Artist::getByName(session, "%CMyArtist").empty());
}
}
// get by filter only works with tracks links...
ScopedTrack track {session, "MyTrack"}; // filters does not work on orphans
{
auto transaction {session.createUniqueTransaction()};
TrackArtistLink::create(session, track.get(), artist1.get(), TrackArtistLinkType::Artist);
TrackArtistLink::create(session, track.get(), artist2.get(), TrackArtistLinkType::Artist);
TrackArtistLink::create(session, track.get(), artist3.get(), TrackArtistLinkType::Artist);
TrackArtistLink::create(session, track.get(), artist4.get(), TrackArtistLinkType::Artist);
TrackArtistLink::create(session, track.get(), artist5.get(), TrackArtistLinkType::Artist);
TrackArtistLink::create(session, track.get(), artist6.get(), TrackArtistLinkType::Artist);
}
{
auto transaction {session.createSharedTransaction()};
bool more;
{
const auto artists {Artist::getByFilter(session, {}, {"MyArtist"}, std::nullopt, Artist::SortMethod::ByName, std::nullopt, more)};
EXPECT_EQ(artists.size(), 6);
}
{
const auto artists {Artist::getByFilter(session, {}, {"MyArtist%"}, std::nullopt, Artist::SortMethod::ByName, std::nullopt, more)};
ASSERT_EQ(artists.size(), 2);
EXPECT_EQ(artists[0].id(), artist1.getId());
EXPECT_EQ(artists[1].id(), artist4.getId());
}
{
const auto artists {Artist::getByFilter(session, {}, {"%MyArtist"}, std::nullopt, Artist::SortMethod::ByName, std::nullopt, more)};
ASSERT_EQ(artists.size(), 2);
EXPECT_EQ(artists[0].id(), artist2.getId());
EXPECT_EQ(artists[1].id(), artist5.getId());
}
{
const auto artists {Artist::getByFilter(session, {}, {"_MyArtist"}, std::nullopt, Artist::SortMethod::ByName, std::nullopt, more)};
ASSERT_EQ(artists.size(), 1);
EXPECT_EQ(artists[0].id(), artist3.getId());
}
}
}
TEST_F(DatabaseFixture, MultiArtistsSortMethod)
{
ScopedArtist artistA {session, "artistA"};
ScopedArtist artistB {session, "artistB"};
{
auto transaction {session.createUniqueTransaction()};
artistA.get().modify()->setSortName("sortNameB");
artistB.get().modify()->setSortName("sortNameA");
}
{
auto transaction {session.createSharedTransaction()};
auto allArtistsByName {Artist::getAll(session, Artist::SortMethod::ByName)};
auto allArtistsBySortName {Artist::getAll(session, Artist::SortMethod::BySortName)};
ASSERT_EQ(allArtistsByName.size(), 2);
EXPECT_EQ(allArtistsByName.front().id(), artistA.getId());
EXPECT_EQ(allArtistsByName.back().id(), artistB.getId());
ASSERT_EQ(allArtistsBySortName.size(), 2);
EXPECT_EQ(allArtistsBySortName.front().id(), artistB.getId());
EXPECT_EQ(allArtistsBySortName.back().id(), artistA.getId());
}
}

Some files were not shown because too many files have changed in this diff Show More