addLabel method

void addLabel({
  1. required LabelTarget target,
  2. required Label label,
  3. required ModerationOpts opts,
})

Implementation

void addLabel({
  required LabelTarget target,
  required Label label,
  required ModerationOpts opts,
}) {
  InterpretedLabelValueDefinition? labelDef;
  if (customLabelValueRegex.hasMatch(label.val)) {
    labelDef = opts.labelDefs[label.src]
            ?.where((e) => e.identifier == label.val)
            .firstOrNull ??
        c.kLabels[c.KnownLabelValue.valueOf(label.val)];
  } else {
    labelDef = c.kLabels[c.KnownLabelValue.valueOf(label.val)];
  }

  if (labelDef == null) {
    return; // ignore labels we don't understand
  }

  final isSelf = label.src == did;
  final labeler = isSelf
      ? null
      : opts.prefs.labelers.where((e) => e.did == label.src).firstOrNull;

  if (!isSelf && labeler == null) {
    return; // skip labelers not configured by the user
  }
  if (isSelf && labelDef.flags.contains(LabelValueDefinitionFlag.noSelf)) {
    return; // skip self-labels that aren't supported
  }

  LabelPreference labelPref = labelDef.defaultSetting;
  if (!labelDef.configurable) {
    labelPref = labelDef.defaultSetting;
  } else if (labelDef.flags.contains(LabelValueDefinitionFlag.adult) &&
      !opts.prefs.adultContentEnabled) {
    labelPref = LabelPreference.hide;
  } else if (labeler?.labels.containsKey(labelDef.identifier) ?? false) {
    labelPref = labeler!.labels[labelDef.identifier]!;
  } else if (opts.prefs.labels.containsKey(labelDef.identifier)) {
    labelPref = opts.prefs.labels[labelDef.identifier]!;
  }

  if (labelPref == LabelPreference.ignore) {
    return; // ignore labels the user has asked to ignore
  }

  if (labelDef.flags.contains(LabelValueDefinitionFlag.unauthed) &&
      opts.userDid != null &&
      opts.userDid!.isNotEmpty) {
    return; // ignore 'unauthed' labels when the user is authed
  }

  int priority;
  final severity = _measureModerationBehaviorSeverity(
    labelDef.behaviors[target],
  );

  if (labelDef.flags.contains(LabelValueDefinitionFlag.noOverride) ||
      (labelDef.flags.contains(LabelValueDefinitionFlag.adult) &&
          !opts.prefs.adultContentEnabled)) {
    priority = 1;
  } else if (labelPref == LabelPreference.hide) {
    priority = 2;
  } else if (severity == ModerationBehaviorSeverity.high) {
    priority = 5; // blurring profile view or content view
  } else if (severity == ModerationBehaviorSeverity.medium) {
    priority = 7; // blurring content list or content media
  } else {
    priority = 8; // blurring avatar, adding alerts
  }

  bool noOverride = false;
  if (labelDef.flags.contains(LabelValueDefinitionFlag.noOverride)) {
    noOverride = true;
  } else if (labelDef.flags.contains(LabelValueDefinitionFlag.adult) &&
      !opts.prefs.adultContentEnabled) {
    noOverride = true;
  }

  causes.add(
    ModerationCause.label(
      data: ModerationCauseLabel(
        source: isSelf || labeler != null
            ? const ModerationCauseSource.user(
                data: ModerationCauseSourceUser(),
              )
            : ModerationCauseSource.labeler(
                data: ModerationCauseSourceLabeler(
                  did: labeler!.did,
                ),
              ),
        label: label,
        labelDef: labelDef,
        target: target,
        setting: labelPref,
        behavior: labelDef.behaviors[target] ?? const {},
        noOverride: noOverride,
        priority: priority,
      ),
    ),
  );
}