lib/ayadn/action.rb in ayadn-1.7.7 vs lib/ayadn/action.rb in ayadn-1.8.0

- old
+ new

@@ -9,10 +9,11 @@ @api = API.new @view = View.new @workers = Workers.new @stream = Stream.new(@api, @view, @workers) @search = Search.new(@api, @view, @workers) + @shell = Thor::Shell::Color.new Settings.load_config Settings.get_token Settings.init_config Logs.create_logger Databases.open_databases @@ -86,11 +87,10 @@ abort(Status.error_missing_post_id) if ids.empty? ids.each do |post_id| print Status.deleting_post(post_id) resp = @api.delete_post(post_id) Check.has_been_deleted(post_id, resp) - sleep 1 unless ids.length == 1 end rescue => e Errors.global_error({error: e, caller: caller, data: [post_id]}) end end @@ -105,11 +105,10 @@ channel_id = @workers.get_channel_id_from_alias(channel) ids.each do |message_id| print Status.deleting_message(message_id) resp = @api.delete_message(channel_id, message_id) Check.message_has_been_deleted(message_id, resp) - sleep 1 unless ids.length == 1 end rescue => e Errors.global_error({error: e, caller: caller, data: [message_id]}) end end @@ -120,11 +119,10 @@ users = @workers.all_but_me(usernames) puts Status.unfollowing(users.join(',')) users.each do |user| resp = @api.unfollow(user) Check.has_been_unfollowed(user, resp) - sleep 1 unless users.length == 1 end rescue => e Errors.global_error({error: e, caller: caller, data: [usernames]}) end end @@ -135,11 +133,10 @@ users = @workers.all_but_me(usernames) puts Status.following(users.join(',')) users.each do |user| resp = @api.follow(user) Check.has_been_followed(user, resp) - sleep 1 unless users.length == 1 end rescue => e Errors.global_error({error: e, caller: caller, data: [usernames]}) end end @@ -150,11 +147,10 @@ users = @workers.all_but_me(usernames) puts Status.unmuting(users.join(',')) users.each do |user| resp = @api.unmute(user) Check.has_been_unmuted(user, resp) - sleep 1 unless users.length == 1 end rescue => e Errors.global_error({error: e, caller: caller, data: [usernames]}) end end @@ -165,11 +161,10 @@ users = @workers.all_but_me(usernames) puts Status.muting(users.join(',')) users.each do |user| resp = @api.mute(user) Check.has_been_muted(user, resp) - sleep 1 unless users.length == 1 end rescue => e Errors.global_error({error: e, caller: caller, data: [usernames]}) end end @@ -180,11 +175,10 @@ users = @workers.all_but_me(usernames) puts Status.unblocking(users.join(',')) users.each do |user| resp = @api.unblock(user) Check.has_been_unblocked(user, resp) - sleep 1 unless users.length == 1 end rescue => e Errors.global_error({error: e, caller: caller, data: [usernames]}) end end @@ -195,11 +189,10 @@ users = @workers.all_but_me(usernames) puts Status.blocking(users.join(',')) users.each do |user| resp = @api.block(user) Check.has_been_blocked(user, resp) - sleep 1 unless users.length == 1 end rescue => e Errors.global_error({error: e, caller: caller, data: [usernames]}) end end @@ -449,10 +442,71 @@ rescue => e Errors.global_error({error: e, caller: caller, data: [channel_id, options]}) end end + def messages_unread(options) + begin + if options[:silent] + Settings.options[:marker][:update_messages] = false + end + puts "\n" + @shell.say_status :searching, "channels with unread PMs" + response = @api.get_channels + unread_channels = [] + response['data'].map do |ch| + if ch['type'] == "net.app.core.pm" && ch['has_unread'] == true + unread_channels << ch['id'] + end + end + abort(Status.no_new_messages) if unread_channels.empty? + unread_messages = {} + unread_channels.each do |id| + @shell.say_status :downloading, "messages from channel #{id}" + since = Databases.pagination["channel:#{id}"] + unless since.nil? + api_options = {count: 20, since_id: since} + else + api_options = {count: 20} + end + ch = @api.get_messages(id, api_options) + last_read_id = ch['meta']['marker']['last_read_id'].to_i + last_message_id = ch['meta']['max_id'] + messages = [] + ch['data'].each do |msg| + messages << msg if msg['id'].to_i > last_read_id + end + unread_messages[id] = [messages, last_message_id] + end + if Settings.options[:marker][:update_messages] == true + unread_messages.each do |k,v| + name = "channel:#{k}" + Databases.pagination[name] = v[1] + resp = @api.update_marker(name, v[1]) + res = JSON.parse(resp) + if res['meta']['code'] != 200 + @shell.say_status :error, "couldn't update channel #{k} as read", :red + else + @shell.say_status :updated, "channel #{k} as read", :green + end + end + end + @view.clear_screen + unread_messages.each do |k,v| + if v[0].length == 1 + puts "\nUnread message from channel #{k}:\n".color(Settings.options[:colors][:unread]).inverse + else + puts "\nUnread messages from channel #{k}:\n".color(Settings.options[:colors][:unread]).inverse + end + @view.show_posts(v[0]) + end + puts "\n" if Settings.options[:timeline][:compact] + rescue => e + Errors.global_error({error: e, caller: caller, data: [options]}) + end + end + def pin(post_id, usertags) require 'pinboard' require 'base64' begin Check.bad_post_id(post_id) @@ -528,10 +582,13 @@ end end def pmess(username, options = {}) begin + if options[:silent] + Settings.options[:marker][:update_messages] = false + end Check.no_username(username) username = [@workers.add_arobase(username)] writer = Post.new puts Status.message_from(username) puts Status.message @@ -543,10 +600,22 @@ if options[:poster] settings = options.dup options = NowWatching.new.get_poster(settings[:poster], settings) end resp = writer.pm({options: options, text: text, username: username}) + if Settings.options[:marker][:update_messages] == true + if resp['meta']['code'] == 200 + data = resp['data'] + name = "channel:#{data['channel_id']}" + Databases.pagination[name] = data['id'] + marked = @api.update_marker(name, data['id']) + updated = JSON.parse(marked) + if updated['meta']['code'] != 200 + raise "couldn't update channel #{data['channel_id']} as read" + end + end + end FileOps.save_message(resp) if Settings.options[:backup][:auto_save_sent_messages] @view.clear_screen puts Status.yourmessage(username[0]) @view.show_posted(resp) rescue => e @@ -596,10 +665,13 @@ end end def send_to_channel(channel_id, options = {}) begin + if options[:silent] + Settings.options[:marker][:update_messages] = false + end channel_id = @workers.get_channel_id_from_alias(channel_id) writer = Post.new puts Status.writing puts Status.message lines_array = writer.compose @@ -609,15 +681,27 @@ if options[:poster] settings = options.dup options = NowWatching.new.get_poster(settings[:poster], settings) end resp = writer.message({options: options, id: channel_id, text: lines_array.join("\n")}) + if Settings.options[:marker][:update_messages] == true + if resp['meta']['code'] == 200 + data = resp['data'] + name = "channel:#{data['channel_id']}" + Databases.pagination[name] = data['id'] + marked = @api.update_marker(name, data['id']) + updated = JSON.parse(marked) + if updated['meta']['code'] != 200 + raise "couldn't update channel #{data['channel_id']} as read" + end + end + end FileOps.save_message(resp) if Settings.options[:backup][:auto_save_sent_messages] @view.clear_screen puts Status.yourpost @view.show_posted(resp) rescue => e - Errors.global_error({error: e, caller: caller, data: [channel_id]}) + Errors.global_error({error: e, caller: caller, data: [channel_id, options]}) end end def nowplaying(options = {}) np = NowPlaying.new(@api, @view, @workers)