|
| 1 | +use crate::features::{ |
| 2 | + accounts::model::{Account, AccountSummary, InviteSummary, Role, SshKeySummary}, |
| 3 | + channels::model::ChannelMemberSummary, |
| 4 | +}; |
| 5 | + |
| 6 | +use super::ClientSession; |
| 7 | + |
| 8 | +pub struct AccountsClient<'a> { |
| 9 | + session: &'a ClientSession, |
| 10 | +} |
| 11 | + |
| 12 | +impl ClientSession { |
| 13 | + pub fn accounts(&self) -> AccountsClient<'_> { |
| 14 | + AccountsClient { session: self } |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +impl AccountsClient<'_> { |
| 19 | + fn actor_id(&self) -> &str { |
| 20 | + self.session.actor_id() |
| 21 | + } |
| 22 | + |
| 23 | + pub async fn create_invite(&self) -> anyhow::Result<String> { |
| 24 | + self.session |
| 25 | + .state() |
| 26 | + .create_invite(self.actor_id().to_string()) |
| 27 | + .await |
| 28 | + } |
| 29 | + |
| 30 | + pub async fn create_invite_with_options( |
| 31 | + &self, |
| 32 | + role: Role, |
| 33 | + ttl_hours: Option<i64>, |
| 34 | + ) -> anyhow::Result<String> { |
| 35 | + self.session |
| 36 | + .state() |
| 37 | + .create_invite_with_options(self.actor_id(), role, ttl_hours) |
| 38 | + .await |
| 39 | + } |
| 40 | + |
| 41 | + pub async fn complete_onboarding(&self, username: &str) -> anyhow::Result<Account> { |
| 42 | + self.session |
| 43 | + .state() |
| 44 | + .complete_onboarding(self.actor_id(), username) |
| 45 | + .await |
| 46 | + } |
| 47 | + |
| 48 | + pub async fn list_accounts(&self) -> anyhow::Result<Vec<AccountSummary>> { |
| 49 | + self.session.state().list_accounts(self.actor_id()).await |
| 50 | + } |
| 51 | + |
| 52 | + pub async fn rename_self(&self, username: &str) -> anyhow::Result<()> { |
| 53 | + self.session |
| 54 | + .state() |
| 55 | + .rename_user(self.actor_id(), self.actor_id(), username) |
| 56 | + .await |
| 57 | + } |
| 58 | + |
| 59 | + pub async fn set_self_display_name(&self, display_name: &str) -> anyhow::Result<()> { |
| 60 | + self.session |
| 61 | + .state() |
| 62 | + .set_display_name(self.actor_id(), self.actor_id(), display_name) |
| 63 | + .await |
| 64 | + } |
| 65 | + |
| 66 | + pub async fn set_user_disabled(&self, username: &str, disabled: bool) -> anyhow::Result<()> { |
| 67 | + self.session |
| 68 | + .state() |
| 69 | + .set_user_disabled(self.actor_id(), username, disabled) |
| 70 | + .await |
| 71 | + } |
| 72 | + |
| 73 | + pub async fn set_user_role(&self, username: &str, role: Role) -> anyhow::Result<()> { |
| 74 | + self.session |
| 75 | + .state() |
| 76 | + .set_user_role(self.actor_id(), username, role) |
| 77 | + .await |
| 78 | + } |
| 79 | + |
| 80 | + pub async fn list_ssh_keys(&self) -> anyhow::Result<Vec<SshKeySummary>> { |
| 81 | + self.session.state().list_ssh_keys(self.actor_id()).await |
| 82 | + } |
| 83 | + |
| 84 | + pub async fn list_my_ssh_keys(&self) -> anyhow::Result<Vec<SshKeySummary>> { |
| 85 | + self.session.state().list_my_ssh_keys(self.actor_id()).await |
| 86 | + } |
| 87 | + |
| 88 | + pub async fn create_device_link_token(&self, label: Option<&str>) -> anyhow::Result<String> { |
| 89 | + self.session |
| 90 | + .state() |
| 91 | + .create_device_link_token(self.actor_id(), label) |
| 92 | + .await |
| 93 | + } |
| 94 | + |
| 95 | + pub async fn add_ssh_key( |
| 96 | + &self, |
| 97 | + username: Option<&str>, |
| 98 | + public_key: &str, |
| 99 | + label: Option<&str>, |
| 100 | + ) -> anyhow::Result<SshKeySummary> { |
| 101 | + self.session |
| 102 | + .state() |
| 103 | + .add_ssh_key(self.actor_id(), username, public_key, label) |
| 104 | + .await |
| 105 | + } |
| 106 | + |
| 107 | + pub async fn label_ssh_key(&self, key: &str, label: &str) -> anyhow::Result<()> { |
| 108 | + self.session |
| 109 | + .state() |
| 110 | + .label_ssh_key(self.actor_id(), key, label) |
| 111 | + .await |
| 112 | + } |
| 113 | + |
| 114 | + pub async fn revoke_ssh_key(&self, key: &str) -> anyhow::Result<()> { |
| 115 | + self.session |
| 116 | + .state() |
| 117 | + .revoke_ssh_key(self.actor_id(), key) |
| 118 | + .await |
| 119 | + } |
| 120 | + |
| 121 | + pub async fn list_invites(&self) -> anyhow::Result<Vec<InviteSummary>> { |
| 122 | + self.session.state().list_invites(self.actor_id()).await |
| 123 | + } |
| 124 | + |
| 125 | + pub async fn revoke_invite(&self, invite_id: &str) -> anyhow::Result<()> { |
| 126 | + self.session |
| 127 | + .state() |
| 128 | + .revoke_invite(self.actor_id(), invite_id) |
| 129 | + .await |
| 130 | + } |
| 131 | + |
| 132 | + pub async fn list_channel_members( |
| 133 | + &self, |
| 134 | + slug: &str, |
| 135 | + ) -> anyhow::Result<Vec<ChannelMemberSummary>> { |
| 136 | + self.session |
| 137 | + .state() |
| 138 | + .list_channel_members(self.actor_id(), slug) |
| 139 | + .await |
| 140 | + } |
| 141 | +} |
0 commit comments