2024-06-26 17:33:03 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get/get.dart';
|
2024-08-21 07:39:29 +00:00
|
|
|
import 'package:solian/exceptions/request.dart';
|
2024-08-21 07:25:50 +00:00
|
|
|
import 'package:solian/exceptions/unauthorized.dart';
|
2024-06-26 17:33:03 +00:00
|
|
|
import 'package:solian/models/account_status.dart';
|
|
|
|
import 'package:solian/providers/auth.dart';
|
|
|
|
import 'package:solian/services.dart';
|
|
|
|
|
|
|
|
class StatusProvider extends GetConnect {
|
|
|
|
static Map<String, (Widget, String, String?)> presetStatuses = {
|
|
|
|
'online': (
|
|
|
|
const Icon(Icons.circle, color: Colors.green),
|
|
|
|
'accountStatusOnline'.tr,
|
|
|
|
null,
|
|
|
|
),
|
|
|
|
'silent': (
|
|
|
|
const Icon(Icons.do_not_disturb_on, color: Colors.red),
|
|
|
|
'accountStatusSilent'.tr,
|
|
|
|
'accountStatusSilentDesc'.tr,
|
|
|
|
),
|
|
|
|
'invisible': (
|
|
|
|
const Icon(Icons.circle, color: Colors.grey),
|
|
|
|
'accountStatusInvisible'.tr,
|
|
|
|
'accountStatusInvisibleDesc'.tr,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onInit() {
|
|
|
|
final AuthProvider auth = Get.find();
|
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
httpClient.baseUrl = ServiceFinder.buildUrl('auth', null);
|
2024-06-26 17:33:03 +00:00
|
|
|
httpClient.addAuthenticator(auth.requestAuthenticator);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Response> getCurrentStatus() async {
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-08-21 07:25:50 +00:00
|
|
|
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
|
2024-06-26 17:33:03 +00:00
|
|
|
|
2024-09-16 03:57:16 +00:00
|
|
|
final client = await auth.configureClient('auth');
|
2024-06-26 17:33:03 +00:00
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
return await client.get('/users/me/status');
|
2024-06-26 17:33:03 +00:00
|
|
|
}
|
|
|
|
|
2024-08-21 07:25:50 +00:00
|
|
|
Future<Response> getSomeoneStatus(String name) => get('/users/$name/status');
|
2024-06-26 17:33:03 +00:00
|
|
|
|
|
|
|
Future<Response> setStatus(
|
|
|
|
String type,
|
|
|
|
String? label,
|
|
|
|
int attitude, {
|
|
|
|
bool isUpdate = false,
|
|
|
|
bool isSilent = false,
|
|
|
|
bool isInvisible = false,
|
|
|
|
DateTime? clearAt,
|
|
|
|
}) async {
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-08-21 07:25:50 +00:00
|
|
|
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
|
2024-06-26 17:33:03 +00:00
|
|
|
|
2024-09-16 03:57:16 +00:00
|
|
|
final client = await auth.configureClient('auth');
|
2024-06-26 17:33:03 +00:00
|
|
|
|
|
|
|
final payload = {
|
|
|
|
'type': type,
|
|
|
|
'label': label,
|
2024-06-27 03:44:27 +00:00
|
|
|
'attitude': attitude,
|
2024-06-26 17:33:03 +00:00
|
|
|
'is_no_disturb': isSilent,
|
|
|
|
'is_invisible': isInvisible,
|
|
|
|
'clear_at': clearAt?.toUtc().toIso8601String()
|
|
|
|
};
|
|
|
|
|
|
|
|
Response resp;
|
|
|
|
if (!isUpdate) {
|
2024-07-16 11:46:53 +00:00
|
|
|
resp = await client.post('/users/me/status', payload);
|
2024-06-26 17:33:03 +00:00
|
|
|
} else {
|
2024-07-16 11:46:53 +00:00
|
|
|
resp = await client.put('/users/me/status', payload);
|
2024-06-26 17:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (resp.statusCode != 200) {
|
2024-08-21 07:39:29 +00:00
|
|
|
throw RequestException(resp);
|
2024-06-26 17:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Response> clearStatus() async {
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-08-21 07:25:50 +00:00
|
|
|
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
|
2024-06-26 17:33:03 +00:00
|
|
|
|
2024-09-16 03:57:16 +00:00
|
|
|
final client = await auth.configureClient('auth');
|
2024-06-26 17:33:03 +00:00
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final resp = await client.delete('/users/me/status');
|
2024-06-26 17:33:03 +00:00
|
|
|
if (resp.statusCode != 200) {
|
2024-08-21 07:39:29 +00:00
|
|
|
throw RequestException(resp);
|
2024-06-26 17:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
2024-07-12 05:15:46 +00:00
|
|
|
static (Widget, Color, String) determineStatus(AccountStatus status,
|
2024-06-26 17:33:03 +00:00
|
|
|
{double size = 14}) {
|
|
|
|
Widget icon;
|
2024-07-12 05:15:46 +00:00
|
|
|
Color color;
|
2024-06-26 17:33:03 +00:00
|
|
|
String? text;
|
|
|
|
|
|
|
|
if (!presetStatuses.keys.contains(status.status?.type)) {
|
|
|
|
text = status.status?.label;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status.isDisturbable && status.isOnline) {
|
2024-07-12 05:15:46 +00:00
|
|
|
color = Colors.green;
|
|
|
|
icon = Icon(Icons.circle, color: color, size: size);
|
2024-06-26 17:33:03 +00:00
|
|
|
text ??= 'accountStatusOnline'.tr;
|
|
|
|
} else if (!status.isDisturbable && status.isOnline) {
|
2024-07-12 05:15:46 +00:00
|
|
|
color = Colors.red;
|
|
|
|
icon = Icon(Icons.do_not_disturb_on, color: color, size: size);
|
2024-06-26 17:33:03 +00:00
|
|
|
text ??= 'accountStatusSilent'.tr;
|
|
|
|
} else {
|
2024-07-12 05:15:46 +00:00
|
|
|
color = Colors.grey;
|
|
|
|
icon = Icon(Icons.circle, color: color, size: size);
|
2024-06-26 17:33:03 +00:00
|
|
|
text ??= 'accountStatusOffline'.tr;
|
|
|
|
}
|
2024-07-12 05:15:46 +00:00
|
|
|
return (icon, color, text);
|
2024-06-26 17:33:03 +00:00
|
|
|
}
|
|
|
|
}
|