2022-05-20 15:33:09 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-05-13 00:24:00 +00:00
|
|
|
use crate::notifications::render_user_notification;
|
2022-05-20 15:33:09 +00:00
|
|
|
use client::{ContactEventKind, User, UserStore};
|
2022-05-11 18:39:01 +00:00
|
|
|
use gpui::{
|
2023-04-12 00:21:56 +00:00
|
|
|
elements::*, impl_internal_actions, AppContext, Entity, ModelHandle, View, ViewContext,
|
2022-05-11 18:39:01 +00:00
|
|
|
};
|
2022-12-04 00:03:46 +00:00
|
|
|
use workspace::notifications::Notification;
|
2022-05-11 18:39:01 +00:00
|
|
|
|
|
|
|
impl_internal_actions!(contact_notifications, [Dismiss, RespondToContactRequest]);
|
|
|
|
|
2023-04-06 21:49:03 +00:00
|
|
|
pub fn init(cx: &mut AppContext) {
|
2022-05-11 18:39:01 +00:00
|
|
|
cx.add_action(ContactNotification::dismiss);
|
|
|
|
cx.add_action(ContactNotification::respond_to_contact_request);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ContactNotification {
|
|
|
|
user_store: ModelHandle<UserStore>,
|
2022-05-20 15:33:09 +00:00
|
|
|
user: Arc<User>,
|
|
|
|
kind: client::ContactEventKind,
|
2022-05-11 18:39:01 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 07:18:44 +00:00
|
|
|
#[derive(Clone, PartialEq)]
|
2022-05-11 18:39:01 +00:00
|
|
|
struct Dismiss(u64);
|
|
|
|
|
2022-06-06 07:18:44 +00:00
|
|
|
#[derive(Clone, PartialEq)]
|
2022-05-11 18:39:01 +00:00
|
|
|
pub struct RespondToContactRequest {
|
|
|
|
pub user_id: u64,
|
|
|
|
pub accept: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Event {
|
|
|
|
Dismiss,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Entity for ContactNotification {
|
|
|
|
type Event = Event;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl View for ContactNotification {
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
"ContactNotification"
|
|
|
|
}
|
|
|
|
|
2023-04-12 18:10:43 +00:00
|
|
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> Element<Self> {
|
2022-05-20 15:33:09 +00:00
|
|
|
match self.kind {
|
2022-05-13 00:24:00 +00:00
|
|
|
ContactEventKind::Requested => render_user_notification(
|
2022-05-20 15:33:09 +00:00
|
|
|
self.user.clone(),
|
2022-05-13 00:24:00 +00:00
|
|
|
"wants to add you as a contact",
|
2023-01-25 17:46:51 +00:00
|
|
|
Some("They won't be alerted if you decline."),
|
2022-10-06 12:00:14 +00:00
|
|
|
Dismiss(self.user.id),
|
2022-05-13 00:24:00 +00:00
|
|
|
vec![
|
|
|
|
(
|
|
|
|
"Decline",
|
|
|
|
Box::new(RespondToContactRequest {
|
2022-05-20 15:33:09 +00:00
|
|
|
user_id: self.user.id,
|
2022-05-13 00:24:00 +00:00
|
|
|
accept: false,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Accept",
|
|
|
|
Box::new(RespondToContactRequest {
|
2022-05-20 15:33:09 +00:00
|
|
|
user_id: self.user.id,
|
2022-05-13 00:24:00 +00:00
|
|
|
accept: true,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
cx,
|
|
|
|
),
|
|
|
|
ContactEventKind::Accepted => render_user_notification(
|
2022-05-20 15:33:09 +00:00
|
|
|
self.user.clone(),
|
2022-05-13 00:24:00 +00:00
|
|
|
"accepted your contact request",
|
2022-05-16 13:37:29 +00:00
|
|
|
None,
|
2022-05-20 15:33:09 +00:00
|
|
|
Dismiss(self.user.id),
|
2022-05-13 00:24:00 +00:00
|
|
|
vec![],
|
|
|
|
cx,
|
|
|
|
),
|
2022-05-11 18:39:01 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Notification for ContactNotification {
|
|
|
|
fn should_dismiss_notification_on_event(&self, event: &<Self as Entity>::Event) -> bool {
|
|
|
|
matches!(event, Event::Dismiss)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ContactNotification {
|
|
|
|
pub fn new(
|
2022-05-20 15:33:09 +00:00
|
|
|
user: Arc<User>,
|
|
|
|
kind: client::ContactEventKind,
|
2022-05-11 18:39:01 +00:00
|
|
|
user_store: ModelHandle<UserStore>,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) -> Self {
|
|
|
|
cx.subscribe(&user_store, move |this, _, event, cx| {
|
2022-05-20 15:33:09 +00:00
|
|
|
if let client::Event::Contact {
|
2022-05-11 18:39:01 +00:00
|
|
|
kind: ContactEventKind::Cancelled,
|
|
|
|
user,
|
|
|
|
} = event
|
|
|
|
{
|
2022-05-20 15:33:09 +00:00
|
|
|
if user.id == this.user.id {
|
2022-05-11 18:39:01 +00:00
|
|
|
cx.emit(Event::Dismiss);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.detach();
|
|
|
|
|
2022-05-20 15:33:09 +00:00
|
|
|
Self {
|
|
|
|
user,
|
|
|
|
kind,
|
|
|
|
user_store,
|
|
|
|
}
|
2022-05-11 18:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn dismiss(&mut self, _: &Dismiss, cx: &mut ViewContext<Self>) {
|
|
|
|
self.user_store.update(cx, |store, cx| {
|
|
|
|
store
|
2022-05-20 15:33:09 +00:00
|
|
|
.dismiss_contact_request(self.user.id, cx)
|
2022-05-11 18:39:01 +00:00
|
|
|
.detach_and_log_err(cx);
|
|
|
|
});
|
|
|
|
cx.emit(Event::Dismiss);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn respond_to_contact_request(
|
|
|
|
&mut self,
|
|
|
|
action: &RespondToContactRequest,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) {
|
|
|
|
self.user_store
|
|
|
|
.update(cx, |store, cx| {
|
|
|
|
store.respond_to_contact_request(action.user_id, action.accept, cx)
|
|
|
|
})
|
|
|
|
.detach();
|
|
|
|
}
|
|
|
|
}
|