Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer streaming URL from instance information #204

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/firefish_streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use megalodon::{generator, streaming::Message};
async fn main() {
env_logger::init();

let Ok(url) = env::var("FIREFISH_STREAMING_URL") else {
println!("Specify FIREFISH_STREAMING_URL!!");
let Ok(url) = env::var("FIREFISH_URL") else {
println!("Specify FIREFISH_URL!!");
return;
};
let Ok(token) = env::var("FIREFISH_ACCESS_TOKEN") else {
Expand All @@ -25,7 +25,7 @@ async fn streaming(url: &str, access_token: String) {
Some(access_token),
None,
);
let streaming = client.local_streaming(url.to_string());
let streaming = client.local_streaming().await;

streaming
.listen(Box::new(|message| match message {
Expand Down
10 changes: 5 additions & 5 deletions examples/mastodon_streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use std::env;
async fn main() {
env_logger::init();

let Ok(url) = env::var("MASTODON_STREAMING_URL") else {
println!("Specify MASTODON_STREAMING_URL!!");
return
let Ok(url) = env::var("MASTODON_URL") else {
println!("Specify MASTODON_URL!!");
return;
};
let Ok(token) = env::var("MASTODON_ACCESS_TOKEN") else {
println!("Specify MASTODON_ACCESS_TOKEN!!");
return
return;
};

streaming(url.as_str(), token).await;
Expand All @@ -24,7 +24,7 @@ async fn streaming(url: &str, access_token: String) {
Some(access_token),
None,
);
let streaming = client.local_streaming(url.to_string());
let streaming = client.local_streaming().await;

streaming
.listen(Box::new(|message| match message {
Expand Down
8 changes: 4 additions & 4 deletions examples/mastodon_unauthorized_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use std::env;
async fn main() {
env_logger::init();

let Ok(url) = env::var("MASTODON_STREAMING_URL") else {
println!("Specify MASTODON_STREAMING_URL!!");
return
let Ok(url) = env::var("MASTODON_URL") else {
println!("Specify MASTODON_URL!!");
return;
};

streaming(url.as_str()).await;
}

async fn streaming(url: &str) {
let client = generator(megalodon::SNS::Mastodon, url.to_string(), None, None);
let streaming = client.local_streaming(url.to_string());
let streaming = client.local_streaming().await;

streaming
.listen(Box::new(|message| match message {
Expand Down
10 changes: 5 additions & 5 deletions examples/pleroma_streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use std::env;
async fn main() {
env_logger::init();

let Ok(url) = env::var("PLEROMA_STREAMING_URL") else {
println!("Specify PLEROMA_STREAMING_URL!!");
return
let Ok(url) = env::var("PLEROMA_URL") else {
println!("Specify PLEROMA_URL!!");
return;
};
let Ok(token) = env::var("PLEROMA_ACCESS_TOKEN") else {
println!("Specify PLEROMA_ACCESS_TOKEN!!");
return
return;
};

streaming(url.as_str(), token).await;
Expand All @@ -24,7 +24,7 @@ async fn streaming(url: &str, access_token: String) {
Some(access_token),
None,
);
let streaming = client.user_streaming(url.to_string());
let streaming = client.user_streaming().await;

streaming
.listen(Box::new(|message| match message {
Expand Down
50 changes: 30 additions & 20 deletions src/firefish/firefish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2429,9 +2429,22 @@ impl megalodon::Megalodon for Firefish {
))
}

fn user_streaming(&self, streaming_url: String) -> Box<dyn Streaming + Send + Sync> {
async fn streaming_url(&self) -> String {
let instance = self.get_instance().await;
if let Ok(instance) = instance {
match instance.json.urls {
Some(urls) => return urls.streaming_api,
_ => {}
};
}

self.base_url.clone()
}

async fn user_streaming(&self) -> Box<dyn Streaming + Send + Sync> {
let streaming_url = self.streaming_url().await;
let c = WebSocket::new(
streaming_url + "/streaming",
streaming_url,
String::from("user"),
None,
self.access_token.clone(),
Expand All @@ -2441,9 +2454,10 @@ impl megalodon::Megalodon for Firefish {
Box::new(c)
}

fn public_streaming(&self, streaming_url: String) -> Box<dyn Streaming + Send + Sync> {
async fn public_streaming(&self) -> Box<dyn Streaming + Send + Sync> {
let streaming_url = self.streaming_url().await;
let c = WebSocket::new(
streaming_url + "/streaming",
streaming_url,
String::from("globalTimeline"),
None,
self.access_token.clone(),
Expand All @@ -2453,9 +2467,10 @@ impl megalodon::Megalodon for Firefish {
Box::new(c)
}

fn local_streaming(&self, streaming_url: String) -> Box<dyn Streaming + Send + Sync> {
async fn local_streaming(&self) -> Box<dyn Streaming + Send + Sync> {
let streaming_url = self.streaming_url().await;
let c = WebSocket::new(
streaming_url + "/streaming",
streaming_url,
String::from("localTimeline"),
None,
self.access_token.clone(),
Expand All @@ -2465,9 +2480,10 @@ impl megalodon::Megalodon for Firefish {
Box::new(c)
}

fn direct_streaming(&self, streaming_url: String) -> Box<dyn Streaming + Send + Sync> {
async fn direct_streaming(&self) -> Box<dyn Streaming + Send + Sync> {
let streaming_url = self.streaming_url().await;
let c = WebSocket::new(
streaming_url + "/streaming",
streaming_url,
String::from("conversation"),
None,
self.access_token.clone(),
Expand All @@ -2477,13 +2493,10 @@ impl megalodon::Megalodon for Firefish {
Box::new(c)
}

fn tag_streaming(
&self,
streaming_url: String,
_tag: String,
) -> Box<dyn Streaming + Send + Sync> {
async fn tag_streaming(&self, _tag: String) -> Box<dyn Streaming + Send + Sync> {
let streaming_url = self.streaming_url().await;
let c = WebSocket::new(
streaming_url + "/streaming",
streaming_url,
String::from("hashtag"),
None,
self.access_token.clone(),
Expand All @@ -2493,13 +2506,10 @@ impl megalodon::Megalodon for Firefish {
Box::new(c)
}

fn list_streaming(
&self,
streaming_url: String,
list_id: String,
) -> Box<dyn Streaming + Send + Sync> {
async fn list_streaming(&self, list_id: String) -> Box<dyn Streaming + Send + Sync> {
let streaming_url = self.streaming_url().await;
let c = WebSocket::new(
streaming_url + "/streaming",
streaming_url,
String::from("list"),
Some(list_id),
self.access_token.clone(),
Expand Down
32 changes: 18 additions & 14 deletions src/friendica/friendica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2692,45 +2692,49 @@ impl megalodon::Megalodon for Friendica {
))
}

fn user_streaming(&self, _streaming_url: String) -> Box<dyn Streaming + Send + Sync> {
async fn streaming_url(&self) -> String {
let instance = self.get_instance().await;
if let Ok(instance) = instance {
match instance.json.urls {
Some(urls) => return urls.streaming_api,
_ => {}
};
}

self.base_url.clone()
}

async fn user_streaming(&self) -> Box<dyn Streaming + Send + Sync> {
let c = WebSocket::new();

Box::new(c)
}

fn public_streaming(&self, _streaming_url: String) -> Box<dyn Streaming + Send + Sync> {
async fn public_streaming(&self) -> Box<dyn Streaming + Send + Sync> {
let c = WebSocket::new();

Box::new(c)
}

fn local_streaming(&self, _streaming_url: String) -> Box<dyn Streaming + Send + Sync> {
async fn local_streaming(&self) -> Box<dyn Streaming + Send + Sync> {
let c = WebSocket::new();

Box::new(c)
}

fn direct_streaming(&self, _streaming_url: String) -> Box<dyn Streaming + Send + Sync> {
async fn direct_streaming(&self) -> Box<dyn Streaming + Send + Sync> {
let c = WebSocket::new();

Box::new(c)
}

fn tag_streaming(
&self,
_streaming_url: String,
_tag: String,
) -> Box<dyn Streaming + Send + Sync> {
async fn tag_streaming(&self, _tag: String) -> Box<dyn Streaming + Send + Sync> {
let c = WebSocket::new();

Box::new(c)
}

fn list_streaming(
&self,
_streaming_url: String,
_list_id: String,
) -> Box<dyn Streaming + Send + Sync> {
async fn list_streaming(&self, _list_id: String) -> Box<dyn Streaming + Send + Sync> {
let c = WebSocket::new();

Box::new(c)
Expand Down
38 changes: 24 additions & 14 deletions src/mastodon/mastodon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3020,8 +3020,21 @@ impl megalodon::Megalodon for Mastodon {
))
}

fn user_streaming(&self, streaming_url: String) -> Box<dyn Streaming + Send + Sync> {
async fn streaming_url(&self) -> String {
let instance = self.get_instance().await;
if let Ok(instance) = instance {
match instance.json.urls {
Some(urls) => return urls.streaming_api,
_ => {}
};
}

self.base_url.clone()
}

async fn user_streaming(&self) -> Box<dyn Streaming + Send + Sync> {
let params = Vec::<String>::new();
let streaming_url = self.streaming_url().await;
let c = WebSocket::new(
streaming_url + "/api/v1/streaming",
String::from("user"),
Expand All @@ -3033,8 +3046,9 @@ impl megalodon::Megalodon for Mastodon {
Box::new(c)
}

fn public_streaming(&self, streaming_url: String) -> Box<dyn Streaming + Send + Sync> {
async fn public_streaming(&self) -> Box<dyn Streaming + Send + Sync> {
let params = Vec::<String>::new();
let streaming_url = self.streaming_url().await;
let c = WebSocket::new(
streaming_url + "/api/v1/streaming",
String::from("public"),
Expand All @@ -3046,8 +3060,9 @@ impl megalodon::Megalodon for Mastodon {
Box::new(c)
}

fn local_streaming(&self, streaming_url: String) -> Box<dyn Streaming + Send + Sync> {
async fn local_streaming(&self) -> Box<dyn Streaming + Send + Sync> {
let params = Vec::<String>::new();
let streaming_url = self.streaming_url().await;
let c = WebSocket::new(
streaming_url + "/api/v1/streaming",
String::from("public:local"),
Expand All @@ -3059,8 +3074,9 @@ impl megalodon::Megalodon for Mastodon {
Box::new(c)
}

fn direct_streaming(&self, streaming_url: String) -> Box<dyn Streaming + Send + Sync> {
async fn direct_streaming(&self) -> Box<dyn Streaming + Send + Sync> {
let params = Vec::<String>::new();
let streaming_url = self.streaming_url().await;
let c = WebSocket::new(
streaming_url + "/api/v1/streaming",
String::from("direct"),
Expand All @@ -3072,12 +3088,9 @@ impl megalodon::Megalodon for Mastodon {
Box::new(c)
}

fn tag_streaming(
&self,
streaming_url: String,
tag: String,
) -> Box<dyn Streaming + Send + Sync> {
async fn tag_streaming(&self, tag: String) -> Box<dyn Streaming + Send + Sync> {
let params = Vec::<String>::from([format!("tag={}", tag)]);
let streaming_url = self.streaming_url().await;
let c = WebSocket::new(
streaming_url + "/api/v1/streaming",
String::from("hashtag"),
Expand All @@ -3089,12 +3102,9 @@ impl megalodon::Megalodon for Mastodon {
Box::new(c)
}

fn list_streaming(
&self,
streaming_url: String,
list_id: String,
) -> Box<dyn Streaming + Send + Sync> {
async fn list_streaming(&self, list_id: String) -> Box<dyn Streaming + Send + Sync> {
let params = Vec::<String>::from([format!("list={}", list_id)]);
let streaming_url = self.streaming_url().await;
let c = WebSocket::new(
streaming_url + "/api/v1/streaming",
String::from("list"),
Expand Down
20 changes: 9 additions & 11 deletions src/megalodon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,28 +744,26 @@ pub trait Megalodon {
// ======================================
// Streaming
// ======================================
/// Get the base URL for streaming endpoints
async fn streaming_url(&self) -> String;

/// Get user streaming object.
fn user_streaming(&self, streaming_url: String) -> Box<dyn Streaming + Send + Sync>;
async fn user_streaming(&self) -> Box<dyn Streaming + Send + Sync>;

/// Get public streaming object.
fn public_streaming(&self, streaming_url: String) -> Box<dyn Streaming + Send + Sync>;
async fn public_streaming(&self) -> Box<dyn Streaming + Send + Sync>;

/// Get local streaming object.
fn local_streaming(&self, streaming_url: String) -> Box<dyn Streaming + Send + Sync>;
async fn local_streaming(&self) -> Box<dyn Streaming + Send + Sync>;

/// Get direct streaming object.
fn direct_streaming(&self, streaming_url: String) -> Box<dyn Streaming + Send + Sync>;
async fn direct_streaming(&self) -> Box<dyn Streaming + Send + Sync>;

/// Get tag streaming object.
fn tag_streaming(&self, streaming_url: String, tag: String)
-> Box<dyn Streaming + Send + Sync>;
async fn tag_streaming(&self, tag: String) -> Box<dyn Streaming + Send + Sync>;

/// Get list streaming object.
fn list_streaming(
&self,
streaming_url: String,
list_id: String,
) -> Box<dyn Streaming + Send + Sync>;
async fn list_streaming(&self, list_id: String) -> Box<dyn Streaming + Send + Sync>;
}

/// Input options for [`Megalodon::register_app`] and [`Megalodon::create_app`].
Expand Down
Loading