cmd/domainblocks.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.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
185
564d92b54b00 Update copyrights
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
     1
// Copyright © 2017-2018 Mikael Berthe <mikael@lilotux.net>
151
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
//
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
// Licensed under the MIT license.
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
// Please see the LICENSE file is this directory.
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
package cmd
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
import (
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
	"os"
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	"github.com/pkg/errors"
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
	"github.com/spf13/cobra"
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
239
605a00e9d1ab Switch to Go modules (and bump Go version requirement)
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    14
	"github.com/McKael/madon/v2"
151
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
)
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
var domainBlocksOpts struct {
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
	show, block, unblock bool
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    20
	limit          uint              // Limit the results
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    21
	sinceID, maxID madon.ActivityID // Query boundaries
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    22
	all            bool              // Try to fetch all results
151
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
// timelinesCmd represents the timelines command
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
var domainBlocksCmd = &cobra.Command{
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	Use:     "domain-blocks --show|--block|--unblock [DOMAINNAME]",
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	Aliases: []string{"domain-block"},
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	Short:   "Display, add or remove user-blocked domains",
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	RunE:    domainBlocksRunE,
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
	Example: `  madonctl domain-blocks --show
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
  madonctl domain-blocks --block   example.com
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
  madonctl domain-blocks --unblock example.com`,
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
func init() {
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
	RootCmd.AddCommand(domainBlocksCmd)
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	domainBlocksCmd.Flags().BoolVar(&domainBlocksOpts.show, "show", false, "List current user-blocked domains")
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
	domainBlocksCmd.Flags().BoolVar(&domainBlocksOpts.block, "block", false, "Block domain")
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
	domainBlocksCmd.Flags().BoolVar(&domainBlocksOpts.unblock, "unblock", false, "Unblock domain")
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
	domainBlocksCmd.Flags().UintVarP(&domainBlocksOpts.limit, "limit", "l", 0, "Limit number of results")
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    44
	domainBlocksCmd.Flags().StringVar(&domainBlocksOpts.sinceID, "since-id", "", "Request IDs greater than a value")
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    45
	domainBlocksCmd.Flags().StringVar(&domainBlocksOpts.maxID, "max-id", "", "Request IDs less (or equal) than a value")
151
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	domainBlocksCmd.Flags().BoolVar(&domainBlocksOpts.all, "all", false, "Fetch all results")
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
func domainBlocksRunE(cmd *cobra.Command, args []string) error {
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
	opt := domainBlocksOpts
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
	var domName madon.DomainName
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
	// Check flags
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
	if opt.block && opt.unblock {
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
		return errors.New("cannot use both --block and --unblock")
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
	}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
	if opt.block || opt.unblock {
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
		if opt.show {
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
			return errors.New("cannot use both --[un]block and --show")
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
		}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
		if len(args) != 1 {
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
			return errors.New("missing domain name")
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
		}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
		domName = madon.DomainName(args[0])
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
	}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
	if !opt.show && !opt.block && !opt.unblock {
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
		return errors.New("missing flag: please provide --show, --block or --unblock")
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
	}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
	// Set up LimitParams
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
	var limOpts *madon.LimitParams
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    74
	if opt.all || opt.limit > 0 || opt.sinceID != "" || opt.maxID != "" {
151
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
		limOpts = new(madon.LimitParams)
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
		limOpts.All = opt.all
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
	}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
	if opt.limit > 0 {
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
		limOpts.Limit = int(opt.limit)
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
	}
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    81
	if opt.maxID != "" {
151
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
		limOpts.MaxID = opt.maxID
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
	}
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    84
	if opt.sinceID != "" {
151
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
		limOpts.SinceID = opt.sinceID
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
	}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
	// Log in
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
	if err := madonInit(true); err != nil {
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
		return err
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
	}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
	var obj interface{}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
	var err error
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
	switch {
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
	case opt.show:
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
		var domainList []madon.DomainName
155
0fd940f16084 Sync with Madon library update
Mikael Berthe <mikael@lilotux.net>
parents: 151
diff changeset
    99
		domainList, err = gClient.GetBlockedDomains(limOpts)
151
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   100
		obj = domainList
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
	case opt.block:
155
0fd940f16084 Sync with Madon library update
Mikael Berthe <mikael@lilotux.net>
parents: 151
diff changeset
   102
		err = gClient.BlockDomain(domName)
151
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   103
	case opt.unblock:
155
0fd940f16084 Sync with Madon library update
Mikael Berthe <mikael@lilotux.net>
parents: 151
diff changeset
   104
		err = gClient.UnblockDomain(domName)
151
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   105
	default:
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   106
		return errors.New("domainBlocksCmd: internal error")
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
	}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
	if err != nil {
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   110
		errPrint("Error: %s", err.Error())
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   111
		os.Exit(1)
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   112
	}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
	if obj == nil {
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
		return nil
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   115
	}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   116
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   117
	p, err := getPrinter()
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   118
	if err != nil {
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   119
		errPrint("Error: %v", err)
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   120
		os.Exit(1)
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   121
	}
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   122
	return p.printObj(obj)
466aabf6c90a Add domain-blocks command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   123
}