mirror of
https://github.com/Xithrius/twitch-tui.git
synced 2024-10-04 09:07:33 +03:00
Implemented some of the needed traits for FollowingList
This commit is contained in:
parent
865083a3e9
commit
609a41afe0
86
src/twitch/channels.rs
Normal file
86
src/twitch/channels.rs
Normal file
@ -0,0 +1,86 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use reqwest::Client;
|
||||
use serde::Deserialize;
|
||||
use tokio::{runtime::Handle, task};
|
||||
|
||||
use crate::handlers::config::TwitchConfig;
|
||||
|
||||
use super::oauth::{get_channel_id, get_twitch_client};
|
||||
|
||||
const FOLLOWER_COUNT: usize = 100;
|
||||
|
||||
#[derive(Deserialize, Debug, Clone, Default)]
|
||||
#[allow(dead_code)]
|
||||
pub struct FollowingUser {
|
||||
broadcaster_id: String,
|
||||
pub broadcaster_login: String,
|
||||
pub broadcaster_name: String,
|
||||
followed_at: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone, Default)]
|
||||
#[allow(dead_code)]
|
||||
struct Pagination {
|
||||
cursor: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone, Default)]
|
||||
#[allow(dead_code)]
|
||||
pub struct FollowingList {
|
||||
pub total: u64,
|
||||
pub data: Vec<FollowingUser>,
|
||||
pagination: Pagination,
|
||||
}
|
||||
|
||||
// https://dev.twitch.tv/docs/api/reference/#get-followed-channels
|
||||
pub async fn get_user_following(client: &Client, user_id: i32) -> FollowingList {
|
||||
client
|
||||
.get(format!(
|
||||
"https://api.twitch.tv/helix/channels/followed?user_id={user_id}&first={FOLLOWER_COUNT}",
|
||||
))
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.error_for_status()
|
||||
.unwrap()
|
||||
.json::<FollowingList>()
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub async fn get_following(twitch_config: &TwitchConfig) -> FollowingList {
|
||||
let oauth_token = twitch_config.token.clone();
|
||||
let app_user = twitch_config.username.clone();
|
||||
|
||||
let client = get_twitch_client(oauth_token).await.unwrap();
|
||||
|
||||
let user_id = get_channel_id(&client, &app_user).await.unwrap();
|
||||
|
||||
get_user_following(&client, user_id).await
|
||||
}
|
||||
|
||||
impl FollowingList {
|
||||
pub fn get_followed_channels(twitch_config: TwitchConfig) -> Self {
|
||||
task::block_in_place(move || {
|
||||
Handle::current()
|
||||
.block_on(async move { get_following(&twitch_config.clone()).await })
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for FollowingUser {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.broadcaster_login.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<FollowingUser>> for FollowingList {
|
||||
fn from(value: Vec<FollowingUser>) -> Self {
|
||||
Self {
|
||||
total: value.len() as u64,
|
||||
data: value,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
mod badges;
|
||||
mod connection;
|
||||
pub mod oauth;
|
||||
pub mod channels;
|
||||
|
||||
use std::{borrow::Borrow, collections::HashMap};
|
||||
|
||||
|
@ -5,8 +5,6 @@ use reqwest::{
|
||||
};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::handlers::config::TwitchConfig;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
pub struct ClientId {
|
||||
@ -76,55 +74,3 @@ pub async fn get_channel_id(client: &Client, channel: &str) -> Result<i32> {
|
||||
.id
|
||||
.parse()?)
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone, Default)]
|
||||
#[allow(dead_code)]
|
||||
pub struct FollowingUser {
|
||||
broadcaster_id: String,
|
||||
pub broadcaster_login: String,
|
||||
pub broadcaster_name: String,
|
||||
followed_at: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone, Default)]
|
||||
#[allow(dead_code)]
|
||||
struct Pagination {
|
||||
cursor: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone, Default)]
|
||||
#[allow(dead_code)]
|
||||
pub struct FollowingList {
|
||||
total: u64,
|
||||
pub data: Vec<FollowingUser>,
|
||||
pagination: Pagination,
|
||||
}
|
||||
|
||||
const FOLLOWER_COUNT: usize = 100;
|
||||
|
||||
// https://dev.twitch.tv/docs/api/reference/#get-followed-channels
|
||||
pub async fn get_user_following(client: &Client, user_id: i32) -> FollowingList {
|
||||
client
|
||||
.get(format!(
|
||||
"https://api.twitch.tv/helix/channels/followed?user_id={user_id}&first={FOLLOWER_COUNT}",
|
||||
))
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.error_for_status()
|
||||
.unwrap()
|
||||
.json::<FollowingList>()
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub async fn get_following(twitch_config: &TwitchConfig) -> FollowingList {
|
||||
let oauth_token = twitch_config.token.clone();
|
||||
let app_user = twitch_config.username.clone();
|
||||
|
||||
let client = get_twitch_client(oauth_token).await.unwrap();
|
||||
|
||||
let user_id = get_channel_id(&client, &app_user).await.unwrap();
|
||||
|
||||
get_user_following(&client, user_id).await
|
||||
}
|
||||
|
@ -23,10 +23,7 @@ use crate::{
|
||||
user_input::events::{Event, Key},
|
||||
},
|
||||
terminal::TerminalAction,
|
||||
twitch::{
|
||||
oauth::{get_following, FollowingList},
|
||||
TwitchAction,
|
||||
},
|
||||
twitch::{self, channels::FollowingList, TwitchAction},
|
||||
ui::components::Component,
|
||||
utils::text::{title_line, TitleStyle},
|
||||
};
|
||||
@ -35,12 +32,6 @@ use super::utils::{centered_rect, InputWidget};
|
||||
|
||||
static FUZZY_FINDER: Lazy<SkimMatcherV2> = Lazy::new(SkimMatcherV2::default);
|
||||
|
||||
fn get_followed_channels(twitch_config: TwitchConfig) -> FollowingList {
|
||||
task::block_in_place(move || {
|
||||
Handle::current().block_on(async move { get_following(&twitch_config.clone()).await })
|
||||
})
|
||||
}
|
||||
|
||||
pub struct FollowingWidget {
|
||||
config: SharedCompleteConfig,
|
||||
focused: bool,
|
||||
@ -56,7 +47,8 @@ impl FollowingWidget {
|
||||
pub fn new(config: SharedCompleteConfig) -> Self {
|
||||
let search_input = InputWidget::new(config.clone(), "Search", None, None, None);
|
||||
|
||||
let channels = get_followed_channels(config.borrow().twitch.clone());
|
||||
// let channels = get_followed_channels(config.borrow().twitch.clone());
|
||||
let channels = FollowingList::default();
|
||||
|
||||
Self {
|
||||
config,
|
||||
@ -119,7 +111,7 @@ impl FollowingWidget {
|
||||
|
||||
pub fn toggle_focus(&mut self) {
|
||||
if !self.focused {
|
||||
self.channels = get_followed_channels(self.config.borrow().twitch.clone());
|
||||
FollowingList::get_followed_channels(self.config.borrow().twitch.clone());
|
||||
}
|
||||
|
||||
self.focused = !self.focused;
|
||||
|
Loading…
Reference in New Issue
Block a user