cmd/notifications.go
changeset 268 4dd196a4ee7c
parent 265 05c40b36d3b2
parent 267 5b91a65ba95a
equal deleted inserted replaced
266:80973a656b81 268:4dd196a4ee7c
    15 	"github.com/McKael/madon/v3"
    15 	"github.com/McKael/madon/v3"
    16 )
    16 )
    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              madon.ActivityID
    21 	types                string
    21 	types                string
    22 	excludeTypes         string
    22 	excludeTypes         string
    23 }
    23 }
    24 
    24 
    25 // notificationsCmd represents the notifications subcommand
    25 // notificationsCmd represents the notifications subcommand
    48 	accountsCmd.AddCommand(notificationsCmd)
    48 	accountsCmd.AddCommand(notificationsCmd)
    49 
    49 
    50 	notificationsCmd.Flags().BoolVar(&notificationsOpts.list, "list", false, "List all current notifications")
    50 	notificationsCmd.Flags().BoolVar(&notificationsOpts.list, "list", false, "List all current notifications")
    51 	notificationsCmd.Flags().BoolVar(&notificationsOpts.clear, "clear", false, "Clear all current notifications")
    51 	notificationsCmd.Flags().BoolVar(&notificationsOpts.clear, "clear", false, "Clear all current notifications")
    52 	notificationsCmd.Flags().BoolVar(&notificationsOpts.dismiss, "dismiss", false, "Delete a notification")
    52 	notificationsCmd.Flags().BoolVar(&notificationsOpts.dismiss, "dismiss", false, "Delete a notification")
    53 	notificationsCmd.Flags().Int64Var(&notificationsOpts.notifID, "notification-id", 0, "Get a notification")
    53 	notificationsCmd.Flags().StringVar(&notificationsOpts.notifID, "notification-id", "", "Get a notification")
    54 	notificationsCmd.Flags().StringVar(&notificationsOpts.types, "notification-types", "", "Filter notifications (mention, favourite, reblog, follow)")
    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)")
    55 	notificationsCmd.Flags().StringVar(&notificationsOpts.excludeTypes, "exclude-types", "", "Exclude notifications types (mention, favourite, reblog, follow)")
    56 }
    56 }
    57 
    57 
    58 func notificationRunE(cmd *cobra.Command, args []string) error {
    58 func notificationRunE(cmd *cobra.Command, args []string) error {
    59 	opt := notificationsOpts
    59 	opt := notificationsOpts
    60 
    60 
    61 	if !opt.list && !opt.clear && opt.notifID < 1 {
    61 	if !opt.list && !opt.clear && opt.notifID == "" {
    62 		return errors.New("missing parameters")
    62 		return errors.New("missing parameters")
    63 	}
    63 	}
    64 
    64 
    65 	if err := madonInit(true); err != nil {
    65 	if err := madonInit(true); err != nil {
    66 		return err
    66 		return err
    67 	}
    67 	}
    68 
    68 
    69 	var limOpts *madon.LimitParams
    69 	var limOpts *madon.LimitParams
    70 	if accountsOpts.all || accountsOpts.limit > 0 || accountsOpts.sinceID > 0 || accountsOpts.maxID > 0 {
    70 	if accountsOpts.all || accountsOpts.limit > 0 || accountsOpts.sinceID != "" || accountsOpts.maxID != "" {
    71 		limOpts = new(madon.LimitParams)
    71 		limOpts = new(madon.LimitParams)
    72 		limOpts.All = accountsOpts.all
    72 		limOpts.All = accountsOpts.all
    73 	}
    73 	}
    74 
    74 
    75 	if accountsOpts.limit > 0 {
    75 	if accountsOpts.limit > 0 {
    76 		limOpts.Limit = int(accountsOpts.limit)
    76 		limOpts.Limit = int(accountsOpts.limit)
    77 	}
    77 	}
    78 	if accountsOpts.maxID > 0 {
    78 	if accountsOpts.maxID != "" {
    79 		limOpts.MaxID = int64(accountsOpts.maxID)
    79 		limOpts.MaxID = accountsOpts.maxID
    80 	}
    80 	}
    81 	if accountsOpts.sinceID > 0 {
    81 	if accountsOpts.sinceID != "" {
    82 		limOpts.SinceID = int64(accountsOpts.sinceID)
    82 		limOpts.SinceID = accountsOpts.sinceID
    83 	}
    83 	}
    84 
    84 
    85 	var filterMap *map[string]bool
    85 	var filterMap *map[string]bool
    86 	if opt.types != "" {
    86 	if opt.types != "" {
    87 		var err error
    87 		var err error
   121 
   121 
   122 		if accountsOpts.keep > 0 && len(notifications) > int(accountsOpts.keep) {
   122 		if accountsOpts.keep > 0 && len(notifications) > int(accountsOpts.keep) {
   123 			notifications = notifications[:accountsOpts.keep]
   123 			notifications = notifications[:accountsOpts.keep]
   124 		}
   124 		}
   125 		obj = notifications
   125 		obj = notifications
   126 	} else if opt.notifID > 0 {
   126 	} else if opt.notifID != "" {
   127 		if opt.dismiss {
   127 		if opt.dismiss {
   128 			err = gClient.DismissNotification(opt.notifID)
   128 			err = gClient.DismissNotification(opt.notifID)
   129 		} else {
   129 		} else {
   130 			obj, err = gClient.GetNotification(opt.notifID)
   130 			obj, err = gClient.GetNotification(opt.notifID)
   131 		}
   131 		}