Add '--exclude-types' to accounts notifications --list
authorMikael Berthe <mikael@lilotux.net>
Tue, 20 Mar 2018 16:47:06 +0100
changeset 197 0b0fd2f02296
parent 196 05861d22b71e
child 198 e9c2947a84a6
Add '--exclude-types' to accounts notifications --list
cmd/notifications.go
--- a/cmd/notifications.go	Tue Mar 20 16:10:05 2018 +0100
+++ b/cmd/notifications.go	Tue Mar 20 16:47:06 2018 +0100
@@ -19,6 +19,7 @@
 	list, clear, dismiss bool
 	notifID              int64
 	types                string
+	excludeTypes         string
 }
 
 // notificationsCmd represents the notifications subcommand
@@ -30,10 +31,16 @@
   madonctl accounts notifications --list --clear
   madonctl accounts notifications --dismiss --notification-id N
   madonctl accounts notifications --notification-id N
+  madonctl accounts notifications --list --exclude-types mention,reblog
   madonctl accounts notifications --list --notification-types mentions
   madonctl accounts notifications --list --notification-types favourites
   madonctl accounts notifications --list --notification-types follows,reblogs`,
-	//Long:    `TBW...`,
+	Long: `Manage notifications
+
+This commands let you list, display and dismiss notifications.
+
+Please note that --notifications-types filters the notifications locally,
+while --exclude-types is supported by the API and should be more efficient.`,
 	RunE: notificationRunE,
 }
 
@@ -44,7 +51,8 @@
 	notificationsCmd.Flags().BoolVar(&notificationsOpts.clear, "clear", false, "Clear all current notifications")
 	notificationsCmd.Flags().BoolVar(&notificationsOpts.dismiss, "dismiss", false, "Delete a notification")
 	notificationsCmd.Flags().Int64Var(&notificationsOpts.notifID, "notification-id", 0, "Get a notification")
-	notificationsCmd.Flags().StringVar(&notificationsOpts.types, "notification-types", "", "Filter notifications (mentions, favourites, reblogs, follows)")
+	notificationsCmd.Flags().StringVar(&notificationsOpts.types, "notification-types", "", "Filter notifications (mention, favourite, reblog, follow)")
+	notificationsCmd.Flags().StringVar(&notificationsOpts.excludeTypes, "exclude-types", "", "Exclude notifications types (mention, favourite, reblog, follow)")
 }
 
 func notificationRunE(cmd *cobra.Command, args []string) error {
@@ -79,16 +87,23 @@
 		var err error
 		filterMap, err = buildFilterMap(opt.types)
 		if err != nil {
-			return nil
+			return errors.Wrap(err, "bad notification filter")
 		}
 	}
 
+	var xTypes []string
+	if xt, err := splitNotificationTypes(opt.excludeTypes); err == nil {
+		xTypes = xt
+	} else {
+		return errors.Wrap(err, "invalid exclude-types argument")
+	}
+
 	var obj interface{}
 	var err error
 
 	if opt.list {
 		var notifications []madon.Notification
-		notifications, err = gClient.GetNotifications(limOpts)
+		notifications, err = gClient.GetNotifications(xTypes, limOpts)
 
 		// Filter notifications
 		if filterMap != nil && len(*filterMap) > 0 {
@@ -136,22 +151,46 @@
 	return p.printObj(obj)
 }
 
+func splitNotificationTypes(types string) ([]string, error) {
+	var typeList []string
+	if types == "" {
+		return typeList, nil
+	}
+	for _, f := range strings.Split(types, ",") {
+		switch f {
+		case "mention", "mentions":
+			f = "mention"
+		case "favourite", "favourites", "favorite", "favorites", "fave", "faves":
+			f = "favourite"
+		case "reblog", "reblogs", "retoot", "retoots":
+			f = "reblog"
+		case "follow", "follows":
+			f = "follow"
+		default:
+			return nil, errors.Errorf("unknown notification type: '%s'", f)
+		}
+		typeList = append(typeList, f)
+	}
+	return typeList, nil
+}
+
 func buildFilterMap(types string) (*map[string]bool, error) {
 	filterMap := make(map[string]bool)
-	if types != "" {
-		for _, f := range strings.Split(types, ",") {
-			switch f {
-			case "mention", "mentions":
-				filterMap["mention"] = true
-			case "favourite", "favourites", "favorite", "favorites", "fave", "faves":
-				filterMap["favourite"] = true
-			case "reblog", "reblogs", "retoot", "retoots":
-				filterMap["reblog"] = true
-			case "follow", "follows":
-				filterMap["follow"] = true
-			default:
-				return nil, errors.Errorf("unknown notification type: '%s'", f)
-			}
+	if types == "" {
+		return &filterMap, nil
+	}
+	for _, f := range strings.Split(types, ",") {
+		switch f {
+		case "mention", "mentions":
+			filterMap["mention"] = true
+		case "favourite", "favourites", "favorite", "favorites", "fave", "faves":
+			filterMap["favourite"] = true
+		case "reblog", "reblogs", "retoot", "retoots":
+			filterMap["reblog"] = true
+		case "follow", "follows":
+			filterMap["follow"] = true
+		default:
+			return nil, errors.Errorf("unknown notification type: '%s'", f)
 		}
 	}
 	return &filterMap, nil