2019-07-17 19:37:18 +02:00
# pragma once
# include <Definitions.h>
# include <map>
# include <vector>
# include <chrono>
# include <PermissionManager.h>
# include <Properties.h>
# include <cstdint>
2020-07-30 20:25:45 +02:00
namespace ts : : server {
class VirtualServer ;
class DataClient ;
struct ClientDatabaseInfo {
ServerId server_id ;
ClientDbId client_database_id ;
std : : string client_unique_id ;
std : : string client_nickname ;
2021-04-14 23:12:51 +02:00
std : : string client_ip ;
2020-07-30 20:25:45 +02:00
std : : chrono : : time_point < std : : chrono : : system_clock > client_created ;
std : : chrono : : time_point < std : : chrono : : system_clock > client_last_connected ;
uint32_t client_total_connections ;
} ;
struct StartupPermissionEntry {
permission : : PermissionSqlType type = permission : : SQL_PERM_CHANNEL ;
uint64_t id = 0 ;
ChannelId channelId = 0 ;
std : : shared_ptr < permission : : PermissionTypeEntry > permission = permission : : PermissionTypeEntry : : unknown ;
permission : : PermissionValue value = 0 ;
permission : : PermissionValue grant = 0 ;
bool flag_skip = false ;
bool flag_negate = false ;
} ;
struct StartupPropertyEntry {
property : : PropertyType type = property : : PropertyType : : PROP_TYPE_UNKNOWN ;
uint64_t id { 0 } ;
const property : : PropertyDescription * info { & property : : undefined_property_description } ;
std : : string value ;
} ;
2020-07-31 17:35:14 +02:00
struct DatabaseClient {
ClientDbId client_database_id ;
std : : string client_unique_id ;
2020-07-30 20:25:45 +02:00
2020-07-31 17:35:14 +02:00
std : : string client_nickname ;
std : : string client_ip ;
std : : string client_created ; /* seconds since epoch */
std : : string client_last_connected ; /* seconds since epoch */
std : : string client_total_connections ;
std : : string client_login_name ;
std : : string client_description ; /* optional and only given sometimes */
2020-07-30 20:25:45 +02:00
} ;
struct FastPropertyEntry {
const property : : PropertyDescription * type ;
std : : string value ;
} ;
2020-07-31 17:35:14 +02:00
struct CachedPermissionManager ;
struct StartupCacheEntry ;
2020-07-30 20:25:45 +02:00
class DatabaseHelper {
public :
2021-03-01 14:16:44 +01:00
static std : : shared_ptr < PropertyManager > default_properties_client ( std : : shared_ptr < PropertyManager > /* properties */ , ClientType /* type */ ) ;
2020-07-30 20:25:45 +02:00
static bool assignDatabaseId ( sql : : SqlManager * , ServerId serverId , std : : shared_ptr < DataClient > ) ;
explicit DatabaseHelper ( sql : : SqlManager * ) ;
~ DatabaseHelper ( ) ;
void loadStartupCache ( ) ;
size_t cacheBinarySize ( ) ;
void clearStartupCache ( ServerId sid = 0 ) ;
2020-07-31 17:35:14 +02:00
void handleServerDelete ( ServerId /* server id */ ) ;
void listDatabaseClients (
ServerId /* server id */ ,
const std : : optional < int64_t > & offset ,
const std : : optional < int64_t > & limit ,
void ( * /* callback */ ) ( void * /* user argument */ , const DatabaseClient & /* client */ ) ,
void * /* user argument */ ) ;
2020-07-30 20:25:45 +02:00
void deleteClient ( const std : : shared_ptr < VirtualServer > & , ClientDbId ) ;
bool validClientDatabaseId ( const std : : shared_ptr < VirtualServer > & , ClientDbId ) ;
std : : deque < std : : shared_ptr < ClientDatabaseInfo > > queryDatabaseInfo ( const std : : shared_ptr < VirtualServer > & , const std : : deque < ClientDbId > & ) ;
std : : deque < std : : shared_ptr < ClientDatabaseInfo > > queryDatabaseInfoByUid ( const std : : shared_ptr < VirtualServer > & , std : : deque < std : : string > ) ;
2021-02-28 19:03:41 +01:00
std : : shared_ptr < permission : : v2 : : PermissionManager > loadClientPermissionManager ( const ServerId & , ClientDbId ) ;
2020-07-30 20:25:45 +02:00
void saveClientPermissions ( const std : : shared_ptr < VirtualServer > & , ClientDbId , const std : : shared_ptr < permission : : v2 : : PermissionManager > & /* permission manager */ ) ;
std : : shared_ptr < permission : : v2 : : PermissionManager > loadChannelPermissions ( const std : : shared_ptr < VirtualServer > & , ChannelId ) ;
void saveChannelPermissions ( const std : : shared_ptr < VirtualServer > & , ChannelId , const std : : shared_ptr < permission : : v2 : : PermissionManager > & /* permission manager */ ) ;
2021-02-26 21:32:57 +01:00
std : : shared_ptr < permission : : v2 : : PermissionManager > loadGroupPermissions ( const ServerId & server_id , GroupId , uint8_t /* target */ ) ;
void saveGroupPermissions ( const ServerId & , GroupId , uint8_t /* target */ , const std : : shared_ptr < permission : : v2 : : PermissionManager > & /* permission manager */ ) ;
2020-07-30 20:25:45 +02:00
std : : shared_ptr < permission : : v2 : : PermissionManager > loadPlaylistPermissions ( const std : : shared_ptr < VirtualServer > & , PlaylistId /* playlist id */ ) ;
void savePlaylistPermissions ( const std : : shared_ptr < VirtualServer > & , PlaylistId , const std : : shared_ptr < permission : : v2 : : PermissionManager > & /* permission manager */ ) ;
2021-03-01 14:16:44 +01:00
std : : shared_ptr < PropertyManager > loadServerProperties ( const std : : shared_ptr < VirtualServer > & ) ; //Read and write
std : : shared_ptr < PropertyManager > loadPlaylistProperties ( const std : : shared_ptr < VirtualServer > & , PlaylistId ) ; //Read and write
std : : shared_ptr < PropertyManager > loadChannelProperties ( const std : : shared_ptr < VirtualServer > & , ChannelId ) ; //Read and write
std : : shared_ptr < PropertyManager > loadClientProperties ( const std : : shared_ptr < VirtualServer > & , ClientDbId , ClientType ) ;
2021-04-14 23:12:51 +02:00
void updateClientIpAddress ( const ServerId & /* server id */ , ClientDbId /* target database id */ , const std : : string & /* ip address */ ) ;
2020-07-30 20:25:45 +02:00
2020-08-18 22:03:07 +02:00
void deleteGroupArtifacts ( ServerId , GroupId ) ;
2020-07-30 20:25:45 +02:00
bool deleteChannelPermissions ( const std : : shared_ptr < VirtualServer > & , ChannelId ) ;
bool deletePlaylist ( const std : : shared_ptr < VirtualServer > & , PlaylistId /* playlist id */ ) ;
std : : deque < std : : unique_ptr < FastPropertyEntry > > query_properties ( ServerId /* server */ , property : : PropertyType /* type */ , uint64_t /* id */ ) ; /* required for server snapshots */
void tick ( ) ;
private :
void loadStartupPermissionCache ( ) ;
void loadStartupPropertyCache ( ) ;
bool use_startup_cache = false ;
threads : : Mutex startup_lock ;
std : : deque < std : : shared_ptr < StartupCacheEntry > > startup_entries ;
sql : : SqlManager * sql = nullptr ;
2020-07-31 17:35:14 +02:00
threads : : Mutex cached_permission_manager_lock ;
std : : deque < std : : unique_ptr < CachedPermissionManager > > cached_permission_managers ;
/* Attention: cached_permission_manager_lock should be locked! */
[ [ nodiscard ] ] inline std : : shared_ptr < permission : : v2 : : PermissionManager > find_cached_permission_manager ( ServerId /* server id */ , ClientDbId /* client id */ ) ;
2020-07-30 20:25:45 +02:00
} ;
2019-07-17 19:37:18 +02:00
}