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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
use std::{mem, sync::Arc};

use tokio::sync::RwLock;

use crate::{
    auth::{Auth, AuthQuery, IDToken, Service},
    channel::{Channel, Message},
    check_name_validity, check_permission,
    error::Error,
    hub::{Hub, HubMember},
    new_id,
    permission::{ChannelPermission, HubPermission, PermissionSetting},
    user::{GenericUser, User},
    Result, ID,
};

/// Start the OAuth login process. Returns a redirect to the given OAuth service's page with the correct parameters.
///
/// # Arguments
///
/// * `auth_manager` - The Authentication manager for the current server instance, wrapped in Arc<Lock>> so that it can be used by multiple threads.
/// * `service` - The OAuth service to use for this login attempt.
pub async fn start_login(auth_manager: Arc<RwLock<Auth>>, service: Service) -> String {
    Auth::start_login(auth_manager, service).await
}

/// Completes the OAuth login request.
///
/// # Arguments
///
/// * `auth_manager` - The Authentication manager for the current server instance, wrapped in Arc<RwLock>> so that it can be used by multiple threads.
/// * `service` - The OAuth service used in the [`start_login`] step.
/// * `query` - The OAuth query containing the `state` string and the OAuth `code` as well as an optional expiry time.
///
/// # Errors
///
/// This function may return an error for any of the reasons outlined in [`Auth::handle_oauth`].
pub async fn complete_login(
    auth_manager: Arc<RwLock<Auth>>,
    service: Service,
    query: AuthQuery,
) -> Result<IDToken> {
    Auth::handle_oauth(auth_manager, service, query).await
}

/// Invalidates all of a user's authentication token sessions.
///
/// # Arguments
///
/// * `auth_manager` - The Authentication manager for the current server instance, wrapped in Arc<RwLock>> so that it can be used by multiple threads.
/// * `user_id` - ID of the user whose tokens should be invalidated.
pub async fn invalidate_tokens(auth_manager: Arc<RwLock<Auth>>, user_id: ID) {
    Auth::invalidate_tokens(auth_manager, user_id).await
}

/// Gets a user's data while removing all of their private information.
///
/// # Arguments
///
/// * `id` - The id of the user whose data is being requested.
/// * `user_id` - ID of the user who is requesting the data.
///
/// # Errors
///
/// This function may return an error if the user's data cannot be loaded for any of the reasons outlined in [`User::load`].
pub async fn get_user_stripped(user_id: &ID, id: ID) -> Result<GenericUser> {
    User::load(&id)
        .await
        .map(|u| User::to_generic(&u, user_id))
        .map_err(|_| Error::UserNotFound)
}

/// Changes a user's username.
/// Returns the user's previous username if successful.
///
/// # Arguments
///
/// * `user` - User whose name is to be changed.
/// * `new_name` - New username for the user.
///
/// # Errors
///
/// This function may return an error for any of the following reasons.
///
/// * The modified user data could not be saved for any of the reasons outlined in [`User::save`].
/// * The user's name could not be changed for any of the reasons outlined in [`User::change_username`].
pub async fn change_username<S: Into<String> + Clone>(user_id: &ID, new_name: S) -> Result<String> {
    let name: String = new_name.into();
    check_name_validity(&name)?;
    let mut user = User::load(user_id).await?;
    let old_name = mem::replace(&mut user.username, name);
    user.save().await?;
    Ok(old_name)
}

/// Changes a user's status.
/// Returns the user's previous status if successful.
///
/// # Arguments
///
/// * `user` - User whose name is to be changed.
/// * `new_status` - New status for the user.
///
/// # Errors
///
/// This function may return an error for any of the following reasons.
///
/// * The modified user data could not be saved for any of the reasons outlined in [`User::save`].
/// * The new status was bigger than [`crate::MAX_STATUS_SIZE`].
pub async fn change_user_status<S: Into<String> + Clone>(
    user_id: &ID,
    new_status: S,
) -> Result<String> {
    let status: String = new_status.into();
    if status.as_bytes().len() > crate::MAX_STATUS_SIZE {
        Err(Error::TooBig)
    } else {
        let mut user = User::load(user_id).await?;
        let old_status = mem::replace(&mut user.status, status);
        user.save().await?;
        Ok(old_status)
    }
}

