{ config, lib, pkgs, ... }:
{
  imports = [
    ./nginx
  ];

  config = lib.mkIf config.system.socialserver.enable {
    services.matrix-synapse = {
      enable = true;
      settings = {
        server_name = "${config.domains.p1}";
        public_baseurl = "https://matrix.${config.domains.p1}";
        suppress_key_server_warning = true;

        listeners = [
          {
            port = 8008;
            bind_addresses = [ "::" "0.0.0.0" ];
            resources = [{
              compress = true;
              names = [
                "client"
                "federation"
              ];
            }];
            type = "http";
            tls = false;
            x_forwarded = true;
          }
        ];

        email = {
          notif_from = "NixFox Matrix <noreply@${config.domains.p2}>";
          smtp_host = "mx.${config.domains.p1}";
          smtp_user = "noreply@${config.domains.p2}";
          smtp_pass = config.secrets.noreplyPassword;
          enable_tls = true;
          smtp_port = 587;
          require_transport_security = true;
        };

        # Disable registration without email
        registrations_require_3pid = [ "email" ];

        # Allow only this range of emails
        allowed_local_3pids = [
          {
            medium = "email";
            pattern = ''^[^@]+@nixfox\.ca$'';
          }
          {
            medium = "email";
            pattern = ''^[^@]+@freecorn1854\.win$'';
          }
          {
            medium = "email";
            pattern = ''^[^@]+@lunamoonlight\.xyz$'';
          }
        ];

        # Set the type of database
        database.name = "sqlite3";

        # Allow account registration
        enable_registration = true;

        # General settings
        url_preview_enabled = true;
        max_upload_size = "50M";
        report_stats = false;
        burst_count = 15;
      };
    };
    environment.persistence."/persist".directories = [
      "/var/lib/matrix-synapse"
    ];
  };
}
