cli: add {Add,Remove}Policy, {Add,Remove}ListOwner

pull/1/head
Manos Pitsidianakis 2022-05-14 08:45:09 +03:00
parent abf2031794
commit 8c7d342e9b
2 changed files with 105 additions and 1 deletions

View File

@ -148,6 +148,30 @@ enum ListCommand {
receive_confirmation: Option<bool>,
enabled: Option<bool>,
},
/// Add policy to list.
AddPolicy {
#[structopt(long)]
announce_only: bool,
#[structopt(long)]
subscriber_only: bool,
#[structopt(long)]
approval_needed: bool,
},
RemovePolicy {
#[structopt(long)]
pk: i64,
},
/// Add list owner to list.
AddListOwner {
#[structopt(long)]
address: String,
#[structopt(long)]
name: Option<String>,
},
RemoveListOwner {
#[structopt(long)]
pk: i64,
},
/// Alias for update-membership --enabled true
EnableMembership { address: String },
/// Alias for update-membership --enabled false
@ -286,8 +310,13 @@ fn run_app(opt: Opt) -> Result<()> {
let list_policy = db.get_list_policy(list.pk)?;
if list_owners.is_empty() {
println!("\tList has no owners: you should add at least one.");
} else {
for owner in list_owners {
println!("\tList owner: {}.", owner);
}
}
if list_policy.is_none() {
if let Some(list_policy) = list_policy {
println!("\tList has post policy: {}.", list_policy);
} else {
println!("\tList has no post policy: you should add one.");
}
@ -350,6 +379,39 @@ fn run_app(opt: Opt) -> Result<()> {
};
db.update_member(changeset)?;
}
AddPolicy {
announce_only,
subscriber_only,
approval_needed,
} => {
let policy = PostPolicy {
pk: 0,
list: list.pk,
announce_only,
subscriber_only,
approval_needed,
};
let new_val = db.set_list_policy(list.pk, policy)?;
println!("Added new policy with pk = {}", new_val.pk());
}
RemovePolicy { pk } => {
db.remove_list_policy(list.pk, pk)?;
println!("Removed policy with pk = {}", pk);
}
AddListOwner { address, name } => {
let list_owner = ListOwner {
pk: 0,
list: list.pk,
address,
name,
};
let new_val = db.add_list_owner(list.pk, list_owner)?;
println!("Added new list owner {}", new_val);
}
RemoveListOwner { pk } => {
db.remove_list_owner(list.pk, pk)?;
println!("Removed list owner with pk = {}", pk);
}
EnableMembership { address } => {
let changeset = ListMembershipChangeset {
list: list.pk,

View File

@ -114,6 +114,16 @@ impl Database {
Ok(ret)
}
pub fn remove_list_policy(&self, list_pk: i64, policy_pk: i64) -> Result<()> {
let mut stmt = self
.connection
.prepare("DELETE FROM post_policy WHERE pk = ? AND list_pk = ?;")?;
stmt.execute(rusqlite::params![&policy_pk, &list_pk,])?;
trace!("remove_list_policy {} {}.", list_pk, policy_pk);
Ok(())
}
pub fn set_list_policy(&self, list_pk: i64, policy: PostPolicy) -> Result<DbVal<PostPolicy>> {
let mut stmt = self.connection.prepare("INSERT OR REPLACE INTO post_policy(list, announce_only, subscriber_only, approval_needed) VALUES (?, ?, ?, ?) RETURNING *;")?;
let ret = stmt.query_row(
@ -232,6 +242,38 @@ impl Database {
Ok(ret)
}
pub fn remove_list_owner(&self, list_pk: i64, owner_pk: i64) -> Result<()> {
self.connection.execute(
"DELETE FROM list_owners WHERE list_pk = ? AND pk = ?;",
rusqlite::params![&list_pk, &owner_pk],
)?;
Ok(())
}
pub fn add_list_owner(&self, list_pk: i64, list_owner: ListOwner) -> Result<DbVal<ListOwner>> {
let mut stmt = self.connection.prepare(
"INSERT OR REPLACE INTO list_owner(list, address, name) VALUES (?, ?, ?) RETURNING *;",
)?;
let ret = stmt.query_row(
rusqlite::params![&list_pk, &list_owner.address, &list_owner.name,],
|row| {
let pk = row.get("pk")?;
Ok(DbVal(
ListOwner {
pk,
list: row.get("list")?,
address: row.get("address")?,
name: row.get("name")?,
},
pk,
))
},
)?;
trace!("add_list_owner {:?}.", &ret);
Ok(ret)
}
pub fn list_members(&self, pk: i64) -> Result<Vec<DbVal<ListMembership>>> {
let mut stmt = self
.connection