Skip to content

Commit

Permalink
feat: introduced community add-ons (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tguntenaar authored Oct 19, 2023
1 parent daced93 commit adadfdc
Show file tree
Hide file tree
Showing 6 changed files with 543 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/access_control/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ impl Into<String> for Member {
)]
#[serde(crate = "near_sdk::serde")]
pub struct MemberMetadata {
description: String,
permissions: HashMap<Rule, HashSet<ActionType>>,
children: HashSet<Member>,
parents: HashSet<Member>,
pub description: String,
pub permissions: HashMap<Rule, HashSet<ActionType>>,
pub children: HashSet<Member>,
pub parents: HashSet<Member>,
}

#[derive(
Expand Down
65 changes: 64 additions & 1 deletion src/community/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use near_sdk::{env, require, AccountId};

pub type CommunityHandle = String;

pub type AddOnId = String;

#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Clone)]
#[serde(crate = "near_sdk::serde")]
pub struct CommunityInputs {
Expand Down Expand Up @@ -45,7 +47,55 @@ pub struct WikiPage {
content_markdown: String,
}

#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Clone)]
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Clone, PartialEq, Debug)]
#[serde(crate = "near_sdk::serde")]
pub struct CommunityAddOn {
pub id: String,
pub addon_id: AddOnId,
pub display_name: String,
pub enabled: bool,
pub parameters: String,
}

#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Clone, PartialEq, Debug)]
#[serde(crate = "near_sdk::serde")]
pub struct AddOn {
pub id: AddOnId,
pub title: String,
pub description: String,
pub icon: String,
// The path to the view on the community page
pub view_widget: String,
// The path to the view on the community configuration page
pub configurator_widget: String,
}

impl AddOn {
pub fn validate(&self) {
if !matches!(self.id.chars().count(), 3..=120) {
panic!("Add-on id must contain 3 to 120 characters");
}

if !matches!(self.title.chars().count(), 3..=120) {
panic!("Add-on title must contain 3 to 120 characters");
}

if !matches!(self.description.chars().count(), 3..=120) {
panic!("Add-on description must contain 3 to 120 characters");
}
if !matches!(self.view_widget.chars().count(), 6..=240) {
panic!("Add-on viewer must contain 6 to 240 characters");
}
if !matches!(self.configurator_widget.chars().count(), 0..=240) {
panic!("Add-on configurator must contain 0 to 240 characters");
}
if !matches!(self.icon.chars().count(), 6..=60) {
panic!("Add-on icon must contain 6 to 60 characters");
}
}
}

#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
#[serde(crate = "near_sdk::serde")]
pub struct Community {
pub admins: Vec<AccountId>,
Expand All @@ -67,6 +117,7 @@ pub struct Community {
pub wiki1: Option<WikiPage>,
pub wiki2: Option<WikiPage>,
pub features: CommunityFeatureFlags,
pub addons: Vec<CommunityAddOn>,
}

#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -110,6 +161,18 @@ impl Community {
);
}

pub fn add_addon(&mut self, addon_config: CommunityAddOn) {
self.addons.push(addon_config);
}

pub fn remove_addon(&mut self, addon_to_remove: CommunityAddOn) {
self.addons.retain(|addon| addon != &addon_to_remove);
}

pub fn set_addons(&mut self, addons: Vec<CommunityAddOn>) {
self.addons = addons;
}

pub fn set_default_admin(&mut self) {
if self.admins.is_empty() {
self.admins = vec![env::predecessor_account_id()];
Expand Down
Loading

0 comments on commit adadfdc

Please sign in to comment.