Skip to content

Commit

Permalink
twiq-light: Move TweetStore to store mod
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Dec 28, 2022
1 parent beab579 commit 31a5e72
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 73 deletions.
3 changes: 1 addition & 2 deletions twiq-light/src/fetch.rs
Expand Up @@ -4,9 +4,8 @@ use tracing::{debug, instrument};

use crate::{
domain::MyTweet,
store::TweetQueueStore,
store::{TweetQueueStore, TweetStore},
token::Token,
tweet_store::TweetStore,
twitter::{
self, GetUsersIdTweetsPathParams, GetUsersIdTweetsQueryParams, TweetResponseDataItem,
},
Expand Down
2 changes: 1 addition & 1 deletion twiq-light/src/import.rs
@@ -1,6 +1,6 @@
use std::{fs, path::Path};

use crate::{domain, tweet_store::TweetStore};
use crate::{domain, store::TweetStore};
use anyhow::Context;
use time::{
format_description::{self, well_known::Iso8601},
Expand Down
4 changes: 1 addition & 3 deletions twiq-light/src/main.rs
Expand Up @@ -12,12 +12,10 @@ mod search;
mod storage;
mod store;
mod token;
mod tweet_store;
mod twitter;

use anyhow::Context;
use store::TweetQueueStore;
use tweet_store::TweetStore;
use store::{TweetQueueStore, TweetStore};

#[derive(Debug, clap::Parser)]
#[command(author, version, about, long_about = None)]
Expand Down
2 changes: 1 addition & 1 deletion twiq-light/src/search.rs
@@ -1,6 +1,6 @@
use tracing::instrument;

use crate::tweet_store::TweetStore;
use crate::store::TweetStore;

#[instrument(skip_all)]
pub async fn run(store: TweetStore, query: Option<String>) -> anyhow::Result<()> {
Expand Down
70 changes: 4 additions & 66 deletions twiq-light/src/store.rs
@@ -1,67 +1,5 @@
use std::collections::VecDeque;
pub mod tweet;
pub mod tweet_queue;

use crate::{
domain::ScheduledTweet,
storage::{firestore::FirestoreStorage, Storage},
token::Token,
};

pub struct TweetQueueStore {
storage: FirestoreStorage,
}

impl TweetQueueStore {
const DATABASE_ID: &str = "(default)";
const COLLECTION_ID: &str = "twiq-light";
const QUEUE_DOCUMENT_ID: &str = "queue";
const TOKEN_DOCUMENT_ID: &str = "token";

pub async fn new(
project_id: String,
google_application_credentials: Option<String>,
) -> anyhow::Result<Self> {
let storage = FirestoreStorage::new(
google_application_credentials,
project_id,
Self::DATABASE_ID.to_owned(),
Self::COLLECTION_ID.to_owned(),
)
.await?;
Ok(Self { storage })
}

pub async fn read_all(&self) -> anyhow::Result<VecDeque<ScheduledTweet>> {
let data = self
.storage
.get_item(Self::QUEUE_DOCUMENT_ID.to_owned())
.await?;
Ok(match data {
Some(d) => serde_json::from_str(&d)?,
None => VecDeque::default(),
})
}

pub async fn read_token(&self) -> anyhow::Result<Option<Token>> {
let data = self
.storage
.get_item(Self::TOKEN_DOCUMENT_ID.to_owned())
.await?;
Ok(match data {
Some(d) => Some(serde_json::from_str(&d)?),
None => None,
})
}

pub async fn write_token(&self, token: &Token) -> anyhow::Result<()> {
self.storage
.set_item(Self::TOKEN_DOCUMENT_ID.to_owned(), token.to_string())
.await
}

pub async fn write_all(&self, data: &VecDeque<ScheduledTweet>) -> anyhow::Result<()> {
let s = serde_json::to_string(&data)?;
self.storage
.set_item(Self::QUEUE_DOCUMENT_ID.to_owned(), s)
.await
}
}
pub use tweet::TweetStore;
pub use tweet_queue::TweetQueueStore;
File renamed without changes.
67 changes: 67 additions & 0 deletions twiq-light/src/store/tweet_queue.rs
@@ -0,0 +1,67 @@
use std::collections::VecDeque;

use crate::{
domain::ScheduledTweet,
storage::{firestore::FirestoreStorage, Storage},
token::Token,
};

pub struct TweetQueueStore {
storage: FirestoreStorage,
}

impl TweetQueueStore {
const DATABASE_ID: &str = "(default)";
const COLLECTION_ID: &str = "twiq-light";
const QUEUE_DOCUMENT_ID: &str = "queue";
const TOKEN_DOCUMENT_ID: &str = "token";

pub async fn new(
project_id: String,
google_application_credentials: Option<String>,
) -> anyhow::Result<Self> {
let storage = FirestoreStorage::new(
google_application_credentials,
project_id,
Self::DATABASE_ID.to_owned(),
Self::COLLECTION_ID.to_owned(),
)
.await?;
Ok(Self { storage })
}

pub async fn read_all(&self) -> anyhow::Result<VecDeque<ScheduledTweet>> {
let data = self
.storage
.get_item(Self::QUEUE_DOCUMENT_ID.to_owned())
.await?;
Ok(match data {
Some(d) => serde_json::from_str(&d)?,
None => VecDeque::default(),
})
}

pub async fn read_token(&self) -> anyhow::Result<Option<Token>> {
let data = self
.storage
.get_item(Self::TOKEN_DOCUMENT_ID.to_owned())
.await?;
Ok(match data {
Some(d) => Some(serde_json::from_str(&d)?),
None => None,
})
}

pub async fn write_token(&self, token: &Token) -> anyhow::Result<()> {
self.storage
.set_item(Self::TOKEN_DOCUMENT_ID.to_owned(), token.to_string())
.await
}

pub async fn write_all(&self, data: &VecDeque<ScheduledTweet>) -> anyhow::Result<()> {
let s = serde_json::to_string(&data)?;
self.storage
.set_item(Self::QUEUE_DOCUMENT_ID.to_owned(), s)
.await
}
}

0 comments on commit 31a5e72

Please sign in to comment.