Skip to content

Validate Mbin community names when creating a community - #420

Open
neo22neo wants to merge 1 commit into
interstellar-app:mainfrom
neo22neo:fix/mbin-community-name-validation
Open

neo22neo wants to merge 1 commit into
interstellar-app:mainfrom
neo22neo:fix/mbin-community-name-validation

Conversation

@neo22neo

@neo22neo neo22neo commented Sep 4, 2026

Copy link
Copy Markdown

Problem

When creating a community on an Mbin server, the form only disabled the submit button on an empty name. Mbin rejects any magazine name outside /^[a-zA-Z0-9_]{2,25}$/ (RegPatterns::MAGAZINE_NAME), so names with spaces, hyphens, accents, etc. failed server-side with no guidance for the user.

Changes

  • lib/src/utils/mbin_community_name.dart (new) — pure, testable helpers:
    • isValidMbinCommunityName — the ^[a-zA-Z0-9_]{2,25}$ check.
    • mbinCommunityNameIssue — why a name is invalid (invalidCharacters / tooShort / tooLong); empty is treated as "not entered yet".
    • suggestMbinCommunityName — best-effort sanitized name (unsupported runs → _, collapsed, trimmed, truncated to 25), or null when nothing usable can be salvaged.
  • lib/src/widgets/text_editor.dart — shared TextEditor gains helperText / errorText passthrough.
  • lib/src/screens/explore/community_owner_panel.dart — in CommunityOwnerPanelGeneral (creation path only), when the active server is Mbin:
    • show the naming rule as helper text,
    • show an inline error while the name is invalid,
    • offer a one-tap Use “…” action to apply the suggested name,
    • keep the submit button disabled until the name is valid.
    • Lemmy / PieFed behaviour is unchanged (still maxLength: 25, no extra validation).
  • lib/l10n/app_en.arb — new keys: community_nameMbinHelp, community_nameInvalidCharacters, community_nameTooShort, community_nameTooLong, community_nameUseSuggestion.
  • pubspec.yaml / test/ — add flutter_test dev dependency and focused unit tests for the new helpers.

Notes

  • The regex matches upstream Mbin (MbinOrg/mbin src/Utils/RegPatterns.php).
  • I don't have a Flutter toolchain in this environment, so dart format / flutter test / flutter gen-l10n were not run locally — please double-check CI.

🤖 Generated with Claude Code

Mbin rejects magazine names that fall outside /^[a-zA-Z0-9_]{2,25}$/, but
the create-community form only disabled submit on an empty name, so an
invalid name failed server-side with no guidance.

- Add mbin_community_name.dart with pure, tested helpers to validate a
  name, describe why it is invalid, and derive a sanitized suggestion.
- Give the shared TextEditor helperText/errorText support.
- In CommunityOwnerPanelGeneral (creation only), on Mbin: show the rule as
  helper text, an inline error while the name is invalid, a one-tap
  "Use suggestion" action, and keep submit disabled until the name is
  valid. Lemmy/PieFed behaviour is unchanged.
- Add flutter_test dev dependency and focused unit tests for the helpers.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N7b7QotawaBJQ4isQKd14x

@jwr1 jwr1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @neo22neo, thank you for submitting your first PR here! I especially like the idea of suggesting a community name based on the title someone has set already. I haven't checked out the code locally yet, but upon first review of the code, I do have a few things to note.

Comment on lines +123 to +124
case MbinCommunityNameIssue.tooLong:
return l(context).community_nameTooLong(mbinCommunityNameMaxLength);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of enforcing the max community length as an error, why don't you use the maxLength field on the TextEditor widget? It would display the current and max length of the text as the user is typing, and prevent the user from typing over that length.

Comment on lines +46 to +55
var suggestion = name
.replaceAll(_mbinCommunityNameInvalidChars, '_')
.replaceAll(RegExp(r'_+'), '_')
.replaceAll(RegExp(r'^_+|_+$'), '');

if (suggestion.length > mbinCommunityNameMaxLength) {
suggestion = suggestion
.substring(0, mbinCommunityNameMaxLength)
.replaceAll(RegExp(r'_+$'), '');
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it work to move the .replaceAll(RegExp(r'^_+|_+$'), '') portion from line 49 to after the if statement, that way we could remove the .replaceAll(RegExp(r'_+$'), '') portion from line 54 that's in the if statement?

/// A best-effort valid name derived from [name], or `null` when nothing
/// usable can be salvaged (e.g. the input has no letters/digits at all) or
/// when [name] is already valid.
String? suggestMbinCommunityName(String name) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One addition that would be nice for this function is to also make the suggested name all lowercase, as that seems to be the general convention for community names.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants