Add --notification-types to the stream command as well
authorMikael Berthe <mikael@lilotux.net>
Sat, 15 Jul 2017 21:58:24 +0200
changeset 166 79c0f8db66ff
parent 165 e76422bfe6ee
child 167 1341bacd01c9
Add --notification-types to the stream command as well
cmd/notifications.go
cmd/stream.go
--- a/cmd/notifications.go	Sat Jul 15 12:07:37 2017 +0200
+++ b/cmd/notifications.go	Sat Jul 15 21:58:24 2017 +0200
@@ -74,21 +74,12 @@
 		limOpts.SinceID = int64(accountsOpts.sinceID)
 	}
 
-	filterMap := make(map[string]bool)
+	var filterMap *map[string]bool
 	if opt.types != "" {
-		for _, f := range strings.Split(opt.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 errors.Errorf("unknown notification type: '%s'", f)
-			}
+		var err error
+		filterMap, err = buildFilterMap(opt.types)
+		if err != nil {
+			return nil
 		}
 	}
 
@@ -100,13 +91,13 @@
 		notifications, err = gClient.GetNotifications(limOpts)
 
 		// Filter notifications
-		if len(filterMap) > 0 {
+		if filterMap != nil && len(*filterMap) > 0 {
 			if verbose {
 				errPrint("Filtering notifications")
 			}
 			var newNotifications []madon.Notification
 			for _, notif := range notifications {
-				if filterMap[notif.Type] {
+				if (*filterMap)[notif.Type] {
 					newNotifications = append(newNotifications, notif)
 				}
 			}
@@ -144,3 +135,24 @@
 	}
 	return p.printObj(obj)
 }
+
+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)
+			}
+		}
+	}
+	return &filterMap, nil
+}
--- a/cmd/stream.go	Sat Jul 15 12:07:37 2017 +0200
+++ b/cmd/stream.go	Sat Jul 15 21:58:24 2017 +0200
@@ -19,6 +19,7 @@
 var streamOpts struct {
 	command           string
 	notificationsOnly bool
+	notificationTypes string
 }
 
 // Maximum number of websockets (1 hashtag <=> 1 ws)
@@ -38,6 +39,8 @@
   madonctl stream public    # Public timeline stream
   madonctl stream :mastodon # Hashtag
   madonctl stream #madonctl
+  madonctl stream --notifications-only
+  madonctl stream --notifications-only --notification-types mentions,follows
 
 Several (up to 4) hashtags can be given.
 Note: madonctl will use 1 websocket per hashtag stream.
@@ -53,6 +56,7 @@
 
 	streamCmd.Flags().StringVar(&streamOpts.command, "command", "", "Execute external command")
 	streamCmd.Flags().BoolVar(&streamOpts.notificationsOnly, "notifications-only", false, "Display only notifications (user stream)")
+	streamCmd.Flags().StringVar(&streamOpts.notificationTypes, "notification-types", "", "Filter notifications (mentions, favourites, reblogs, follows)")
 }
 
 func streamRunE(cmd *cobra.Command, args []string) error {
@@ -99,6 +103,15 @@
 		return err
 	}
 
+	var filterMap *map[string]bool
+	if streamOpts.notificationTypes != "" {
+		var err error
+		filterMap, err = buildFilterMap(streamOpts.notificationTypes)
+		if err != nil {
+			return err
+		}
+	}
+
 	evChan := make(chan madon.StreamEvent, 10)
 	stop := make(chan bool)
 	done := make(chan bool)
@@ -188,6 +201,9 @@
 				continue
 			case "notification":
 				n := ev.Data.(madon.Notification)
+				if filterMap != nil && !(*filterMap)[n.Type] {
+					continue
+				}
 				if p.printObj(&n); err != nil {
 					break LISTEN
 				}