Add full blocking/unblocking capabilities

codemagic-setup
Hank Grabowski 2023-05-03 15:49:40 -04:00
rodzic 8979012034
commit b7673f2a02
11 zmienionych plików z 309 dodań i 152 usunięć

Wyświetl plik

@ -16,7 +16,7 @@ import 'globals.dart';
import 'models/auth/profile.dart';
import 'models/instance_info.dart';
import 'services/auth_service.dart';
import 'services/blocks_service.dart';
import 'services/blocks_manager.dart';
import 'services/connections_manager.dart';
import 'services/direct_message_service.dart';
import 'services/entry_manager_service.dart';
@ -121,8 +121,8 @@ Future<void> dependencyInjectionInitialization() async {
getIt.registerSingleton<ActiveProfileSelector<InteractionsManager>>(
ActiveProfileSelector((p) => InteractionsManager(p))
..subscribeToProfileSwaps());
getIt.registerSingleton<ActiveProfileSelector<BlocksService>>(
ActiveProfileSelector((p) => BlocksService(p))
getIt.registerSingleton<ActiveProfileSelector<BlocksManager>>(
ActiveProfileSelector((p) => BlocksManager(p))
..subscribeToProfileSwaps());
setupUpdateTimers();
}

Wyświetl plik

@ -44,36 +44,6 @@ import 'paging_data.dart';
const _maxProcessingMillis = 3;
const _processingSleep = Duration(milliseconds: 1);
class BlocksClient extends FriendicaClient {
static final _logger = Logger('$DirectMessagingClient');
BlocksClient(super.credentials) : super();
FutureResult<PagedResponse<List<Connection>>, ExecError> getBlocks(
PagingData page) async {
_networkStatusService.startNotificationUpdate();
final url = 'https://$serverName/api/v1/blocks';
final request = Uri.parse('$url&${page.toQueryParameters()}');
_logger.finest(() => 'Getting blocks for $page');
final result =
await _getApiListRequest(request).transformAsync((response) async {
final blocks = <Connection>[];
final st = Stopwatch()..start();
for (final json in response.data) {
if (st.elapsedMilliseconds > _maxProcessingMillis) {
await Future.delayed(_processingSleep, () => st.reset());
}
blocks.add(ConnectionMastodonExtensions.fromJson(json));
}
return PagedResponse(blocks,
id: response.id, previous: response.previous, next: response.next);
});
_networkStatusService.finishNotificationUpdate();
return result.execErrorCast();
}
}
class DirectMessagingClient extends FriendicaClient {
static final _logger = Logger('$DirectMessagingClient');
@ -501,6 +471,33 @@ class RelationshipsClient extends FriendicaClient {
RelationshipsClient(super.credentials) : super();
FutureResult<PagedResponse<List<Connection>>, ExecError> getBlocks(
PagingData page) async {
_networkStatusService.startNotificationUpdate();
final url = 'https://$serverName/api/v1/blocks';
final request = Uri.parse('$url&${page.toQueryParameters()}');
_logger.finest(() => 'Getting blocks for $page');
final result =
await _getApiListRequest(request).transformAsync((response) async {
final blocks = <Connection>[];
final st = Stopwatch()..start();
for (final json in response.data) {
if (st.elapsedMilliseconds > _maxProcessingMillis) {
await Future.delayed(_processingSleep, () => st.reset());
}
blocks.add(
ConnectionMastodonExtensions.fromJson(json)
.copy(status: ConnectionStatus.blocked),
);
}
return PagedResponse(blocks,
id: response.id, previous: response.previous, next: response.next);
});
_networkStatusService.finishNotificationUpdate();
return result.execErrorCast();
}
FutureResult<PagedResponse<List<Connection>>, ExecError> getMyFollowing(
PagingData page) async {
_logger.fine(() => 'Getting following with paging data $page');
@ -647,30 +644,47 @@ class RelationshipsClient extends FriendicaClient {
: ExecError(type: ErrorType.localError, message: error.toString()));
}
FutureResult<Connection, ExecError> blockConnection(
Connection connection) async {
final id = connection.id;
final url = Uri.parse('https://$serverName/api/v1/accounts/$id/block');
final result = await postUrl(url, {}, headers: _headers).transform(
(_) => connection.copy(status: ConnectionStatus.blocked),
);
return result.execErrorCast();
}
FutureResult<Connection, ExecError> unblockConnection(
Connection connection) async {
final id = connection.id;
final url = Uri.parse('https://$serverName/api/v1/accounts/$id/unblock');
final result = await postUrl(url, {}, headers: _headers)
.transformAsync((jsonString) async {
return _updateConnectionFromFollowRequestResult(connection, jsonString);
});
return result.execErrorCast();
}
FutureResult<Connection, ExecError> followConnection(
Connection connection) async {
final id = connection.id;
final url = Uri.parse('https://$serverName/api/v1/accounts/$id/follow');
final result = await postUrl(url, {}, headers: _headers)
.andThenSuccessAsync((jsonString) async {
final result =
await postUrl(url, {}, headers: _headers).transform((jsonString) {
return _updateConnectionFromFollowRequestResult(connection, jsonString);
});
return result.mapError((error) => error is ExecError
? error
: ExecError(type: ErrorType.localError, message: error.toString()));
return result.execErrorCast();
}
FutureResult<Connection, ExecError> unFollowConnection(
Connection connection) async {
final id = connection.id;
final url = Uri.parse('https://$serverName/api/v1/accounts/$id/unfollow');
final result = await postUrl(url, {}, headers: _headers)
.andThenSuccessAsync((jsonString) async {
final result =
await postUrl(url, {}, headers: _headers).transform((jsonString) {
return _updateConnectionFromFollowRequestResult(connection, jsonString);
});
return result.mapError((error) => error is ExecError
? error
: ExecError(type: ErrorType.localError, message: error.toString()));
return result.execErrorCast();
}
Connection _updateConnectionFromFollowRequestResult(

Wyświetl plik

@ -11,7 +11,7 @@ import 'di_initialization.dart';
import 'globals.dart';
import 'routes.dart';
import 'services/auth_service.dart';
import 'services/blocks_service.dart';
import 'services/blocks_manager.dart';
import 'services/connections_manager.dart';
import 'services/direct_message_service.dart';
import 'services/entry_manager_service.dart';
@ -28,7 +28,9 @@ import 'utils/old_android_letsencrypte_cert.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
MediaKit.ensureInitialized();
if (kReleaseMode) {
MediaKit.ensureInitialized();
}
// await dotenv.load(fileName: '.env');
const enablePreview = false;
Logger.root.level = Level.FINER;
@ -110,8 +112,8 @@ class App extends StatelessWidget {
create: (_) =>
getIt<ActiveProfileSelector<InteractionsManager>>(),
),
ChangeNotifierProvider<ActiveProfileSelector<BlocksService>>(
create: (_) => getIt<ActiveProfileSelector<BlocksService>>(),
ChangeNotifierProvider<ActiveProfileSelector<BlocksManager>>(
create: (_) => getIt<ActiveProfileSelector<BlocksManager>>(),
),
],
child: MaterialApp.router(

Wyświetl plik

@ -113,6 +113,7 @@ class Connection {
}
enum ConnectionStatus {
blocked(0),
youFollowThem(1),
theyFollowYou(2),
mutual(3),
@ -145,6 +146,8 @@ extension FriendStatusWriter on ConnectionStatus {
return "You";
case ConnectionStatus.unknown:
return 'Unknown';
case ConnectionStatus.blocked:
return 'Blocked';
}
}
}

Wyświetl plik

@ -4,23 +4,18 @@ import 'package:provider/provider.dart';
import '../controls/padding.dart';
import '../routes.dart';
import '../services/blocks_service.dart';
import '../services/blocks_manager.dart';
import '../utils/active_profile_selector.dart';
import '../utils/snackbar_builder.dart';
class BlocksAndFiltersScreen extends StatelessWidget {
const BlocksAndFiltersScreen({super.key});
@override
Widget build(BuildContext context) {
final blocks = context
.watch<ActiveProfileSelector<BlocksService>>()
.activeEntry
.transform((s) => s.getBlocks())
.withError(
(error) => buildSnackbar(context, 'Error getting blocks: $error'),
)
.getValueOrElse(() => []);
final manager =
context.watch<ActiveProfileSelector<BlocksManager>>().activeEntry.value;
final blocks = manager.getBlocks();
return Scaffold(
appBar: AppBar(
title: const Text('Blocks & Filters'),
@ -48,7 +43,8 @@ class BlocksAndFiltersScreen extends StatelessWidget {
softWrap: true,
),
trailing: ElevatedButton(
onPressed: () {},
onPressed: () async =>
await manager.unblockConnection(contact),
child: const Text('Unblock'),
),
);

Wyświetl plik

@ -13,6 +13,7 @@ import '../routes.dart';
import '../services/connections_manager.dart';
import '../services/feature_version_checker.dart';
import '../services/follow_requests_manager.dart';
import '../services/network_status_service.dart';
import '../services/notifications_manager.dart';
import '../utils/active_profile_selector.dart';
import '../utils/url_opening_utils.dart';
@ -33,6 +34,7 @@ class _FollowRequestAdjudicationScreenState
@override
Widget build(BuildContext context) {
final nss = getIt<NetworkStatusService>();
final fm =
getIt<ActiveProfileSelector<FollowRequestsManager>>().activeEntry.value;
final cm = context
@ -71,7 +73,15 @@ class _FollowRequestAdjudicationScreenState
break;
case ConnectionStatus.you:
case ConnectionStatus.unknown:
body = Text('Invalid state, nothing to do here: ${contact.status}');
body = Text(nss.connectionUpdateStatus.value
? 'Loading...'
: 'Invalid state, nothing to do here: ${contact.status}');
break;
case ConnectionStatus.blocked:
// we should never get here because a blocked user shouldn't be allowed to create a connection request.
body = const Text(
'Use is blocked. Unblock to accept connection request.',
);
break;
}
}

Wyświetl plik

@ -10,6 +10,7 @@ import '../models/connection.dart';
import '../models/group_data.dart';
import '../routes.dart';
import '../services/auth_service.dart';
import '../services/blocks_manager.dart';
import '../services/connections_manager.dart';
import '../utils/active_profile_selector.dart';
import '../utils/snackbar_builder.dart';
@ -37,17 +38,20 @@ class _UserProfileScreenState extends State<UserProfileScreen> {
@override
Widget build(BuildContext context) {
final manager = context
final connectionManager = context
.watch<ActiveProfileSelector<ConnectionsManager>>()
.activeEntry
.value;
final body = manager.getById(widget.userId).fold(onSuccess: (profile) {
final blocksManager =
context.watch<ActiveProfileSelector<BlocksManager>>().activeEntry.value;
final body =
connectionManager.getById(widget.userId).fold(onSuccess: (profile) {
final notMyProfile =
getIt<AccountsService>().currentProfile.userId != profile.id;
return RefreshIndicator(
onRefresh: () async {
await manager.fullRefresh(profile);
await connectionManager.fullRefresh(profile);
},
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
@ -73,11 +77,15 @@ class _UserProfileScreenState extends State<UserProfileScreen> {
const VerticalPadding(),
Text('( ${profile.status.label()} )'),
const VerticalPadding(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
Wrap(
spacing: 10.0,
runSpacing: 10.0,
children: [
if (notMyProfile)
buildConnectionStatusToggle(context, profile, manager),
if (notMyProfile) ...[
buildConnectionStatusToggle(
context, profile, connectionManager),
buildBlockToggle(context, profile, blocksManager),
],
ElevatedButton(
onPressed: () => context.pushNamed(
ScreenPaths.userPosts,
@ -107,7 +115,7 @@ class _UserProfileScreenState extends State<UserProfileScreen> {
const VerticalPadding(),
if (profile.status == ConnectionStatus.mutual ||
profile.status == ConnectionStatus.youFollowThem)
buildGroups(context, profile, manager),
buildGroups(context, profile, connectionManager),
],
),
),
@ -183,6 +191,66 @@ class _UserProfileScreenState extends State<UserProfileScreen> {
);
}
Widget buildBlockToggle(
BuildContext context,
Connection profile,
BlocksManager manager,
) {
late Widget blockToggleButton;
switch (profile.status) {
case ConnectionStatus.blocked:
blockToggleButton = ElevatedButton(
onPressed: isUpdating
? null
: () async {
final confirm =
await showYesNoDialog(context, 'Unblock ${profile.name}');
if (confirm != true) {
return;
}
setState(() {
isUpdating = true;
});
await manager.unblockConnection(profile);
setState(() {
isUpdating = false;
});
},
child: const Text('Unblock'),
);
break;
case ConnectionStatus.mutual:
case ConnectionStatus.theyFollowYou:
case ConnectionStatus.youFollowThem:
case ConnectionStatus.none:
case ConnectionStatus.unknown:
blockToggleButton = ElevatedButton(
onPressed: isUpdating
? null
: () async {
final confirm =
await showYesNoDialog(context, 'Block ${profile.name}');
if (confirm != true) {
return;
}
setState(() {
isUpdating = true;
});
await manager.blockConnection(profile);
setState(() {
isUpdating = false;
});
},
child: const Text('Block'),
);
break;
case ConnectionStatus.you:
blockToggleButton = const SizedBox();
break;
}
return blockToggleButton;
}
Widget buildConnectionStatusToggle(
BuildContext context,
Connection profile,
@ -234,6 +302,7 @@ class _UserProfileScreenState extends State<UserProfileScreen> {
child: const Text('Follow'),
);
break;
case ConnectionStatus.blocked:
case ConnectionStatus.you:
case ConnectionStatus.unknown:
followToggleButton = const SizedBox();

Wyświetl plik

@ -0,0 +1,145 @@
import 'dart:collection';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:result_monad/result_monad.dart';
import '../friendica_client/friendica_client.dart';
import '../friendica_client/paged_response.dart';
import '../friendica_client/paging_data.dart';
import '../globals.dart';
import '../models/auth/profile.dart';
import '../models/connection.dart';
import '../utils/active_profile_selector.dart';
import 'connections_manager.dart';
class BlocksManager extends ChangeNotifier {
static final _logger = Logger('$BlocksManager');
final Profile profile;
final _blocks = <Connection>[];
final _pages = <PagedResponse>[];
var mayHaveMore = true;
var initialized = false;
BlocksManager(this.profile);
void clear() {
_blocks.clear();
_pages.clear();
mayHaveMore = true;
}
List<Connection> getBlocks() {
if (!initialized) {
updateBlocks(nextOnly: false);
}
return UnmodifiableListView(_blocks);
}
Future<void> blockConnection(Connection connection) async {
_logger
.finest('Attempting to block ${connection.name}: ${connection.status}');
await RelationshipsClient(profile)
.blockConnection(connection)
.withResult((blockedUser) {
getIt<ActiveProfileSelector<ConnectionsManager>>()
.getForProfile(profile)
.withResult(
(cm) => cm.upsertConnection(blockedUser),
);
}).match(
onSuccess: (blockedUser) {
_logger.finest(
'Successfully blocked ${blockedUser.name}: ${blockedUser.status}');
final existingIndex = _blocks.indexOf(connection);
if (existingIndex < 0) {
_blocks.add(blockedUser);
_sortBlocks();
} else {
_blocks.removeAt(existingIndex);
_blocks.insert(existingIndex, blockedUser);
}
notifyListeners();
},
onError: (error) {
_logger.severe('Error blocking ${connection.name}: $error');
},
);
}
Future<void> unblockConnection(Connection connection) async {
_logger.finest(
'Attempting to unblock ${connection.name}: ${connection.status}');
await RelationshipsClient(profile)
.unblockConnection(connection)
.withResult((blockedUser) {
getIt<ActiveProfileSelector<ConnectionsManager>>()
.getForProfile(profile)
.withResult(
(cm) => cm.upsertConnection(blockedUser),
);
}).match(
onSuccess: (unblockedUser) {
_logger.finest(
'Successfully unblocked ${unblockedUser.name}: ${unblockedUser.status}');
final existingIndex = _blocks.indexOf(connection);
if (existingIndex >= 0) {
_blocks.removeAt(existingIndex);
_sortBlocks();
}
notifyListeners();
},
onError: (error) {
_logger.severe('Error unblocking ${connection.name}: $error');
},
);
}
Future<void> updateBlocks({required bool nextOnly}) async {
if (nextOnly) {
clear();
}
final client = RelationshipsClient(profile);
final bootstrapping = _pages.isEmpty;
var page = bootstrapping ? PagingData() : _pages.last.next;
while (page != null) {
page = await client
.getBlocks(page)
.withResult((result) {
_blocks.addAll(result.data);
_pages.add(result);
})
.withError(
(error) => _logger.severe('Error getting blocks data: $error'),
)
.fold<PagingData?>(
onSuccess: (result) => result.next,
onError: (error) => null,
);
if (nextOnly) {
break;
}
}
getIt<ActiveProfileSelector<ConnectionsManager>>()
.getForProfile(profile)
.withResult((cm) => cm.upsertAllConnections(_blocks));
_sortBlocks();
notifyListeners();
}
void _sortBlocks() {
_blocks.sort(
(b1, b2) => b1.name.toLowerCase().compareTo(
b2.name.toLowerCase(),
),
);
}
}

Wyświetl plik

@ -1,82 +0,0 @@
import 'dart:collection';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:result_monad/result_monad.dart';
import '../friendica_client/friendica_client.dart';
import '../friendica_client/paged_response.dart';
import '../friendica_client/paging_data.dart';
import '../globals.dart';
import '../models/auth/profile.dart';
import '../models/connection.dart';
import '../utils/active_profile_selector.dart';
import 'connections_manager.dart';
class BlocksService extends ChangeNotifier {
static final _logger = Logger('$BlocksService');
final Profile profile;
final _blocks = <Connection>[];
final _pages = <PagedResponse>[];
var mayHaveMore = true;
var initialized = false;
BlocksService(this.profile);
void clear() {
_blocks.clear();
_pages.clear();
mayHaveMore = true;
}
List<Connection> getBlocks() {
if (!initialized) {
updateBlocks(nextOnly: false);
}
return UnmodifiableListView(_blocks);
}
Future<void> updateBlocks({required bool nextOnly}) async {
if (nextOnly) {
clear();
}
final client = BlocksClient(profile);
final bootstrapping = _pages.isEmpty;
var page = bootstrapping ? PagingData() : _pages.last.next;
while (page != null) {
page = await client
.getBlocks(page)
.withResult((result) {
_blocks.addAll(result.data);
_pages.add(result);
})
.withError(
(error) => _logger.severe('Error getting blocks data: $error'),
)
.fold<PagingData?>(
onSuccess: (result) => result.next,
onError: (error) => null,
);
if (nextOnly) {
break;
}
}
getIt<ActiveProfileSelector<ConnectionsManager>>()
.getForProfile(profile)
.withResult((cm) => cm.upsertAllConnections(_blocks));
_blocks.sort(
(b1, b2) => b1.name.toLowerCase().compareTo(
b2.name.toLowerCase(),
),
);
notifyListeners();
}
}

Wyświetl plik

@ -357,8 +357,8 @@ class ConnectionsManager extends ChangeNotifier {
}
Result<Connection, ExecError> getById(String id, {bool forceUpdate = false}) {
return conRepo.getById(id).andThenSuccess((c) {
if (forceUpdate) {
return conRepo.getById(id).transform((c) {
if (c.status == ConnectionStatus.unknown && forceUpdate) {
_refreshConnection(c, true);
}
return c;

Wyświetl plik

@ -33,7 +33,7 @@ Future<void> executeUpdatesForProfile(Profile profile) async {
.getForProfile(profile)
.withResultAsync((info) async {
final dt = DateTime.now().difference(info.lastMyConnectionsUpdate);
_logger.info('Time since last connections update: $dt');
_logger.finer('Time since last connections update: $dt');
if (dt >= _connectionsRefreshInterval) {
await getIt<ActiveProfileSelector<ConnectionsManager>>()
.getForProfile(profile)