Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(rexctl): add admin, invite, registration, branding commands

Hampus4576a5de03d4
diff --git a/fluxer_rexctl/cmd/admins.go b/fluxer_rexctl/cmd/admins.go
new file mode 100644
index 000000000..ea642c1a1
--- /dev/null
+++ b/fluxer_rexctl/cmd/admins.go
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package cmd
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/spf13/cobra"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/adminapi"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
+)
+
+func newAdminsCmd(app *App) *cobra.Command {
+ c := &cobra.Command{Use: "admins", Short: "Choose who can manage your instance", Args: noArgs}
+ add := &cobra.Command{Use: "add <user>", Short: "Make an account an admin", Args: exactArgs(userArg)}
+ add.RunE = func(c *cobra.Command, args []string) error {
+ return adminWrite(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {
+ u, err := api.ResolveUser(ctx, args[0])
+ if err != nil {
+ return err
+ }
+ if err := api.SetAdmin(ctx, u.ID, true); err != nil {
+ return err
+ }
+ app.UI.OK("%s is an admin.", u.Username)
+ return nil
+ })
+ }
+ remove := &cobra.Command{Use: "remove <user>", Short: "Remove admin access from an account", Args: exactArgs(userArg)}
+ remove.RunE = func(c *cobra.Command, args []string) error {
+ return adminWrite(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {
+ u, err := api.ResolveUser(ctx, args[0])
+ if err != nil {
+ return err
+ }
+ if !u.Admin {
+ app.UI.OK("%s isn't an admin.", u.Username)
+ return nil
+ }
+ if err := api.SetAdmin(ctx, u.ID, false); err != nil {
+ return err
+ }
+ app.UI.OK("Removed admin access from %s.", u.Username)
+ return nil
+ })
+ }
+ signInLink := &cobra.Command{Use: "sign-in-link <user>", Short: "Print a link that signs an admin in once", Args: exactArgs(userArg)}
+ signInLink.RunE = func(c *cobra.Command, args []string) error {
+ return adminWrite(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {
+ u, err := api.ResolveUser(ctx, args[0])
+ if err != nil {
+ return err
+ }
+ if !u.Admin {
+ return ui.Refused(fmt.Sprintf("%s isn't an admin.", u.Username), fmt.Sprintf("Run rexctl admins add %s first.", u.Username))
+ }
+ return printSignInLink(ctx, app, api, u.ID, u.Username)
+ })
+ }
+ c.AddCommand(add, remove, signInLink)
+ return c
+}
+
+func printSignInLink(ctx context.Context, app *App, api *adminapi.Client, id, username string) error {
+ link, expires, err := api.SignInLink(ctx, id)
+ if err != nil {
+ return err
+ }
+ app.UI.OK("Made a link that signs %s in. It works once, until %s.", username, expires.Local().Format("15:04 on 2 January"))
+ printLink(app, link)
+ return nil
+}
diff --git a/fluxer_rexctl/cmd/branding.go b/fluxer_rexctl/cmd/branding.go
new file mode 100644
index 000000000..a878fd2b5
--- /dev/null
+++ b/fluxer_rexctl/cmd/branding.go
@@ -0,0 +1,100 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package cmd
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+ "os"
+ "regexp"
+ "strings"
+
+ "github.com/spf13/cobra"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/adminapi"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
+)
+
+type brandingFlags struct {
+ Name string
+ Colour string
+ Logo string
+ Icon string
+}
+
+var hexColour = regexp.MustCompile(`^#?(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$`)
+
+func readImage(path, flag string) ([]byte, error) {
+ data, err := os.ReadFile(path)
+ if err != nil {
+ return nil, ui.Failed(fmt.Sprintf("Couldn't read %s.", path), fmt.Sprintf("Pass --%s with the path to an image file.", flag))
+ }
+ switch http.DetectContentType(data) {
+ case "image/png", "image/jpeg", "image/gif", "image/webp":
+ return data, nil
+ }
+ return nil, ui.Usage(fmt.Sprintf("%s isn't a PNG, JPEG, GIF or WebP image.", path))
+}
+
+func newBrandingCmd(app *App) *cobra.Command {
+ c := &cobra.Command{Use: "branding", Short: "Change your instance's name, colour and images", Args: noArgs}
+ var f brandingFlags
+ set := &cobra.Command{Use: "set", Short: "Change the name, colour, logo or icon", Args: noArgs}
+ set.Flags().StringVar(&f.Name, "name", "", "your instance's name")
+ set.Flags().StringVar(&f.Colour, "colour", "", "accent colour as a hex code, like #4641d9")
+ set.Flags().StringVar(&f.Logo, "logo", "", "image file for the logo")
+ set.Flags().StringVar(&f.Icon, "icon", "", "image file for the icon")
+ set.RunE = func(c *cobra.Command, _ []string) error {
+ if f.Name == "" && f.Colour == "" && f.Logo == "" && f.Icon == "" {
+ return ui.Usage("Pass --name, --colour, --logo or --icon.")
+ }
+ name := strings.TrimSpace(f.Name)
+ colour := strings.TrimSpace(f.Colour)
+ if colour != "" {
+ if !hexColour.MatchString(colour) {
+ return ui.Usage("Pass --colour as a hex code, like #4641d9.")
+ }
+ colour = "#" + strings.ToLower(strings.TrimPrefix(colour, "#"))
+ }
+ var logo, icon []byte
+ var err error
+ if f.Logo != "" {
+ if logo, err = readImage(f.Logo, "logo"); err != nil {
+ return err
+ }
+ }
+ if f.Icon != "" {
+ if icon, err = readImage(f.Icon, "icon"); err != nil {
+ return err
+ }
+ }
+ return adminWrite(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {
+ if err := api.SetBranding(ctx, name, colour); err != nil {
+ return err
+ }
+ if name != "" {
+ app.UI.OK("Your instance is called %s.", name)
+ }
+ if colour != "" {
+ app.UI.OK("Set the accent colour to %s.", colour)
+ }
+ if logo != nil {
+ if err := api.UploadBranding(ctx, adminapi.Logo, logo); err != nil {
+ return err
+ }
+ app.UI.OK("Uploaded the logo.")
+ }
+ if icon != nil {
+ if err := api.UploadBranding(ctx, adminapi.Icon, icon); err != nil {
+ return err
+ }
+ app.UI.OK("Uploaded the icon.")
+ }
+ return nil
+ })
+ }
+ c.AddCommand(set)
+ return c
+}
diff --git a/fluxer_rexctl/cmd/invites.go b/fluxer_rexctl/cmd/invites.go
new file mode 100644
index 000000000..abf1efad3
--- /dev/null
+++ b/fluxer_rexctl/cmd/invites.go
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package cmd
+
+import (
+ "context"
+ "fmt"
+ "strconv"
+ "strings"
+ "time"
+
+ "github.com/spf13/cobra"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/adminapi"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
+)
+
+func parseExpiry(s string) (time.Duration, error) {
+ s = strings.ToLower(strings.TrimSpace(s))
+ bad := ui.Usage(fmt.Sprintf("Pass --expires as a time like 12h, 7d or 4w, or never. %q isn't one.", s))
+ if s == "never" || s == "0" {
+ return 0, nil
+ }
+ if len(s) < 2 {
+ return 0, bad
+ }
+ n, err := strconv.Atoi(s[:len(s)-1])
+ if err != nil || n <= 0 {
+ return 0, bad
+ }
+ unit := map[byte]time.Duration{'m': time.Minute, 'h': time.Hour, 'd': 24 * time.Hour, 'w': 7 * 24 * time.Hour}[s[len(s)-1]]
+ if unit == 0 {
+ return 0, bad
+ }
+ return time.Duration(n) * unit, nil
+}
+
+func expiryText(expiresAt *string) string {
+ if expiresAt == nil {
+ return "never"
+ }
+ t, err := time.Parse(time.RFC3339, *expiresAt)
+ if err != nil {
+ return *expiresAt
+ }
+ t = t.Local()
+ if time.Until(t) < 24*time.Hour {
+ return t.Format("15:04 on 2 January")
+ }
+ return t.Format("2 January")
+}
+
+func expiryWhen(rfc string) string {
+ t, err := time.Parse(time.RFC3339, rfc)
+ if err != nil {
+ return "on " + rfc
+ }
+ t = t.Local()
+ if time.Until(t) < 24*time.Hour {
+ return t.Format("at 15:04 on 2 January")
+ }
+ return t.Format("on 2 January")
+}
+
+func newInvitesCmd(app *App) *cobra.Command {
+ c := &cobra.Command{Use: "invites", Short: "Manage invite links for new accounts", Args: noArgs}
+
+ var uses int
+ var expires, label string
+ create := &cobra.Command{Use: "create", Short: "Create an invite link", Args: noArgs}
+ create.Flags().IntVar(&uses, "uses", 1, "how many people can use the link, or 0 for no limit")
+ create.Flags().StringVar(&expires, "expires", "7d", "how long the link works, like 12h, 7d or never")
+ create.Flags().StringVar(&label, "label", "", "a note to help you recognise the link")
+ create.RunE = func(c *cobra.Command, _ []string) error {
+ if uses < 0 {
+ return ui.Usage("Pass --uses as 0 or more.")
+ }
+ ttl, err := parseExpiry(expires)
+ if err != nil {
+ return err
+ }
+ return adminWrite(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {
+ cfg, err := api.Config(ctx)
+ if err != nil {
+ return err
+ }
+ if !cfg.Registration.AdminRegistrationUrlsEnabled {
+ return ui.Refused("Invite links are turned off on your instance.", "Run rexctl registration set invite to turn them on.")
+ }
+ inv, err := api.CreateInvite(ctx, adminapi.InviteOptions{Uses: uses, Expires: ttl, Label: label})
+ if err != nil {
+ return err
+ }
+ who := "any number of people"
+ if uses > 0 {
+ who = plural(uses, "person", "people")
+ }
+ if inv.ExpiresAt == nil {
+ app.UI.OK("Created an invite for %s. It never expires.", who)
+ } else {
+ app.UI.OK("Created an invite for %s. It expires %s.", who, expiryWhen(*inv.ExpiresAt))
+ }
+ printLink(app, inv.URL)
+ return nil
+ })
+ }
+
+ list := &cobra.Command{Use: "list", Short: "List invite links", Args: noArgs}
+ app.jsonFlag(list)
+ list.RunE = func(c *cobra.Command, _ []string) error {
+ return adminRead(c, app, func(ctx context.Context, api *adminapi.Client, inst *instance.Instance) error {
+ invites, err := api.ListInvites(ctx, inst.Origin())
+ if err != nil {
+ return err
+ }
+ if app.Flags.JSON {
+ return app.UI.JSON(invites)
+ }
+ if len(invites) == 0 {
+ app.UI.Info("There are no working invite links. Run rexctl invites create to make one.")
+ return nil
+ }
+ rows := make([][]string, 0, len(invites))
+ for _, inv := range invites {
+ used := strconv.Itoa(inv.Uses)
+ if inv.MaxUses != nil {
+ used = fmt.Sprintf("%d of %d", inv.Uses, *inv.MaxUses)
+ }
+ rows = append(rows, []string{inv.ID, inv.Label, used, expiryText(inv.ExpiresAt), inv.URL})
+ }
+ app.UI.Table([]string{"ID", "Label", "Used", "Expires", "Link"}, rows)
+ return nil
+ })
+ }
+
+ revoke := &cobra.Command{Use: "revoke <id>", Short: "Stop an invite link from working", Args: exactArgs("the invite ID")}
+ revoke.RunE = func(c *cobra.Command, args []string) error {
+ return adminWrite(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {
+ if err := api.RevokeInvite(ctx, args[0]); err != nil {
+ return err
+ }
+ app.UI.OK("The invite link %s doesn't work anymore.", args[0])
+ return nil
+ })
+ }
+
+ c.AddCommand(create, list, revoke)
+ return c
+}
diff --git a/fluxer_rexctl/cmd/registration.go b/fluxer_rexctl/cmd/registration.go
new file mode 100644
index 000000000..7b68129c6
--- /dev/null
+++ b/fluxer_rexctl/cmd/registration.go
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package cmd
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/spf13/cobra"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/adminapi"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
+)
+
+var registrationDone = map[string]string{
+ "open": "Anyone can create an account.",
+ "approval": "Anyone can ask for an account, and an admin approves each one.",
+ "invite": "Only people with an invite link can create an account.",
+ "closed": "Nobody can create an account, and invite links stop working.",
+}
+
+func newRegistrationCmd(app *App) *cobra.Command {
+ c := &cobra.Command{Use: "registration", Short: "Choose who can create an account", Args: noArgs}
+ set := &cobra.Command{
+ Use: "set <open|approval|invite|closed>",
+ Short: "Set who can create an account",
+ Args: exactArgs("open, approval, invite or closed"),
+ ValidArgs: adminapi.RegistrationModes,
+ }
+ set.RunE = func(c *cobra.Command, args []string) error {
+ patch, err := adminapi.RegistrationPatch(args[0])
+ if err != nil {
+ return ui.Usage(fmt.Sprintf("Pass %s.", adminapi.JoinOr(adminapi.RegistrationModes)))
+ }
+ return adminWrite(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {
+ if err := api.PatchConfig(ctx, patch); err != nil {
+ return err
+ }
+ app.UI.OK("%s", registrationDone[args[0]])
+ return nil
+ })
+ }
+
+ pending := &cobra.Command{Use: "pending", Short: "List people waiting for approval", Args: noArgs}
+ app.jsonFlag(pending)
+ pending.RunE = func(c *cobra.Command, _ []string) error {
+ return adminRead(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {
+ list, err := api.PendingRegistrations(ctx)
+ if err != nil {
+ return err
+ }
+ if app.Flags.JSON {
+ return app.UI.JSON(list)
+ }
+ if len(list) == 0 {
+ app.UI.Info("Nobody is waiting for approval.")
+ return nil
+ }
+ rows := make([][]string, 0, len(list))
+ for _, p := range list {
+ rows = append(rows, []string{p.ID, p.Username, p.Email, shortDate(p.RequestedAt)})
+ }
+ app.UI.Table([]string{"ID", "Username", "Email", "Asked on"}, rows)
+ return nil
+ })
+ }
+
+ review := func(approve bool) func(*cobra.Command, []string) error {
+ return func(c *cobra.Command, args []string) error {
+ return adminWrite(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {
+ p, err := api.ReviewRegistration(ctx, args[0], approve)
+ if err != nil {
+ return err
+ }
+ if approve {
+ app.UI.OK("Approved %s.", p.Username)
+ } else {
+ app.UI.OK("Turned down %s.", p.Username)
+ }
+ return nil
+ })
+ }
+ }
+ approve := &cobra.Command{Use: "approve <id>", Short: "Approve a person waiting for approval", Args: exactArgs("the request ID"), RunE: review(true)}
+ deny := &cobra.Command{Use: "deny <id>", Short: "Turn down a person waiting for approval", Args: exactArgs("the request ID"), RunE: review(false)}
+ c.AddCommand(set, pending, approve, deny)
+ return c
+}