Compare commits
	
		
			2 Commits 
		
	
	
		
			80e0fce32a
			...
			176bde9d01
		
	
	| Author | SHA1 | Date | 
|---|---|---|
| 
							
							
								
									
								
								 | 
						176bde9d01 | 6 years ago | 
| 
							
							
								
									
								
								 | 
						dc88755e7b | 6 years ago | 
@ -0,0 +1,88 @@ | 
				
			||||
//! Mastodon API types and functions building on elefren
 | 
				
			||||
 | 
				
			||||
use serde::Deserialize; | 
				
			||||
use serde::Serialize; | 
				
			||||
use crate::store::Store; | 
				
			||||
use crate::Config; | 
				
			||||
use elefren::{Registration, Mastodon}; | 
				
			||||
use elefren::http_send::HttpSender; | 
				
			||||
use elefren::helpers::cli; | 
				
			||||
use elefren::scopes::Scopes; | 
				
			||||
use std::str::FromStr; | 
				
			||||
use failure::Fallible; | 
				
			||||
 | 
				
			||||
const KEY_OAUTH_REGISTRATION: &str = "oauth.registration"; | 
				
			||||
const KEY_OAUTH_SESSION: &str = "oauth.session"; | 
				
			||||
 | 
				
			||||
pub type EleRegistratered = elefren::registration::Registered<HttpSender>; | 
				
			||||
pub type EleSession = elefren::Mastodon<HttpSender>; | 
				
			||||
pub type EleStreamSocket = websocket::sync::Client<native_tls::TlsStream<std::net::TcpStream>>; | 
				
			||||
 | 
				
			||||
/// Wrapper for the long tuple with Registration state
 | 
				
			||||
#[derive(Serialize,Deserialize,Debug)] | 
				
			||||
pub struct ElefrenRegistration { | 
				
			||||
    pub parts: (String, String, String, String, Scopes, bool), | 
				
			||||
} | 
				
			||||
 | 
				
			||||
 | 
				
			||||
/// Register "app" in the server software
 | 
				
			||||
pub fn register(store : &mut Store, config : &Config) -> EleRegistratered { | 
				
			||||
    match store.get::<ElefrenRegistration>(KEY_OAUTH_REGISTRATION) { | 
				
			||||
        Some(reg) => { | 
				
			||||
            info!("Loaded registration from store"); | 
				
			||||
 | 
				
			||||
            EleRegistratered::from_parts( | 
				
			||||
                // this sucks
 | 
				
			||||
                ®.parts.0, ®.parts.1, ®.parts.2, ®.parts.3, reg.parts.4, reg.parts.5 | 
				
			||||
            ) | 
				
			||||
        } | 
				
			||||
        None => { | 
				
			||||
            info!("Creating a new registration"); | 
				
			||||
 | 
				
			||||
            let registered = Registration::new(&format!("https://{}", config.instance)) | 
				
			||||
                .client_name("manabu") | 
				
			||||
                .scopes(Scopes::from_str("read write").expect("err parse scopes")) | 
				
			||||
                .build().expect("error register"); | 
				
			||||
            store.put(KEY_OAUTH_REGISTRATION, ElefrenRegistration { parts : registered.clone().into_parts() }); | 
				
			||||
            registered | 
				
			||||
        } | 
				
			||||
    } | 
				
			||||
} | 
				
			||||
 | 
				
			||||
/// Open mastodon API session (get access token)
 | 
				
			||||
pub fn open_session(store : &mut Store, registered: EleRegistratered) -> EleSession { | 
				
			||||
    match store.get::<elefren::Data>(KEY_OAUTH_SESSION) { | 
				
			||||
        Some(data) => { | 
				
			||||
            info!("Reusing saved authorization."); | 
				
			||||
 | 
				
			||||
            let cli = Mastodon::from(data); | 
				
			||||
            // TODO check if the session is live, somehow
 | 
				
			||||
            cli | 
				
			||||
        } | 
				
			||||
        None => { | 
				
			||||
            info!("Creating new authorization."); | 
				
			||||
 | 
				
			||||
            let cli = cli::authenticate(registered).expect("error auth"); | 
				
			||||
            store.put(KEY_OAUTH_SESSION, cli.data.clone()); | 
				
			||||
            cli | 
				
			||||
        } | 
				
			||||
    } | 
				
			||||
} | 
				
			||||
 | 
				
			||||
/// Open streaming api websocket
 | 
				
			||||
pub fn open_stream_websocket(session : &EleSession, stream_name : &str) -> Fallible<EleStreamSocket> { | 
				
			||||
    let connector = native_tls::TlsConnector::new()?; | 
				
			||||
 | 
				
			||||
    let hostname = &session.data.base[session.data.base.find("://").unwrap()+3..]; | 
				
			||||
 | 
				
			||||
    let url = format!("wss://{host}/api/v1/streaming/?stream={sname}&access_token={token}", | 
				
			||||
                      host=hostname, | 
				
			||||
                      sname=stream_name, | 
				
			||||
                      token=session.data.token); | 
				
			||||
 | 
				
			||||
    debug!("WS url = {}", &url); | 
				
			||||
 | 
				
			||||
    Ok(websocket::ClientBuilder::new(&url) | 
				
			||||
        .expect("Error create ClientBuilder") | 
				
			||||
        .connect_secure(Some(connector))?) | 
				
			||||
} | 
				
			||||
					Loading…
					
					
				
		Reference in new issue