cmd/notifications.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: 45
diff changeset
     9
	"os"
165
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    10
	"strings"
47
82d8b6074309 Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents: 45
diff changeset
    11
45
b58a7ea1aeb2 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 44
diff changeset
    12
	"github.com/pkg/errors"
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
	"github.com/spf13/cobra"
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    14
239
605a00e9d1ab Switch to Go modules (and bump Go version requirement)
Mikael Berthe <mikael@lilotux.net>
parents: 197
diff changeset
    15
	"github.com/McKael/madon/v2"
0
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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
var notificationsOpts struct {
19
4c81f4393773 Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    19
	list, clear, dismiss bool
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    20
	notifID              madon.ActivityID
165
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    21
	types                string
197
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    22
	excludeTypes         string
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
// notificationsCmd represents the notifications subcommand
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
var notificationsCmd = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	Use:     "notifications", // XXX
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	Aliases: []string{"notification", "notif"},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	Short:   "Manage notifications",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	Example: `  madonctl accounts notifications --list
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
  madonctl accounts notifications --list --clear
19
4c81f4393773 Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    32
  madonctl accounts notifications --dismiss --notification-id N
165
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    33
  madonctl accounts notifications --notification-id N
197
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    34
  madonctl accounts notifications --list --exclude-types mention,reblog
165
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    35
  madonctl accounts notifications --list --notification-types mentions
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    36
  madonctl accounts notifications --list --notification-types favourites
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    37
  madonctl accounts notifications --list --notification-types follows,reblogs`,
197
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    38
	Long: `Manage notifications
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    39
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    40
This commands let you list, display and dismiss notifications.
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    41
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    42
Please note that --notifications-types filters the notifications locally,
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    43
while --exclude-types is supported by the API and should be more efficient.`,
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	RunE: notificationRunE,
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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
func init() {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
	accountsCmd.AddCommand(notificationsCmd)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
	notificationsCmd.Flags().BoolVar(&notificationsOpts.list, "list", false, "List all current notifications")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
	notificationsCmd.Flags().BoolVar(&notificationsOpts.clear, "clear", false, "Clear all current notifications")
19
4c81f4393773 Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    52
	notificationsCmd.Flags().BoolVar(&notificationsOpts.dismiss, "dismiss", false, "Delete a notification")
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    53
	notificationsCmd.Flags().StringVar(&notificationsOpts.notifID, "notification-id", "", "Get a notification")
197
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    54
	notificationsCmd.Flags().StringVar(&notificationsOpts.types, "notification-types", "", "Filter notifications (mention, favourite, reblog, follow)")
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    55
	notificationsCmd.Flags().StringVar(&notificationsOpts.excludeTypes, "exclude-types", "", "Exclude notifications types (mention, favourite, reblog, follow)")
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
func notificationRunE(cmd *cobra.Command, args []string) error {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
	opt := notificationsOpts
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    61
	if !opt.list && !opt.clear && opt.notifID == "" {
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
		return errors.New("missing parameters")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
	if err := madonInit(true); err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
		return err
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    69
	var limOpts *madon.LimitParams
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    70
	if accountsOpts.all || accountsOpts.limit > 0 || accountsOpts.sinceID != "" || accountsOpts.maxID != "" {
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 19
diff changeset
    71
		limOpts = new(madon.LimitParams)
30
14561d44211b Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
    72
		limOpts.All = accountsOpts.all
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    73
	}
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    74
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    75
	if accountsOpts.limit > 0 {
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 19
diff changeset
    76
		limOpts.Limit = int(accountsOpts.limit)
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 19
diff changeset
    77
	}
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    78
	if accountsOpts.maxID != "" {
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    79
		limOpts.MaxID = accountsOpts.maxID
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    80
	}
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    81
	if accountsOpts.sinceID != "" {
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    82
		limOpts.SinceID = accountsOpts.sinceID
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    83
	}
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 19
diff changeset
    84
166
79c0f8db66ff Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents: 165
diff changeset
    85
	var filterMap *map[string]bool
165
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    86
	if opt.types != "" {
166
79c0f8db66ff Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents: 165
diff changeset
    87
		var err error
79c0f8db66ff Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents: 165
diff changeset
    88
		filterMap, err = buildFilterMap(opt.types)
79c0f8db66ff Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents: 165
diff changeset
    89
		if err != nil {
197
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    90
			return errors.Wrap(err, "bad notification filter")
165
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    91
		}
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    92
	}
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    93
197
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    94
	var xTypes []string
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    95
	if xt, err := splitNotificationTypes(opt.excludeTypes); err == nil {
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    96
		xTypes = xt
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    97
	} else {
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    98
		return errors.Wrap(err, "invalid exclude-types argument")
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    99
	}
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   100
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
	var obj interface{}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
	var err error
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   103
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
	if opt.list {
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   105
		var notifications []madon.Notification
197
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   106
		notifications, err = gClient.GetNotifications(xTypes, limOpts)
165
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   107
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   108
		// Filter notifications
166
79c0f8db66ff Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents: 165
diff changeset
   109
		if filterMap != nil && len(*filterMap) > 0 {
165
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   110
			if verbose {
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   111
				errPrint("Filtering notifications")
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   112
			}
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   113
			var newNotifications []madon.Notification
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   114
			for _, notif := range notifications {
166
79c0f8db66ff Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents: 165
diff changeset
   115
				if (*filterMap)[notif.Type] {
165
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   116
					newNotifications = append(newNotifications, notif)
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   117
				}
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   118
			}
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   119
			notifications = newNotifications
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   120
		}
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   121
167
1341bacd01c9 Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents: 166
diff changeset
   122
		if accountsOpts.keep > 0 && len(notifications) > int(accountsOpts.keep) {
1341bacd01c9 Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents: 166
diff changeset
   123
			notifications = notifications[:accountsOpts.keep]
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   124
		}
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   125
		obj = notifications
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
   126
	} else if opt.notifID != "" {
19
4c81f4393773 Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   127
		if opt.dismiss {
4c81f4393773 Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   128
			err = gClient.DismissNotification(opt.notifID)
4c81f4393773 Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   129
		} else {
4c81f4393773 Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   130
			obj, err = gClient.GetNotification(opt.notifID)
4c81f4393773 Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   131
		}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   132
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   133
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   134
	if err == nil && opt.clear {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   135
		err = gClient.ClearNotifications()
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   136
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   137
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   138
	if err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   139
		errPrint("Error: %s", err.Error())
47
82d8b6074309 Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents: 45
diff changeset
   140
		os.Exit(1)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   141
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   142
	if obj == nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   143
		return nil
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   144
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   145
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   146
	p, err := getPrinter()
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   147
	if err != nil {
81
b1671f83e91b Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents: 47
diff changeset
   148
		errPrint("Error: %s", err.Error())
b1671f83e91b Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents: 47
diff changeset
   149
		os.Exit(1)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   150
	}
110
57843255fd1a Refactor printers
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   151
	return p.printObj(obj)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   152
}
166
79c0f8db66ff Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents: 165
diff changeset
   153
197
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   154
func splitNotificationTypes(types string) ([]string, error) {
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   155
	var typeList []string
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   156
	if types == "" {
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   157
		return typeList, nil
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   158
	}
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   159
	for _, f := range strings.Split(types, ",") {
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   160
		switch f {
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   161
		case "mention", "mentions":
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   162
			f = "mention"
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   163
		case "favourite", "favourites", "favorite", "favorites", "fave", "faves":
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   164
			f = "favourite"
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   165
		case "reblog", "reblogs", "retoot", "retoots":
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   166
			f = "reblog"
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   167
		case "follow", "follows":
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   168
			f = "follow"
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   169
		default:
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   170
			return nil, errors.Errorf("unknown notification type: '%s'", f)
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   171
		}
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   172
		typeList = append(typeList, f)
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   173
	}
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   174
	return typeList, nil
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   175
}
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   176
166
79c0f8db66ff Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents: 165
diff changeset
   177
func buildFilterMap(types string) (*map[string]bool, error) {
79c0f8db66ff Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents: 165
diff changeset
   178
	filterMap := make(map[string]bool)
197
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   179
	if types == "" {
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   180
		return &filterMap, nil
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   181
	}
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   182
	for _, f := range strings.Split(types, ",") {
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   183
		switch f {
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   184
		case "mention", "mentions":
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   185
			filterMap["mention"] = true
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   186
		case "favourite", "favourites", "favorite", "favorites", "fave", "faves":
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   187
			filterMap["favourite"] = true
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   188
		case "reblog", "reblogs", "retoot", "retoots":
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   189
			filterMap["reblog"] = true
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   190
		case "follow", "follows":
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   191
			filterMap["follow"] = true
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   192
		default:
0b0fd2f02296 Add '--exclude-types' to accounts notifications --list
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   193
			return nil, errors.Errorf("unknown notification type: '%s'", f)
166
79c0f8db66ff Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents: 165
diff changeset
   194
		}
79c0f8db66ff Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents: 165
diff changeset
   195
	}
79c0f8db66ff Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents: 165
diff changeset
   196
	return &filterMap, nil
79c0f8db66ff Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents: 165
diff changeset
   197
}