cmd/timelines.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: 167
diff changeset
     1
// Copyright © 2017-2018 Mikael Berthe <mikael@lilotux.net>
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
//
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
// Licensed under the MIT license.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
// Please see the LICENSE file is this directory.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
package cmd
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
import (
47
82d8b6074309 Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents: 44
diff changeset
     9
	"os"
198
e9c2947a84a6 Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    10
	"strings"
47
82d8b6074309 Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents: 44
diff changeset
    11
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
	"github.com/spf13/cobra"
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    13
239
605a00e9d1ab Switch to Go modules (and bump Go version requirement)
Mikael Berthe <mikael@lilotux.net>
parents: 235
diff changeset
    14
	"github.com/McKael/madon/v2"
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
var timelineOpts struct {
200
488f5fc5be98 Add option --only-media to madonctl timelines (Mastodon 2.3.0+)
Mikael Berthe <mikael@lilotux.net>
parents: 198
diff changeset
    18
	local, onlyMedia bool
488f5fc5be98 Add option --only-media to madonctl timelines (Mastodon 2.3.0+)
Mikael Berthe <mikael@lilotux.net>
parents: 198
diff changeset
    19
	limit, keep      uint
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    20
	sinceID, maxID   madon.ActivityID
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
// timelineCmd represents the timelines command
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
var timelineCmd = &cobra.Command{
235
747f108d5821 timeline: Allow "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents: 200
diff changeset
    25
	Use:     "timeline [home|public|direct|:HASHTAG|!list_id] [--local]",
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	Aliases: []string{"tl"},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	Short:   "Fetch a timeline",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	Long: `
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
The timeline command fetches a timeline (home, local or federated).
235
747f108d5821 timeline: Allow "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents: 200
diff changeset
    30
The timeline "direct" contains only direct messages (that is, messages with
747f108d5821 timeline: Allow "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents: 200
diff changeset
    31
visibility set to "direct").
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
It can also get a hashtag-based timeline if the keyword or prefixed with
198
e9c2947a84a6 Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    33
':' or '#', or a list-based timeline (use !ID with the list ID).`,
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
	Example: `  madonctl timeline
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
  madonctl timeline public --local
198
e9c2947a84a6 Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    36
  madonctl timeline '!42'
235
747f108d5821 timeline: Allow "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents: 200
diff changeset
    37
  madonctl timeline :mastodon
747f108d5821 timeline: Allow "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents: 200
diff changeset
    38
  madonctl timeline direct`,
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	RunE:      timelineRunE,
235
747f108d5821 timeline: Allow "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents: 200
diff changeset
    40
	ValidArgs: []string{"home", "public", "direct"},
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
func init() {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	RootCmd.AddCommand(timelineCmd)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	timelineCmd.Flags().BoolVar(&timelineOpts.local, "local", false, "Posts from the local instance")
200
488f5fc5be98 Add option --only-media to madonctl timelines (Mastodon 2.3.0+)
Mikael Berthe <mikael@lilotux.net>
parents: 198
diff changeset
    47
	timelineCmd.Flags().BoolVar(&timelineOpts.onlyMedia, "only-media", false, "Only statuses with media attachments")
167
1341bacd01c9 Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    48
	timelineCmd.Flags().UintVarP(&timelineOpts.limit, "limit", "l", 0, "Limit number of API results")
1341bacd01c9 Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    49
	timelineCmd.Flags().UintVarP(&timelineOpts.keep, "keep", "k", 0, "Limit number of results")
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    50
	timelineCmd.PersistentFlags().StringVar(&timelineOpts.sinceID, "since-id", "", "Request IDs greater than a value")
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    51
	timelineCmd.PersistentFlags().StringVar(&timelineOpts.maxID, "max-id", "", "Request IDs less (or equal) than a value")
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
func timelineRunE(cmd *cobra.Command, args []string) error {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
	opt := timelineOpts
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    56
	var limOpts *madon.LimitParams
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    57
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    58
	if opt.limit > 0 || opt.sinceID != "" || opt.maxID != "" {
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    59
		limOpts = new(madon.LimitParams)
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    60
	}
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    61
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    62
	if opt.limit > 0 {
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    63
		limOpts.Limit = int(opt.limit)
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    64
	}
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    65
	if opt.maxID != "" {
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
    66
		limOpts.MaxID = opt.maxID
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    67
	}
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    68
	if opt.sinceID != "" {
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
    69
		limOpts.SinceID = opt.sinceID
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    70
	}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
	tl := "home"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
	if len(args) > 0 {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
		tl = args[0]
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
198
e9c2947a84a6 Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    77
	// Home timeline and list-based timeline require to be logged in
235
747f108d5821 timeline: Allow "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents: 200
diff changeset
    78
	if err := madonInit(tl == "home" || tl == "direct" || strings.HasPrefix(tl, "!")); err != nil {
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
		return err
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
200
488f5fc5be98 Add option --only-media to madonctl timelines (Mastodon 2.3.0+)
Mikael Berthe <mikael@lilotux.net>
parents: 198
diff changeset
    82
	sl, err := gClient.GetTimelines(tl, opt.local, opt.onlyMedia, limOpts)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
	if err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
		errPrint("Error: %s", err.Error())
47
82d8b6074309 Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents: 44
diff changeset
    85
		os.Exit(1)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
167
1341bacd01c9 Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    88
	if opt.keep > 0 && len(sl) > int(opt.keep) {
1341bacd01c9 Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    89
		sl = sl[:opt.keep]
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    90
	}
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    91
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
	p, err := getPrinter()
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
	if err != nil {
81
b1671f83e91b Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents: 47
diff changeset
    94
		errPrint("Error: %s", err.Error())
b1671f83e91b Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents: 47
diff changeset
    95
		os.Exit(1)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
	}
110
57843255fd1a Refactor printers
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
    97
	return p.printObj(sl)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
}