Skip to content

Commit

Permalink
use last modified from card on firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
TBS1996 committed Dec 30, 2024
1 parent 5d60ede commit 8235074
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 50 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,5 @@ petgraph = "0.6.5"
once_cell = "1.20.2"
strum = { version = "0.24", features = ["derive"] }
fancy-regex = "0.14.0"
prost-types = "0.13.4"

3 changes: 1 addition & 2 deletions speki-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use incread::inc_path;
use review::{review_menu, view_card};
use speki_core::{current_time, App, TimeProvider};
use speki_core::{
CardType, Attribute, AttributeCard, BackSide, CType, Card, CardId, ClassCard, EventCard,
Attribute, AttributeCard, BackSide, CType, Card, CardId, CardType, ClassCard, EventCard,
InstanceCard, NormalCard, SimpleRecall, StatementCard, TimeStamp, UnfinishedCard,
};
use utils::{
Expand Down Expand Up @@ -328,7 +328,6 @@ async fn main() {
} else if cli.recall.is_some() {
let id = cli.recall.unwrap();
let id: uuid::Uuid = id.parse().unwrap();
let id = CardId(id);
let x = app.load_card(id).await.unwrap().recall_rate();
dbg!(x);
} else if cli.concept.is_some() {
Expand Down
33 changes: 13 additions & 20 deletions speki-cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dialoguer::{theme::ColorfulTheme, Input, Select};
use speki_core::{CardType, App, Attribute, Card, CardId};
use speki_core::{App, Attribute, Card, CardId};
use speki_dto::AttributeId;
use speki_provider::paths;
use std::path::Path;
Expand All @@ -24,11 +24,9 @@ pub async fn select_from_subclass_cards(app: &App, class: CardId) -> Option<Card
.filter(|card| block_on(card.load_ancestor_classes()).contains(&class))
.collect();

enumselector::select_item_with_formatter(cards, |card: &Arc<Card>| {
block_on(card.print())
})?
.id()
.into()
enumselector::select_item_with_formatter(cards, |card: &Arc<Card>| block_on(card.print()))?
.id()
.into()
}

pub async fn select_from_all_instance_cards(app: &App) -> Option<CardId> {
Expand All @@ -38,23 +36,19 @@ pub async fn select_from_all_instance_cards(app: &App) -> Option<CardId> {
.into_iter()
.filter(|card| card.is_instance())
.collect();
enumselector::select_item_with_formatter(cards, |card: &Arc<Card>| {
block_on(card.print())
})?
.id()
.into()
enumselector::select_item_with_formatter(cards, |card: &Arc<Card>| block_on(card.print()))?
.id()
.into()
}

use futures::executor::block_on;
use std::sync::Arc;

pub async fn select_from_all_class_cards(app: &App) -> Option<CardId> {
let cards = app.load_all_cards().await;
enumselector::select_item_with_formatter(cards, |card: &Arc<Card>| {
block_on(card.print())
})?
.id()
.into()
enumselector::select_item_with_formatter(cards, |card: &Arc<Card>| block_on(card.print()))?
.id()
.into()
}

pub async fn select_from_class_attributes(app: &App, class: CardId) -> Option<AttributeId> {
Expand All @@ -75,10 +69,9 @@ pub fn select_from_attributes(attributes: Vec<Attribute>) -> Option<AttributeId>
}

pub async fn select_from_all_cards(app: &App) -> Option<CardId> {
enumselector::select_item_with_formatter(
app.load_all_cards().await,
|card: &Arc<Card>| block_on(card.print()).to_owned(),
)?
enumselector::select_item_with_formatter(app.load_all_cards().await, |card: &Arc<Card>| {
block_on(card.print()).to_owned()
})?
.id()
.into()
}
Expand Down
5 changes: 3 additions & 2 deletions speki-core/src/card/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ mod tests {
};

let record = Record {
id: id.to_string(),
content: s,
last_modified: self.time.current_time().as_secs(),
};
Expand Down Expand Up @@ -812,8 +813,8 @@ mod tests {
self.get_all(ty)
}

async fn save_content(&self, ty: Cty, id: Uuid, content: String) {
self.save(ty, id, content);
async fn save_content(&self, ty: Cty, record: Record) {
self.save(ty, record.id.parse().unwrap(), record.content);
}

async fn delete_content(&self, id: Uuid, ty: Cty) {
Expand Down
34 changes: 22 additions & 12 deletions speki-dto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ trait Item {
MergeRes::Neither
}
}

fn into_record(self) -> Record
where
Self: Sized,
{
let id = self.id().to_string();
let last_modified = self.last_modified().as_secs();
let content = self.serialize();

Record {
id,
content,
last_modified,
}
}
}

enum MergeRes<T> {
Expand Down Expand Up @@ -237,6 +252,7 @@ pub struct Review {

#[derive(Debug, Deserialize, Clone)]
pub struct Record {
pub id: String,
pub content: String,
pub last_modified: u64,
}
Expand Down Expand Up @@ -264,7 +280,7 @@ pub trait SpekiProvider: Sync {
.collect()
}

async fn save_content(&self, ty: Cty, id: Uuid, content: String);
async fn save_content(&self, ty: Cty, record: Record);

async fn delete_content(&self, id: Uuid, ty: Cty);

Expand Down Expand Up @@ -292,8 +308,8 @@ pub trait SpekiProvider: Sync {
}

async fn save_card(&self, card: RawCard) {
self.save_content(Cty::Card, card.id, toml::to_string(&card).unwrap())
.await;
let record = card.into_record();
self.save_content(Cty::Card, record).await;
}

async fn load_card(&self, id: CardId) -> Option<RawCard> {
Expand Down Expand Up @@ -321,12 +337,8 @@ pub trait SpekiProvider: Sync {
}

async fn save_attribute(&self, attribute: AttributeDTO) {
self.save_content(
Cty::Attribute,
attribute.id,
toml::to_string(&attribute).unwrap(),
)
.await;
self.save_content(Cty::Attribute, attribute.into_record())
.await;
}

async fn load_attribute(&self, id: AttributeId) -> Option<AttributeDTO> {
Expand All @@ -348,9 +360,7 @@ pub trait SpekiProvider: Sync {
}

async fn save_reviews(&self, reviews: History) {
let id = reviews.id;
let s = toml::to_string(&reviews).unwrap();
self.save_content(Cty::Review, id, s).await;
self.save_content(Cty::Review, reviews.into_record()).await;
}

async fn load_config(&self) -> Config;
Expand Down
7 changes: 5 additions & 2 deletions speki-provider/src/browserfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,11 @@ impl SpekiProvider for BrowserFsProvider {
js::load_all_files(self.folder_path(ty)).await
}

async fn save_content(&self, ty: Cty, id: Uuid, content: String) {
js::save_file(self.content_path(ty, id), &content);
async fn save_content(&self, ty: Cty, record: Record) {
js::save_file(
self.content_path(ty, record.id.parse().unwrap()),
&record.content,
);
}

async fn load_content(&self, id: Uuid, ty: Cty) -> Option<String> {
Expand Down
6 changes: 3 additions & 3 deletions speki-provider/src/dexie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ impl SpekiProvider for DexieProvider {
deleteContent(&cty_as_jsvalue(ty), &id);
}

async fn save_content(&self, ty: Cty, id: Uuid, content: String) {
let id = JsValue::from_str(&id.to_string());
let content = JsValue::from_str(&content);
async fn save_content(&self, ty: Cty, record: Record) {
let id = JsValue::from_str(&record.id.to_string());
let content = JsValue::from_str(&record.content);
saveContent(&cty_as_jsvalue(ty), &id, &content);
}

Expand Down
9 changes: 7 additions & 2 deletions speki-provider/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ fn load_record_from_path(path: &Path) -> Option<Record> {
return None;
}
let content = fs::read_to_string(&path).unwrap();
let id = path.file_name().unwrap().to_str().unwrap().to_string();
let last_modified = last_modified_path(path).unwrap().as_secs();
Some(Record {
id,
content,
last_modified,
})
Expand Down Expand Up @@ -92,8 +94,11 @@ impl SpekiProvider for FileProvider {
out
}

async fn save_content(&self, ty: Cty, id: Uuid, content: String) {
let path = file_path(ty, id);
async fn save_content(&self, ty: Cty, record: Record) {
let id = record.id;
let content = record.content;

let path = file_path(ty, id.parse().unwrap());
let mut file = fs::File::create(path).unwrap();
file.write_all(&mut content.as_bytes()).unwrap();
}
Expand Down
1 change: 1 addition & 0 deletions speki-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ once_cell.workspace = true
speki-provider = { workspace = true, features = ["dexie", "browserfs"] }
strum.workspace = true
fancy-regex.workspace = true
prost-types.workspace = true


speki-dto = { path = "../speki-dto" }
Expand Down
5 changes: 3 additions & 2 deletions speki-web/assets/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export async function loadRecord(userId, tableName, contentId) {

const data = docSnap.data();
return {
id: docSnap.id,
content: data.content,
last_modified: data.lastModified ? Math.floor(data.lastModified.toMillis() / 1000) : null
};
Expand Down Expand Up @@ -84,12 +85,12 @@ export async function loadAllIds(userId, tableName) {
return querySnapshot.docs.map(doc => doc.id);
}

export async function saveContent(userId, tableName, contentId, content) {
export async function saveContent(userId, tableName, contentId, content, lastModified) {
const docRef = doc(db, `users/${userId}/${tableName}`, contentId);
await setDoc(docRef, {
id: contentId,
content,
lastModified: serverTimestamp()
lastModified
}, { merge: true });
}

Expand Down
Loading

0 comments on commit 8235074

Please sign in to comment.