Archived
1
0
Fork 0

Move Actions::FriendRequest implementation from reducer

This commit is contained in:
Braiden Vasco 2017-07-28 03:28:43 +00:00
parent 03f70d605f
commit 8c9eb609d9
5 changed files with 30 additions and 36 deletions

View file

@ -3,7 +3,7 @@
require 'obredux'
require 'actions/load_friends'
require 'actions/friend_request'
require 'actions/add_friend'
require 'actions/friend_message'
require 'actions/change_friend_name'
require 'actions/change_friend_status'

18
lib/actions/add_friend.rb Normal file
View file

@ -0,0 +1,18 @@
# frozen_string_literal: true
module Actions
class AddFriend < Obredux::Action
attr_reader :friend
def initialize(friend)
self.friend = friend
end
private
def friend=(value)
raise TypeError, "expected friend to be a #{Tox::Friend}" unless value.is_a? Tox::Friend
@friend = value
end
end
end

View file

@ -1,24 +0,0 @@
# frozen_string_literal: true
module Actions
class FriendRequest < Obredux::Action
attr_reader :tox_client, :public_key
def initialize(tox_client, public_key)
self.tox_client = tox_client
self.public_key = public_key
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
def public_key=(value)
raise TypeError, "expected public key to be a #{Tox::PublicKey}" unless value.is_a? Tox::PublicKey
@public_key = value
end
end
end

View file

@ -95,7 +95,9 @@ private
end
def on_friend_request(public_key, _text)
store.dispatch Actions::FriendRequest.new @tox_client, public_key
friend = @tox_client.friend_add_norequest public_key
store.dispatch Actions::AddFriend.new friend
end
def on_friend_message(friend, text)

View file

@ -100,8 +100,8 @@ private
case action
when Actions::LoadFriends
load_friends
when Actions::FriendRequest
friend_request
when Actions::AddFriend
add_friend
when Actions::FriendMessage
friend_message
when Actions::ChangeFriendName
@ -162,19 +162,17 @@ private
).freeze
end
def friend_request
friend = action.tox_client.friend_add_norequest action.public_key
def add_friend
state.merge(
data: state[:data].merge(
active_friend_index: state[:data][:active_friend_index] || state[:data][:friends].count,
friends: state[:data][:friends].merge(
friend.number => {
public_key: friend.public_key.to_hex.freeze,
name: friend.name.freeze,
status: friend.status,
status_message: friend.status_message.freeze,
action.friend.number => {
public_key: action.friend.public_key.to_hex.freeze,
name: action.friend.name.freeze,
status: action.friend.status,
status_message: action.friend.status_message.freeze,
history: [].freeze,
new_message: {
text: '',