Skip to content
Draft

Rules #258

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 11 additions & 32 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@
"profile_edit": "Edit profile",
"profile_import": "Import profile",
"profile_delete": "Delete profile",
"profile_name": "Name",
"profile_setAsMain": "Set as main profile",
"profile_setAsMain_help": "The main profile serves as the foundation for all other profiles. Changes made to it will apply to all profiles unless a specific setting has been overridden.",
"profile_autoSelect": "Automatically select on app start",
Expand Down Expand Up @@ -497,33 +496,12 @@
}
}
},
"filterLists": "Filter lists",
"filterList_activateFilter": "Activate Filter",
"filterList_new": "New filter list",
"filterList_edit": "Edit filter list",
"filterList_import": "Import filter list",
"filterList_delete": "Delete filter list",
"filterList_name": "Name",
"filterList_phrases": "Phrases",
"filterList_addPhrase": "Add phrase",
"filterList_showWithContentWarning": "Show with content warning",
"filterList_matchMode": "Match mode",
"filterList_matchMode_simple": "Simple",
"filterList_matchMode_simple_help": "Will literally match any occurrence of the phrase. For example, the phrase \"app\" will match \"app\", and also \"apps\", \"application\", \"yapp\", etc.",
"filterList_matchMode_wholeWords": "Whole words",
"filterList_matchMode_wholeWords_help": "Will only match if there is a word boundary at both ends of the checked text, in other words, the phrase must be the whole word and cannot be in the middle of a word. For example, the phrase \"app\" will match \"app\", but not \"apps\", \"application\", \"yapp\", etc.",
"filterList_matchMode_regex": "Regular expression",
"filterList_matchMode_regex_help": "Will match content that is found by the specified regular expression, which is derived from the phrase.",
"filterList_caseSensitive": "Case sensitive",
"filterList_caseSensitive_help": "When enabled, will only match content with the same casing as the phrase.",
"filterListWarningX": "Filter list matches: {lists}",
"@filterListWarningX": {
"placeholders": {
"lists": {
"type": "String"
}
}
},
"rules": "Rules",
"rule_activate": "Activate rule",
"rule_new": "New rule",
"rule_edit": "Edit rule",
"rule_import": "Import rule",
"rule_delete": "Delete rule",
"confirmUnsubscribe": "You are about to unsubscribe. Continue?",
"confirmUnfollow": "You are about to unfollow. Continue?",
"altText": "Alt text",
Expand Down Expand Up @@ -637,9 +615,9 @@
}
}
},
"configShare_filterList_title": "Interstellar filter list",
"configShare_filterList_info": "Contains {count} phrases",
"@configShare_filterList_info": {
"configShare_rule_title": "Interstellar rule",
"configShare_rule_info": "Contains {count} actions",
"@configShare_rule_info": {
"placeholders": {
"count": {
"type": "int"
Expand Down Expand Up @@ -670,5 +648,6 @@
"errorPage_firstPage_button": "Try Again",
"errorPage_newPage": "Something went wrong. Tap to try again.",
"errorPage_caughtUp": "You're all caught up.\nLoad older items?",
"exitMessage": "Press BACK again to exit."
"exitMessage": "Press BACK again to exit.",
"name": "Name"
}
69 changes: 33 additions & 36 deletions lib/src/controller/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:interstellar/src/controller/database.dart';
import 'package:interstellar/src/controller/feed.dart';
import 'package:interstellar/src/controller/filter_list.dart';
import 'package:interstellar/src/controller/profile.dart';
import 'package:interstellar/src/controller/rule.dart';
import 'package:interstellar/src/controller/server.dart';
import 'package:interstellar/src/models/post.dart';
import 'package:interstellar/src/utils/jwt_http_client.dart';
Expand All @@ -32,7 +33,7 @@ class AppController with ChangeNotifier {
final _mainStore = StoreRef.main();
final _accountStore = StoreRef<String, JsonMap>('account');
final _feedStore = StoreRef<String, JsonMap>('feeds');
final _filterListStore = StoreRef<String, JsonMap>('filterList');
final _ruleStore = StoreRef<String, JsonMap>('rules');
final _profileStore = StoreRef<String, JsonMap>('profile');
final _serverStore = StoreRef<String, JsonMap>('server');
final _readStore = StoreRef<String, JsonMap>('read');
Expand Down Expand Up @@ -88,8 +89,8 @@ class AppController with ChangeNotifier {
late Map<String, Feed> _feeds;
Map<String, Feed> get feeds => _feeds;

late Map<String, FilterList> _filterLists;
Map<String, FilterList> get filterLists => _filterLists;
late Map<String, Rule> _rules;
Map<String, Rule> get rules => _rules;

late Function refreshState;

Expand All @@ -110,7 +111,7 @@ class AppController with ChangeNotifier {
_logger = Logger(
printer: SimplePrinter(printTime: true, colors: false),
output: FileOutput(file: await logFile),
filter: ProductionFilter()
filter: ProductionFilter(),
);
logger.i('Initializing interstellar');

Expand Down Expand Up @@ -175,10 +176,10 @@ class AppController with ChangeNotifier {
)).map((record) => MapEntry(record.key, Feed.fromJson(record.value))),
);

_filterLists = Map.fromEntries(
(await _filterListStore.find(db)).map(
(record) => MapEntry(record.key, FilterList.fromJson(record.value)),
),
_rules = Map.fromEntries(
(await _ruleStore.find(
db,
)).map((record) => MapEntry(record.key, Rule.fromJson(record.value))),
);

_translator = SimplyTranslator(EngineType.libre);
Expand Down Expand Up @@ -581,67 +582,63 @@ class AppController with ChangeNotifier {
await _feedStore.record(FieldKey.escape(oldName)).delete(db);
}

Future<void> setFilterList(String name, FilterList value) async {
_filterLists[name] = value;
Future<void> setRule(String name, Rule value) async {
_rules[name] = value;

notifyListeners();

await _filterListStore
.record(FieldKey.escape(name))
.put(db, value.toJson());
await _ruleStore.record(FieldKey.escape(name)).put(db, value.toJson());
}

Future<void> removeFilterList(String name) async {
_filterLists.remove(name);
Future<void> removeRule(String name) async {
_rules.remove(name);

// Remove a profile's activation value if it is for this filter list
// Remove a profile's activation value if it is for this rule
for (var record in await _profileStore.find(db)) {
final profile = ProfileOptional.fromJson(record.value);
if (profile.filterLists?.containsKey(name) == true) {
final newProfileFilterLists = {...profile.filterLists!};
if (profile.rules?.containsKey(name) == true) {
final newProfileFilterLists = {...profile.rules!};
newProfileFilterLists.remove(name);
await _profileRecord(record.key).put(
db,
profile.copyWith(filterLists: newProfileFilterLists).toJson(),
);
await _profileRecord(
record.key,
).put(db, profile.copyWith(rules: newProfileFilterLists).toJson());
}
}

_rebuildProfile();

notifyListeners();

await _filterListStore.record(FieldKey.escape(name)).delete(db);
await _ruleStore.record(FieldKey.escape(name)).delete(db);
}

Future<void> renameFilterList(String oldName, String newName) async {
_filterLists[newName] = _filterLists[oldName]!;
_filterLists.remove(oldName);
Future<void> renameRule(String oldName, String newName) async {
_rules[newName] = _rules[oldName]!;
_rules.remove(oldName);

// Update a profile's activation value if it is for this filter list
for (var record in await _profileStore.find(db)) {
final profile = ProfileOptional.fromJson(record.value);
if (profile.filterLists?.containsKey(oldName) == true) {
if (profile.rules?.containsKey(oldName) == true) {
final newProfileFilterLists = {
...profile.filterLists!,
newName: profile.filterLists![oldName]!,
...profile.rules!,
newName: profile.rules![oldName]!,
};
newProfileFilterLists.remove(oldName);
await _profileRecord(record.key).put(
db,
profile.copyWith(filterLists: newProfileFilterLists).toJson(),
);
await _profileRecord(
record.key,
).put(db, profile.copyWith(rules: newProfileFilterLists).toJson());
}
}

_rebuildProfile();

notifyListeners();

await _filterListStore
await _ruleStore
.record(FieldKey.escape(newName))
.put(db, _filterLists[newName]!.toJson());
await _filterListStore.record(FieldKey.escape(oldName)).delete(db);
.put(db, _rules[newName]!.toJson());
await _ruleStore.record(FieldKey.escape(oldName)).delete(db);
}

Future<void> vibrate(HapticsType type) async {
Expand Down
33 changes: 17 additions & 16 deletions lib/src/controller/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class ProfileRequired with _$ProfileRequired {
required bool fullImageSizeThreads,
required bool fullImageSizeMicroblogs,
// Feed defaults
@FeedViewConverter()
required FeedView feedDefaultView,
@FeedViewConverter() required FeedView feedDefaultView,
required FeedSource feedDefaultFilter,
required FeedSort feedDefaultThreadsSort,
required FeedSort feedDefaultMicroblogSort,
Expand All @@ -75,7 +74,7 @@ class ProfileRequired with _$ProfileRequired {
required SwipeAction swipeActionRightLong,
required double swipeActionThreshold,
// Filter list activations
required Map<String, bool> filterLists,
required Map<String, bool> rules,
required bool showErrors,
}) = _ProfileRequired;

Expand Down Expand Up @@ -139,7 +138,9 @@ class ProfileRequired with _$ProfileRequired {
feedDefaultMicroblogSort:
profile?.feedDefaultMicroblogSort ??
defaultProfile.feedDefaultMicroblogSort,
feedDefaultCombinedSort: profile?.feedDefaultCombinedSort ?? defaultProfile.feedDefaultCombinedSort,
feedDefaultCombinedSort:
profile?.feedDefaultCombinedSort ??
defaultProfile.feedDefaultCombinedSort,
feedDefaultExploreSort:
profile?.feedDefaultExploreSort ??
defaultProfile.feedDefaultExploreSort,
Expand Down Expand Up @@ -178,7 +179,7 @@ class ProfileRequired with _$ProfileRequired {
profile?.swipeActionRightLong ?? defaultProfile.swipeActionRightLong,
swipeActionThreshold:
profile?.swipeActionThreshold ?? defaultProfile.swipeActionThreshold,
filterLists: profile?.filterLists ?? defaultProfile.filterLists,
rules: profile?.rules ?? defaultProfile.rules,
showErrors: profile?.showErrors ?? defaultProfile.showErrors,
);

Expand Down Expand Up @@ -230,7 +231,7 @@ class ProfileRequired with _$ProfileRequired {
swipeActionRightShort: SwipeAction.bookmark,
swipeActionRightLong: SwipeAction.reply,
swipeActionThreshold: 0.20,
filterLists: {},
rules: {},
showErrors: true,
);
}
Expand Down Expand Up @@ -270,8 +271,7 @@ class ProfileOptional with _$ProfileOptional {
required bool? fullImageSizeThreads,
required bool? fullImageSizeMicroblogs,
// Feed defaults
@FeedViewConverter()
required FeedView? feedDefaultView,
@FeedViewConverter() required FeedView? feedDefaultView,
required FeedSource? feedDefaultFilter,
required FeedSort? feedDefaultThreadsSort,
required FeedSort? feedDefaultMicroblogSort,
Expand All @@ -296,7 +296,7 @@ class ProfileOptional with _$ProfileOptional {
required SwipeAction? swipeActionRightLong,
required double? swipeActionThreshold,
// Filter list activations
required Map<String, bool>? filterLists,
required Map<String, bool>? rules,
required bool? showErrors,
}) = _ProfileOptional;

Expand Down Expand Up @@ -351,7 +351,7 @@ class ProfileOptional with _$ProfileOptional {
swipeActionRightShort: null,
swipeActionRightLong: null,
swipeActionThreshold: null,
filterLists: null,
rules: null,
showErrors: null,
);

Expand Down Expand Up @@ -397,7 +397,8 @@ class ProfileOptional with _$ProfileOptional {
other.feedDefaultThreadsSort ?? feedDefaultThreadsSort,
feedDefaultMicroblogSort:
other.feedDefaultMicroblogSort ?? feedDefaultMicroblogSort,
feedDefaultCombinedSort: other.feedDefaultCombinedSort ?? feedDefaultCombinedSort,
feedDefaultCombinedSort:
other.feedDefaultCombinedSort ?? feedDefaultCombinedSort,
feedDefaultExploreSort:
other.feedDefaultExploreSort ?? feedDefaultExploreSort,
feedDefaultCommentSort:
Expand Down Expand Up @@ -428,9 +429,9 @@ class ProfileOptional with _$ProfileOptional {
other.swipeActionRightLong ?? this.swipeActionRightLong,
swipeActionThreshold:
other.swipeActionThreshold ?? this.swipeActionThreshold,
filterLists: filterLists != null && other.filterLists != null
? {...filterLists!, ...other.filterLists!}
: other.filterLists ?? filterLists,
rules: rules != null && other.rules != null
? {...rules!, ...other.rules!}
: other.rules ?? rules,
showErrors: other.showErrors ?? showErrors,
);
}
Expand Down Expand Up @@ -475,7 +476,7 @@ class ProfileOptional with _$ProfileOptional {

// Remove fields that depend on a certain setup
ProfileOptional exportReady() {
return copyWith(autoSwitchAccount: null, filterLists: null);
return copyWith(autoSwitchAccount: null, rules: null);
}
}

Expand All @@ -496,4 +497,4 @@ class FeedViewConverter implements JsonConverter<FeedView, String> {

@override
String toJson(FeedView view) => view.name;
}
}
Loading