2022-08-26 19:28:01 +00:00
|
|
|
import 'package:graphql/client.dart';
|
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
|
|
|
import 'package:selfprivacy/logic/api_maps/graphql_maps/api_map.dart';
|
2022-09-05 04:34:47 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/disk_volumes.graphql.dart';
|
2022-08-29 00:40:35 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/schema.graphql.dart';
|
2022-08-26 19:28:01 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server_api.graphql.dart';
|
2022-09-05 04:34:47 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server_settings.graphql.dart';
|
2022-08-26 19:28:01 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/services.graphql.dart';
|
2022-09-04 10:45:03 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/users.graphql.dart';
|
2022-09-14 23:31:25 +00:00
|
|
|
import 'package:selfprivacy/logic/models/auto_upgrade_settings.dart';
|
2022-09-04 10:45:03 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
2022-08-26 19:28:01 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/api_token.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/json/server_disk_volume.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/json/server_job.dart';
|
2022-08-29 00:40:35 +00:00
|
|
|
import 'package:selfprivacy/logic/models/service.dart';
|
2022-09-14 23:31:25 +00:00
|
|
|
import 'package:selfprivacy/logic/models/ssh_settings.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/system_settings.dart';
|
2022-08-26 19:28:01 +00:00
|
|
|
|
|
|
|
part 'jobs_api.dart';
|
|
|
|
part 'server_actions_api.dart';
|
|
|
|
part 'services_api.dart';
|
2022-09-04 10:45:03 +00:00
|
|
|
part 'users_api.dart';
|
2022-09-05 04:34:47 +00:00
|
|
|
part 'volume_api.dart';
|
2022-08-26 19:28:01 +00:00
|
|
|
|
2022-08-29 00:40:35 +00:00
|
|
|
class GenericMutationResult {
|
|
|
|
GenericMutationResult({
|
|
|
|
required this.success,
|
|
|
|
required this.code,
|
|
|
|
this.message,
|
|
|
|
});
|
|
|
|
final bool success;
|
|
|
|
final int code;
|
|
|
|
final String? message;
|
|
|
|
}
|
|
|
|
|
|
|
|
class GenericJobMutationReturn extends GenericMutationResult {
|
|
|
|
GenericJobMutationReturn({
|
2022-10-26 16:26:09 +00:00
|
|
|
required super.success,
|
|
|
|
required super.code,
|
|
|
|
super.message,
|
2022-08-29 00:40:35 +00:00
|
|
|
this.job,
|
|
|
|
});
|
|
|
|
final ServerJob? job;
|
|
|
|
}
|
|
|
|
|
2022-08-29 20:35:06 +00:00
|
|
|
class ServerApi extends ApiMap
|
2022-09-05 04:34:47 +00:00
|
|
|
with VolumeApi, JobsApi, ServerActionsApi, ServicesApi, UsersApi {
|
2022-08-26 19:28:01 +00:00
|
|
|
ServerApi({
|
|
|
|
this.hasLogger = false,
|
|
|
|
this.isWithToken = true,
|
|
|
|
this.customToken = '',
|
2022-09-09 14:57:34 +00:00
|
|
|
});
|
|
|
|
|
2022-08-26 19:28:01 +00:00
|
|
|
@override
|
|
|
|
bool hasLogger;
|
|
|
|
@override
|
|
|
|
bool isWithToken;
|
|
|
|
@override
|
|
|
|
String customToken;
|
|
|
|
@override
|
2022-09-09 14:57:34 +00:00
|
|
|
String? get rootAddress => getIt<ApiConfigModel>().serverDomain?.domainName;
|
2022-08-26 19:28:01 +00:00
|
|
|
|
|
|
|
Future<String?> getApiVersion() async {
|
|
|
|
QueryResult response;
|
|
|
|
String? apiVersion;
|
|
|
|
|
|
|
|
try {
|
|
|
|
final GraphQLClient client = await getClient();
|
|
|
|
response = await client.query$GetApiVersion();
|
|
|
|
if (response.hasException) {
|
|
|
|
print(response.exception.toString());
|
|
|
|
}
|
|
|
|
apiVersion = response.data!['api']['version'];
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
return apiVersion;
|
|
|
|
}
|
|
|
|
|
2022-09-02 05:59:46 +00:00
|
|
|
Future<bool> isUsingBinds() async {
|
|
|
|
QueryResult response;
|
|
|
|
bool usesBinds = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
final GraphQLClient client = await getClient();
|
|
|
|
response = await client.query$SystemIsUsingBinds();
|
|
|
|
if (response.hasException) {
|
|
|
|
print(response.exception.toString());
|
|
|
|
}
|
|
|
|
usesBinds = response.data!['system']['info']['usingBinds'];
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
return usesBinds;
|
|
|
|
}
|
|
|
|
|
2022-08-26 19:28:01 +00:00
|
|
|
Future<List<ApiToken>> getApiTokens() async {
|
|
|
|
QueryResult response;
|
|
|
|
List<ApiToken> tokens = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
final GraphQLClient client = await getClient();
|
|
|
|
response = await client.query$GetApiTokens();
|
|
|
|
if (response.hasException) {
|
|
|
|
print(response.exception.toString());
|
|
|
|
}
|
|
|
|
tokens = response.data!['api']['devices']
|
|
|
|
.map<ApiToken>((final e) => ApiToken.fromJson(e))
|
|
|
|
.toList();
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> switchService(final String uid, final bool needTurnOn) async {
|
|
|
|
try {
|
|
|
|
final GraphQLClient client = await getClient();
|
|
|
|
if (needTurnOn) {
|
|
|
|
final variables = Variables$Mutation$EnableService(serviceId: uid);
|
|
|
|
final mutation = Options$Mutation$EnableService(variables: variables);
|
|
|
|
await client.mutate$EnableService(mutation);
|
|
|
|
} else {
|
|
|
|
final variables = Variables$Mutation$DisableService(serviceId: uid);
|
|
|
|
final mutation = Options$Mutation$DisableService(variables: variables);
|
|
|
|
await client.mutate$DisableService(mutation);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
}
|
2022-09-12 17:38:22 +00:00
|
|
|
|
|
|
|
Future<void> setAutoUpgradeSettings(
|
2022-09-14 23:31:25 +00:00
|
|
|
final AutoUpgradeSettings settings,
|
2022-09-12 17:38:22 +00:00
|
|
|
) async {
|
|
|
|
try {
|
|
|
|
final GraphQLClient client = await getClient();
|
|
|
|
final input = Input$AutoUpgradeSettingsInput(
|
2022-09-14 23:31:25 +00:00
|
|
|
allowReboot: settings.allowReboot,
|
|
|
|
enableAutoUpgrade: settings.enable,
|
2022-09-12 17:38:22 +00:00
|
|
|
);
|
|
|
|
final variables = Variables$Mutation$ChangeAutoUpgradeSettings(
|
|
|
|
settings: input,
|
|
|
|
);
|
|
|
|
final mutation = Options$Mutation$ChangeAutoUpgradeSettings(
|
|
|
|
variables: variables,
|
|
|
|
);
|
|
|
|
await client.mutate$ChangeAutoUpgradeSettings(mutation);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 23:31:25 +00:00
|
|
|
|
|
|
|
Future<void> setTimezone(final String timezone) async {
|
|
|
|
try {
|
|
|
|
final GraphQLClient client = await getClient();
|
|
|
|
final variables = Variables$Mutation$ChangeTimezone(
|
|
|
|
timezone: timezone,
|
|
|
|
);
|
|
|
|
final mutation = Options$Mutation$ChangeTimezone(
|
|
|
|
variables: variables,
|
|
|
|
);
|
|
|
|
await client.mutate$ChangeTimezone(mutation);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<SystemSettings> getSystemSettings() async {
|
|
|
|
QueryResult<Query$SystemSettings> response;
|
|
|
|
SystemSettings settings = SystemSettings(
|
|
|
|
autoUpgradeSettings: AutoUpgradeSettings(
|
|
|
|
allowReboot: false,
|
|
|
|
enable: false,
|
|
|
|
),
|
|
|
|
sshSettings: SshSettings(
|
|
|
|
enable: false,
|
|
|
|
passwordAuthentication: false,
|
|
|
|
),
|
|
|
|
timezone: 'Unknown',
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
final GraphQLClient client = await getClient();
|
|
|
|
response = await client.query$SystemSettings();
|
|
|
|
if (response.hasException) {
|
|
|
|
print(response.exception.toString());
|
|
|
|
}
|
|
|
|
settings = SystemSettings.fromGraphQL(response.parsedData!.system);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|
2022-08-26 19:28:01 +00:00
|
|
|
}
|