use super::users::User;
use crate::{
response::TwitchResult,
TwitchClient,
};
use serde::Deserialize;
use serde_json::Value;
use std::{
self,
collections::HashMap,
io::Write,
};
pub fn get_by_name(
c: &TwitchClient,
name: &str,
) -> TwitchResult<Community>
{
let r = c.get::<Community>(&format!("/communities?name={}", name))?;
Ok(r)
}
pub fn get_by_id(
c: &TwitchClient,
id: &str,
) -> TwitchResult<Community>
{
let r = c.get::<Community>(&format!("/communities/{}", id))?;
Ok(r)
}
pub fn update<'a>(
c: &TwitchClient,
community_id: &str,
data: &'a UpdateSettings,
) -> TwitchResult<Community>
{
let mut settings: HashMap<String, &str> = HashMap::new();
if let Some(summary) = data.summary {
settings.insert("summary".to_owned(), summary);
}
if let Some(description) = data.description {
settings.insert("description".to_owned(), description);
}
if let Some(rules) = data.rules {
settings.insert("rules".to_owned(), rules);
}
if let Some(email) = data.email {
settings.insert("email".to_owned(), email);
}
let r = c.put::<HashMap<String, &str>, Community>(
&format!("/communities/{}", community_id),
&settings,
)?;
Ok(r)
}
pub fn bans<'c>(
c: &'c TwitchClient,
community_id: &str,
) -> TwitchResult<CommunityBanIterator<'c>>
{
let iter = CommunityBanIterator {
client: c,
community_id: String::from(community_id),
cur: None,
cursor: None,
};
Ok(iter)
}
pub fn ban(
c: &TwitchClient,
community_id: &str,
user_id: &str,
) -> TwitchResult<Value>
{
let r = c.put::<Value, Value>(
&format!("/communities/{}/bans/{}", community_id, user_id),
&Value::Null,
)?;
Ok(r)
}
pub fn unban(
c: &TwitchClient,
community_id: &str,
user_id: &str,
) -> TwitchResult<Value>
{
let r = c.delete::<Value>(&format!(
"/communities/{}/bans/{}",
community_id, user_id
))?;
Ok(r)
}
pub fn set_avatar_image(
c: &TwitchClient,
community_id: &str,
avatar_img: &str,
) -> TwitchResult<Value>
{
let mut data: HashMap<String, &str> = HashMap::new();
data.insert("avatar_image".to_owned(), avatar_img);
let r = c.post::<HashMap<String, &str>, Value>(
&format!("/communities/{}/images/avatar", community_id),
&data,
)?;
Ok(r)
}
pub fn delete_avatar_image(
c: &TwitchClient,
community_id: &str,
) -> TwitchResult<Value>
{
let r = c.delete::<Value>(&format!(
"/communities/{}/images/avatar",
community_id
))?;
Ok(r)
}
pub fn set_cover_image(
c: &TwitchClient,
community_id: &str,
cover_img: &str,
) -> TwitchResult<Value>
{
let mut data: HashMap<String, &str> = HashMap::new();
data.insert("cover_image".to_owned(), cover_img);
let r = c.post::<HashMap<String, &str>, Value>(
&format!("/communities/{}/images/cover", community_id),
&data,
)?;
Ok(r)
}
pub fn delete_cover_image(
c: &TwitchClient,
community_id: &str,
) -> TwitchResult<Value>
{
let r = c.delete::<Value>(&format!(
"/communities/{}/images/cover",
community_id
))?;
Ok(r)
}
pub fn moderators(
c: &TwitchClient,
community_id: &str,
) -> TwitchResult<Moderators>
{
let r = c.get::<Moderators>(&format!(
"/communities/{}/moderators",
community_id
))?;
Ok(r)
}
pub fn new_moderator(
c: &TwitchClient,
community_id: &str,
user_id: &str,
) -> TwitchResult<Value>
{
let r = c.put::<Value, Value>(
&format!("/communities/{}/moderators/{}", community_id, user_id),
&Value::Null,
)?;
Ok(r)
}
pub fn delete_moderator(
c: &TwitchClient,
community_id: &str,
user_id: &str,
) -> TwitchResult<Value>
{
let r = c.delete::<Value>(&format!(
"/communities/{}/moderators/{}",
community_id, user_id
))?;
Ok(r)
}
pub fn permissions(
c: &TwitchClient,
community_id: &str,
) -> TwitchResult<HashMap<String, bool>>
{
let r = c.get::<HashMap<String, bool>>(&format!(
"/communities/{}/permissions",
community_id
))?;
Ok(r)
}
pub fn report_channel(
c: &TwitchClient,
community_id: &str,
channel_id: &str,
) -> TwitchResult<Value>
{
let mut data: HashMap<String, &str> = HashMap::new();
data.insert("channel_id".to_owned(), channel_id);
let r = c.post::<HashMap<String, &str>, Value>(
&format!("/communities/{}/report_channel", community_id),
&data,
)?;
Ok(r)
}
pub fn timeouts<'c>(
c: &'c TwitchClient,
community_id: &str,
) -> TwitchResult<TimeoutIterator<'c>>
{
let iter = TimeoutIterator {
client: c,
community_id: String::from(community_id),
cur: None,
cursor: None,
};
Ok(iter)
}
pub fn timeout(
c: &TwitchClient,
community_id: &str,
user_id: &str,
duration: i32,
reason: Option<String>,
) -> TwitchResult<Value>
{
let mut data: HashMap<String, String> = HashMap::new();
data.insert("duration".to_owned(), duration.to_string());
if let Some(reason) = reason {
data.insert("reason".to_owned(), reason);
}
let r = c.put::<HashMap<String, String>, Value>(
&format!("/communities/{}/timeouts/{}", community_id, user_id),
&data,
)?;
Ok(r)
}
pub fn delete_timeout(
c: &TwitchClient,
community_id: &str,
user_id: &str,
) -> TwitchResult<Value>
{
let r = c.delete::<Value>(&format!(
"/communities/{}/timeouts/{}",
community_id, user_id
))?;
Ok(r)
}
pub fn top(c: &TwitchClient) -> TwitchResult<TopCommunities> {
let iter = TopCommunities {
client: c,
cur: None,
cursor: None,
};
Ok(iter)
}
#[derive(Deserialize, Debug)]
pub struct Community {
#[serde(rename = "_id")]
pub id: String,
pub avatar_image_url: String,
pub cover_image_url: String,
pub description: String,
pub description_html: String,
pub language: String,
pub name: String,
pub owner_id: String,
pub rules: String,
pub rules_html: String,
pub summary: String,
}
pub struct UpdateSettings<'a> {
pub summary: Option<&'a str>,
pub description: Option<&'a str>,
pub rules: Option<&'a str>,
pub email: Option<&'a str>,
}
#[derive(Deserialize, Debug)]
pub struct Moderators {
pub moderators: Vec<User>,
}
#[derive(Debug)]
pub struct TopCommunities<'c> {
client: &'c TwitchClient,
cur: Option<SerdeTopCommunities>,
cursor: Option<String>,
}
#[derive(Deserialize, Debug)]
pub struct TopCommunity {
#[serde(rename = "_id")]
pub id: String,
pub avatar_image_url: String,
pub channels: i32,
pub name: String,
pub viewers: i32,
}
#[derive(Deserialize, Debug)]
struct SerdeTopCommunities {
pub communities: Vec<TopCommunity>,
pub _cursor: Option<String>,
}
impl<'c> Iterator for TopCommunities<'c> {
type Item = TopCommunity;
fn next(&mut self) -> Option<TopCommunity> {
let url = "/communities/top?".to_string();
next_result_cursor!(self, &url, SerdeTopCommunities, communities)
}
}
pub struct CommunityBanIterator<'c> {
client: &'c TwitchClient,
community_id: String,
cur: Option<SerdeCommunityBan>,
cursor: Option<String>,
}
#[derive(Deserialize, Debug)]
struct SerdeCommunityBan {
pub banned_users: Vec<CommunityBan>,
pub _cursor: Option<String>,
}
#[derive(Deserialize, Debug)]
pub struct CommunityBan {
pub user_id: String,
pub display_name: String,
pub name: String,
pub bio: Option<String>,
pub avatar_image_url: Option<String>,
pub start_timestamp: i64,
}
impl<'c> Iterator for CommunityBanIterator<'c> {
type Item = CommunityBan;
fn next(&mut self) -> Option<CommunityBan> {
let url = format!("/communities/{}/bans?", &self.community_id);
next_result_cursor!(self, &url, SerdeCommunityBan, banned_users)
}
}
pub struct TimeoutIterator<'c> {
client: &'c TwitchClient,
community_id: String,
cur: Option<SerdeTimeout>,
cursor: Option<String>,
}
#[derive(Deserialize, Debug)]
struct SerdeTimeout {
pub timed_out_users: Vec<TimeoutUser>,
pub _cursor: Option<String>,
}
#[derive(Deserialize, Debug)]
pub struct TimeoutUser {
pub user_id: String,
pub display_name: String,
pub name: String,
pub bio: Option<String>,
pub avatar_image_url: Option<String>,
pub start_timestamp: i64,
pub end_timestamp: i64,
}
impl<'c> Iterator for TimeoutIterator<'c> {
type Item = TimeoutUser;
fn next(&mut self) -> Option<TimeoutUser> {
let url = format!("/communities/{}/timeouts?", &self.community_id);
next_result_cursor!(self, &url, SerdeTimeout, timed_out_users)
}
}