accept / deny friend requests

This commit is contained in:
2026-03-15 21:29:41 -04:00
parent a32f6392b7
commit 3e6df06411
6 changed files with 81 additions and 21 deletions
+1 -1
Submodule iris updated: 1102dc7338...c118bb446b
+1
View File
@@ -2,4 +2,5 @@ use super::*;
pub const MODAL_BG: UiColor = UiColor::BLACK.brighter(0.05); pub const MODAL_BG: UiColor = UiColor::BLACK.brighter(0.05);
pub const GREEN: UiColor = UiColor::rgb(0, 150, 0); pub const GREEN: UiColor = UiColor::rgb(0, 150, 0);
pub const RED: UiColor = UiColor::rgb(255, 100, 100);
pub const DARK: UiColor = UiColor::BLACK.brighter(0.02); pub const DARK: UiColor = UiColor::BLACK.brighter(0.02);
+60 -4
View File
@@ -1,4 +1,9 @@
use openworm::net::{AddFriend, AddFriendResp, GenerateToken, RequestFriends, ServerPerms}; use openworm::net::{
AddFriend, AddFriendResp, AnswerFriendRequest, FriendRequestAction, GenerateToken,
RequestFriends, ServerPerms, UserId,
};
use crate::net::NetHandle;
use super::*; use super::*;
@@ -99,13 +104,34 @@ fn friends_list(rsc: &mut Rsc, session: &Session) -> WeakWidget {
let mut all = Span::empty(Dir::DOWN); let mut all = Span::empty(Dir::DOWN);
if !resp.incoming.is_empty() { if !resp.incoming.is_empty() {
all.push(section_label("incoming").add_strong(rsc)); all.push(section_label("incoming").add_strong(rsc));
all.push(user_list(&resp.incoming, &session, rsc).add_strong(rsc)) all.push(
resp.incoming
.rsc_map(|id, rsc| {
(
user_rect(id, &session, rsc),
accept_req(id, &con, rsc),
deny_req(id, &con, rsc),
)
.span(Dir::RIGHT)
.add(rsc)
})
.span(Dir::DOWN)
.add_strong(rsc),
)
} }
all.push(section_label("friends").add_strong(rsc)); all.push(section_label("friends").add_strong(rsc));
all.push(user_list(&resp.current, &session, rsc).add_strong(rsc)); all.push(
user_list(&resp.current, &session)
.span(Dir::DOWN)
.add_strong(rsc),
);
if !resp.outgoing.is_empty() { if !resp.outgoing.is_empty() {
all.push(section_label("outgoing").add_strong(rsc)); all.push(section_label("outgoing").add_strong(rsc));
all.push(user_list(&resp.outgoing, &session, rsc).add_strong(rsc)) all.push(
user_list(&resp.outgoing, &session)
.span(Dir::DOWN)
.add_strong(rsc),
)
} }
all.set_ptr(ptr, rsc); all.set_ptr(ptr, rsc);
}); });
@@ -113,3 +139,33 @@ fn friends_list(rsc: &mut Rsc, session: &Session) -> WeakWidget {
}); });
ptr ptr
} }
pub fn accept_req(id: UserId, con: &NetHandle, rsc: &mut Rsc) -> WeakWidget {
let button = Button::submit("󰸞", rsc);
let con = con.clone();
rsc.events.register(button, Submit, move |_, rsc| {
let con = con.clone();
rsc.tasks.spawn(async move |_| {
con.send(AnswerFriendRequest {
id,
action: FriendRequestAction::Accept,
});
});
});
button.width(60).add(rsc)
}
pub fn deny_req(id: UserId, con: &NetHandle, rsc: &mut Rsc) -> WeakWidget {
let button = Button::new("X", color::RED, rsc);
let con = con.clone();
rsc.events.register(button, Submit, move |_, rsc| {
let con = con.clone();
rsc.tasks.spawn(async move |_| {
con.send(AnswerFriendRequest {
id,
action: FriendRequestAction::Deny,
});
});
});
button.width(60).add(rsc)
}
-16
View File
@@ -152,22 +152,6 @@ pub fn section_label(text: impl Into<String>) -> TextBuilder<Rsc> {
wtext(text).size(30) wtext(text).size(30)
} }
pub fn user_list<'a>(
ids: impl IntoIterator<Item = &'a UserId>,
session: &Session,
rsc: &mut Rsc,
) -> Span {
let mut span = Span::empty(Dir::DOWN);
for id in ids {
let thing = (session.username(*id, rsc),)
.span(Dir::RIGHT)
.gap(30)
.pad(15);
span.push(thing.add_strong(rsc));
}
span
}
impl Client { impl Client {
pub fn error(&self, text: &str, rsc: &mut Rsc) { pub fn error(&self, text: &str, rsc: &mut Rsc) {
rsc[self.notif].inner = Some(werror(text, rsc)); rsc[self.notif].inner = Some(werror(text, rsc));
+15
View File
@@ -47,3 +47,18 @@ impl Session {
wid wid
} }
} }
pub fn user_list<'a>(
ids: impl IntoIterator<Item = &'a UserId>,
session: &Session,
) -> impl IntoIterator<Item = impl FnOnce(&mut Rsc) -> WeakWidget> {
ids.rsc_map(|id, rsc: &mut Rsc| user_rect(*id, session, rsc))
}
pub fn user_rect(id: UserId, session: &Session, rsc: &mut Rsc) -> WeakWidget {
(session.username(id, rsc),)
.span(Dir::RIGHT)
.gap(30)
.pad(15)
.add(rsc) as WeakWidget
}
+4
View File
@@ -164,6 +164,10 @@ impl ClientHandler {
if user.friends.outgoing.contains(&other_id) { if user.friends.outgoing.contains(&other_id) {
reply!(AddFriendResp::AlreadySent); reply!(AddFriendResp::AlreadySent);
} }
// TODO: just accept in this case?
if user.friends.incoming.contains(&other_id) {
reply!(AddFriendResp::AlreadySent);
}
if other_id == user_id { if other_id == user_id {
reply!(AddFriendResp::CannotAddSelf); reply!(AddFriendResp::CannotAddSelf);
} }