/// Changes a user's description.
/// Returns the user's previous description if successful.
///
/// # Arguments
///
/// * `user` - User whose name is to be changed.
/// * `new_description` - New description for the user.
///
/// # Errors
///
/// This function may return an error for any of the following reasons.
///
/// * The modified user data could not be saved for any of the reasons outlined in [`User::save`].
/// * The new status was bigger than [`crate::MAX_DESCRIPTION_SIZE`].
pub async fn change_user_description<S: Into<String> + Clone>(
    user_id: &ID,
    new_description: S,
) -> Result<String> {
    let description: String = new_description.into();
    if description.as_bytes().len() > crate::MAX_DESCRIPTION_SIZE {
        Err(Error::TooBig)
    } else {
        let mut user = User::load(user_id).await?;
        let old_description = mem::replace(&mut user.description, description);
        user.save().await?;
        Ok(old_description)
    }
}

/// Creates a hub, returning the ID of the new hub if successful.
/// Also adds a default channel named "chat" that all users have access to by default.
///
/// # Arguments
///
/// * `owner_id` - ID of the user who should be marked as the owner/creator of the hub.
/// * `name` - The name of the new hub.
///
/// # Errors
///
/// This function may return an error for any of the reasons outlined in the following functions:
///
/// * The user's data could not be saved for any of the reasons outlined in [`User::save`].
/// * The hub failed to save for any of the reasons outlined in [`Hub::save`].
/// * The given name failed to pass the checks for any of the reasons outlined in [`check_name_validity`].
/// * The default channel could not be created for any of the reaons outlined in [`Hub::new_channel`].
pub async fn create_hub(owner_id: &ID, name: String) -> Result<ID> {
    check_name_validity(&name)?;
    let mut id = new_id();
    while Hub::load(&id).await.is_ok() {
        id = new_id();
    }
    let mut owner = User::load(owner_id).await?;
    let mut new_hub = Hub::new(name, id.clone(), &owner);
    let channel_id = new_hub.new_channel(owner_id, "chat".to_string()).await?;
    if let Some(group) = new_hub.groups.get_mut(&new_hub.default_group) {
        group.set_channel_permission(
            channel_id.clone(),
            crate::permission::ChannelPermission::ViewChannel,
            Some(true),
        );
        group.set_channel_permission(
            channel_id.clone(),
            crate::permission::ChannelPermission::SendMessage,
            Some(true),
        );
    }
    owner.in_hubs.push(id.clone());
    owner.save().await?;
    new_hub.save().await?;
    Ok(id)
}

/// Gets a hub stripped of data the given user should not be able to see.
///
/// # Arguments
///
/// * `user_id` - ID of the user to check the visiblity of objects for.
/// * `hub_id` - ID of the hub to get.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * The user is not in the hub.
/// * The hub failed to load for any of the reasons outlined in [`Hub::load`].
pub async fn get_hub(user_id: &ID, hub_id: &ID) -> Result<Hub> {
    let hub = Hub::load(hub_id).await?;
    hub.strip(user_id)
}

/// Deletes a hub.
///
/// # Arguments
///
/// * `user_id` - ID of the user to check for permission to perform the operation.
/// * `hub_id` - ID of the hub to delete.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * The user is not in the hub.
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
/// * The user does not have permission to delete the hub.
/// * The hub's data files could not be deleted.
pub async fn delete_hub(user_id: &ID, hub_id: &ID) -> Result<()> {
    let hub = Hub::load(hub_id).await?;
    let member = hub.get_member(user_id)?;
    check_permission!(member, HubPermission::All, hub);
    tokio::fs::remove_file(hub.get_info_path()).await?;
    tokio::fs::remove_dir_all(hub.get_data_path()).await?;
    Ok(())
}

/// Changes the name of a hub.
///
/// # Arguments
///
/// * `user_id` - ID of the user to check for permission to perform the operation.
/// * `hub_id` - The ID of the hub whose name is to be changed.
/// * `new_name` - The new name to be given to the hub.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * THe user is not in the hub.
/// * The user does not have permission to rename the hub.
/// * The given name failed to pass the checks for any of the reasons outlined in [`check_name_validity`].
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
/// * The hub could not be saved for any of the reasons outlined by [`Hub::save`].
pub async fn rename_hub<S: Into<String> + Clone>(
    user_id: &ID,
    hub_id: &ID,
    new_name: S,
) -> Result<String> {
    let new_name: String = new_name.into();
    check_name_validity(&new_name)?;
    let mut hub = Hub::load(hub_id).await?;
    let member = hub.get_member(user_id)?;
    check_permission!(member, HubPermission::Administrate, hub);
    let old_name = mem::replace(&mut hub.name, new_name);
    hub.save().await?;
    Ok(old_name)
}

