Solian/lib/providers/notify.dart

184 lines
5.6 KiB
Dart
Raw Normal View History

2024-04-24 15:19:26 +00:00
import 'dart:convert';
import 'package:flutter/material.dart';
2024-04-28 13:49:03 +00:00
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
2024-04-29 12:22:06 +00:00
import 'package:livekit_client/livekit_client.dart';
2024-04-28 13:49:03 +00:00
import 'package:permission_handler/permission_handler.dart';
2024-05-12 12:15:12 +00:00
import 'package:solian/models/keypair.dart';
2024-05-11 15:22:35 +00:00
import 'package:solian/models/packet.dart';
2024-04-24 15:19:26 +00:00
import 'package:solian/models/pagination.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/utils/services_url.dart';
2024-04-24 15:19:26 +00:00
import 'package:solian/models/notification.dart' as model;
import 'package:web_socket_channel/web_socket_channel.dart';
2024-04-28 13:49:03 +00:00
import 'dart:math' as math;
2024-04-24 15:19:26 +00:00
class NotifyProvider extends ChangeNotifier {
bool isOpened = false;
2024-04-28 13:49:03 +00:00
int unreadAmount = 0;
2024-04-24 15:19:26 +00:00
List<model.Notification> notifications = List.empty(growable: true);
2024-05-11 15:22:35 +00:00
final FlutterLocalNotificationsPlugin localNotify = FlutterLocalNotificationsPlugin();
2024-04-28 13:49:03 +00:00
NotifyProvider() {
initNotify();
requestPermissions();
}
void initNotify() {
2024-04-29 12:22:06 +00:00
const androidSettings = AndroidInitializationSettings('app_icon');
const darwinSettings = DarwinInitializationSettings(
notificationCategories: [
2024-05-01 16:49:38 +00:00
DarwinNotificationCategory('general'),
2024-04-29 12:22:06 +00:00
],
);
2024-05-11 15:22:35 +00:00
const linuxSettings = LinuxInitializationSettings(defaultActionName: 'Open notification');
const InitializationSettings initializationSettings = InitializationSettings(
2024-04-29 12:22:06 +00:00
android: androidSettings,
iOS: darwinSettings,
macOS: darwinSettings,
linux: linuxSettings,
2024-04-28 13:49:03 +00:00
);
localNotify.initialize(initializationSettings);
}
Future<void> requestPermissions() async {
2024-05-01 16:49:38 +00:00
if (lkPlatformIs(PlatformType.macOS) || lkPlatformIs(PlatformType.linux)) {
2024-05-01 09:37:34 +00:00
return;
2024-05-01 16:49:38 +00:00
}
2024-04-28 13:49:03 +00:00
await Permission.notification.request();
}
2024-04-24 15:19:26 +00:00
Future<void> fetch(AuthProvider auth) async {
if (!await auth.isAuthorized()) return;
var uri = getRequestUri('passport', '/api/notifications?skip=0&take=25');
var res = await auth.client!.get(uri);
if (res.statusCode == 200) {
2024-05-11 15:22:35 +00:00
final result = PaginationResult.fromJson(jsonDecode(utf8.decode(res.bodyBytes)));
notifications = result.data?.map((x) => model.Notification.fromJson(x)).toList() ?? List.empty(growable: true);
2024-04-24 15:19:26 +00:00
}
notifyListeners();
}
2024-05-12 12:59:33 +00:00
WebSocketChannel? _channel;
2024-05-12 12:15:12 +00:00
Future<WebSocketChannel?> connect(
AuthProvider auth, {
Keypair? Function(String id)? onKexRequest,
Function(Keypair kp)? onKexProvide,
}) async {
if (auth.client == null) await auth.loadClient();
2024-05-12 12:15:12 +00:00
if (!await auth.isAuthorized()) return null;
2024-04-24 15:19:26 +00:00
2024-05-02 04:16:01 +00:00
await auth.client!.refreshToken(auth.client!.currentRefreshToken!);
2024-04-24 15:19:26 +00:00
var ori = getRequestUri('passport', '/api/ws');
2024-04-24 15:19:26 +00:00
var uri = Uri(
scheme: ori.scheme.replaceFirst('http', 'ws'),
host: ori.host,
port: ori.port,
2024-04-24 15:19:26 +00:00
path: ori.path,
2024-05-11 15:22:35 +00:00
queryParameters: {'tk': Uri.encodeComponent(auth.client!.currentToken!)},
2024-04-24 15:19:26 +00:00
);
2024-05-12 12:59:33 +00:00
isOpened = true;
_channel = WebSocketChannel.connect(uri);
await _channel!.ready;
2024-04-24 15:19:26 +00:00
2024-05-12 12:59:33 +00:00
_channel!.stream.listen(
2024-05-11 15:22:35 +00:00
(event) {
final result = NetworkPackage.fromJson(jsonDecode(event));
switch (result.method) {
case 'notifications.new':
final result = model.Notification.fromJson(jsonDecode(event));
2024-05-12 12:15:12 +00:00
unreadAmount++;
notifications.add(result);
notifyListeners();
2024-05-11 15:22:35 +00:00
notifyMessage(result.subject, result.content);
2024-05-12 12:15:12 +00:00
break;
case 'kex.request':
if (onKexRequest == null || result.payload == null) break;
final resp = onKexRequest(result.payload!['keypair_id']);
if (resp == null) break;
2024-05-12 12:59:33 +00:00
_channel!.sink.add(jsonEncode(
2024-05-12 12:15:12 +00:00
NetworkPackage(method: 'kex.provide', payload: {
'request_id': result.payload!['request_id'],
'keypair_id': resp.id,
'public_key': resp.publicKey,
'algorithm': resp.algorithm,
}).toJson(),
));
break;
case 'kex.provide':
if (onKexProvide == null || result.payload == null) break;
onKexProvide(Keypair(
id: result.payload!['keypair_id'],
algorithm: result.payload?['algorithm'] ?? 'aes',
publicKey: result.payload!['public_key'],
privateKey: result.payload?['private_key'],
));
break;
2024-05-11 15:22:35 +00:00
}
},
2024-05-12 12:59:33 +00:00
onError: (_, __) => Future.delayed(const Duration(seconds: 3), () => connect(auth)),
onDone: () => Future.delayed(const Duration(seconds: 1), () => connect(auth)),
2024-05-11 15:22:35 +00:00
);
2024-04-24 15:19:26 +00:00
2024-05-12 12:59:33 +00:00
return _channel!;
}
void disconnect() {
_channel = null;
isOpened = false;
2024-04-24 15:19:26 +00:00
}
2024-04-28 13:49:03 +00:00
void notifyMessage(String title, String body) {
2024-04-29 12:22:06 +00:00
const androidSettings = AndroidNotificationDetails(
'general',
'General',
importance: Importance.high,
priority: Priority.high,
silent: true,
);
const darwinSettings = DarwinNotificationDetails(
presentAlert: true,
presentBanner: true,
presentBadge: true,
presentSound: false,
);
const linuxSettings = LinuxNotificationDetails();
2024-04-28 13:49:03 +00:00
localNotify.show(
math.max(1, math.Random().nextInt(100000000)),
title,
body,
const NotificationDetails(
2024-04-29 12:22:06 +00:00
android: androidSettings,
iOS: darwinSettings,
macOS: darwinSettings,
linux: linuxSettings,
2024-04-28 13:49:03 +00:00
),
);
}
2024-04-25 15:03:16 +00:00
void clearAt(int index) {
notifications.removeAt(index);
notifyListeners();
}
2024-05-12 12:15:12 +00:00
void clearRealtimeNotifications() {
2024-04-24 15:19:26 +00:00
notifications = notifications.where((x) => !x.isRealtime).toList();
notifyListeners();
2024-04-24 15:19:26 +00:00
}
2024-04-28 13:49:03 +00:00
void allRead() {
unreadAmount = 0;
notifyListeners();
}
2024-04-24 15:19:26 +00:00
}