lemmur-app-android/lib/pages/settings/blocks/blocks_store.dart

111 lines
2.9 KiB
Dart
Raw Normal View History

2021-09-14 23:45:26 +02:00
import 'package:lemmy_api_client/v3.dart';
import 'package:mobx/mobx.dart';
2021-10-02 17:42:28 +02:00
import '../../../util/async_store.dart';
2021-10-02 23:15:15 +02:00
import 'community_block_store.dart';
import 'user_block_store.dart';
2021-09-14 23:45:26 +02:00
part 'blocks_store.g.dart';
class BlocksStore = _BlocksStore with _$BlocksStore;
abstract class _BlocksStore with Store {
final String instanceHost;
final Jwt token;
2021-10-02 23:15:15 +02:00
_BlocksStore({required this.instanceHost, required this.token});
2021-09-14 23:45:26 +02:00
2021-10-02 23:15:15 +02:00
@observable
2021-11-05 16:00:03 +01:00
ObservableList<UserBlockStore>? _blockedUsers;
2021-10-02 23:15:15 +02:00
@observable
2021-11-05 16:00:03 +01:00
ObservableList<CommunityBlockStore>? _blockedCommunities;
2021-09-14 23:45:26 +02:00
final blocksState = AsyncStore<FullSiteView>();
2021-11-05 16:00:03 +01:00
final userBlockingState = AsyncStore<BlockedPerson>();
final communityBlockingState = AsyncStore<BlockedCommunity>();
2021-10-02 23:15:15 +02:00
@computed
Iterable<UserBlockStore>? get blockedUsers =>
_blockedUsers?.where((u) => u.blocked);
2021-09-14 23:45:26 +02:00
2021-10-02 23:15:15 +02:00
@computed
Iterable<CommunityBlockStore>? get blockedCommunities =>
_blockedCommunities?.where((c) => c.blocked);
2021-09-14 23:45:26 +02:00
2021-10-02 23:15:15 +02:00
@computed
bool get isUsable => blockedUsers != null && blockedCommunities != null;
2021-09-14 23:45:26 +02:00
2021-11-05 16:00:03 +01:00
@action
Future<void> blockUser(Jwt token, int id) async {
if (_blockedUsers == null) {
throw StateError("_blockedUsers can't be null at this moment");
}
final res = await userBlockingState.runLemmy(
instanceHost,
BlockPerson(
personId: id,
block: true,
auth: token.raw,
),
);
if (res != null &&
!_blockedUsers!.any((element) => element.person.id == id)) {
2021-11-05 16:00:03 +01:00
_blockedUsers!.add(
UserBlockStore(
instanceHost: instanceHost,
person: res.personView.person,
token: token,
),
);
}
}
@action
Future<void> blockCommunity(Jwt token, int id) async {
if (_blockedCommunities == null) {
throw StateError("_blockedCommunities can't be null at this moment");
}
final res = await communityBlockingState.runLemmy(
instanceHost,
BlockCommunity(
communityId: id,
block: true,
auth: token.raw,
),
);
if (res != null &&
2021-11-06 17:19:39 +01:00
!_blockedCommunities!.any((element) => element.community.id == id)) {
2021-11-05 16:00:03 +01:00
_blockedCommunities!.add(
CommunityBlockStore(
instanceHost: instanceHost,
community: res.communityView.community,
token: token,
),
);
}
}
2021-09-14 23:45:26 +02:00
@action
Future<void> refresh() async {
final result =
await blocksState.runLemmy(instanceHost, GetSite(auth: token.raw));
if (result != null) {
2021-10-02 23:15:15 +02:00
_blockedUsers = result.myUser!.personBlocks
.map((e) => UserBlockStore(
instanceHost: instanceHost, token: token, person: e.target))
2021-11-05 16:00:03 +01:00
.toList()
.asObservable();
2021-10-02 23:15:15 +02:00
_blockedCommunities = result.myUser!.communityBlocks
.map((e) => CommunityBlockStore(
instanceHost: instanceHost, token: token, community: e.community))
2021-11-05 16:00:03 +01:00
.toList()
.asObservable();
2021-09-14 23:45:26 +02:00
}
}
}