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
use rspc::Type;
use serde::{Deserialize, Serialize};

use crate::util::{jar::RequestJar, responses::DataWrapper, Error};

use super::GroupRole;

#[derive(Debug, Serialize, Deserialize, Clone, Type)]
#[serde(rename_all = "camelCase")]
pub struct GroupPermissions {
    pub group_posts_permissions: GroupPostPermissions,
    pub group_membership_permissions: GroupMembershipPermissions,
    pub group_management_permissions: GroupManagementPermissions,
    pub group_economy_permissions: GroupEconomyPermissions,
    pub group_open_cloud_permissions: GroupOpenCloudPermissions,
}

#[derive(Debug, Serialize, Deserialize, Clone, Type)]
#[serde(rename_all = "camelCase")]
pub struct GroupPostPermissions {
    pub view_wall: bool,
    pub post_to_wall: bool,
    pub delete_from_wall: bool,
    pub view_status: bool,
    pub post_to_status: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone, Type)]
#[serde(rename_all = "camelCase")]
pub struct GroupMembershipPermissions {
    pub change_rank: bool,
    pub invite_members: bool,
    pub remove_members: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone, Type)]
#[serde(rename_all = "camelCase")]
pub struct GroupManagementPermissions {
    pub manage_relationships: bool,
    pub manage_clan: bool,
    pub view_audit_logs: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone, Type)]
#[serde(rename_all = "camelCase")]
pub struct GroupEconomyPermissions {
    pub spend_group_funds: bool,
    pub advertise_group: bool,
    pub create_items: bool,
    pub manage_items: bool,
    pub add_group_places: bool,
    pub manage_group_games: bool,
    pub view_group_payouts: bool,
    pub view_analytics: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone, Type)]
#[serde(rename_all = "camelCase")]
pub struct GroupOpenCloudPermissions {
    pub use_cloud_authentication: bool,
    pub administer_cloud_authentication: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone, Type)]
#[serde(rename_all = "camelCase")]
pub struct RolePermissions {
    pub group_id: u32,
    pub role: GroupRole,
    pub permissions: GroupPermissions,
}

/// Gets the permissions for a specific role in a group
///
/// # Error codes
/// - 1: Group is invalid or does not exist.
/// - 2: The roleset is invalid or does not exist.
/// - 3: You are not authorized to view/edit permissions for this role.
pub async fn role_permissions(
    jar: &RequestJar,
    group_id: u32,
    role_id: u32,
) -> Result<RolePermissions, Box<Error>> {
    let url = format!(
        "https://groups.roblox.com/v1/groups/{}/roles/{}/permissions",
        group_id, role_id
    );

    let response = jar.get_json(&url).await?;

    Ok(response)
}

#[derive(Debug, Serialize, Deserialize, Clone, Type)]
#[serde(rename_all = "camelCase")]
pub struct UpdateRolePermissionsRequestPermissions {
    pub delete_from_wall: Option<bool>,
    pub post_to_wall: Option<bool>,
    pub invite_members: Option<bool>,
    pub post_to_status: Option<bool>,
    pub remove_members: Option<bool>,
    pub view_status: Option<bool>,
    pub view_wall: Option<bool>,
    pub change_rank: Option<bool>,
    pub advertise_group: Option<bool>,
    pub manage_relationships: Option<bool>,
    pub add_group_places: Option<bool>,
    pub view_audit_logs: Option<bool>,
    pub create_items: Option<bool>,
    pub manage_items: Option<bool>,
    pub spend_group_funds: Option<bool>,
    pub manage_clan: Option<bool>,
    pub manage_group_games: Option<bool>,
    pub use_cloud_authentication: Option<bool>,
    pub administer_cloud_authentication: Option<bool>,
    pub view_analytics: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Type)]
#[serde(rename_all = "camelCase")]
pub struct UpdateRolePermissionsRequest {
    pub permissions: UpdateRolePermissionsRequestPermissions,
}

/// Sets the permissions for a specific role in a group
///
/// # Error codes
/// - 1: Group is invalid or does not exist.
/// - 2: The roleset is invalid or does not exist.
/// - 3: You are not authorized to view/edit permissions for this role.
/// - 4: This role's permissions can not be modified.
pub async fn update_role_permissions(
    jar: &RequestJar,
    group_id: u32,
    role_id: u32,
    permissions: UpdateRolePermissionsRequestPermissions,
) -> Result<(), Box<Error>> {
    let url = format!(
        "https://groups.roblox.com/v1/groups/{}/roles/{}/permissions",
        group_id, role_id
    );

    jar.patch_json(&url, &(UpdateRolePermissionsRequest { permissions }))
        .await?;

    Ok(())
}

/// Gets the permissions for the group's guest role
///
/// # Error codes
/// - 1: Group is invalid or does not exist.
/// - 2: The roleset is invalid or does not exist.
/// - 3: You are not authorized to view/edit permissions for this role.
pub async fn guest_permissions(
    jar: &RequestJar,
    group_id: u32,
) -> Result<RolePermissions, Box<Error>> {
    let url = format!(
        "https://groups.roblox.com/v1/groups/{}/roles/guest/permissions",
        group_id,
    );

    let response = jar.get_json(&url).await?;

    Ok(response)
}

/// Gets the permissions for all the group's roles
///
/// # Error codes
/// - 1: Group is invalid or does not exist.
///
/// *Note: None were provided in the documentation*
pub async fn permissions(
    jar: &RequestJar,
    group_id: u32,
) -> Result<Vec<RolePermissions>, Box<Error>> {
    let url = format!(
        "https://groups.roblox.com/v1/groups/{}/roles/permissions",
        group_id,
    );

    let response = jar
        .get_json::<DataWrapper<Vec<RolePermissions>>>(&url)
        .await?;

    Ok(response.data)
}