/// Changes the description of a hub.
///
/// # Arguments
///
/// * `user_id` - ID of the user to check for permission to perform the operation.
/// * `hub_id` - The ID of the hub whose name is to be changed.
/// * `new_description` - The content for the hub's description
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * THe user is not in the hub.
/// * The user does not have permission to change the hub's description.
/// * The given description is bigger than [`crate::MAX_DESCRIPTION_SIZE`].
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
/// * The hub could not be saved for any of the reasons outlined by [`Hub::save`].
pub async fn change_hub_description<S: Into<String> + Clone>(
    user_id: &ID,
    hub_id: &ID,
    new_description: S,
) -> Result<String> {
    let new_description: String = new_description.into();
    if new_description.as_bytes().len() > crate::MAX_DESCRIPTION_SIZE {
        Err(Error::TooBig)
    } else {
        let mut hub = Hub::load(hub_id).await?;
        let member = hub.get_member(user_id)?;
        check_permission!(member, HubPermission::Administrate, hub);
        let old_name = mem::replace(&mut hub.description, new_description);
        hub.save().await?;
        Ok(old_name)
    }
}

/// Changes a user's nickname in a hub.
///
/// # Arguments
///
/// * `user_id` - ID of the user whose nickname is to be changed.
/// * `hub_id` - ID of the hub where the new nickname should be applied.
/// * `new_name` - New nickname to be used.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * THe user is not in the hub.
/// * The new name failed to pass the checks for any of the reasons outlined in [`check_name_validity`].
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
/// * The hub could not be saved for any of the reasons outlined by [`Hub::save`].
pub async fn change_nickname<S: Into<String> + Clone>(
    user_id: &ID,
    hub_id: &ID,
    new_name: S,
) -> Result<String> {
    let new_name: String = new_name.into();
    check_name_validity(&new_name)?;
    let mut hub = Hub::load(hub_id).await?;
    let member = hub.get_member_mut(user_id)?;
    let old_name = mem::replace(&mut member.nickname, new_name);
    hub.save().await?;
    Ok(old_name)
}

/// Checks if a user is banned from a hub.
/// Returns `true` if they are and `false` if they aren't.
///
/// # Arguments
///
/// * `actor_id` - ID of the user who is checking.
/// * `hub_id` - The hub in which to check the ban status.
/// * `user_id` - The user whose ban status is to be checked.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * The user who is checking is not in the hub.
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
pub async fn user_banned(actor_id: &ID, hub_id: &ID, user_id: &ID) -> Result<bool> {
    let hub = Hub::load(hub_id).await?;
    if hub.members.contains_key(actor_id) {
        Ok(hub.bans.contains(user_id))
    } else {
        Err(Error::NotInHub)
    }
}

/// Checks if a user is muted in a hub.
/// Returns `true` if they are and `false` if they aren't.
///
/// # Arguments
///
/// * `actor_id` - ID of the user who is checking.
/// * `hub_id` - The hub in which to check the mute status.
/// * `user_id` - The user whose mute status is to be checked.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * The user who is checking is not in the hub.
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
pub async fn user_muted(actor_id: &ID, hub_id: &ID, user_id: &ID) -> Result<bool> {
    let hub = Hub::load(hub_id).await?;
    if hub.members.contains_key(actor_id) {
        Ok(hub.mutes.contains(user_id))
    } else {
        Err(Error::NotInHub)
    }
}

/// Gets the information on a member of a hub.
///
/// # Arguments
///
/// * `actor_id` - ID of the user who is requesting the information.
/// * `hub_id` - Hub from which to get the information.
/// * `user_id` - ID of the user whose information is being requested.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * The requesting user is not in the hub.
/// * The user whose information is being requested is not in the hub.
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
pub async fn get_hub_member(actor_id: &ID, hub_id: &ID, user_id: &ID) -> Result<HubMember> {
    let hub = Hub::load(hub_id).await?;
    let member = hub.get_member(actor_id)?;
    if actor_id == user_id {
        return Ok(member);
    } else {
        hub.get_member(user_id)
    }
}

