/*
* Copyright (C) 2013 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 .
*/
#pragma once
#include
#include
#include
#include
#include "Types.hpp"
namespace Database {
class Artist;
class Release;
class Session;
class TrackList;
class Track;
// User selectable audio formats
// Do not change values
enum class AudioFormat
{
MP3 = 1,
OGG_OPUS = 2,
OGG_VORBIS = 3,
WEBM_VORBIS = 4,
MATROSKA_OPUS = 5,
};
using Bitrate = std::size_t;
class User;
class AuthToken
{
public:
using pointer = Wt::Dbo::ptr;
AuthToken() = default;
AuthToken(const std::string& value, const Wt::WDateTime& expiry, Wt::Dbo::ptr user);
// Utility
static pointer create(Session& session, const std::string& value, const Wt::WDateTime&expiry, Wt::Dbo::ptr user);
static void removeExpiredTokens(Session& session, const Wt::WDateTime& now);
static pointer getByValue(Session& session, const std::string& value);
static pointer getById(Session& session, IdType tokenId);
// Accessors
const Wt::WDateTime& getExpiry() const { return _expiry; }
Wt::Dbo::ptr getUser() const { return _user; }
const std::string& getValue() const { return _value; }
template
void persist(Action& a)
{
Wt::Dbo::field(a, _value, "value");
Wt::Dbo::field(a, _expiry, "expiry");
Wt::Dbo::belongsTo(a, _user, "user", Wt::Dbo::OnDeleteCascade);
}
private:
std::string _value;
Wt::WDateTime _expiry;
Wt::Dbo::ptr _user;
};
class User : public Wt::Dbo::Dbo
{
public:
using pointer = Wt::Dbo::ptr;
static const std::size_t MinNameLength = 3;
static const std::size_t MaxNameLength = 15;
enum class Type
{
REGULAR,
ADMIN,
DEMO
};
struct PasswordHash
{
std::string salt;
std::string hash;
};
// list of audio parameters
static const std::set audioTranscodeAllowedBitrates;
User();
User(const std::string& loginName, const PasswordHash& passwordHash);
// utility
static pointer create(Session& session, const std::string& loginName, const PasswordHash& passwordHash);
static pointer getById(Session& session, IdType id);
static pointer getByLoginName(Session& session, const std::string& loginName);
static std::vector getAll(Session& session);
static pointer getDemo(Session& session);
// accessors
const std::string& getLoginName() const { return _loginName; }
PasswordHash getPasswordHash() const { return PasswordHash {_passwordSalt, _passwordHash}; }
Wt::WDateTime getLastLogin() const { return _lastLogin; }
std::size_t getAuthTokensCount() const { return _authTokens.size(); }
// write
void setLastLogin(const Wt::WDateTime& dateTime) { _lastLogin = dateTime; }
void setPasswordHash(const PasswordHash& passwordHash) { _passwordSalt = passwordHash.salt; _passwordHash = passwordHash.hash; }
void setType(Type type) { _type = type; }
void setAudioTranscodeEnable(bool value) { _audioTranscodeEnable = value; }
void setAudioTranscodeFormat(AudioFormat format) { _audioTranscodeFormat = format; }
void setAudioTranscodeBitrate(Bitrate bitrate);
void setMaxAudioTranscodeBitrate(Bitrate bitrate);
void setCurPlayingTrackPos(std::size_t pos) { _curPlayingTrackPos = pos; }
void setRadio(bool val) { _radio = val; }
void setRepeatAll(bool val) { _repeatAll = val; }
void clearAuthTokens();
// read
bool isAdmin() const { return _type == Type::ADMIN; }
bool isDemo() const { return _type == Type::DEMO; }
bool getAudioTranscodeEnable() const { return _audioTranscodeEnable; }
Bitrate getAudioTranscodeBitrate() const;
AudioFormat getAudioTranscodeFormat() const { return _audioTranscodeFormat; }
Bitrate getMaxAudioTranscodeBitrate() const;
std::size_t getCurPlayingTrackPos() const { return _curPlayingTrackPos; }
bool isRepeatAllSet() const { return _repeatAll; }
bool isRadioSet() const { return _radio; }
Wt::Dbo::ptr getPlayedTrackList(Session& session) const;
Wt::Dbo::ptr getQueuedTrackList(Session& session) const;
void starArtist(Wt::Dbo::ptr artist);
void unstarArtist(Wt::Dbo::ptr artist);
bool hasStarredArtist(Wt::Dbo::ptr artist) const;
std::vector> getStarredArtists() const;
void starRelease(Wt::Dbo::ptr release);
void unstarRelease(Wt::Dbo::ptr release);
bool hasStarredRelease(Wt::Dbo::ptr release) const;
std::vector> getStarredReleases(std::optional offset = {}, std::optional size = {}) const;
void starTrack(Wt::Dbo::ptr