getModerationPrefs method

ModerationPrefs getModerationPrefs({
  1. List<String> appLabelers = const [_kBskyLabelerDid],
})

Implementation

ModerationPrefs getModerationPrefs({
  List<String> appLabelers = const [_kBskyLabelerDid],
}) {
  bool adultContentEnabled = false;
  final labels = <String, LabelPreference>{};
  final mutedWords = <MutedWord>[];
  final hiddenPosts = <AtUri>[];

  final labelers = <Map<String, dynamic>>[];
  final labelPrefs = <ContentLabelPreference>[];
  for (final preference in preferences) {
    switch (preference) {
      case UPreferenceAdultContent():
        adultContentEnabled = preference.data.isEnabled;
        break;
      case UPreferenceLabelersPref():
        labelers.addAll(preference.data.labelers.map(
          (e) => {'did': e.did, 'labels': <String, LabelPreference>{}},
        ));
        break;
      case UPreferenceMutedWords():
        mutedWords.addAll(preference.data.items);
        break;
      case UPreferenceHiddenPosts():
        hiddenPosts.addAll(preference.data.items);
        break;
      case UPreferenceContentLabel():
        labelPrefs.add(preference.data);
        break;
    }
  }

  for (final labelPref in labelPrefs) {
    final pref = _getModerationLabelPreference(labelPref.visibility);

    if (labelPref.labelerDid != null && labelers.isNotEmpty) {
      final labeler =
          labelers.where((e) => e['did'] == labelPref.labelerDid).firstOrNull;

      if (labeler != null && labeler.isNotEmpty) {
        labeler['labels'][labelPref.label] = pref;
      }
    } else {
      labels[_getModerationLabel(labelPref.label)] = pref;
    }
  }

  return ModerationPrefs(
    adultContentEnabled: adultContentEnabled,
    labels: {
      ...kDefaultLabelSettings.map((k, v) => MapEntry(k.value, v)),
      ...labels,
    },
    labelers: [
      ...appLabelers
          .map((e) => {'did': e, 'labels': <String, LabelPreference>{}}),
      ...labelers,
    ]
        .map(
          (e) => ModerationPrefsLabeler(
            did: e['did'],
            labels: e['labels'],
          ),
        )
        .toList(),
    mutedWords: mutedWords,
    hiddenPosts: hiddenPosts,
  );
}