⬆️ Upgrade to support latest version of server

This commit is contained in:
2024-07-16 19:46:53 +08:00
parent 286dd8193d
commit da265da61d
42 changed files with 221 additions and 297 deletions

View File

@ -27,7 +27,7 @@ class StatusProvider extends GetConnect {
void onInit() {
final AuthProvider auth = Get.find();
httpClient.baseUrl = ServiceFinder.services['passport'];
httpClient.baseUrl = ServiceFinder.buildUrl('auth', null);
httpClient.addAuthenticator(auth.requestAuthenticator);
}
@ -35,13 +35,13 @@ class StatusProvider extends GetConnect {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = auth.configureClient('passport');
final client = auth.configureClient('auth');
return await client.get('/api/users/me/status');
return await client.get('/users/me/status');
}
Future<Response> getSomeoneStatus(String name) =>
get('/api/users/$name/status');
get('/users/$name/status');
Future<Response> setStatus(
String type,
@ -55,7 +55,7 @@ class StatusProvider extends GetConnect {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = auth.configureClient('passport');
final client = auth.configureClient('auth');
final payload = {
'type': type,
@ -68,9 +68,9 @@ class StatusProvider extends GetConnect {
Response resp;
if (!isUpdate) {
resp = await client.post('/api/users/me/status', payload);
resp = await client.post('/users/me/status', payload);
} else {
resp = await client.put('/api/users/me/status', payload);
resp = await client.put('/users/me/status', payload);
}
if (resp.statusCode != 200) {
@ -84,9 +84,9 @@ class StatusProvider extends GetConnect {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = auth.configureClient('passport');
final client = auth.configureClient('auth');
final resp = await client.delete('/api/users/me/status');
final resp = await client.delete('/users/me/status');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}

View File

@ -8,8 +8,7 @@ import 'package:get/get.dart';
import 'package:get/get_connect/http/src/request/request.dart';
import 'package:mutex/mutex.dart';
import 'package:solian/controllers/chat_events_controller.dart';
import 'package:solian/providers/account.dart';
import 'package:solian/providers/chat.dart';
import 'package:solian/providers/websocket.dart';
import 'package:solian/services.dart';
class TokenSet {
@ -48,7 +47,7 @@ class RiskyAuthenticateException implements Exception {
class AuthProvider extends GetConnect {
final tokenEndpoint =
Uri.parse('${ServiceFinder.services['passport']}/api/auth/token');
Uri.parse(ServiceFinder.buildUrl('auth', '/auth/token'));
static const clientId = 'solian';
static const clientSecret = '_F4%q2Eea3';
@ -60,7 +59,7 @@ class AuthProvider extends GetConnect {
@override
void onInit() {
httpClient.baseUrl = ServiceFinder.services['passport'];
httpClient.baseUrl = ServiceFinder.buildUrl('auth', null);
loadCredentials();
}
@ -68,7 +67,7 @@ class AuthProvider extends GetConnect {
try {
credentialsRefreshMutex.acquire();
if (!credentials!.isExpired) return;
final resp = await post('/api/auth/token', {
final resp = await post('/auth/token', {
'refresh_token': credentials!.refreshToken,
'grant_type': 'refresh_token',
});
@ -111,7 +110,7 @@ class AuthProvider extends GetConnect {
sendUserAgent: true,
);
client.httpClient.addAuthenticator(requestAuthenticator);
client.httpClient.baseUrl = ServiceFinder.services[service];
client.httpClient.baseUrl = ServiceFinder.buildUrl(service, null);
return client;
}
@ -140,10 +139,10 @@ class AuthProvider extends GetConnect {
) async {
_cachedUserProfileResponse = null;
final client = ServiceFinder.configureClient('passport');
final client = ServiceFinder.configureClient('auth');
// Create ticket
final resp = await client.post('/api/auth', {
final resp = await client.post('/auth', {
'username': username,
'password': password,
});
@ -154,7 +153,7 @@ class AuthProvider extends GetConnect {
}
// Assign token
final tokenResp = await post('/api/auth/token', {
final tokenResp = await post('/auth/token', {
'code': resp.body['ticket']['grant_token'],
'grant_type': 'grant_token',
});
@ -173,9 +172,8 @@ class AuthProvider extends GetConnect {
value: jsonEncode(credentials!.toJson()),
);
Get.find<AccountProvider>().connect();
Get.find<AccountProvider>().notifyPrefetch();
Get.find<ChatProvider>().connect();
Get.find<WebSocketProvider>().connect();
Get.find<WebSocketProvider>().notifyPrefetch();
return credentials!;
}
@ -183,10 +181,9 @@ class AuthProvider extends GetConnect {
void signout() {
_cachedUserProfileResponse = null;
Get.find<ChatProvider>().disconnect();
Get.find<AccountProvider>().disconnect();
Get.find<AccountProvider>().notifications.clear();
Get.find<AccountProvider>().notificationUnread.value = 0;
Get.find<WebSocketProvider>().disconnect();
Get.find<WebSocketProvider>().notifications.clear();
Get.find<WebSocketProvider>().notificationUnread.value = 0;
final chatHistory = ChatEventController();
chatHistory.initialize().then((_) async {
@ -207,9 +204,9 @@ class AuthProvider extends GetConnect {
return _cachedUserProfileResponse!;
}
final client = configureClient('passport');
final client = configureClient('auth');
final resp = await client.get('/api/users/me');
final resp = await client.get('/users/me');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
} else {

View File

@ -1,76 +0,0 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:get/get.dart';
import 'package:solian/models/packet.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/services.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
class ChatProvider extends GetxController {
RxBool isConnected = false.obs;
RxBool isConnecting = false.obs;
WebSocketChannel? websocket;
StreamController<NetworkPackage> stream = StreamController.broadcast();
void connect({noRetry = false}) async {
if (isConnected.value) {
return;
} else {
disconnect();
}
final AuthProvider auth = Get.find();
await auth.ensureCredentials();
if (auth.credentials == null) await auth.loadCredentials();
final uri = Uri.parse(
'${ServiceFinder.services['messaging']}/api/ws?tk=${auth.credentials!.accessToken}'
.replaceFirst('http', 'ws'),
);
isConnecting.value = true;
try {
websocket = WebSocketChannel.connect(uri);
await websocket?.ready;
} catch (e) {
if (!noRetry) {
await auth.refreshCredentials();
return connect(noRetry: true);
}
}
listen();
isConnected.value = true;
isConnecting.value = false;
}
void disconnect() {
websocket?.sink.close(WebSocketStatus.normalClosure);
websocket = null;
isConnected.value = false;
}
void listen() {
websocket?.stream.listen(
(event) {
final packet = NetworkPackage.fromJson(jsonDecode(event));
stream.sink.add(packet);
},
onDone: () {
isConnected.value = false;
Future.delayed(const Duration(seconds: 1), () => connect());
},
onError: (err) {
isConnected.value = false;
Future.delayed(const Duration(seconds: 3), () => connect());
},
);
}
}

View File

@ -58,7 +58,7 @@ class AttachmentProvider extends GetConnect {
@override
void onInit() {
httpClient.baseUrl = ServiceFinder.services['paperclip'];
httpClient.baseUrl = ServiceFinder.buildUrl('files', null);
}
final Map<int, Response> _cachedResponses = {};
@ -68,7 +68,7 @@ class AttachmentProvider extends GetConnect {
return _cachedResponses[id]!;
}
final resp = await get('/api/attachments/$id/meta');
final resp = await get('/attachments/$id/meta');
_cachedResponses[id] = resp;
return resp;
@ -81,7 +81,7 @@ class AttachmentProvider extends GetConnect {
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = auth.configureClient(
'paperclip',
'files',
timeout: const Duration(minutes: 3),
);
@ -108,7 +108,7 @@ class AttachmentProvider extends GetConnect {
if (ratio != null) 'ratio': ratio,
}),
});
final resp = await client.post('/api/attachments', payload);
final resp = await client.post('/attachments', payload);
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
@ -126,9 +126,9 @@ class AttachmentProvider extends GetConnect {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = auth.configureClient('paperclip');
final client = auth.configureClient('files');
var resp = await client.put('/api/attachments/$id', {
var resp = await client.put('/attachments/$id', {
'metadata': {
if (ratio != null) 'ratio': ratio,
},
@ -148,9 +148,9 @@ class AttachmentProvider extends GetConnect {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = auth.configureClient('paperclip');
final client = auth.configureClient('files');
var resp = await client.delete('/api/attachments/$id');
var resp = await client.delete('/attachments/$id');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}

View File

@ -60,7 +60,7 @@ class ChatCallProvider extends GetxController {
final client = auth.configureClient('messaging');
final resp = await client.post(
'/api/channels/global/${channel.value!.alias}/calls/ongoing/token',
'/channels/global/${channel.value!.alias}/calls/ongoing/token',
{},
);
if (resp.statusCode == 200) {

View File

@ -33,7 +33,7 @@ class ChannelProvider extends GetxController {
final client = auth.configureClient('messaging');
final resp = await client.get('/api/channels/$realm/$alias');
final resp = await client.get('/channels/$realm/$alias');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
@ -48,7 +48,7 @@ class ChannelProvider extends GetxController {
final client = auth.configureClient('messaging');
final resp = await client.get('/api/channels/$realm/$alias/me');
final resp = await client.get('/channels/$realm/$alias/me');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
@ -63,7 +63,7 @@ class ChannelProvider extends GetxController {
final client = auth.configureClient('messaging');
final resp = await client.get('/api/channels/$realm/$alias/calls/ongoing');
final resp = await client.get('/channels/$realm/$alias/calls/ongoing');
if (resp.statusCode == 404) {
return null;
} else if (resp.statusCode != 200) {
@ -79,7 +79,7 @@ class ChannelProvider extends GetxController {
final client = auth.configureClient('messaging');
final resp = await client.get('/api/channels/$scope');
final resp = await client.get('/channels/$scope');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
@ -93,7 +93,7 @@ class ChannelProvider extends GetxController {
final client = auth.configureClient('messaging');
final resp = await client.get('/api/channels/$realm/me/available');
final resp = await client.get('/channels/$realm/me/available');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
@ -107,7 +107,7 @@ class ChannelProvider extends GetxController {
final client = auth.configureClient('messaging');
final resp = await client.post('/api/channels/$scope', payload);
final resp = await client.post('/channels/$scope', payload);
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
@ -132,7 +132,7 @@ class ChannelProvider extends GetxController {
final prof = await auth.getProfile();
final client = auth.configureClient('messaging');
final resp = await client.post('/api/channels/$scope/dm', {
final resp = await client.post('/channels/$scope/dm', {
'alias': const Uuid().v4().replaceAll('-', '').substring(0, 12),
'name': 'DM',
'description':
@ -153,7 +153,7 @@ class ChannelProvider extends GetxController {
final client = auth.configureClient('messaging');
final resp = await client.put('/api/channels/$scope/$id', payload);
final resp = await client.put('/channels/$scope/$id', payload);
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}

View File

@ -5,7 +5,7 @@ import 'package:solian/services.dart';
class FeedProvider extends GetConnect {
@override
void onInit() {
httpClient.baseUrl = ServiceFinder.services['interactive'];
httpClient.baseUrl = ServiceFinder.buildUrl('interactive', null);
}
Future<Response> listFeed(int page,
@ -17,7 +17,7 @@ class FeedProvider extends GetConnect {
if (category != null) 'category=$category',
if (realm != null) 'realmId=$realm',
];
final resp = await get('/api/feed?${queries.join('&')}');
final resp = await get('/feed?${queries.join('&')}');
if (resp.statusCode != 200) {
throw Exception(resp.body);
}
@ -34,7 +34,7 @@ class FeedProvider extends GetConnect {
'offset=$page',
];
final client = auth.configureClient('interactive');
final resp = await client.get('/api/drafts?${queries.join('&')}');
final resp = await client.get('/drafts?${queries.join('&')}');
if (resp.statusCode != 200) {
throw Exception(resp.body);
}
@ -48,7 +48,7 @@ class FeedProvider extends GetConnect {
'offset=$page',
if (realm != null) 'realmId=$realm',
];
final resp = await get('/api/posts?${queries.join('&')}');
final resp = await get('/posts?${queries.join('&')}');
if (resp.statusCode != 200) {
throw Exception(resp.body);
}
@ -57,7 +57,7 @@ class FeedProvider extends GetConnect {
}
Future<Response> listPostReplies(String alias, int page) async {
final resp = await get('/api/posts/$alias/replies?take=${10}&offset=$page');
final resp = await get('/posts/$alias/replies?take=${10}&offset=$page');
if (resp.statusCode != 200) {
throw Exception(resp.body);
}
@ -66,7 +66,7 @@ class FeedProvider extends GetConnect {
}
Future<Response> getPost(String alias) async {
final resp = await get('/api/posts/$alias');
final resp = await get('/posts/$alias');
if (resp.statusCode != 200) {
throw Exception(resp.body);
}
@ -75,7 +75,7 @@ class FeedProvider extends GetConnect {
}
Future<Response> getArticle(String alias) async {
final resp = await get('/api/articles/$alias');
final resp = await get('/articles/$alias');
if (resp.statusCode != 200) {
throw Exception(resp.body);
}

View File

@ -6,9 +6,9 @@ class RealmProvider extends GetxController {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = auth.configureClient('passport');
final client = auth.configureClient('auth');
final resp = await client.get('/api/realms/$alias');
final resp = await client.get('/realms/$alias');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
@ -20,9 +20,9 @@ class RealmProvider extends GetxController {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = auth.configureClient('passport');
final client = auth.configureClient('auth');
final resp = await client.get('/api/realms/me/available');
final resp = await client.get('/realms/me/available');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}

View File

@ -8,17 +8,17 @@ class FriendProvider extends GetConnect {
void onInit() {
final AuthProvider auth = Get.find();
httpClient.baseUrl = ServiceFinder.services['passport'];
httpClient.baseUrl = ServiceFinder.buildUrl('auth', null);
httpClient.addAuthenticator(auth.requestAuthenticator);
}
Future<Response> listFriendship() => get('/api/users/me/friends');
Future<Response> listFriendship() => get('/users/me/friends');
Future<Response> listFriendshipWithStatus(int status) =>
get('/api/users/me/friends?status=$status');
get('/users/me/friends?status=$status');
Future<Response> createFriendship(String username) async {
final resp = await post('/api/users/me/friends?related=$username', {});
final resp = await post('/users/me/friends?related=$username', {});
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
@ -31,7 +31,7 @@ class FriendProvider extends GetConnect {
final prof = await auth.getProfile();
final otherside = relationship.getOtherside(prof.body['id']);
final resp = await put('/api/users/me/friends/${otherside.id}', {
final resp = await put('/users/me/friends/${otherside.id}', {
'status': status,
});
if (resp.statusCode != 200) {

View File

@ -23,7 +23,7 @@ Future<Event?> getRemoteEvent(int id, Channel channel, String scope) async {
final client = auth.configureClient('messaging');
final resp = await client.get(
'/api/channels/$scope/${channel.alias}/events/$id',
'/channels/$scope/${channel.alias}/events/$id',
);
if (resp.statusCode == 404) {
@ -53,7 +53,7 @@ Future<(List<Event>, int)?> getRemoteEvents(
final client = auth.configureClient('messaging');
final resp = await client.get(
'/api/channels/$scope/${channel.alias}/events?take=$take&offset=$offset',
'/channels/$scope/${channel.alias}/events?take=$take&offset=$offset',
);
if (resp.statusCode != 200) {

View File

@ -16,7 +16,7 @@ import 'package:solian/services.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class AccountProvider extends GetxController {
class WebSocketProvider extends GetxController {
final FlutterLocalNotificationsPlugin localNotify =
FlutterLocalNotificationsPlugin();
@ -29,6 +29,8 @@ class AccountProvider extends GetxController {
WebSocketChannel? websocket;
StreamController<NetworkPackage> stream = StreamController.broadcast();
@override
onInit() {
FirebaseMessaging.instance
@ -58,10 +60,10 @@ class AccountProvider extends GetxController {
if (auth.credentials == null) await auth.loadCredentials();
final uri = Uri.parse(
'${ServiceFinder.services['passport']}/api/ws?tk=${auth.credentials!.accessToken}'
.replaceFirst('http', 'ws'),
);
final uri = Uri.parse(ServiceFinder.buildUrl(
'dealer',
'/api/ws?tk=${auth.credentials!.accessToken}',
).replaceFirst('http', 'ws'));
isConnecting.value = true;
@ -91,6 +93,8 @@ class AccountProvider extends GetxController {
websocket?.stream.listen(
(event) {
final packet = NetworkPackage.fromJson(jsonDecode(event));
stream.sink.add(packet);
switch (packet.method) {
case 'notifications.new':
final notification = Notification.fromJson(packet.payload!);
@ -166,9 +170,9 @@ class AccountProvider extends GetxController {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) return;
final client = auth.configureClient('passport');
final client = auth.configureClient('auth');
final resp = await client.get('/api/notifications?skip=0&take=100');
final resp = await client.get('/notifications?skip=0&take=100');
if (resp.statusCode == 200) {
final result = PaginationResult.fromJson(resp.body);
final data = result.data?.map((x) => Notification.fromJson(x)).toList();
@ -199,9 +203,9 @@ class AccountProvider extends GetxController {
token = await FirebaseMessaging.instance.getToken();
}
final client = auth.configureClient('passport');
final client = auth.configureClient('auth');
final resp = await client.post('/api/notifications/subscribe', {
final resp = await client.post('/notifications/subscribe', {
'provider': provider,
'device_token': token,
'device_id': deviceUuid,