actions-test/lib/backend/user.dart
Jakob Meier 5d333522a5
Simplified network requests & snackbars
and rewrote the component's network requests.

showSimpleSnackbar, allows displaying a simple snackbar,
with text and one action button, that can be clicked.

doNetworkRequest is supposed to be a wrapper for the
already existing post* functions.
It aims to make network requests and error handling easier,
by containing all the try&catch blocks
and being able to show snackbars.
2023-03-24 13:25:34 +01:00

71 lines
1.8 KiB
Dart

import 'package:localstore/localstore.dart';
import './resolve_url.dart';
class User {
const User(
{required this.username, required this.password, required this.server});
final String username;
final String password;
final OutbagServer server;
Future<void> toDisk() async {
final db = Localstore.instance;
await db
.collection('meta')
.doc('auth')
.set({'username': username, 'password': password});
await server.toDisk();
}
static Future<User> fromDisk() async {
final db = Localstore.instance;
final data = await db.collection('meta').doc('auth').get();
final server = await OutbagServer.fromDisk();
return User(
username: data?['username'],
password: data?['password'],
server: server,
);
}
static Future<void> removeDisk() async {
final db = Localstore.instance;
await db.collection('meta').doc('auth').delete();
return;
}
static listen(Function(Map<String, dynamic>) cb) async {
final db = Localstore.instance;
final stream = db.collection('meta').stream;
stream.listen(cb);
}
}
class AccountMeta {
final int permissions;
final String username;
final bool discvoverable;
final int maxRoomCount;
final int maxRoomSize;
final int maxRoomMemberCount;
const AccountMeta(
{required this.permissions,
required this.username,
required this.maxRoomSize,
required this.maxRoomCount,
required this.maxRoomMemberCount,
required this.discvoverable});
factory AccountMeta.fromJSON(dynamic json) {
return AccountMeta(
permissions: json['rights'],
username: json['name'],
maxRoomSize: json['maxRoomSize'],
maxRoomCount: json['maxRooms'],
maxRoomMemberCount: json['maxUsersPerRoom'],
discvoverable: json['viewable'] == 1);
}
}