Solian/lib/providers/content/channel.dart

166 lines
5.0 KiB
Dart
Raw Normal View History

2024-05-28 16:14:41 +00:00
import 'package:flutter/material.dart';
2024-05-25 16:11:00 +00:00
import 'package:get/get.dart';
import 'package:solian/exceptions/request.dart';
import 'package:solian/exceptions/unauthorized.dart';
2024-07-12 13:59:16 +00:00
import 'package:solian/models/channel.dart';
2024-05-25 16:11:00 +00:00
import 'package:solian/providers/auth.dart';
2024-07-23 17:17:41 +00:00
import 'package:solian/widgets/account/relative_select.dart';
2024-05-28 16:14:41 +00:00
import 'package:uuid/uuid.dart';
2024-05-25 16:11:00 +00:00
class ChannelProvider extends GetxController {
2024-07-12 13:59:16 +00:00
RxBool isLoading = false.obs;
RxList<Channel> availableChannels = RxList.empty(growable: true);
List<Channel> get groupChannels =>
availableChannels.where((x) => x.type == 0).toList();
List<Channel> get directChannels =>
availableChannels.where((x) => x.type == 1).toList();
Future<void> refreshAvailableChannel() async {
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
2024-07-12 13:59:16 +00:00
isLoading.value = true;
final resp = await listAvailableChannel();
isLoading.value = false;
availableChannels.value =
resp.body.map((x) => Channel.fromJson(x)).toList().cast<Channel>();
availableChannels.refresh();
}
2024-05-25 17:21:08 +00:00
Future<Response> getChannel(String alias, {String realm = 'global'}) async {
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
2024-05-25 17:21:08 +00:00
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('messaging');
2024-05-25 17:21:08 +00:00
final resp = await client.get('/channels/$realm/$alias');
2024-05-25 17:21:08 +00:00
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-05-25 17:21:08 +00:00
}
return resp;
}
2024-07-12 13:59:16 +00:00
Future<Response> getMyChannelProfile(String alias,
{String realm = 'global'}) async {
2024-06-08 16:09:01 +00:00
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
2024-06-08 16:09:01 +00:00
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('messaging');
2024-06-08 16:09:01 +00:00
final resp = await client.get('/channels/$realm/$alias/me');
2024-06-08 16:09:01 +00:00
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-06-08 16:09:01 +00:00
}
return resp;
}
2024-05-31 17:25:45 +00:00
Future<Response?> getChannelOngoingCall(String alias,
{String realm = 'global'}) async {
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
2024-05-31 17:25:45 +00:00
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('messaging');
2024-05-31 17:25:45 +00:00
final resp = await client.get('/channels/$realm/$alias/calls/ongoing');
2024-05-31 17:25:45 +00:00
if (resp.statusCode == 404) {
return null;
} else if (resp.statusCode != 200) {
throw RequestException(resp);
2024-05-31 17:25:45 +00:00
}
return resp;
}
2024-05-29 15:22:24 +00:00
Future<Response> listChannel({String scope = 'global'}) async {
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
2024-05-29 15:22:24 +00:00
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('messaging');
2024-05-29 15:22:24 +00:00
final resp = await client.get('/channels/$scope');
2024-05-29 15:22:24 +00:00
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-05-29 15:22:24 +00:00
}
return resp;
}
Future<Response> listAvailableChannel({String scope = 'global'}) async {
2024-05-25 16:11:00 +00:00
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
2024-05-25 16:11:00 +00:00
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('messaging');
2024-05-25 16:11:00 +00:00
final resp = await client.get('/channels/$scope/me/available');
2024-05-25 16:11:00 +00:00
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-05-25 16:11:00 +00:00
}
return resp;
}
2024-05-28 16:14:41 +00:00
Future<Response> createChannel(String scope, dynamic payload) async {
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
2024-05-28 16:14:41 +00:00
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('messaging');
2024-05-28 16:14:41 +00:00
final resp = await client.post('/channels/$scope', payload);
2024-05-28 16:14:41 +00:00
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-05-28 16:14:41 +00:00
}
return resp;
}
Future<Response?> createDirectChannel(
BuildContext context, String scope) async {
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
2024-05-28 16:14:41 +00:00
final related = await showModalBottomSheet(
useRootNavigator: true,
context: context,
2024-07-23 17:17:41 +00:00
builder: (context) => RelativeSelector(
2024-05-28 16:14:41 +00:00
title: 'channelOrganizeDirectHint'.tr,
),
);
if (related == null) return null;
2024-07-24 17:18:47 +00:00
final prof = auth.userProfile.value!;
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('messaging');
2024-05-28 16:14:41 +00:00
final resp = await client.post('/channels/$scope/dm', {
2024-05-28 16:14:41 +00:00
'alias': const Uuid().v4().replaceAll('-', '').substring(0, 12),
'name': 'DM',
'description':
2024-07-24 17:18:47 +00:00
'A direct message channel between @${prof['name']} and @${related.name}',
2024-05-28 16:14:41 +00:00
'related_user': related.id,
'is_encrypted': false,
});
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-05-28 16:14:41 +00:00
}
return resp;
}
Future<Response> updateChannel(String scope, int id, dynamic payload) async {
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
2024-05-28 16:14:41 +00:00
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('messaging');
2024-05-28 16:14:41 +00:00
final resp = await client.put('/channels/$scope/$id', payload);
2024-05-28 16:14:41 +00:00
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-05-28 16:14:41 +00:00
}
return resp;
}
2024-05-25 16:11:00 +00:00
}