aboutsummaryrefslogblamecommitdiffstats
path: root/old_code.md
blob: acf7bfab44b0358292cac0ac0843f14c907b2c60 (plain) (tree)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460











































































































































































































































































































































































































































































                                                                                                                                                                                                                                                                                             
// pub struct State<'a> {
//     // open_chat: Option<OpenChat<'a>>,
//     chats: Vec<BorrowedChat<'a>>,
//     roster: Vec<BorrowedContact<'a>>,
// }

// impl Macaw {
//     pub fn state(&self) -> State {
//         let chats = self
//             .chats
//             .iter()
//             .map(|(_, chat)| {
//                 let user = chat.user.as_ref().borrow();
//                 let contact = self.roster.get(&user.inner.jid);
//                 let latest_message = match &chat.message {
//                     Some(the_message) => {
//                         let message: Ref<'_, OwnedMessage> = the_message.as_ref().borrow();
//                         let user = {
//                             let user = message.user.as_ref().borrow();
//                             let contact = self.roster.get(&user.inner.jid);

//                             BorrowedUser {
//                                 inner: user,
//                                 contact,
//                             }
//                         };
//                         Some(BorrowedMessage {
//                             inner: message,
//                             user,
//                         })
//                     }
//                     None => None,
//                 };
//                 BorrowedChat {
//                     inner: &chat,
//                     user: BorrowedUser {
//                         inner: user,
//                         contact,
//                     },
//                     latest_message,
//                 }
//             })
//             .collect();
//         State {
//             // open_chat: todo!(),
//             chats,
//             roster: todo!(),
//         }
//     }
// }

// pub enum Node<'a> {
//     User(BorrowedUser<'a>),
//     Message(BorrowedMessage<'a>),
//     Contact(BorrowedContact<'a>),
//     Chat(BorrowedChat<'a>),
//     OpenChat(OpenChat<'a>),
// }

// pub enum Orphan {
//     User(JID),
//     Message(Uuid),
// }

// pub struct BorrowedUser<'a> {
//     inner: Ref<'a, OwnedUser>,
//     // inner: &'a OwnedUser,
//     contact: Option<&'a OwnedContact>,
// }

// pub struct BorrowedMessage<'a> {
//     inner: Ref<'a, OwnedMessage>,
//     // user: Rc<RefCell<User>>,
//     user: BorrowedUser<'a>,
// }

// pub struct BorrowedContact<'a> {
//     inner: &'a OwnedContact,
//     user: BorrowedUser<'a>,
// }

// pub struct BorrowedChat<'a> {
//     inner: &'a OwnedChat,
//     // user: Rc<RefCell<OwnedUser>>,
//     user: BorrowedUser<'a>,
//     // latest_message: Option<Rc<RefCell<OwnedMessage>>>,
//     latest_message: Option<BorrowedMessage<'a>>,
// }

// pub struct OpenChat<'a> {
//     chat: BorrowedChat<'a>,
//     messages: Vec<BorrowedMessage<'a>>,
// }

// there are two states: an editing state and a reading state.
// because the state is self-referential, each time there is a change, the state needs to be recalculated.
// pub struct State<'a> {
//     // open_chat: Option<OpenChat<'a>>,
//     chats: Vec<BorrowedChat<'a>>,
//     roster: Vec<BorrowedContact<'a>>,
// }

// pub struct AState {
//     open_chat: Option<OwnedOpenChat>,
//     chats: HashMap<JID, (Chat, (User, Option<Contact>), Option<ChatMessage>)>,
//     roster: HashMap<JID, (Contact, User)>,
// }

// pub struct OwnedOpenChat {
//     chat: (Chat, (User, Option<Contact>)),
//     messages: IndexMap<Uuid, (ChatMessage, (User, Option<Contact>))>,
// }

// pub struct MacawUser {
//     inner: User,
//     contact: Option<Contact>,
// }

// impl Deref for MacawUser {

// }

// other option is forget about all this bullshit and just build the relations graph again after each update?
// impl Macaw {
//     pub async fn insert_contact(
//         &mut self,
//         contact: Contact,
//     ) -> Result<(), CommandError<DatabaseError>> {
//         let user_jid = contact.user_jid.clone();
//         if !self.users.contains_key(&user_jid) {
//             // TODO: all methods only on logged-in client
//             match &self.client {
//                 Account::LoggedIn(client) => {
//                     let user = client.get_user(user_jid.clone()).await?;
//                     self.users.insert(user_jid, user);
//                 }
//                 Account::LoggedOut(login_modal) => todo!(),
//             }
//         } else {
//             // up the dependency count
//         }
//         self.roster.insert(contact.user_jid.clone(), contact);
//         Ok(())
//     }

