cmd/notifications.go
changeset 197 0b0fd2f02296
parent 185 564d92b54b00
child 239 605a00e9d1ab
equal deleted inserted replaced
196:05861d22b71e 197:0b0fd2f02296
    17 
    17 
    18 var notificationsOpts struct {
    18 var notificationsOpts struct {
    19 	list, clear, dismiss bool
    19 	list, clear, dismiss bool
    20 	notifID              int64
    20 	notifID              int64
    21 	types                string
    21 	types                string
       
    22 	excludeTypes         string
    22 }
    23 }
    23 
    24 
    24 // notificationsCmd represents the notifications subcommand
    25 // notificationsCmd represents the notifications subcommand
    25 var notificationsCmd = &cobra.Command{
    26 var notificationsCmd = &cobra.Command{
    26 	Use:     "notifications", // XXX
    27 	Use:     "notifications", // XXX
    28 	Short:   "Manage notifications",
    29 	Short:   "Manage notifications",
    29 	Example: `  madonctl accounts notifications --list
    30 	Example: `  madonctl accounts notifications --list
    30   madonctl accounts notifications --list --clear
    31   madonctl accounts notifications --list --clear
    31   madonctl accounts notifications --dismiss --notification-id N
    32   madonctl accounts notifications --dismiss --notification-id N
    32   madonctl accounts notifications --notification-id N
    33   madonctl accounts notifications --notification-id N
       
    34   madonctl accounts notifications --list --exclude-types mention,reblog
    33   madonctl accounts notifications --list --notification-types mentions
    35   madonctl accounts notifications --list --notification-types mentions
    34   madonctl accounts notifications --list --notification-types favourites
    36   madonctl accounts notifications --list --notification-types favourites
    35   madonctl accounts notifications --list --notification-types follows,reblogs`,
    37   madonctl accounts notifications --list --notification-types follows,reblogs`,
    36 	//Long:    `TBW...`,
    38 	Long: `Manage notifications
       
    39 
       
    40 This commands let you list, display and dismiss notifications.
       
    41 
       
    42 Please note that --notifications-types filters the notifications locally,
       
    43 while --exclude-types is supported by the API and should be more efficient.`,
    37 	RunE: notificationRunE,
    44 	RunE: notificationRunE,
    38 }
    45 }
    39 
    46 
    40 func init() {
    47 func init() {
    41 	accountsCmd.AddCommand(notificationsCmd)
    48 	accountsCmd.AddCommand(notificationsCmd)
    42 
    49 
    43 	notificationsCmd.Flags().BoolVar(&notificationsOpts.list, "list", false, "List all current notifications")
    50 	notificationsCmd.Flags().BoolVar(&notificationsOpts.list, "list", false, "List all current notifications")
    44 	notificationsCmd.Flags().BoolVar(&notificationsOpts.clear, "clear", false, "Clear all current notifications")
    51 	notificationsCmd.Flags().BoolVar(&notificationsOpts.clear, "clear", false, "Clear all current notifications")
    45 	notificationsCmd.Flags().BoolVar(&notificationsOpts.dismiss, "dismiss", false, "Delete a notification")
    52 	notificationsCmd.Flags().BoolVar(&notificationsOpts.dismiss, "dismiss", false, "Delete a notification")
    46 	notificationsCmd.Flags().Int64Var(&notificationsOpts.notifID, "notification-id", 0, "Get a notification")
    53 	notificationsCmd.Flags().Int64Var(&notificationsOpts.notifID, "notification-id", 0, "Get a notification")
    47 	notificationsCmd.Flags().StringVar(&notificationsOpts.types, "notification-types", "", "Filter notifications (mentions, favourites, reblogs, follows)")
    54 	notificationsCmd.Flags().StringVar(&notificationsOpts.types, "notification-types", "", "Filter notifications (mention, favourite, reblog, follow)")
       
    55 	notificationsCmd.Flags().StringVar(&notificationsOpts.excludeTypes, "exclude-types", "", "Exclude notifications types (mention, favourite, reblog, follow)")
    48 }
    56 }
    49 
    57 
    50 func notificationRunE(cmd *cobra.Command, args []string) error {
    58 func notificationRunE(cmd *cobra.Command, args []string) error {
    51 	opt := notificationsOpts
    59 	opt := notificationsOpts
    52 
    60 
    77 	var filterMap *map[string]bool
    85 	var filterMap *map[string]bool
    78 	if opt.types != "" {
    86 	if opt.types != "" {
    79 		var err error
    87 		var err error
    80 		filterMap, err = buildFilterMap(opt.types)
    88 		filterMap, err = buildFilterMap(opt.types)
    81 		if err != nil {
    89 		if err != nil {
    82 			return nil
    90 			return errors.Wrap(err, "bad notification filter")
    83 		}
    91 		}
       
    92 	}
       
    93 
       
    94 	var xTypes []string
       
    95 	if xt, err := splitNotificationTypes(opt.excludeTypes); err == nil {
       
    96 		xTypes = xt
       
    97 	} else {
       
    98 		return errors.Wrap(err, "invalid exclude-types argument")
    84 	}
    99 	}
    85 
   100 
    86 	var obj interface{}
   101 	var obj interface{}
    87 	var err error
   102 	var err error
    88 
   103 
    89 	if opt.list {
   104 	if opt.list {
    90 		var notifications []madon.Notification
   105 		var notifications []madon.Notification
    91 		notifications, err = gClient.GetNotifications(limOpts)
   106 		notifications, err = gClient.GetNotifications(xTypes, limOpts)
    92 
   107 
    93 		// Filter notifications
   108 		// Filter notifications
    94 		if filterMap != nil && len(*filterMap) > 0 {
   109 		if filterMap != nil && len(*filterMap) > 0 {
    95 			if verbose {
   110 			if verbose {
    96 				errPrint("Filtering notifications")
   111 				errPrint("Filtering notifications")
   134 		os.Exit(1)
   149 		os.Exit(1)
   135 	}
   150 	}
   136 	return p.printObj(obj)
   151 	return p.printObj(obj)
   137 }
   152 }
   138 
   153 
       
   154 func splitNotificationTypes(types string) ([]string, error) {
       
   155 	var typeList []string
       
   156 	if types == "" {
       
   157 		return typeList, nil
       
   158 	}
       
   159 	for _, f := range strings.Split(types, ",") {
       
   160 		switch f {
       
   161 		case "mention", "mentions":
       
   162 			f = "mention"
       
   163 		case "favourite", "favourites", "favorite", "favorites", "fave", "faves":
       
   164 			f = "favourite"
       
   165 		case "reblog", "reblogs", "retoot", "retoots":
       
   166 			f = "reblog"
       
   167 		case "follow", "follows":
       
   168 			f = "follow"
       
   169 		default:
       
   170 			return nil, errors.Errorf("unknown notification type: '%s'", f)
       
   171 		}
       
   172 		typeList = append(typeList, f)
       
   173 	}
       
   174 	return typeList, nil
       
   175 }
       
   176 
   139 func buildFilterMap(types string) (*map[string]bool, error) {
   177 func buildFilterMap(types string) (*map[string]bool, error) {
   140 	filterMap := make(map[string]bool)
   178 	filterMap := make(map[string]bool)
   141 	if types != "" {
   179 	if types == "" {
   142 		for _, f := range strings.Split(types, ",") {
   180 		return &filterMap, nil
   143 			switch f {
   181 	}
   144 			case "mention", "mentions":
   182 	for _, f := range strings.Split(types, ",") {
   145 				filterMap["mention"] = true
   183 		switch f {
   146 			case "favourite", "favourites", "favorite", "favorites", "fave", "faves":
   184 		case "mention", "mentions":
   147 				filterMap["favourite"] = true
   185 			filterMap["mention"] = true
   148 			case "reblog", "reblogs", "retoot", "retoots":
   186 		case "favourite", "favourites", "favorite", "favorites", "fave", "faves":
   149 				filterMap["reblog"] = true
   187 			filterMap["favourite"] = true
   150 			case "follow", "follows":
   188 		case "reblog", "reblogs", "retoot", "retoots":
   151 				filterMap["follow"] = true
   189 			filterMap["reblog"] = true
   152 			default:
   190 		case "follow", "follows":
   153 				return nil, errors.Errorf("unknown notification type: '%s'", f)
   191 			filterMap["follow"] = true
   154 			}
   192 		default:
       
   193 			return nil, errors.Errorf("unknown notification type: '%s'", f)
   155 		}
   194 		}
   156 	}
   195 	}
   157 	return &filterMap, nil
   196 	return &filterMap, nil
   158 }
   197 }