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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use gtk::{
    gio,
    glib::{self, clone},
    prelude::*,
    subclass::prelude::*,
};
use indexmap::{map::Entry, IndexMap};
use matrix_sdk::RoomMemberships;
use ruma::{events::room::power_levels::RoomPowerLevels, OwnedUserId, UserId};
use tracing::error;

use super::{Event, Member, Membership, Room};
use crate::{prelude::*, spawn, spawn_tokio, utils::LoadingState};

mod imp {
    use std::cell::{Cell, RefCell};

    use super::*;

    #[derive(Debug, Default, glib::Properties)]
    #[properties(wrapper_type = super::MemberList)]
    pub struct MemberList {
        /// The list of known members.
        pub members: RefCell<IndexMap<OwnedUserId, Member>>,
        /// The room these members belong to.
        #[property(get, set = Self::set_room, construct_only)]
        pub room: glib::WeakRef<Room>,
        /// The loading state of the list.
        #[property(get, builder(LoadingState::default()))]
        pub state: Cell<LoadingState>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for MemberList {
        const NAME: &'static str = "MemberList";
        type Type = super::MemberList;
        type Interfaces = (gio::ListModel,);
    }

    #[glib::derived_properties]
    impl ObjectImpl for MemberList {}

    impl ListModelImpl for MemberList {
        fn item_type(&self) -> glib::Type {
            Member::static_type()
        }

        fn n_items(&self) -> u32 {
            self.members.borrow().len() as u32
        }

        fn item(&self, position: u32) -> Option<glib::Object> {
            let members = self.members.borrow();

            members
                .get_index(position as usize)
                .map(|(_user_id, member)| member.clone().upcast())
        }
    }

    impl MemberList {
        /// Set the room these members belong to.
        fn set_room(&self, room: Room) {
            let obj = self.obj();

            let own_member = room.own_member();
            self.members
                .borrow_mut()
                .insert(own_member.user_id().clone(), own_member);

            if let Some(member) = room.direct_member() {
                self.members
                    .borrow_mut()
                    .insert(member.user_id().clone(), member);
            }

            self.room.set(Some(&room));
            obj.notify_room();

            spawn!(
                glib::Priority::LOW,
                clone!(@weak obj => async move {
                    obj.load().await;
                })
            );
        }
    }
}

glib::wrapper! {
    /// List of all Members in a room. Implements ListModel.
    ///
    /// Members are sorted in "insertion order", not anything useful.
    pub struct MemberList(ObjectSubclass<imp::MemberList>)
        @implements gio::ListModel;
}

impl MemberList {
    pub fn new(room: &Room) -> Self {
        glib::Object::builder::<Self>()
            .property("room", room)
            .build()
    }

    /// Set whether this list is being loaded.
    fn set_state(&self, state: LoadingState) {
        if self.state() == state {
            return;
        }

        self.imp().state.set(state);
        self.notify_state();
    }

    pub fn reload(&self) {
        self.set_state(LoadingState::Initial);

        spawn!(clone!(@weak self as obj => async move {
            obj.load().await;
        }));
    }

    /// Load this list.
    async fn load(&self) {
        let Some(room) = self.room() else {
            return;
        };
        if matches!(self.state(), LoadingState::Loading | LoadingState::Ready) {
            return;
        }

        self.set_state(LoadingState::Loading);

        let matrix_room = room.matrix_room();

        // First load what we have locally.
        let matrix_room_clone = matrix_room.clone();
        let handle = spawn_tokio!(async move {
            let mut memberships = RoomMemberships::all();
            memberships.remove(RoomMemberships::LEAVE);

            matrix_room_clone.members_no_sync(memberships).await
        });

        match handle.await.unwrap() {
            Ok(members) => {
                self.update_from_room_members(&members);

                if matrix_room.are_members_synced() {
                    // Nothing more to do, we can stop here.
                    self.set_state(LoadingState::Ready);
                    return;
                }
            }
            Err(error) => {
                error!("Could not load room members from store: {error}");
            }
        }

        // We don't have everything locally, request the rest from the server.
        let matrix_room = matrix_room.clone();
        let handle = spawn_tokio!(async move {
            let mut memberships = RoomMemberships::all();
            memberships.remove(RoomMemberships::LEAVE);

            matrix_room.members(memberships).await
        });

        // FIXME: We should retry to load the room members if the request failed
        match handle.await.unwrap() {
            Ok(members) => {
                // Add all members needed to display room events.
                self.update_from_room_members(&members);
                self.set_state(LoadingState::Ready);
            }
            Err(error) => {
                self.set_state(LoadingState::Error);
                error!(%error, "Could not load room members from server");
            }
        }
    }

