Skip to content

Commit

Permalink
Merge pull request #18 from sugyan/main
Browse files Browse the repository at this point in the history
Release 2024-03-27 22:37:24 +0900
  • Loading branch information
sugyan committed Mar 27, 2024
2 parents e8dde3f + 72c669a commit 3c7f341
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 150 deletions.
148 changes: 28 additions & 120 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tauri-build = { version = "2.0.0-beta", features = [] }

[dependencies]
async-trait = "0.1.77"
atrium-api = "0.18.2"
atrium-api = "0.19.0"
atrium-xrpc-client = "0.5.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
82 changes: 56 additions & 26 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::consts::EmitEvent;
use crate::error::Error;
use crate::events::PostEvent;
use crate::session_store::FileStore;
use crate::state::State;
use atrium_api::agent::AtpAgent;
use atrium_api::app::bsky::feed::defs::{FeedViewPost, ReplyRefParentEnum};
use atrium_api::types::Collection;
use atrium_xrpc_client::reqwest::ReqwestClient;
use std::collections::HashSet;
use std::collections::HashMap;
use std::ops::DerefMut;
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -61,38 +64,65 @@ async fn background_task(
mut receiver: tokio::sync::oneshot::Receiver<()>,
app_handle: tauri::AppHandle,
) -> Result<(), Error> {
let mut cids = HashSet::new();
async fn check_new_post(
app_handle: &tauri::AppHandle,
agent: &Arc<AtpAgent<FileStore, ReqwestClient>>,
cids: &mut HashMap<String, FeedViewPost>,
) -> Result<(), Error> {
println!("checking posts");
let output = agent
.api
.app
.bsky
.feed
.get_timeline(atrium_api::app::bsky::feed::get_timeline::Parameters {
algorithm: None,
cursor: None,
limit: 30.try_into().ok(),
})
.await?;
for post in output.feed.iter().rev() {
let cid = post.post.cid.as_ref().to_string();
if let Some(prev) = cids.get(&cid) {
if post.reason != prev.reason
|| post.post.reply_count != prev.post.reply_count
|| post.post.repost_count != prev.post.repost_count
|| post.post.like_count != prev.post.like_count
{
app_handle
.emit(EmitEvent::Post.as_ref(), PostEvent::Update(post.clone()))
.expect("failed to emit post event");
}
} else {
if let Some(reply) = &post.reply {
if let ReplyRefParentEnum::PostView(post_view) = &reply.parent {
if let Some(prev) = cids.get(&post_view.cid.as_ref().to_string()) {
app_handle
.emit(
EmitEvent::Post.as_ref(),
PostEvent::Delete(prev.post.clone()),
)
.expect("failed to emit post event");
}
}
}
app_handle
.emit(EmitEvent::Post.as_ref(), PostEvent::Add(post.clone()))
.expect("failed to emit post event");
}
cids.insert(cid, post.clone());
}
Ok(())
}
let mut cids = HashMap::new();
let mut interval = tokio::time::interval(Duration::from_secs(60));
loop {
tokio::select! {
_ = &mut receiver => {
println!("cancelling");
break;
}
_ = interval.tick() => {
println!("checking for new posts");
if agent.api.app.bsky.feed.get_timeline(atrium_api::app::bsky::feed::get_timeline::Parameters {
algorithm: None,
cursor: None,
limit: 1.try_into().ok(),
}).await?.feed.first().map(|post| !cids.contains(post.post.cid.as_ref())).unwrap_or_default() {
println!("found new post");
let output = agent.api.app.bsky.feed.get_timeline(atrium_api::app::bsky::feed::get_timeline::Parameters {
algorithm: None,
cursor: None,
limit: 30.try_into().ok(),
}).await?;
for post in output.feed.iter().rev() {
let cid = *post.post.cid.as_ref();
if cids.contains(&cid) {
continue;
}
cids.insert(cid);
println!("emit {cid}");
app_handle.emit("post", post).expect("failed to emit post event");
}
}
}
_ = interval.tick() => check_new_post(&app_handle, &agent, &mut cids).await?,
}
}
Ok(())
Expand Down
12 changes: 12 additions & 0 deletions src-tauri/src/consts.rs
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
pub const MENU_ID_RELOAD: &str = "tauri-reload";

pub enum EmitEvent {
Post,
}

impl AsRef<str> for EmitEvent {
fn as_ref(&self) -> &str {
match self {
Self::Post => "post",
}
}
}
10 changes: 10 additions & 0 deletions src-tauri/src/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use atrium_api::app::bsky::feed::defs::{FeedViewPost, PostView};
use serde::Serialize;

#[derive(Serialize, Clone)]
#[serde(tag = "$type", rename_all = "lowercase")]
pub enum PostEvent {
Add(FeedViewPost),
Update(FeedViewPost),
Delete(PostView),
}
1 change: 1 addition & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod commands;
mod consts;
mod error;
mod events;
mod session_store;
mod state;

Expand Down

0 comments on commit 3c7f341

Please sign in to comment.