mina_p2p/connection/outgoing_effectful/
p2p_connection_outgoing_effectful_effects.rs1use mina_core::bug_condition;
2use redux::ActionMeta;
3
4use crate::{
5 connection::{
6 outgoing::{P2pConnectionOutgoingAction, P2pConnectionOutgoingError},
7 P2pConnectionService,
8 },
9 webrtc,
10};
11
12use super::P2pConnectionOutgoingEffectfulAction;
13
14impl P2pConnectionOutgoingEffectfulAction {
15 pub fn effects<Store, S>(self, _meta: &ActionMeta, store: &mut Store)
16 where
17 Store: crate::P2pStore<S>,
18 Store::Service: P2pConnectionService,
19 {
20 match self {
21 P2pConnectionOutgoingEffectfulAction::RandomInit { peers } => {
22 let picked_peer = store.service().random_pick(&peers);
23 if let Some(picked_peer) = picked_peer {
24 store.dispatch(P2pConnectionOutgoingAction::Reconnect {
25 opts: picked_peer,
26 rpc_id: None,
27 });
28 } else {
29 bug_condition!("Picked peer is None");
30 }
31 }
32 P2pConnectionOutgoingEffectfulAction::Init { opts, .. } => {
33 let peer_id = *opts.peer_id();
34 store.service().outgoing_init(opts);
35 store.dispatch(P2pConnectionOutgoingAction::OfferSdpCreatePending { peer_id });
36 }
37 P2pConnectionOutgoingEffectfulAction::OfferSend {
38 peer_id,
39 offer,
40 signaling_method,
41 } => {
42 match signaling_method {
43 webrtc::SignalingMethod::Http(_)
44 | webrtc::SignalingMethod::Https(_)
45 | webrtc::SignalingMethod::HttpsProxy(_, _)
46 | webrtc::SignalingMethod::Proxied(_, _, _) => {
47 let Some(url) = signaling_method.http_url() else {
48 return;
49 };
50 store.service().http_signaling_request(url, *offer);
51 }
52 webrtc::SignalingMethod::P2p { .. } => {
53 bug_condition!("`P2pConnectionOutgoingEffectfulAction::OfferSend` shouldn't be called for `webrtc::SignalingMethod::P2p`");
54 return;
55 }
56 }
57 store.dispatch(P2pConnectionOutgoingAction::OfferSendSuccess { peer_id });
58 }
59 P2pConnectionOutgoingEffectfulAction::AnswerSet { peer_id, answer } => {
60 store.service().set_answer(peer_id, *answer);
61 store.dispatch(P2pConnectionOutgoingAction::FinalizePending { peer_id });
62 }
63 P2pConnectionOutgoingEffectfulAction::ConnectionAuthorizationEncryptAndSend {
64 peer_id,
65 other_pub_key,
66 auth,
67 } => {
68 store
69 .service()
70 .auth_encrypt_and_send(peer_id, &other_pub_key, auth);
71 }
72 P2pConnectionOutgoingEffectfulAction::ConnectionAuthorizationDecryptAndCheck {
73 peer_id,
74 other_pub_key,
75 expected_auth,
76 auth,
77 } => {
78 if store
79 .service()
80 .auth_decrypt(&other_pub_key, auth)
81 .is_some_and(|remote_auth| remote_auth == expected_auth)
82 {
83 store.dispatch(P2pConnectionOutgoingAction::Success { peer_id });
84 } else {
85 store.dispatch(P2pConnectionOutgoingAction::Error {
86 peer_id,
87 error: P2pConnectionOutgoingError::ConnectionAuthError,
88 });
89 }
90 }
91 }
92 }
93}