diff --git a/lib/actions.rb b/lib/actions.rb index 0fec56e..b8ceef2 100644 --- a/lib/actions.rb +++ b/lib/actions.rb @@ -8,7 +8,7 @@ require 'actions/add_friend_message' require 'actions/change_friend_name' require 'actions/change_friend_status' require 'actions/change_friend_status_message' -require 'actions/new_message_enter' +require 'actions/add_out_friend_message' require 'actions/new_message_putc' module Actions diff --git a/lib/actions/add_out_friend_message.rb b/lib/actions/add_out_friend_message.rb new file mode 100644 index 0000000..55e205a --- /dev/null +++ b/lib/actions/add_out_friend_message.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module Actions + class AddOutFriendMessage < Obredux::Action + attr_reader :friend, :text, :error + + def initialize(friend, text, error) + self.friend = friend + self.text = text + self.error = error + end + + private + + def friend=(value) + raise TypeError, "expected friend to be a #{Tox::Friend}" unless value.is_a? Tox::Friend + @friend = value + end + + def text=(value) + raise TypeError, "expected text to be a #{String}" unless value.is_a? String + @text = value.frozen? ? value : value.dup.freeze + end + + def error=(value) + @error = !!value + end + end +end diff --git a/lib/actions/new_message_enter.rb b/lib/actions/new_message_enter.rb deleted file mode 100644 index f2a86e6..0000000 --- a/lib/actions/new_message_enter.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -module Actions - class NewMessageEnter < Obredux::Action - attr_reader :tox_client - - def initialize(tox_client) - self.tox_client = tox_client - end - - private - - def tox_client=(value) - raise TypeError, "expected Tox client to be a #{Tox::Client}" unless value.is_a? Tox::Client - @tox_client = value - end - end -end diff --git a/lib/main.rb b/lib/main.rb index dfc958a..39781fb 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -137,7 +137,26 @@ private end def on_new_message_enter - store.dispatch Actions::NewMessageEnter.new @tox_client + return if state[:data][:active_friend_index].nil? + + friend_number = state[:data][:friends].keys[state[:data][:active_friend_index]] + + return if friend_number.nil? + + text = state[:data][:friends][friend_number][:new_message][:text].strip.freeze + + return if text.empty? + + friend = @tox_client.friend friend_number + error = false + + begin + friend.send_message text + rescue + error = true + end + + store.dispatch Actions::AddOutFriendMessage.new friend, text, error end def on_new_message_putc(char) diff --git a/lib/reducer.rb b/lib/reducer.rb index 5445186..a5f5063 100644 --- a/lib/reducer.rb +++ b/lib/reducer.rb @@ -104,8 +104,6 @@ private menu_up when Actions::MenuDown menu_down - when Actions::NewMessageEnter - new_message_enter when Actions::NewMessagePutc new_message_putc when Actions::NewMessageLeft @@ -229,48 +227,6 @@ private ).freeze end - def new_message_enter - return state if state[:data][:active_friend_index].nil? - - friend_number = state[:data][:friends].keys[state[:data][:active_friend_index]] - - return state if friend_number.nil? - - text = state[:data][:friends][friend_number][:new_message][:text].strip.freeze - - return state if text.empty? - - error = false - - begin - action.tox_client.friend(friend_number).send_message text - rescue - error = true - end - - state.merge( - data: state[:data].merge( - friends: state[:data][:friends].merge( - friend_number => state[:data][:friends][friend_number].merge( - new_message: state[:data][:friends][friend_number][:new_message].merge( - text: '', - cursor_pos: 0, - ), - - history: (state[:data][:friends][friend_number][:history] + [ - error: error, - out: true, - received: false, - time: Time.now.utc.freeze, - name: action.tox_client.name.freeze, - text: text, - ]).freeze, - ).freeze, - ).freeze, - ).freeze, - ).freeze - end - def new_message_putc return state if state[:data][:active_friend_index].nil? diff --git a/lib/reducers/data.rb b/lib/reducers/data.rb index 438169b..361137e 100644 --- a/lib/reducers/data.rb +++ b/lib/reducers/data.rb @@ -25,6 +25,8 @@ module Reducers change_friend_status_message when Actions::ChangeFriendStatus change_friend_status + when Actions::AddOutFriendMessage + add_out_friend_message else state end @@ -115,5 +117,27 @@ module Reducers ).freeze, ).freeze end + + def add_out_friend_message + state.merge( + friends: state[:friends].merge( + action.friend.number => state[:friends][action.friend.number].merge( + new_message: state[:friends][action.friend.number][:new_message].merge( + text: '', + cursor_pos: 0, + ), + + history: (state[:friends][action.friend.number][:history] + [ + error: action.error, + out: true, + received: false, + time: Time.now.utc.freeze, + name: action.friend.client.name.freeze, + text: action.text, + ]).freeze, + ).freeze, + ).freeze, + ).freeze + end end end