    /// Updates members with the given RoomMember values.
    ///
    /// If some of the values do not correspond to existing members, new members
    /// are created.
    fn update_from_room_members(&self, new_members: &[matrix_sdk::room::RoomMember]) {
        let Some(room) = self.room() else {
            return;
        };
        let imp = self.imp();
        let mut members = imp.members.borrow_mut();
        let prev_len = members.len();
        for member in new_members {
            if let Entry::Vacant(entry) = members.entry(member.user_id().into()) {
                entry.insert(Member::new(&room, member.user_id().to_owned()));
            }
        }
        let num_members_added = members.len().saturating_sub(prev_len);

        // We can't have the mut borrow active when members are updated or items_changed
        // is emitted because that will probably cause reads of the members
        // field.
        std::mem::drop(members);

        {
            for room_member in new_members {
                let member = imp.members.borrow().get(room_member.user_id()).cloned();
                if let Some(member) = member {
                    member.update_from_room_member(room_member);
                }
            }

            // Restore the members activity according to the known timeline events.
            for item in room.timeline().items().iter::<glib::Object>().rev() {
                let Ok(item) = item else {
                    // The iterator is broken, stop.
                    break;
                };
                let Ok(event) = item.downcast::<Event>() else {
                    continue;
                };
                if !event.counts_as_unread() {
                    continue;
                }

                let member = imp.members.borrow().get(&event.sender_id()).cloned();
                if let Some(member) = member {
                    member.set_latest_activity(event.origin_server_ts_u64());
                }
            }
        }

        if num_members_added > 0 {
            // IndexMap preserves insertion order, so all the new items will be at the end.
            self.items_changed(prev_len as u32, 0, num_members_added as u32);
        }
    }

    /// Returns the member with the given ID, if it exists in the list.
    pub fn get(&self, user_id: &UserId) -> Option<Member> {
        self.imp().members.borrow().get(user_id).cloned()
    }

    /// Returns the member with the given ID.
    ///
    /// Creates a new member first if there is no member with the given ID.
    pub fn get_or_create(&self, user_id: OwnedUserId) -> Member {
        let mut members = self.imp().members.borrow_mut();
        let mut was_member_added = false;
        let prev_len = members.len();
        let member = members
            .entry(user_id)
            .or_insert_with_key(|user_id| {
                was_member_added = true;
                Member::new(&self.room().unwrap(), user_id.clone())
            })
            .clone();

        // We can't have the borrow active when items_changed is emitted because that
        // will probably cause reads of the members field.
        std::mem::drop(members);
        if was_member_added {
            // IndexMap preserves insertion order so the new member will be at the end.
            self.items_changed(prev_len as u32, 0, 1);
        }

        member
    }

    /// Update a room member with the SDK's data.
    ///
    /// Creates a new member first if there is no member matching the given
    /// event.
    pub(super) fn update_member(&self, user_id: OwnedUserId) {
        self.get_or_create(user_id).update();
    }

    /// Updates the room members' power level.
    pub(super) fn update_power_levels(&self, power_levels: &RoomPowerLevels) {
        // We need to go through the whole list because we don't know who was
        // added/removed.
        for (user_id, member) in &*self.imp().members.borrow() {
            member.set_power_level(power_levels.for_user(user_id).into());
        }
    }

    /// Returns the Membership of a given UserId.
    ///
    /// If the user has no Membership, Membership::Leave will be returned
    pub fn get_membership(&self, user_id: &UserId) -> Membership {
        self.imp()
            .members
            .borrow()
            .get(user_id)
            .map_or(Membership::Leave, |member| member.membership())
    }
}