lib/ayadn/blacklist.rb in ayadn-1.8.2 vs lib/ayadn/blacklist.rb in ayadn-2.0

- old
+ new

@@ -3,48 +3,48 @@ class Blacklist < Thor desc "add TYPE TARGET", "Adds a mention, hashtag, client or username to your blacklist" map "create" => :add long_desc Descriptions.blacklist_add def add(*args) - abort(Status.type_and_target_missing) if args.length < 2 + if args.length < 2 + Status.new.type_and_target_missing + exit + end BlacklistWorkers.new.add(args) - puts Status.done + Status.new.done end desc "remove TYPE TARGET", "Removes a mention, hashtag, client or username from your blacklist" map "delete" => :remove long_desc Descriptions.blacklist_remove def remove(*args) - abort(Status.type_and_target_missing) if args.length < 2 + if args.length < 2 + Status.new.type_and_target_missing + exit + end BlacklistWorkers.new.remove(args) - puts Status.done + Status.new.done end desc "list", "List the content of your blacklist" long_desc Descriptions.blacklist_list option :raw, aliases: "-x", type: :boolean, desc: "Outputs the raw list in CSV" + option :compact, aliases: "-k", type: :boolean, desc: "Force the view to be compact if not already" def list BlacklistWorkers.new.list(options) end - desc "import DATABASE", "Imports a blacklist database from another Ayadn account" - long_desc Descriptions.blacklist_import - def import(database) - BlacklistWorkers.new.import(database) - end + # desc "import DATABASE", "Imports a blacklist database from another Ayadn account" + # long_desc Descriptions.blacklist_import + # def import(database) + # BlacklistWorkers.new.import(database) + # end - desc "convert", "Convert your current blacklist database to the new format" - long_desc Descriptions.blacklist_convert - def convert - BlacklistWorkers.new.convert - puts Status.done - end - desc "clear", "Clear your blacklist database" def clear BlacklistWorkers.new.clear - puts Status.done + Status.new.done end end class BlacklistWorkers @@ -54,112 +54,102 @@ Settings.init_config Logs.create_logger Databases.open_databases @workers = Workers.new end - def import(database) - begin - new_db = File.realpath(database) - if File.exist?(new_db) - Databases.import_blacklist(new_db) - Logs.rec.info "Imported '#{new_db}' values in blacklist database." - else - puts "\nFile '#{new_db}' doesn't exist.\n\n".color(:red) - Logs.rec.warn "File '#{new_db}' doesn't exist." - end - ensure - Databases.close_all - end - end - def convert - begin - Databases.convert_blacklist - ensure - Databases.close_all - end - end + # def import(database) + # begin + # new_db = File.realpath(database) + # if File.exist?(new_db) + # Databases.import_blacklist(new_db) + # Logs.rec.info "Imported '#{new_db}' values in blacklist database." + # else + # puts "\nFile '#{new_db}' doesn't exist.\n\n".color(:red) + # Logs.rec.warn "File '#{new_db}' doesn't exist." + # end + # end + # end def clear begin - puts "\n\nAre you sure you want to erase all the content of your blacklist database?\n\n[y/N]\n".color(:red) + Status.new.ask_clear_blacklist input = STDIN.getch if input == 'y' || input == 'Y' Databases.clear_blacklist Logs.rec.info "Cleared the blacklist database." else - abort Status.canceled + Status.new.canceled + exit end - ensure - Databases.close_all end end def add(args) begin type = args.shift case type when 'user', 'username', 'account' + target = @workers.remove_arobase_if_present(args) + Databases.add_to_blacklist('user', target) target = @workers.add_arobases_to_usernames args - Databases.add_user_to_blacklist(target) Logs.rec.info "Added '#{target}' to blacklist of users." when 'mention', 'mentions' + target = @workers.remove_arobase_if_present(args) + Databases.add_to_blacklist('mention', target) target = @workers.add_arobases_to_usernames args - Databases.add_mention_to_blacklist(target) Logs.rec.info "Added '#{target}' to blacklist of mentions." when 'client', 'source' - Databases.add_client_to_blacklist(args) + Databases.add_to_blacklist('client', args) Logs.rec.info "Added '#{args}' to blacklist of clients." when 'hashtag', 'tag' - Databases.add_hashtag_to_blacklist(args) + Databases.add_to_blacklist('hashtag', args) Logs.rec.info "Added '#{args}' to blacklist of hashtags." else - puts Status.wrong_arguments + Status.new.wrong_arguments end - ensure - Databases.close_all end end def remove(args) begin type = args.shift case type when 'user', 'username', 'account' - temp = @workers.add_arobases_to_usernames args - target = temp.map {|u| "-#{u}"} - Databases.remove_from_blacklist(target) + Databases.remove_from_blacklist(args) + target = @workers.add_arobases_to_usernames(args) Logs.rec.info "Removed '#{target}' from blacklist of users." when 'mention', 'mentions' - target = @workers.add_arobases_to_usernames args - Databases.remove_from_blacklist(target) + Databases.remove_from_blacklist(args) + target = @workers.add_arobases_to_usernames(args) Logs.rec.info "Removed '#{target}' from blacklist of mentions." when 'client', 'source', 'hashtag', 'tag' Databases.remove_from_blacklist(args) Logs.rec.info "Removed '#{type}:#{args}' from blacklist." else - puts Status.wrong_arguments + Status.new.wrong_arguments end - ensure - Databases.close_all end end def list(options) begin + Settings.options[:timeline][:compact] = true if options[:compact] == true show_list(options) - ensure - Databases.close_all end end private def show_list(options) - list = Databases.blacklist + list = Databases.all_blacklist unless list.empty? if options[:raw] - list.each {|v,k| puts "#{v},#{k}"} + xx = list.map {|obj| [obj[0], obj[1].to_s.force_encoding("UTF-8")] } + puts xx.to_json else + puts "\n" puts Workers.new.build_blacklist_list(list) + puts "\n" end else - abort(Status.empty_list) + Status.new.empty_list + exit end end end end