Move Actions::FriendRequest implementation from reducer
This commit is contained in:
parent
03f70d605f
commit
8c9eb609d9
5 changed files with 30 additions and 36 deletions
|
@ -3,7 +3,7 @@
|
||||||
require 'obredux'
|
require 'obredux'
|
||||||
|
|
||||||
require 'actions/load_friends'
|
require 'actions/load_friends'
|
||||||
require 'actions/friend_request'
|
require 'actions/add_friend'
|
||||||
require 'actions/friend_message'
|
require 'actions/friend_message'
|
||||||
require 'actions/change_friend_name'
|
require 'actions/change_friend_name'
|
||||||
require 'actions/change_friend_status'
|
require 'actions/change_friend_status'
|
||||||
|
|
18
lib/actions/add_friend.rb
Normal file
18
lib/actions/add_friend.rb
Normal 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
|
|
@ -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
|
|
|
@ -95,7 +95,9 @@ private
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_friend_request(public_key, _text)
|
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
|
end
|
||||||
|
|
||||||
def on_friend_message(friend, text)
|
def on_friend_message(friend, text)
|
||||||
|
|
|
@ -100,8 +100,8 @@ private
|
||||||
case action
|
case action
|
||||||
when Actions::LoadFriends
|
when Actions::LoadFriends
|
||||||
load_friends
|
load_friends
|
||||||
when Actions::FriendRequest
|
when Actions::AddFriend
|
||||||
friend_request
|
add_friend
|
||||||
when Actions::FriendMessage
|
when Actions::FriendMessage
|
||||||
friend_message
|
friend_message
|
||||||
when Actions::ChangeFriendName
|
when Actions::ChangeFriendName
|
||||||
|
@ -162,19 +162,17 @@ private
|
||||||
).freeze
|
).freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
def friend_request
|
def add_friend
|
||||||
friend = action.tox_client.friend_add_norequest action.public_key
|
|
||||||
|
|
||||||
state.merge(
|
state.merge(
|
||||||
data: state[:data].merge(
|
data: state[:data].merge(
|
||||||
active_friend_index: state[:data][:active_friend_index] || state[:data][:friends].count,
|
active_friend_index: state[:data][:active_friend_index] || state[:data][:friends].count,
|
||||||
|
|
||||||
friends: state[:data][:friends].merge(
|
friends: state[:data][:friends].merge(
|
||||||
friend.number => {
|
action.friend.number => {
|
||||||
public_key: friend.public_key.to_hex.freeze,
|
public_key: action.friend.public_key.to_hex.freeze,
|
||||||
name: friend.name.freeze,
|
name: action.friend.name.freeze,
|
||||||
status: friend.status,
|
status: action.friend.status,
|
||||||
status_message: friend.status_message.freeze,
|
status_message: action.friend.status_message.freeze,
|
||||||
history: [].freeze,
|
history: [].freeze,
|
||||||
new_message: {
|
new_message: {
|
||||||
text: '',
|
text: '',
|
||||||
|
|
Reference in a new issue