//     pub fn get_contact(&self, jid: &JID) -> Option<(&Contact, &User)> {}

//     pub fn get_contact_mut(&mut self, jid: &JID) -> Option<&mut Contact> {}

//     pub fn remove_contact(&mut self, jid: JID) -> Option<Contact> {
//         self.roster.
//     }

//     // updating is fine to do directly on any part of the store.
//     // if a contact is retrieved, it is guaranteed that there is a user in the store, because when the contact was inserted, a user was added to the user store, and the userstore is a droppable store that does not allow you to remove items if there are other things depending on it.
// }

// pub struct MacawContact<'a> {
//     inner: Contact,
//     user: &'a User,
// }

// messages must be tombstoned because of this
// items are never directly added to or removed from a droppablestore
// remove can only be called from a store that called insert
// if a store calls insert it must call remove if something is removed from that store
pub struct DroppableStore<K: Clone, V: Clone>(HashMap<K, (usize, V)>);

impl<K: Clone, V: Clone> DroppableStore<K, V> {
    pub fn capacity(&self) -> usize {
        self.0.capacity()
    }

    pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
    where
        Q: std::hash::Hash + Eq,
        K: Borrow<Q> + std::hash::Hash + Eq,
    {
        self.0.contains_key(k)
    }

    pub fn get<Q>(&self, k: &Q) -> Option<&V>
    where
        Q: std::hash::Hash + Eq,
        K: Borrow<Q> + std::hash::Hash + Eq,
    {
        self.0.get(k).map(|(_, v)| v)
    }

    pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
    where
        Q: std::hash::Hash + Eq + ?Sized,
        K: Borrow<Q> + std::hash::Hash + Eq,
    {
        self.0.get_key_value(k).map(|(k, (_, v))| (k, v))
    }

    pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
    where
        Q: std::hash::Hash + Eq + ?Sized,
        K: Borrow<Q> + std::hash::Hash + Eq,
    {
        self.0.get_mut(k).map(|(_, v)| v)
    }

