cmd/notifications.go
changeset 166 79c0f8db66ff
parent 165 e76422bfe6ee
child 167 1341bacd01c9
equal deleted inserted replaced
165:e76422bfe6ee 166:79c0f8db66ff
    72 	}
    72 	}
    73 	if accountsOpts.sinceID > 0 {
    73 	if accountsOpts.sinceID > 0 {
    74 		limOpts.SinceID = int64(accountsOpts.sinceID)
    74 		limOpts.SinceID = int64(accountsOpts.sinceID)
    75 	}
    75 	}
    76 
    76 
    77 	filterMap := make(map[string]bool)
    77 	var filterMap *map[string]bool
    78 	if opt.types != "" {
    78 	if opt.types != "" {
    79 		for _, f := range strings.Split(opt.types, ",") {
    79 		var err error
    80 			switch f {
    80 		filterMap, err = buildFilterMap(opt.types)
    81 			case "mention", "mentions":
    81 		if err != nil {
    82 				filterMap["mention"] = true
    82 			return nil
    83 			case "favourite", "favourites", "favorite", "favorites", "fave", "faves":
       
    84 				filterMap["favourite"] = true
       
    85 			case "reblog", "reblogs", "retoot", "retoots":
       
    86 				filterMap["reblog"] = true
       
    87 			case "follow", "follows":
       
    88 				filterMap["follow"] = true
       
    89 			default:
       
    90 				return errors.Errorf("unknown notification type: '%s'", f)
       
    91 			}
       
    92 		}
    83 		}
    93 	}
    84 	}
    94 
    85 
    95 	var obj interface{}
    86 	var obj interface{}
    96 	var err error
    87 	var err error
    98 	if opt.list {
    89 	if opt.list {
    99 		var notifications []madon.Notification
    90 		var notifications []madon.Notification
   100 		notifications, err = gClient.GetNotifications(limOpts)
    91 		notifications, err = gClient.GetNotifications(limOpts)
   101 
    92 
   102 		// Filter notifications
    93 		// Filter notifications
   103 		if len(filterMap) > 0 {
    94 		if filterMap != nil && len(*filterMap) > 0 {
   104 			if verbose {
    95 			if verbose {
   105 				errPrint("Filtering notifications")
    96 				errPrint("Filtering notifications")
   106 			}
    97 			}
   107 			var newNotifications []madon.Notification
    98 			var newNotifications []madon.Notification
   108 			for _, notif := range notifications {
    99 			for _, notif := range notifications {
   109 				if filterMap[notif.Type] {
   100 				if (*filterMap)[notif.Type] {
   110 					newNotifications = append(newNotifications, notif)
   101 					newNotifications = append(newNotifications, notif)
   111 				}
   102 				}
   112 			}
   103 			}
   113 			notifications = newNotifications
   104 			notifications = newNotifications
   114 		}
   105 		}
   142 		errPrint("Error: %s", err.Error())
   133 		errPrint("Error: %s", err.Error())
   143 		os.Exit(1)
   134 		os.Exit(1)
   144 	}
   135 	}
   145 	return p.printObj(obj)
   136 	return p.printObj(obj)
   146 }
   137 }
       
   138 
       
   139 func buildFilterMap(types string) (*map[string]bool, error) {
       
   140 	filterMap := make(map[string]bool)
       
   141 	if types != "" {
       
   142 		for _, f := range strings.Split(types, ",") {
       
   143 			switch f {
       
   144 			case "mention", "mentions":
       
   145 				filterMap["mention"] = true
       
   146 			case "favourite", "favourites", "favorite", "favorites", "fave", "faves":
       
   147 				filterMap["favourite"] = true
       
   148 			case "reblog", "reblogs", "retoot", "retoots":
       
   149 				filterMap["reblog"] = true
       
   150 			case "follow", "follows":
       
   151 				filterMap["follow"] = true
       
   152 			default:
       
   153 				return nil, errors.Errorf("unknown notification type: '%s'", f)
       
   154 			}
       
   155 		}
       
   156 	}
       
   157 	return &filterMap, nil
       
   158 }