/// Adds the given user to a hub.
///
/// # Arguments
///
/// * `user_id` - ID of the user to add to the hub.
/// * `hub_id` - ID of the hub the user is to be added to.
///
/// # Errors
///
/// * The user could not be added to the hub for any of the reasons outlined by [`User::join_hub`].
/// * The hub could not be saved for any of the reasons outlined by [`Hub::save`].
pub async fn join_hub(user_id: &ID, hub_id: &ID) -> Result<()> {
    let mut user = User::load(user_id).await?;
    let mut hub = Hub::load(hub_id).await?;
    user.join_hub(&mut hub).await?;
    hub.save().await?;
    user.save().await
}

/// Removes the given user from a hub.
///
/// # Arguments
///
/// * `user_id` - ID of the user to remove from the hub.
/// * `hub_id` - ID of the hub the user is to be removed from.
///
/// # Errors
///
/// * The user could not be removed from the hub for any of the reasons outlined by [`User::leave_hub`].
/// * The hub could not be saved for any of the reasons outlined by [`Hub::save`].
pub async fn leave_hub(user_id: &ID, hub_id: &ID) -> Result<()> {
    let mut user = User::load(user_id).await?;
    user.remove_hub(hub_id)?;
    let mut hub = Hub::load(hub_id).await?;
    hub.user_leave(&user)?;
    hub.save().await?;
    user.save().await
}

async fn hub_user_op(actor_id: &ID, hub_id: &ID, user_id: &ID, op: HubPermission) -> Result<()> {
    let mut hub = Hub::load(hub_id).await?;
    let member = hub.get_member(actor_id)?;
    check_permission!(member, op, hub);
    match op {
        HubPermission::Kick => hub.kick_user(user_id).await?,
        HubPermission::Ban => hub.ban_user(user_id.clone()).await?,
        HubPermission::Unban => hub.unban_user(user_id),
        HubPermission::Mute => hub.mute_user(user_id.clone()),
        HubPermission::Unmute => hub.unmute_user(user_id),
        _ => return Err(Error::UnexpectedServerArg),
    }
    hub.save().await
}

