/* * 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 . */ #include "ScrobblingService.hpp" #include "ScrobblingService.impl.hpp" #include "services/database/Artist.hpp" #include "services/database/Db.hpp" #include "services/database/Listen.hpp" #include "services/database/Release.hpp" #include "services/database/Session.hpp" #include "services/database/StarredArtist.hpp" #include "services/database/StarredRelease.hpp" #include "services/database/StarredTrack.hpp" #include "services/database/Track.hpp" #include "services/database/User.hpp" #include "utils/Logger.hpp" #include "internal/InternalScrobbler.hpp" #include "listenbrainz/ListenBrainzScrobbler.hpp" namespace Scrobbling { using namespace Database; std::unique_ptr createScrobblingService(boost::asio::io_context& ioContext, Db& db) { return std::make_unique(ioContext, db); } ScrobblingService::ScrobblingService(boost::asio::io_context& ioContext, Db& db) : _db{ db } { LMS_LOG(SCROBBLING, INFO) << "Starting service..."; _scrobblers.emplace(Scrobbler::Internal, std::make_unique(_db)); _scrobblers.emplace(Scrobbler::ListenBrainz, std::make_unique(ioContext, _db)); LMS_LOG(SCROBBLING, INFO) << "Service started!"; } ScrobblingService::~ScrobblingService() { LMS_LOG(SCROBBLING, INFO) << "Service stopped!"; } void ScrobblingService::listenStarted(const Listen& listen) { if (std::optional scrobbler{ getUserScrobbler(listen.userId) }) _scrobblers[*scrobbler]->listenStarted(listen); } void ScrobblingService::listenFinished(const Listen& listen, std::optional duration) { if (std::optional scrobbler{ getUserScrobbler(listen.userId) }) _scrobblers[*scrobbler]->listenFinished(listen, duration); } void ScrobblingService::addTimedListen(const TimedListen& listen) { if (std::optional scrobbler{ getUserScrobbler(listen.userId) }) _scrobblers[*scrobbler]->addTimedListen(listen); } std::optional ScrobblingService::getUserScrobbler(UserId userId) { std::optional scrobbler; Session& session{ _db.getTLSSession() }; auto transaction{ session.createSharedTransaction() }; if (const User::pointer user{ User::find(session, userId) }) scrobbler = user->getScrobbler(); return scrobbler; } ScrobblingService::ArtistContainer ScrobblingService::getRecentArtists(UserId userId, const std::vector& clusterIds, std::optional linkType, Range range) { ArtistContainer res; auto scrobbler{ getUserScrobbler(userId) }; if (!scrobbler) return res; Session& session{ _db.getTLSSession() }; auto transaction{ session.createSharedTransaction() }; res = Database::Listen::getRecentArtists(session, userId, *scrobbler, clusterIds, linkType, range); return res; } ScrobblingService::ReleaseContainer ScrobblingService::getRecentReleases(UserId userId, const std::vector& clusterIds, Range range) { ReleaseContainer res; auto scrobbler{ getUserScrobbler(userId) }; if (!scrobbler) return res; Session& session{ _db.getTLSSession() }; auto transaction{ session.createSharedTransaction() }; res = Database::Listen::getRecentReleases(session, userId, *scrobbler, clusterIds, range); return res; } ScrobblingService::TrackContainer ScrobblingService::getRecentTracks(UserId userId, const std::vector& clusterIds, Range range) { TrackContainer res; auto scrobbler{ getUserScrobbler(userId) }; if (!scrobbler) return res; Session& session{ _db.getTLSSession() }; auto transaction{ session.createSharedTransaction() }; res = Database::Listen::getRecentTracks(session, userId, *scrobbler, clusterIds, range); return res; } // Top ScrobblingService::ArtistContainer ScrobblingService::getTopArtists(UserId userId, const std::vector& clusterIds, std::optional linkType, Range range) { ArtistContainer res; auto scrobbler{ getUserScrobbler(userId) }; if (!scrobbler) return res; Session& session{ _db.getTLSSession() }; auto transaction{ session.createSharedTransaction() }; res = Database::Listen::getTopArtists(session, userId, *scrobbler, clusterIds, linkType, range); return res; } ScrobblingService::ReleaseContainer ScrobblingService::getTopReleases(UserId userId, const std::vector& clusterIds, Range range) { ReleaseContainer res; auto scrobbler{ getUserScrobbler(userId) }; if (!scrobbler) return res; Session& session{ _db.getTLSSession() }; auto transaction{ session.createSharedTransaction() }; res = Database::Listen::getTopReleases(session, userId, *scrobbler, clusterIds, range); return res; } ScrobblingService::TrackContainer ScrobblingService::getTopTracks(UserId userId, const std::vector& clusterIds, Range range) { TrackContainer res; auto scrobbler{ getUserScrobbler(userId) }; if (!scrobbler) return res; Session& session{ _db.getTLSSession() }; auto transaction{ session.createSharedTransaction() }; res = Database::Listen::getTopTracks(session, userId, *scrobbler, clusterIds, range); return res; } void ScrobblingService::star(UserId userId, ArtistId artistId) { star(userId, artistId); } void ScrobblingService::unstar(UserId userId, ArtistId artistId) { unstar(userId, artistId); } bool ScrobblingService::isStarred(UserId userId, ArtistId artistId) { return isStarred(userId, artistId); } Wt::WDateTime ScrobblingService::getStarredDateTime(UserId userId, ArtistId artistId) { return getStarredDateTime(userId, artistId); } ScrobblingService::ArtistContainer ScrobblingService::getStarredArtists(UserId userId, const std::vector& clusterIds, std::optional linkType, ArtistSortMethod sortMethod, Range range) { auto scrobbler{ getUserScrobbler(userId) }; if (!scrobbler) return {}; Artist::FindParameters params; params.setStarringUser(userId, *scrobbler); params.setClusters(clusterIds); params.setLinkType(linkType); params.setSortMethod(sortMethod); params.setRange(range); Session& session{ _db.getTLSSession() }; auto transaction{ session.createSharedTransaction() }; return Artist::find(session, params); } void ScrobblingService::star(UserId userId, ReleaseId releaseId) { star(userId, releaseId); } void ScrobblingService::unstar(UserId userId, ReleaseId releaseId) { unstar(userId, releaseId); } bool ScrobblingService::isStarred(UserId userId, ReleaseId releaseId) { return isStarred(userId, releaseId); } Wt::WDateTime ScrobblingService::getStarredDateTime(UserId userId, ReleaseId releaseId) { return getStarredDateTime(userId, releaseId); } ScrobblingService::ReleaseContainer ScrobblingService::getStarredReleases(UserId userId, const std::vector& clusterIds, Range range) { auto scrobbler{ getUserScrobbler(userId) }; if (!scrobbler) return {}; Release::FindParameters params; params.setStarringUser(userId, *scrobbler); params.setClusters(clusterIds); params.setSortMethod(ReleaseSortMethod::StarredDateDesc); params.setRange(range); Session& session{ _db.getTLSSession() }; auto transaction{ session.createSharedTransaction() }; return Release::find(session, params); } void ScrobblingService::star(UserId userId, TrackId trackId) { star(userId, trackId); } void ScrobblingService::unstar(UserId userId, TrackId trackId) { unstar(userId, trackId); } bool ScrobblingService::isStarred(UserId userId, TrackId trackId) { return isStarred(userId, trackId); } Wt::WDateTime ScrobblingService::getStarredDateTime(UserId userId, TrackId trackId) { return getStarredDateTime(userId, trackId); } ScrobblingService::TrackContainer ScrobblingService::getStarredTracks(UserId userId, const std::vector& clusterIds, Range range) { auto scrobbler{ getUserScrobbler(userId) }; if (!scrobbler) return {}; Track::FindParameters params; params.setStarringUser(userId, *scrobbler); params.setClusters(clusterIds); params.setSortMethod(TrackSortMethod::StarredDateDesc); params.setRange(range); Session& session{ _db.getTLSSession() }; auto transaction{ session.createSharedTransaction() }; return Track::find(session, params); } } // ns Scrobbling