Skip to main content

fractal/session_view/create_direct_chat_dialog/
user.rs

1use gtk::{glib, prelude::*, subclass::prelude::*};
2use matrix_sdk::ruma::{OwnedMxcUri, OwnedUserId};
3
4use crate::{
5    components::PillSource,
6    prelude::*,
7    session::{Room, Session, User},
8};
9
10mod imp {
11    use super::*;
12
13    #[derive(Debug, Default, glib::Properties)]
14    #[properties(wrapper_type = super::DirectChatUser)]
15    pub struct DirectChatUser {
16        /// The direct chat with this user, if any.
17        #[property(get, set = Self::set_direct_chat, explicit_notify, nullable)]
18        direct_chat: glib::WeakRef<Room>,
19    }
20
21    #[glib::object_subclass]
22    impl ObjectSubclass for DirectChatUser {
23        const NAME: &'static str = "DirectChatUser";
24        type Type = super::DirectChatUser;
25        type ParentType = User;
26    }
27
28    #[glib::derived_properties]
29    impl ObjectImpl for DirectChatUser {}
30
31    impl PillSourceImpl for DirectChatUser {
32        fn identifier(&self) -> String {
33            self.obj().upcast_ref::<User>().user_id_string()
34        }
35    }
36
37    impl DirectChatUser {
38        /// Set the direct chat with this user.
39        fn set_direct_chat(&self, direct_chat: Option<&Room>) {
40            if self.direct_chat.upgrade().as_ref() == direct_chat {
41                return;
42            }
43
44            self.direct_chat.set(direct_chat);
45            self.obj().notify_direct_chat();
46        }
47    }
48}
49
50glib::wrapper! {
51    /// A User in the context of creating a direct chat.
52    pub struct DirectChatUser(ObjectSubclass<imp::DirectChatUser>)
53        @extends PillSource, User;
54}
55
56impl DirectChatUser {
57    pub fn new(
58        session: &Session,
59        user_id: OwnedUserId,
60        display_name: Option<&str>,
61        avatar_url: Option<OwnedMxcUri>,
62    ) -> Self {
63        let display_name = display_name.unwrap_or_else(|| user_id.localpart());
64
65        let obj: Self = glib::Object::builder()
66            .property("session", session)
67            .property("display-name", display_name)
68            .build();
69
70        let user = obj.upcast_ref::<User>();
71        user.set_avatar_url(avatar_url);
72        user.imp().set_user_id(user_id);
73        obj.set_direct_chat(user.direct_chat());
74
75        obj
76    }
77}