macro_rules! action_fns {
  ($($(#[$attr:meta])* => ($fnName:ident, $variant:ident)),*) => {
    $(
      $(#[$attr])*
      pub async fn $fnName(actor_id: &ID, hub_id: &ID, user_id: &ID) -> Result<()> {
          hub_user_op(actor_id, hub_id, user_id, HubPermission::$variant).await
      }
    )*
  }
}
action_fns! {
/// Kicks a user from a hub.
///
/// # Arguments
///
/// * `actor_id` - ID of the user who is doing the kicking.
/// * `hub_id` - Hub from which the user is being kicked.
/// * `user_id` - ID of the user who is to be kicked.
///
/// # Errors
///
/// This function may fail for any of the following reasons:
///
/// * The user doing the kicking is not in the hub
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
/// * The user to be kicked is not in the hub.
/// * The user doing the kicking does not have permission to kick other users.
/// * The kick failed for any of the reasons outlined by [`Hub::kick_user`].
=> (kick_user, Kick),
/// Bans a user from a hub.
///
/// # Arguments
///
/// * `actor_id` - ID of the user who is performing the ban.
/// * `hub_id` - Hub from which the user is being banned.
/// * `user_id` - ID of the user who is to be banned.
///
/// # Errors
///
/// This function may fail for any of the following reasons:
///
/// * The user performing the ban is not in the hub
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
/// * The user performing the ban does not have permission to ban other users.
/// * The ban failed for any of the reasons outlined by [`Hub::ban_user`].
=> (ban_user, Ban),
/// Unbans a user from a hub.
///
/// # Arguments
///
/// * `actor_id` - ID of the user who is unbanning.
/// * `hub_id` - Hub from which the user is being unbanned.
/// * `user_id` - ID of the user who is to be unbanned.
///
/// # Errors
///
/// This function may fail for any of the following reasons:
///
/// * The user performing the unban is not in the hub
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
/// * The user doing the unban does not have permission to unban other users.
/// * The unban failed for any of the reasons outlined by [`Hub::unban_user`].
=> (unban_user, Unban),
/// Mutes a user in a hub.
///
/// # Arguments
///
/// * `actor_id` - ID of the user who is muting.
/// * `hub_id` - Hub in which the user is being muted.
/// * `user_id` - ID of the user who is to be muted.
///
/// # Errors
///
/// This function may fail for any of the following reasons:
///
/// * The user performing the mute is not in the hub
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
/// * The user performing the mute does not have permission to mute other users.
/// * The mute failed for any of the reasons outlined by [`Hub::mute_user`].
=> (mute_user, Mute),
/// Unmutes a user in a hub.
///
/// # Arguments
///
/// * `actor_id` - ID of the user who is unmuting.
/// * `hub_id` - Hub in which the user is being unmuted.
/// * `user_id` - ID of the user who is to be unmuted.
///
/// # Errors
///
/// This function may fail for any of the following reasons:
///
/// * The user performing the unmute is not in the hub
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
/// * The user performing the unmute does not have permission to unmute other users.
/// * The unmute failed for any of the reasons outlined by [`Hub::unmute_user`].
=> (unmute_user, Unmute)
}

/// Creates a text channel in a hub.
/// Returns the ID of the new channel if successful.
///
/// # Arguments
///
/// * `user_id` - ID of the user to check for permission to create the channel.
/// * `hub_id` - ID of the hub in which the channel should be created.
/// * `name` - Name for the new channel.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * THe user is not in the hub.
/// * The name failed to pass the checks for any of the reasons outlined in [`check_name_validity`].
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
/// * The hub could not be saved for any of the reasons outlined by [`Hub::save`].
/// * The user does not have permission to create new channels.
/// * The channel could not be created for any of the reasons outlined by [`Hub::new_channel`].
pub async fn create_channel<S: Into<String> + Clone>(
    user_id: &ID,
    hub_id: &ID,
    name: S,
) -> Result<ID> {
    check_name_validity(&name.clone().into())?;
    let mut hub = Hub::load(hub_id).await?;
    let channel_id = hub.new_channel(user_id, name.into()).await?;
    hub.save().await?;
    Ok(channel_id)
}

/// Gets a channel's information.
///
/// # Arguments
///
/// * `user_id` - ID of the user that is requesting the information.
/// * `hub_id` - ID of the hub the channel is in.
/// * `channel_id` - ID of the channel to get.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * The user is not in the hub.
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
/// * The channel does not exist.
pub async fn get_channel(user_id: &ID, hub_id: &ID, channel_id: &ID) -> Result<Channel> {
    let hub = Hub::load(hub_id).await?;
    Ok(hub.get_channel(user_id, channel_id)?.clone())
}

/// Renames a text channel in a hub.
/// Returns the previous name of the channel if successful.
///
/// # Arguments
///
/// * `user_id` - ID of the user to check for permission to rename the channel.
/// * `hub_id` - ID of the hub that has the channel.
/// * `channel_id` - ID of the channel to be renamed.
/// * `new_name` - New name for the channel.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * THe user is not in the hub.
/// * The name failed to pass the checks for any of the reasons outlined in [`check_name_validity`].
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
/// * The hub could not be saved for any of the reasons outlined by [`Hub::save`].
/// * The user does not have permission to rename channels.
/// * The channel could not be renamed for any of the reasons outlined by [`Hub::rename_channel`].
pub async fn rename_channel<S: Into<String> + Clone>(
    user_id: &ID,
    hub_id: &ID,
    channel_id: &ID,
    new_name: S,
) -> Result<String> {
    check_name_validity(&new_name.clone().into())?;
    let mut hub = Hub::load(hub_id).await?;
    let old_name = hub
        .rename_channel(user_id, channel_id, new_name.into())
        .await?;
    hub.save().await?;
    Ok(old_name)
}

/// Renames a text channel in a hub.
/// Returns the previous name of the channel if successful.
///
/// # Arguments
///
/// * `user_id` - ID of the user to check for permission to rename the channel.
/// * `hub_id` - ID of the hub that has the channel.
/// * `channel_id` - ID of the channel to be renamed.
/// * `new_name` - New name for the channel.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * THe user is not in the hub.
/// * The name failed to pass the checks for any of the reasons outlined in [`check_name_validity`].
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
/// * The hub could not be saved for any of the reasons outlined by [`Hub::save`].
/// * The user does not have permission to rename channels.
/// * The channel could not be renamed for any of the reasons outlined by [`Hub::rename_channel`].
pub async fn change_channel_description<S: Into<String> + Clone>(
    user_id: &ID,
    hub_id: &ID,
    channel_id: &ID,
    new_description: S,
) -> Result<String> {
    let description: String = new_description.into();
    if description.as_bytes().len() > crate::MAX_DESCRIPTION_SIZE {
        Err(Error::TooBig)
    } else {
        let mut hub = Hub::load(hub_id).await?;
        let old_name = hub
            .change_channel_description(user_id, channel_id, description)
            .await?;
        hub.save().await?;
        Ok(old_name)
    }
}

/// Deletes a text channel in a hub.
///
/// # Arguments
///
/// * `user_id` - ID of the user to check for permission to delete channels.
/// * `hub_id` - ID of the hub that has the channel.
/// * `channel_id` - ID of the channel to be deleted.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * THe user is not in the hub.
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
/// * The hub could not be saved for any of the reasons outlined by [`Hub::save`].
/// * The user does not have permission to delete channels.
/// * The channel could not be deleted for any of the reasons outlined by [`Hub::delete_channel`].
pub async fn delete_channel(user_id: &ID, hub_id: &ID, channel_id: &ID) -> Result<()> {
    let mut hub = Hub::load(hub_id).await?;
    hub.delete_channel(user_id, channel_id).await?;
    hub.save().await
}

/// Sends a message in a text channel in a hub.
/// Returns the message if successful.
///
/// # Arguments
///
/// * `user_id` - ID of the user who is sending the message.
/// * `hub_id` - ID of the hub where the message is being sent.
/// * `channel_id` - ID of the channel where the message is being sent.
/// * `message` - The actual message to be sent.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * The user is not in the hub.
/// * The message is too big (maximum size in bytes is determined by [`crate::MESSAGE_MAX_SIZE`]).
/// * The message could not be sent for any of the reasons outlined by [`Hub::send_message`].
/// * The channel could not be gotten for any of the reasons outlined by [`Hub::get_channel`].
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
pub async fn send_message(
    user_id: &ID,
    hub_id: &ID,
    channel_id: &ID,
    message: String,
) -> Result<Message> {
    if message.as_bytes().len() < crate::MESSAGE_MAX_SIZE {
        let mut hub = Hub::load(hub_id).await?;
        hub.send_message(user_id, channel_id, message).await
    } else {
        Err(Error::TooBig)
    }
}

/// Gets a message from a text channel in a hub.
///
/// # Arguments
///
/// * `user_id` - ID of the user who is requesting the message.
/// * `hub_id` - ID of the hub where the message is located.
/// * `channel_id` - ID of the channel where the message is located.
/// * `message_id` - ID of the message to retreive.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * The user is not in the hub.
/// * The channel could not be found in the hub.
/// * The message could not be found.
/// * The channel could not be gotten for any of the reasons outlined by [`Hub::get_channel`].
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
pub async fn get_message(
    user_id: &ID,
    hub_id: &ID,
    channel_id: &ID,
    message_id: &ID,
) -> Result<Message> {
    let hub = Hub::load(hub_id).await?;
    let channel = Hub::get_channel(&hub, user_id, channel_id)?;
    if let Some(message) = channel.get_message(message_id).await {
        Ok(message)
    } else {
        Err(Error::MessageNotFound)
    }
}

/// Gets messages sent after a given message.
/// If successful they are returned in an array. The array is orderd oldest message to newest
/// If there are no messages after the given message or the given message is not found, an empty array is returned.
///
/// # Arguments
///
/// * `user_id` - ID of the user who is requesting the message.
/// * `hub_id` - ID of the hub where the message is located.
/// * `channel_id` - ID of the channel where the message is located.
/// * `from` - ID of the message to start from.
/// * `max` - The maximum number of messages to retreive.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * The user is not in the hub.
/// * The channel could not be found in the hub.
/// * The channel could not be gotten for any of the reasons outlined by [`Hub::get_channel`].
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
pub async fn get_messages_after(
    user_id: &ID,
    hub_id: &ID,
    channel_id: &ID,
    from: &ID,
    max: usize,
) -> Result<Vec<Message>> {
    let hub = Hub::load(hub_id).await?;
    let channel = Hub::get_channel(&hub, user_id, channel_id)?;
    Ok(channel.get_messages_after(from, max).await)
}

/// Gets a set of messages between two times (both in milliseconds since Unix Epoch).
/// If successful they are returned in an array. The array is orderd oldest message to newest
/// unless the `invert` argument is `true` in which case the order is newest to oldest message.
/// If there are no messages in the given time frame, an empty array is returned.
///
/// # Arguments
///
/// * `user_id` - ID of the user who is requesting the message.
/// * `hub_id` - ID of the hub where the message is located.
/// * `channel_id` - ID of the channel where the message is located.
/// * `from` - Earliest time a message can be sent to be included in the results.
/// * `to` - Latest time a message can be sent to be included in the results.
/// * `invert` - If true the search is done from newest message to oldest message, if false the search is done from oldest message to newest message.
/// * `max` - The maximum number of messages to retreive.
///
/// # Errors
///
/// This function may return an error for any of the following reasons:
///
/// * The user is not in the hub.
/// * The channel could not be found in the hub.
/// * The channel could not be gotten for any of the reasons outlined by [`Hub::get_channel`].
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
pub async fn get_messages(
    user_id: &ID,
    hub_id: &ID,
    channel_id: &ID,
    from: u128,
    to: u128,
    invert: bool,
    max: usize,
) -> Result<Vec<Message>> {
    let hub = Hub::load(hub_id).await?;
    let channel = Hub::get_channel(&hub, user_id, channel_id)?;
    Ok(channel.get_messages(from, to, invert, max).await)
}

/// Sets a hub wide permission for a hub member.
///
/// # Arguments
///
/// * `user_id` - ID of the user who is making the change.
/// * `hub_id` - The hub in which the change is being made.
/// * `member_id` - The hub member whose permissions are being changed.
/// * `permission` - The permission whose setting is being changed.
/// * `value` - The new setting for the permission.
///
/// # Errors
///
/// This function may return an error for any of the following reasons.
///
/// * The user making the change is not in the hub.
/// * The user whose permission is being changed is not in the hub.
/// * The user making the change does not have permission to do so.
/// * The hub could not be saved for any of the reasons outlined by [`Hub::save`].
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
pub async fn set_member_hub_permission(
    user_id: &ID,
    hub_id: &ID,
    member_id: &ID,
    permission: HubPermission,
    value: PermissionSetting,
) -> Result<()> {
    let mut hub = Hub::load(hub_id).await?;
    {
        let member = hub.get_member(user_id)?;
        check_permission!(member, HubPermission::Administrate, hub);
    }
    let member = hub.get_member_mut(member_id)?;
    member.set_permission(permission, value);
    hub.save().await
}

/// Sets a channel specific permission for a hub member.
///
/// # Arguments
///
/// * `user_id` - ID of the user who is making the change.
/// * `hub_id` - The hub in which the change is being made.
/// * `member_id` - The hub member whose permissions are being changed.
/// * `channel_id` - The channel that the change should apply to.
/// * `permission` - The permission whose setting is being changed.
/// * `value` - The new setting for the permission.
///
/// # Errors
///
/// This function may return an error for any of the following reasons.
///
/// * The user making the change is not in the hub.
/// * The user whose permission is being changed is not in the hub.
/// * The user making the change does not have permission to do so.
/// * The hub could not be saved for any of the reasons outlined by [`Hub::save`].
/// * The hub could not be loaded for any of the reasons outlined by [`Hub::load`].
pub async fn set_member_channel_permission(
    user_id: &ID,
    hub_id: &ID,
    member_id: &ID,
    channel_id: &ID,
    permission: ChannelPermission,
    value: PermissionSetting,
) -> Result<()> {
    let mut hub = Hub::load(hub_id).await?;
    {
        let member = hub.get_member(user_id)?;
        check_permission!(member, HubPermission::Administrate, hub);
    }
    let member = hub.get_member_mut(member_id)?;
    member.set_channel_permission(channel_id, permission, value);
    hub.save().await
}