    pub fn insert(&mut self, k: K, v: V) -> Option<V>
    where
        K: std::hash::Hash + Eq,
    {
        if let Some((count, old_v)) = self.0.get_mut(&k) {
            *count += 1;
            let output = std::mem::replace(old_v, v);
            Some(output)
        } else {
            self.0.insert(k, (1, v));
            None
        }
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn iter(&self) -> impl Iterator<Item = (&K, &V)> {
        self.0.iter().map(|(k, (_, v))| (k, v))
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)> {
        self.0.iter_mut().map(|(k, (_, v))| (k, v))
    }

    pub fn keys(&self) -> impl Iterator<Item = &K> {
        self.0.keys()
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn new() -> Self {
        Self(HashMap::new())
    }

    pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
    where
        K: Borrow<Q> + std::hash::Hash + Eq,
        Q: std::hash::Hash + Eq + ?Sized,
    {
        if let Some((count, old_v)) = self.0.get_mut(&k) {
            if *count == 1 {
                self.0.remove(&k).map(|(_, v)| v)
            } else {
                *count = *count - 1;
                Some(old_v.clone())
            }
        } else {
            None
        }
    }

    pub fn remove_entry<Q>(&mut self, k: &K) -> Option<(K, V)>
    where
        K: std::hash::Hash + Eq,
    {
        if let Some((count, old_v)) = self.0.get_mut(&k) {
            if *count == 1 {
                self.0.remove_entry(&k).map(|(k, (_, v))| (k, v))
            } else {
                *count = *count - 1;
                Some((k.clone(), old_v.clone()))
            }
        } else {
            None
        }
    }

    pub fn reserve(&mut self, additional: usize)
    where
        K: std::hash::Hash + Eq,
    {
        self.0.reserve(additional)
    }

    pub fn shrink_to(&mut self, min_capacity: usize)
    where
        K: std::hash::Hash + Eq,
    {
        self.0.shrink_to(min_capacity)
    }

    pub fn shrink_to_fit(&mut self)
    where
        K: std::hash::Hash + Eq,
    {
        self.0.shrink_to_fit()
    }

    pub fn try_reserve(
        &mut self,
        additional: usize,
    ) -> Result<(), std::collections::TryReserveError>
    where
        K: std::hash::Hash + Eq,
    {
        self.0.try_reserve(additional)
    }

    pub fn values(&self) -> impl Iterator<Item = &V> {
        self.0.values().map(|(_, v)| v)
    }

    pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> {
        self.0.values_mut().map(|(_, v)| v)
    }

    pub fn with_capacity(capacity: usize) -> Self {
        Self(HashMap::with_capacity(capacity))
    }
}

// #[derive(Debug, Clone)]
// pub struct MacawChat {
//     inner: Arc<Mutex<Chat>>,
//     jid: JID,
//     latest_message: Option<MacawChatMessage>,
//     store: MacawChatStore,
// }

// impl Drop for MacawChat {
//     fn drop(&mut self) {
//         let mut store = match self.store.0.lock() {
//             Ok(s) => s,
//             Err(s) => s.into_inner(),
//         };
//         if Arc::<Mutex<Chat>>::strong_count(&self.inner) == 2 {
//             store.shift_remove(&self.jid);
//         }
//     }
// }

// #[derive(Debug, Clone)]
// pub struct MacawChatStore(Arc<Mutex<IndexMap<JID, MacawChat>>>);

// #[derive(Debug, Clone)]
// pub struct MacawChatMessage {
//     inner: Arc<Mutex<ChatMessage>>,
//     id: Uuid,
//     user: BorrowedUser,
//     store: MacawChatMessageStore,
// }

// impl Drop for MacawChatMessage {
//     fn drop(&mut self) {
//         let mut store = match self.store.0.lock() {
//             Ok(s) => s,
//             Err(s) => s.into_inner(),
//         };
//         if Rc::<ChatMessage>::strong_count(&self.inner) == 1 {
//             store.shift_remove(&self.id)
//         }
//     }
// }

// #[derive(Debug, Clone)]
// pub struct MacawChatMessageStore(HashMap<Uuid, MacawChatMessage>);

// impl Deref for MacawChatMessageStore {
//     type Target = HashMap<Uuid, (ChatMessage, BorrowedUser)>;

//     fn deref(&self) -> &Self::Target {
//         &self.0
//     }
// }

// impl DerefMut for MacawChatMessageStore {
//     fn deref_mut(&mut self) -> &mut Self::Target {
//         &mut self.0
//     }
// }

// #[derive(Debug, Clone)]
// pub struct BorrowedUser {
//     inner: Rc<User>,
//     contact: Option<Box<BorrowedContact>>,
//     store: Rc<RefCell<MacawUserStore>>,
// }

// impl Drop for BorrowedUser {
//     fn drop(&mut self) {
//         if Rc::<User>::strong_count(&self.inner) == 1 {
//             self.store.borrow_mut().remove(&self.inner.jid);
//         }
//     }
// }

// impl Deref for BorrowedUser {
//     type Target = User;

//     fn deref(&self) -> &Self::Target {
//         self.inner.as_ref()
//     }
// }

// #[derive(Debug)]
// pub struct MacawUserStore(HashMap<JID, (User, Option<BorrowedContact>)>);

// impl Deref for MacawUserStore {
//     type Target = HashMap<JID, (User, Option<BorrowedContact>)>;

//     fn deref(&self) -> &Self::Target {
//         &self.0
//     }
// }

// impl DerefMut for MacawUserStore {
//     fn deref_mut(&mut self) -> &mut Self::Target {
//         &mut self.0
//     }
// }

// #[derive(Debug, Clone)]
// pub struct BorrowedContact {
//     inner: Rc<Contact>,
//     user: Box<BorrowedUser>,
//     store: Rc<RefCell<MacawRosterStore>>,
// }

// impl Drop for BorrowedContact {
//     fn drop(&mut self) {
//         if Rc::<Contact>::strong_count(&self.inner) == 1 {
//             self.store.borrow_mut().remove(&self.inner.user_jid);
//         }
//     }
// }

// impl Deref for BorrowedContact {
//     type Target = Contact;

//     fn deref(&self) -> &Self::Target {
//         self.inner.as_ref()
//     }
// }

// #[derive(Debug)]
// pub struct MacawRosterStore(HashMap<JID, (Contact, BorrowedUser)>);

// impl Deref for MacawRosterStore {
//     type Target = HashMap<JID, (Contact, BorrowedUser)>;

//     fn deref(&self) -> &Self::Target {
//         &self.0
//     }
// }

// impl DerefMut for MacawRosterStore {
//     fn deref_mut(&mut self) -> &mut Self::Target {
//         &mut self.0
//     }
// }