Draupnir

repository·main·Indexed 42 days ago

https://github.com/the-draupnir-project/draupnir

A unified, extensible platform for growing and managing Matrix communities. It provides automated moderation via community-curated policy lists, advanced homeserver administration, and an interactive prompt-based UX. Draupnir serves as a forwards and backwards compatible drop-in replacement for Mjolnir and includes specialized libraries such as matrix-protection-suite, interface-manager, and matrix-basic-types.

Tokens
53.4K
Snippets
176
Records
227
Agent score
54%

What's inside Draupnir

  1. Overview of matrix-protection-suite

    main
    The matrix-protection-suite is a library providing interfaces and models for interacting with Matrix policy lists. It is designed to be backend-agnostic, meaning these models are not tied to a specific Matrix client implementation. The library extracts core components from Draupnir and Mjolnir to allow them to be used in various environments, including other bots, widgets, and web applications.
  2. What @the-draupnir-project/interface-manager does

    main

    The @the-draupnir-project/interface-manager library provides a command-oriented presentation interface specifically designed for Matrix bots. It handles three primary responsibilities:

    1. Command Parsing: Interpreting user input into structured commands.
    2. Rendering: Generating visual representations of command outputs.
    3. Interaction Prompts: Managing how the bot interacts with users via prompts.

    Additionally, it includes a JSXFactory that allows developers to write JSX templates. These templates are transformed into org.matrix.custom.html for rich Matrix rendering, with a plain text fallback for environments that do not support HTML.

  3. Use distinct types for Matrix identifiers

    main

    The @the-draupnir-project/matrix-basic-types library provides specialized types for Matrix identifiers to prevent accidental mixing of string values. Instead of using generic strings, use the specific types provided for StringUserID, MatrixRoomID, and eventID. This ensures type safety when handling room IDs, user IDs, and event IDs throughout your application.

    async function acceptInvitation(
      inviteSender: StringUserID,
      room: MatrixRoomID
    ): Promise<Result<void>> {
      console.log(
        `accepting invitation from ${inviteSender} to ${room.toPermalink()}`
      );
      return await client.joinRoom(room.toRoomIDOrAlias(), room.getViaServers());
    }
  4. Technical architecture of Draupnir

    main

    Draupnir is built on several core technical improvements over traditional moderation bots:

    • Responsiveness: It does not need to request data from the homeserver before applying bans.
    • Revision System: Uses an advanced persistent revision system for room state, members, policies, and policy matches, processing only simple deltas.
    • Backing Store: Offers a room state backing store to allow fast startup even when deployed far from the homeserver.
    • Extensibility: Core functionality is implemented as protections that can be dynamically enabled/disabled or customized using JS/TS.
    • Core Packages:
      • matrix-protection-suite: Provides Matrix client code, event parsing, and types.
      • interface-manager: Provides an advanced command-oriented interface.
      • matrix-basic-types: Handles Matrix string types.
  5. How Draupnir's Prompt UX works

    main

    Draupnir uses an interactive prompt-based UX to reduce the need for manual commands. Instead of typing complex strings, Draupnir sends interactive prompts to your management room for common activities:

    • Banning/Unbanning: When you ban or unban a user via your standard Matrix client, Draupnir will prompt you in the management room to decide if that action should be added to a policy list.
    • Command usage: If you prefer using commands, the ban command now takes the user/entity as the first argument. You can provide just the entity, and Draupnir will prompt you for the policy list and the ban reason.
    • Automated Prompts: Prompts are automatically sent for inviting the bot to protect rooms, watching policy lists, and managing bans.
  6. Migrating from Mjolnir to Draupnir

    main

    Draupnir is a forwards and backwards compatible drop-in replacement for Mjolnir. Migration is straightforward and typically requires no manual steps beyond updating your server configuration to use the latest Draupnir Docker image instead of the Mjolnir image:

    # Use the latest Draupnir image
    gnuxie/draupnir:latest

    Because Draupnir maintains backwards compatibility, you can test it and switch back to Mjolnir if necessary.

  7. Initial setup commands after deployment

    main

    Once Draupnir is running, use these commands to initialize your community protections:

    1. Create a ban list: Create a new ban list with a shortcode and an alias. This will automatically invite you to the room created for managing that list.
      !draupnir list create <shortcode> <alias>
      # Example:
      !draupnir list create my-coc code-of-conduct-ban-list
    2. Explore capabilities: Use the help command to see all available functions.
      !draupnir help

    For detailed configuration, refer to the config/default.yaml file in the repository.

    !draupnir list create my-coc code-of-conduct-ban-list
    !draupnir help
  8. Use Draupnir administrative commands

    main

    Draupnir provides a comprehensive CLI interface for managing Matrix servers (specifically Synapse) and implementing moderation policies. Commands are organized into logical subcommands under the draupnir prefix.

    Common administrative tasks include managing aliases, bans, protections, rooms, and rules. Additionally, Draupnir can proxy or extend native synapse admin commands.

  9. Manage report polling pagination state

    main

    The ReportPoller uses a pagination mechanism to fetch reports from the Synapse admin API. It tracks progress using an opaque token (or the total count) and persists this state to the Matrix user's account data under the event type org.matrix.mjolnir.report_poll.

    This ensures that if the bot restarts, it can resume polling from the last successfully processed report without duplication.

  10. Manage command hierarchies with CommandTable

    main

    The CommandTable interface (implemented by StandardCommandTable) is used to organize and register commands within a hierarchical structure. It allows you to define commands with specific designators (sequences of strings) and supports subcommands, command importing, and type translation.

    Key capabilities include:

    • Interning Commands: Registering a CommandDescription with a specific designator path.
    • Importing Tables: Bringing in commands from another CommandTable and prefixing them with a baseDesignator.
    • Command Lookup: Finding a matching command from a PresentationArgumentStream.
    • Type Translation: Registering PresentationTypeTranslator instances to handle conversions between different presentation types.
    import { StandardCommandTable } from '@the-draupnir-project/interface-manager';
    
    const table = new StandardCommandTable('my-commands');
    // Register a command with designator ['cmd', 'subcmd']
    // table.internCommand(commandDescription, ['cmd', 'subcmd']);