Solian/lib/providers/content/channel.dart

164 lines
4.9 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';
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-05-28 16:14:41 +00:00
import 'package:solian/widgets/account/friend_select.dart';
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 (!await auth.isAuthorized) throw Exception('unauthorized');
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 (!await auth.isAuthorized) throw Exception('unauthorized');
2024-06-22 14:39:32 +00:00
final client = auth.configureClient('messaging');
2024-05-25 17:21:08 +00:00
final resp = await client.get('/api/channels/$realm/$alias');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
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 (!await auth.isAuthorized) throw Exception('unauthorized');
2024-06-22 14:39:32 +00:00
final client = auth.configureClient('messaging');
2024-06-08 16:09:01 +00:00
final resp = await client.get('/api/channels/$realm/$alias/me');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
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 (!await auth.isAuthorized) throw Exception('unauthorized');
2024-06-22 14:39:32 +00:00
final client = auth.configureClient('messaging');
2024-05-31 17:25:45 +00:00
final resp = await client.get('/api/channels/$realm/$alias/calls/ongoing');
if (resp.statusCode == 404) {
return null;
} else if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
return resp;
}
2024-05-29 15:22:24 +00:00
Future<Response> listChannel({String scope = 'global'}) async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
2024-06-22 14:39:32 +00:00
final client = auth.configureClient('messaging');
2024-05-29 15:22:24 +00:00
final resp = await client.get('/api/channels/$scope');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
return resp;
}
2024-05-25 16:11:00 +00:00
Future<Response> listAvailableChannel({String realm = 'global'}) async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
2024-06-22 14:39:32 +00:00
final client = auth.configureClient('messaging');
2024-05-25 16:11:00 +00:00
final resp = await client.get('/api/channels/$realm/me/available');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
return resp;
}
2024-05-28 16:14:41 +00:00
Future<Response> createChannel(String scope, dynamic payload) async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
2024-06-22 14:39:32 +00:00
final client = auth.configureClient('messaging');
2024-05-28 16:14:41 +00:00
final resp = await client.post('/api/channels/$scope', payload);
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
return resp;
}
Future<Response?> createDirectChannel(
BuildContext context, String scope) async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final related = await showModalBottomSheet(
useRootNavigator: true,
context: context,
builder: (context) => FriendSelect(
title: 'channelOrganizeDirectHint'.tr,
),
);
if (related == null) return null;
final prof = await auth.getProfile();
2024-06-22 14:39:32 +00:00
final client = auth.configureClient('messaging');
2024-05-28 16:14:41 +00:00
final resp = await client.post('/api/channels/$scope/dm', {
'alias': const Uuid().v4().replaceAll('-', '').substring(0, 12),
'name': 'DM',
'description':
'A direct message channel between @${prof.body['name']} and @${related.name}',
'related_user': related.id,
'is_encrypted': false,
});
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
return resp;
}
Future<Response> updateChannel(String scope, int id, dynamic payload) async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
2024-06-22 14:39:32 +00:00
final client = auth.configureClient('messaging');
2024-05-28 16:14:41 +00:00
final resp = await client.put('/api/channels/$scope/$id', payload);
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
return resp;
}
2024-05-25 16:11:00 +00:00
}