cmd/notifications.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 15 Jul 2017 12:07:37 +0200
changeset 165 e76422bfe6ee
parent 110 57843255fd1a
child 166 79c0f8db66ff
permissions -rw-r--r--
Add --notification-types flag to filter notifications Suggested by @wxl. Note that the filter is applied after the limits since limits are currently applied at the API call level.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net>
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
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    15
	"github.com/McKael/madon"
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
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
    20
	notifID              int64
165
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    21
	types                string
0
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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
// notificationsCmd represents the notifications subcommand
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
var notificationsCmd = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	Use:     "notifications", // XXX
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	Aliases: []string{"notification", "notif"},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	Short:   "Manage notifications",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	Example: `  madonctl accounts notifications --list
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
  madonctl accounts notifications --list --clear
19
4c81f4393773 Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    31
  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
    32
  madonctl accounts notifications --notification-id N
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    33
  madonctl accounts notifications --list --notification-types mentions
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    34
  madonctl accounts notifications --list --notification-types favourites
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    35
  madonctl accounts notifications --list --notification-types follows,reblogs`,
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
	//Long:    `TBW...`,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
	RunE: notificationRunE,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
func init() {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
	accountsCmd.AddCommand(notificationsCmd)
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
	notificationsCmd.Flags().BoolVar(&notificationsOpts.list, "list", false, "List all current notifications")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	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
    45
	notificationsCmd.Flags().BoolVar(&notificationsOpts.dismiss, "dismiss", false, "Delete a notification")
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
    46
	notificationsCmd.Flags().Int64Var(&notificationsOpts.notifID, "notification-id", 0, "Get a notification")
165
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    47
	notificationsCmd.Flags().StringVar(&notificationsOpts.types, "notification-types", "", "Filter notifications (mentions, favourites, reblogs, follows)")
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
}
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
func notificationRunE(cmd *cobra.Command, args []string) error {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
	opt := notificationsOpts
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
	if !opt.list && !opt.clear && opt.notifID < 1 {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
		return errors.New("missing parameters")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
	}
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
	if err := madonInit(true); err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
		return err
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    61
	var limOpts *madon.LimitParams
30
14561d44211b Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
    62
	if accountsOpts.all || accountsOpts.limit > 0 || accountsOpts.sinceID > 0 || accountsOpts.maxID > 0 {
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 19
diff changeset
    63
		limOpts = new(madon.LimitParams)
30
14561d44211b Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
    64
		limOpts.All = accountsOpts.all
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    65
	}
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    66
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    67
	if accountsOpts.limit > 0 {
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 19
diff changeset
    68
		limOpts.Limit = int(accountsOpts.limit)
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 19
diff changeset
    69
	}
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    70
	if accountsOpts.maxID > 0 {
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
    71
		limOpts.MaxID = int64(accountsOpts.maxID)
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    72
	}
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    73
	if accountsOpts.sinceID > 0 {
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
    74
		limOpts.SinceID = int64(accountsOpts.sinceID)
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    75
	}
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 19
diff changeset
    76
165
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    77
	filterMap := make(map[string]bool)
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    78
	if opt.types != "" {
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    79
		for _, f := range strings.Split(opt.types, ",") {
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    80
			switch f {
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    81
			case "mention", "mentions":
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    82
				filterMap["mention"] = true
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    83
			case "favourite", "favourites", "favorite", "favorites", "fave", "faves":
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    84
				filterMap["favourite"] = true
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    85
			case "reblog", "reblogs", "retoot", "retoots":
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    86
				filterMap["reblog"] = true
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    87
			case "follow", "follows":
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    88
				filterMap["follow"] = true
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    89
			default:
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    90
				return errors.Errorf("unknown notification type: '%s'", f)
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
	}
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    94
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
	var obj interface{}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
	var err error
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
	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
    99
		var notifications []madon.Notification
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 19
diff changeset
   100
		notifications, err = gClient.GetNotifications(limOpts)
165
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   101
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   102
		// Filter notifications
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   103
		if len(filterMap) > 0 {
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   104
			if verbose {
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   105
				errPrint("Filtering notifications")
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   106
			}
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   107
			var newNotifications []madon.Notification
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   108
			for _, notif := range notifications {
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   109
				if filterMap[notif.Type] {
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   110
					newNotifications = append(newNotifications, notif)
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   111
				}
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
			notifications = newNotifications
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   114
		}
e76422bfe6ee Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   115
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 19
diff changeset
   116
		if accountsOpts.limit > 0 && len(notifications) > int(accountsOpts.limit) {
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   117
			notifications = notifications[:accountsOpts.limit]
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   118
		}
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   119
		obj = notifications
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   120
	} else if opt.notifID > 0 {
19
4c81f4393773 Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   121
		if opt.dismiss {
4c81f4393773 Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   122
			err = gClient.DismissNotification(opt.notifID)
4c81f4393773 Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   123
		} else {
4c81f4393773 Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   124
			obj, err = gClient.GetNotification(opt.notifID)
4c81f4393773 Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   125
		}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   126
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   127
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   128
	if err == nil && opt.clear {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   129
		err = gClient.ClearNotifications()
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   130
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   131
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   132
	if err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   133
		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
   134
		os.Exit(1)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   135
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   136
	if obj == nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   137
		return nil
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   138
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   139
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   140
	p, err := getPrinter()
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   141
	if err != nil {
81
b1671f83e91b Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents: 47
diff changeset
   142
		errPrint("Error: %s", err.Error())
b1671f83e91b Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents: 47
diff changeset
   143
		os.Exit(1)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   144
	}
110
57843255fd1a Refactor printers
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   145
	return p.printObj(obj)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   146
}