1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
use std::{collections::HashSet, str::FromStr};
use filamento::{
error::{CommandError, SubscribeError},
roster::ContactStoreFields,
};
use jid::{BareJID, JID};
use leptos::{html::Input, prelude::*};
use reactive_stores::Store;
use thiserror::Error;
use crate::{
client::Client,
roster::{Roster, RosterStoreFields},
};
#[derive(Clone, Debug, Error)]
pub enum AddContactError {
#[error("Missing JID")]
MissingJID,
#[error("Invalid JID: {0}")]
InvalidJID(#[from] jid::ParseError),
#[error("Subscription: {0}")]
Db(#[from] CommandError<SubscribeError>),
}
#[component]
// TODO: rename
pub fn AddContact() -> impl IntoView {
let requests: ReadSignal<HashSet<BareJID>> =
use_context().expect("no pending subscriptions in context");
let set_requests: WriteSignal<HashSet<BareJID>> =
use_context().expect("no pending subscriptions write signal in context");
let roster: Store<Roster> = use_context().expect("no roster in context");
let jid = RwSignal::new("".to_string());
// TODO: compartmentalise into error component, form component...
let (error, set_error) = signal(None::<AddContactError>);
let error_message = move || {
error.with(|error| {
if let Some(error) = error {
view! { <div class="error">{error.to_string()}</div> }.into_any()
} else {
view! {}.into_any()
}
})
};
let (add_contact_pending, set_add_contact_pending) = signal(false);
let client = use_context::<Client>().expect("client not in context");
let client2 = client.clone();
let client3 = client.clone();
let client4 = client.clone();
let add_contact = Action::new_local(move |_| {
let client = client.clone();
async move {
set_add_contact_pending.set(true);
if jid.read_untracked().is_empty() {
set_error.set(Some(AddContactError::MissingJID));
set_add_contact_pending.set(false);
return;
}
let jid = match JID::from_str(&jid.read_untracked()) {
Ok(j) => j.to_bare(),
Err(e) => {
set_error.set(Some(e.into()));
set_add_contact_pending.set(false);
return;
}
};
let chat_jid = jid;
// TODO: more options?
match client.buddy_request(chat_jid).await {
Ok(c) => c,
Err(e) => {
set_error.set(Some(e.into()));
set_add_contact_pending.set(false);
return;
}
};
set_add_contact_pending.set(false);
}
});
let jid_input = NodeRef::<Input>::new();
let _focus = Effect::new(move |_| {
if let Some(input) = jid_input.get() {
let _ = input.focus();
input.set_text_content(Some(""));
// input.style("height: 0");
// let height = input.scroll_height();
// input.style(format!("height: {}px", height));
}
});
let outgoing = move || {
roster
.contacts()
.get()
.into_iter()
.filter(
|(jid, contact)| match *contact.contact.subscription().read() {
filamento::roster::Subscription::None => false,
filamento::roster::Subscription::PendingOut => true,
filamento::roster::Subscription::PendingIn => false,
filamento::roster::Subscription::PendingInPendingOut => true,
filamento::roster::Subscription::OnlyOut => false,
filamento::roster::Subscription::OnlyIn => false,
filamento::roster::Subscription::OutPendingIn => false,
filamento::roster::Subscription::InPendingOut => true,
filamento::roster::Subscription::Buddy => false,
},
)
.collect::<Vec<_>>()
};
let accept_friend_request = Action::new_local(move |jid: &BareJID| {
let client = client2.clone();
let jid = jid.clone();
async move {
// TODO: error
client.accept_buddy_request(jid).await;
}
});
let reject_friend_request = Action::new_local(move |jid: &BareJID| {
let client = client3.clone();
let jid = jid.clone();
async move {
// TODO: error
client.unsubscribe_contact(jid.clone()).await;
set_requests.write().remove(&jid);
}
});
let cancel_subscription_request = Action::new_local(move |jid: &BareJID| {
let client = client4.clone();
let jid = jid.clone();
async move {
// TODO: error
client.unsubscribe_from_contact(jid).await;
}
});
view! {
<div class="add-contact-menu">
<div>
{error_message}
<form on:submit=move |ev| {
ev.prevent_default();
add_contact.dispatch(());
}>
<input
disabled=add_contact_pending
placeholder="JID"
type="text"
node_ref=jid_input
bind:value=jid
name="jid"
id="jid"
autofocus="true"
/>
<input
disabled=add_contact_pending
class="button"
type="submit"
value="Send Friend Request"
/>
</form>
</div>
{move || {
if !requests.read().is_empty() {
view! {
<div>
<h3>Incoming Subscription Requests</h3>
<For
each=move || requests.get()
key=|request| request.clone()
let(request)
>
{
let request2 = request.clone();
let request3 = request.clone();
let jid_string = move || request.to_string();
view! {
<div class="jid-with-button">
<div class="jid">{jid_string}</div>
<div>
<div
class="button"
on:click=move |_| {
accept_friend_request.dispatch(request2.clone());
}
>
Accept
</div>
<div
class="button"
on:click=move |_| {
reject_friend_request.dispatch(request3.clone());
}
>
Reject
</div>
</div>
</div>
}
}
</For>
</div>
}
.into_any()
} else {
view! {}.into_any()
}
}}
{move || {
if !outgoing().is_empty() {
view! {
<div>
<h3>Pending Outgoing Subscription Requests</h3>
<For
each=move || outgoing()
key=|(jid, _contact)| jid.clone()
let((jid, contact))
>
{
let jid2 = jid.clone();
let jid_string = move || jid.to_string();
view! {
<div class="jid-with-button">
<div class="jid">{jid_string}</div>
<div
class="button"
on:click=move |_| {
cancel_subscription_request.dispatch(jid2.clone());
}
>
Cancel
</div>
</div>
}
}
</For>
</div>
}
.into_any()
} else {
view! {}.into_any()
}
}}
</div>
}
}
|