cmd/stream.go
changeset 166 79c0f8db66ff
parent 144 15ecc49bbb25
child 174 0b30e670aa82
equal deleted inserted replaced
165:e76422bfe6ee 166:79c0f8db66ff
    17 )
    17 )
    18 
    18 
    19 var streamOpts struct {
    19 var streamOpts struct {
    20 	command           string
    20 	command           string
    21 	notificationsOnly bool
    21 	notificationsOnly bool
       
    22 	notificationTypes string
    22 }
    23 }
    23 
    24 
    24 // Maximum number of websockets (1 hashtag <=> 1 ws)
    25 // Maximum number of websockets (1 hashtag <=> 1 ws)
    25 const maximumHashtagStreamWS = 4
    26 const maximumHashtagStreamWS = 4
    26 
    27 
    36 	Example: `  madonctl stream           # User timeline stream
    37 	Example: `  madonctl stream           # User timeline stream
    37   madonctl stream local     # Local timeline stream
    38   madonctl stream local     # Local timeline stream
    38   madonctl stream public    # Public timeline stream
    39   madonctl stream public    # Public timeline stream
    39   madonctl stream :mastodon # Hashtag
    40   madonctl stream :mastodon # Hashtag
    40   madonctl stream #madonctl
    41   madonctl stream #madonctl
       
    42   madonctl stream --notifications-only
       
    43   madonctl stream --notifications-only --notification-types mentions,follows
    41 
    44 
    42 Several (up to 4) hashtags can be given.
    45 Several (up to 4) hashtags can be given.
    43 Note: madonctl will use 1 websocket per hashtag stream.
    46 Note: madonctl will use 1 websocket per hashtag stream.
    44   madonctl stream #madonctl,#mastodon,#golang
    47   madonctl stream #madonctl,#mastodon,#golang
    45   madonctl stream :madonctl,mastodon,api`,
    48   madonctl stream :madonctl,mastodon,api`,
    51 func init() {
    54 func init() {
    52 	RootCmd.AddCommand(streamCmd)
    55 	RootCmd.AddCommand(streamCmd)
    53 
    56 
    54 	streamCmd.Flags().StringVar(&streamOpts.command, "command", "", "Execute external command")
    57 	streamCmd.Flags().StringVar(&streamOpts.command, "command", "", "Execute external command")
    55 	streamCmd.Flags().BoolVar(&streamOpts.notificationsOnly, "notifications-only", false, "Display only notifications (user stream)")
    58 	streamCmd.Flags().BoolVar(&streamOpts.notificationsOnly, "notifications-only", false, "Display only notifications (user stream)")
       
    59 	streamCmd.Flags().StringVar(&streamOpts.notificationTypes, "notification-types", "", "Filter notifications (mentions, favourites, reblogs, follows)")
    56 }
    60 }
    57 
    61 
    58 func streamRunE(cmd *cobra.Command, args []string) error {
    62 func streamRunE(cmd *cobra.Command, args []string) error {
    59 	streamName := "user"
    63 	streamName := "user"
    60 	tag := ""
    64 	tag := ""
    95 		}
    99 		}
    96 	}
   100 	}
    97 
   101 
    98 	if err := madonInit(true); err != nil {
   102 	if err := madonInit(true); err != nil {
    99 		return err
   103 		return err
       
   104 	}
       
   105 
       
   106 	var filterMap *map[string]bool
       
   107 	if streamOpts.notificationTypes != "" {
       
   108 		var err error
       
   109 		filterMap, err = buildFilterMap(streamOpts.notificationTypes)
       
   110 		if err != nil {
       
   111 			return err
       
   112 		}
   100 	}
   113 	}
   101 
   114 
   102 	evChan := make(chan madon.StreamEvent, 10)
   115 	evChan := make(chan madon.StreamEvent, 10)
   103 	stop := make(chan bool)
   116 	stop := make(chan bool)
   104 	done := make(chan bool)
   117 	done := make(chan bool)
   186 					break LISTEN
   199 					break LISTEN
   187 				}
   200 				}
   188 				continue
   201 				continue
   189 			case "notification":
   202 			case "notification":
   190 				n := ev.Data.(madon.Notification)
   203 				n := ev.Data.(madon.Notification)
       
   204 				if filterMap != nil && !(*filterMap)[n.Type] {
       
   205 					continue
       
   206 				}
   191 				if p.printObj(&n); err != nil {
   207 				if p.printObj(&n); err != nil {
   192 					break LISTEN
   208 					break LISTEN
   193 				}
   209 				}
   194 				continue
   210 				continue
   195 			case "delete":
   211 			case "delete":