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 = <String>[];

  // Seed with the app labelers so that label prefs scoped to an app
  // labeler attach to its entry, following the official client behavior.
  // Keyed by DID and insertion ordered, so a labeler the user subscribes to
  // on top of the seeded app labelers is never listed twice.
  final labelers = <String, Map<String, LabelPreference>>{
    for (final did in appLabelers) did: <String, LabelPreference>{},
  };
  final labelPrefs = <ContentLabelPref>[];
  for (final preference in preferences) {
    switch (preference) {
      case UPreferencesAdultContentPref(:final data):
        adultContentEnabled = data.enabled;
      case UPreferencesLabelersPref(:final data):
        for (final labeler in data.labelers) {
          // Keep the already seeded entry instead of appending a duplicate;
          // dropping it would also discard the prefs scoped to it.
          labelers.putIfAbsent(
            labeler.did,
            () => <String, LabelPreference>{},
          );
        }
      case UPreferencesMutedWordsPref(:final data):
        mutedWords.addAll(data.items);
      case UPreferencesHiddenPostsPref(:final data):
        hiddenPosts.addAll(data.items.map((e) => e.toString()));
      case UPreferencesContentLabelPref(:final data):
        labelPrefs.add(data);
      default:
        break;
    }
  }

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

    if (labelPref.labelerDid != null) {
      final labelerLabels = labelers[labelPref.labelerDid];

      // A labeler-scoped pref only applies to the matching labeler; if the
      // labeler is not subscribed, the pref is dropped and must never fall
      // through to the global labels.
      if (labelerLabels == null) continue;

      // The legacy remap applies here exactly as it does globally: keyed
      // under the legacy identifier the preference would never be read.
      labelerLabels[_getModerationLabel(labelPref.label)] = pref;
    } else {
      labels[_getModerationLabel(labelPref.label)] = pref;
    }
  }

  return ModerationPrefs(
    adultContentEnabled: adultContentEnabled,
    labels: {
      ...kDefaultLabelSettings.map((k, v) => MapEntry(k.value, v)),
      ...labels,
    },
    labelers: labelers.entries
        .map((e) => ModerationPrefsLabeler(did: e.key, labels: e.value))
        .toList(),
    mutedWords: mutedWords,
    hiddenPosts: hiddenPosts,
  );
}