2023-01-16 21:31:17 +00:00
|
|
|
import 'package:result_monad/result_monad.dart';
|
|
|
|
|
|
|
|
import '../../models/connection.dart';
|
|
|
|
import '../../models/exec_error.dart';
|
|
|
|
import '../interfaces/connections_repo_intf.dart';
|
|
|
|
|
|
|
|
class MemoryConnectionsRepo implements IConnectionsRepo {
|
|
|
|
final _connectionsById = <String, Connection>{};
|
|
|
|
final _connectionsByName = <String, Connection>{};
|
|
|
|
final _myContacts = <Connection>[];
|
|
|
|
|
2023-01-24 03:36:14 +00:00
|
|
|
@override
|
|
|
|
void clear() {
|
|
|
|
_connectionsById.clear();
|
|
|
|
_connectionsByName.clear();
|
|
|
|
_myContacts.clear();
|
|
|
|
}
|
|
|
|
|
2023-01-16 21:31:17 +00:00
|
|
|
@override
|
2023-01-18 05:15:52 +00:00
|
|
|
List<Connection> getKnownUsersByName(String name) {
|
|
|
|
return _connectionsByName.values.where((it) {
|
2023-01-16 21:31:17 +00:00
|
|
|
final normalizedHandle = it.handle.toLowerCase();
|
|
|
|
final normalizedName = it.name.toLowerCase();
|
|
|
|
final normalizedQuery = name.toLowerCase();
|
|
|
|
return normalizedHandle.contains(normalizedQuery) ||
|
|
|
|
normalizedName.contains(normalizedQuery);
|
2023-01-18 05:15:52 +00:00
|
|
|
}).toList();
|
2023-01-16 21:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-04-19 00:33:09 +00:00
|
|
|
bool upsertConnection(Connection connection) {
|
2023-01-16 21:31:17 +00:00
|
|
|
_connectionsById[connection.id] = connection;
|
|
|
|
_connectionsByName[connection.name] = connection;
|
|
|
|
int index = _myContacts.indexWhere((c) => c.id == connection.id);
|
|
|
|
if (index >= 0) {
|
|
|
|
_myContacts.removeAt(index);
|
|
|
|
}
|
|
|
|
switch (connection.status) {
|
|
|
|
case ConnectionStatus.youFollowThem:
|
|
|
|
case ConnectionStatus.theyFollowYou:
|
|
|
|
case ConnectionStatus.mutual:
|
|
|
|
if (index > 0) {
|
|
|
|
_myContacts.insert(index, connection);
|
|
|
|
} else {
|
|
|
|
_myContacts.add(connection);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-01-18 05:15:52 +00:00
|
|
|
List<Connection> getMyContacts() {
|
|
|
|
return _myContacts;
|
2023-01-16 21:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Result<Connection, ExecError> getById(String id) {
|
|
|
|
final result = _connectionsById[id];
|
|
|
|
if (result == null) {
|
|
|
|
return Result.error(ExecError(
|
|
|
|
type: ErrorType.notFound,
|
|
|
|
message: '$id not found',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result.ok(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Result<Connection, ExecError> getByName(String name) {
|
|
|
|
final result = _connectionsByName[name];
|
|
|
|
if (result == null) {
|
|
|
|
return Result.error(ExecError(
|
|
|
|
type: ErrorType.notFound,
|
|
|
|
message: '$name not found',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result.ok(result);
|
|
|
|
}
|
2023-01-25 18:51:53 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Result<Connection, ExecError> getByHandle(String handle) {
|
|
|
|
final result =
|
|
|
|
_connectionsById.values.where((c) => c.handle == handle).toList();
|
|
|
|
if (result.isEmpty) {
|
|
|
|
return Result.error(ExecError(
|
|
|
|
type: ErrorType.notFound,
|
|
|
|
message: '$handle not found',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result.ok(result.first);
|
|
|
|
}
|
2023-01-16 21:31:17 +00:00
|
|
|
}
|