import 'package:get_it/get_it.dart'; import 'package:moxxmpp/moxxmpp.dart'; import 'package:moxxyv2/service/service.dart'; import 'package:moxxyv2/shared/events.dart'; enum BlockPushType { block, unblock } class BlocklistService { BlocklistService() : _blocklistCache = List.empty(growable: true), _requestedBlocklist = false; final List _blocklistCache; bool _requestedBlocklist; Future> _requestBlocklist() async { final manager = GetIt.I.get().getManagerById(blockingManager)!; _blocklistCache ..clear() ..addAll(await manager.getBlocklist()); _requestedBlocklist = true; return _blocklistCache; } /// Returns the blocklist from the database Future> getBlocklist() async { if (!_requestedBlocklist) { _blocklistCache ..clear() ..addAll(await _requestBlocklist()); } return _blocklistCache; } void onUnblockAllPush() { _blocklistCache.clear(); sendEvent( BlocklistUnblockAllEvent(), ); } Future onBlocklistPush(BlockPushType type, List items) async { // We will fetch it later when getBlocklist is called if (!_requestedBlocklist) return; final newBlocks = List.empty(growable: true); final removedBlocks = List.empty(growable: true); for (final item in items) { switch (type) { case BlockPushType.block: { if (_blocklistCache.contains(item)) continue; _blocklistCache.add(item); newBlocks.add(item); } break; case BlockPushType.unblock: { _blocklistCache.removeWhere((i) => i == item); removedBlocks.add(item); } break; } } sendEvent( BlocklistPushEvent( added: newBlocks, removed: removedBlocks, ), ); } Future blockJid(String jid) async { final manager = GetIt.I.get().getManagerById(blockingManager)!; return manager.block([ jid ]); } Future unblockJid(String jid) async { final manager = GetIt.I.get().getManagerById(blockingManager)!; return manager.unblock([ jid ]); } Future unblockAll() async { final manager = GetIt.I.get().getManagerById(blockingManager)!; return manager.unblockAll(); } }