cmd/suggestions.go
author rjp <zimpenfish@gmail.com>
Mon, 23 Jan 2023 16:39:02 +0000
changeset 267 5b91a65ba95a
parent 239 605a00e9d1ab
child 268 4dd196a4ee7c
permissions -rw-r--r--
Update to handle non-int64 IDs Pleroma/Akkoma and GotoSocial use opaque IDs rather than `int64`s like Mastodon which means that `madon` can't talk to either of those. This commit updates everything that can be an ID to `madon.ActivityID` which is an alias for `string` - can't create a specific type for it since there's more than a few places where they're concatenated directly to strings for URLs, etc. Which means it could just as easily be a direct `string` type itself but I find that having distinct types can often make the code more readable and understandable. One extra bit is that `statusOpts` has grown a `_hasReplyTo` boolean to indicate whether the `--in-reply-to` flag was given or not because we can't distinguish because "empty because default" or "empty because given and empty". Another way around this would be to set the default to some theoretically impossible or unlikely string but you never know when someone might spin up an instance where, e.g., admin posts have negative integer IDs.

// Copyright © 2018 Mikael Berthe <mikael@lilotux.net>
//
// Licensed under the MIT license.
// Please see the LICENSE file is this directory.

package cmd

import (
	"os"

	"github.com/pkg/errors"
	"github.com/spf13/cobra"

	"github.com/McKael/madon/v2"
)

var suggestionsOpts struct {
	accountID  madon.ActivityID
	accountIDs string

	//limit uint
	keep uint
	//all bool
}

//suggestionsCmd represents the suggestions command
var suggestionsCmd = &cobra.Command{
	Use:     "suggestions",
	Aliases: []string{"suggestion"},
	Short:   "Display and remove the follow suggestions",
	RunE:    suggestionsGetRunE, // Defaults to list
}

func init() {
	RootCmd.AddCommand(suggestionsCmd)

	// Subcommands
	suggestionsCmd.AddCommand(suggestionsSubcommands...)

	//suggestionsGetSubcommand.Flags().UintVarP(&suggestionsOpts.limit, "limit", "l", 0, "Limit number of API results")
	suggestionsGetSubcommand.Flags().UintVarP(&suggestionsOpts.keep, "keep", "k", 0, "Limit number of results")
	//suggestionsGetSubcommand.Flags().BoolVar(&suggestionsOpts.all, "all", false, "Fetch all results")

	suggestionsDeleteSubcommand.Flags().StringVarP(&suggestionsOpts.accountID, "account-id", "a", "", "Account ID number")
	suggestionsDeleteSubcommand.Flags().StringVar(&suggestionsOpts.accountIDs, "account-ids", "", "Comma-separated list of account IDs")
}

var suggestionsSubcommands = []*cobra.Command{
	suggestionsGetSubcommand,
	suggestionsDeleteSubcommand,
}

var suggestionsGetSubcommand = &cobra.Command{
	Use:     "list",
	Short:   "Display the suggestions (default subcommand)",
	Long:    `Display the list of account suggestions.`,
	Aliases: []string{"ls", "get", "display", "show"},
	RunE:    suggestionsGetRunE,
}

var suggestionsDeleteSubcommand = &cobra.Command{
	Use:     "delete",
	Short:   "Remove an account from the suggestion list",
	Aliases: []string{"remove", "del", "rm"},
	RunE:    suggestionsDeleteRunE,
}

func suggestionsGetRunE(cmd *cobra.Command, args []string) error {
	opt := suggestionsOpts

	/*
		// Note: The API currently does not support pagination
		// Set up LimitParams
		var limOpts *madon.LimitParams
		if opt.all || opt.limit > 0 {
			limOpts = new(madon.LimitParams)
			limOpts.All = opt.all
		}
		if opt.limit > 0 {
			limOpts.Limit = int(opt.limit)
		}
	*/

	// We need to be logged in
	if err := madonInit(true); err != nil {
		return err
	}

	var obj interface{}
	var err error

	var accountList []madon.Account
	accountList, err = gClient.GetSuggestions(nil)

	if opt.keep > 0 && len(accountList) > int(opt.keep) {
		accountList = accountList[:opt.keep]
	}

	obj = accountList

	if err != nil {
		errPrint("Error: %s", err.Error())
		os.Exit(1)
	}
	if obj == nil {
		return nil
	}

	p, err := getPrinter()
	if err != nil {
		errPrint("Error: %v", err)
		os.Exit(1)
	}
	return p.printObj(obj)
}

func suggestionsDeleteRunE(cmd *cobra.Command, args []string) error {
	opt := suggestionsOpts
	var ids []madon.ActivityID
	var err error

	if opt.accountID == "" && len(opt.accountIDs) == 0 {
		return errors.New("missing account IDs")
	}
	if opt.accountID != "" && len(opt.accountIDs) > 0 {
		return errors.New("incompatible options")
	}

	ids, err = splitIDs(opt.accountIDs)
	if err != nil {
		return errors.New("cannot parse account IDs")
	}
	if opt.accountID != "" { // Allow --account-id
		ids = []madon.ActivityID{opt.accountID}
	}
	if len(ids) < 1 {
		return errors.New("missing account IDs")
	}

	// We need to be logged in
	if err := madonInit(true); err != nil {
		return err
	}

	for _, id := range ids {
		if e := gClient.DeleteSuggestion(id); err != nil {
			errPrint("Cannot remove account %d: %s", id, e)
			err = e
		}
	}

	if err != nil {
		os.Exit(1)
	}
	return nil
}