Skip to content

Commit

Permalink
unified Id
Browse files Browse the repository at this point in the history
  • Loading branch information
xiao-e-yun committed Feb 1, 2025
1 parent 5129f13 commit 447e8ca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
9 changes: 4 additions & 5 deletions src/creator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn sync_creators(
let link = || Link::new("fanbox", &format!("https://{}.fanbox.cc/", creator.id()));

let author = match get_alias_stmt
.query_row([&alias], |row| row.get::<_, u32>(0))
.query_row([&alias], |row| row.get::<_, AuthorId>(0))
.optional()?
{
Some(id) => {
Expand All @@ -109,7 +109,7 @@ pub fn sync_creators(
links.push(link);
links.sort();
let links = serde_json::to_string(&links)?;
update_author_stmt.execute(params![links, author.id.raw()])?;
update_author_stmt.execute(params![links, author.id])?;
}

author
Expand All @@ -126,7 +126,7 @@ pub fn sync_creators(
let author =
insert_author_stmt.query_row(params![name, links], row_to_author)?;
insert_alias_stmt
.execute(params![alias, author.id.raw()])
.execute(params![alias, author.id])
.unwrap();
author
}
Expand All @@ -141,8 +141,7 @@ pub fn sync_creators(
let links: Vec<Link> =
serde_json::from_str(&links).expect("Author links is not valid JSON");

let thumb: Option<u32> = row.get("id")?;
let thumb: Option<FileMetaId> = thumb.map(FileMetaId::new);
let thumb: Option<FileMetaId> = row.get("id")?;

let updated: DateTime<Utc> = row.get("updated")?;

Expand Down
34 changes: 17 additions & 17 deletions src/post/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
use chrono::{DateTime, Utc};
use futures::future::join_all;
use log::{error, info};
use post_archiver::{AuthorId, Content, PostId};
use post_archiver::{AuthorId, Content, FileMetaId, PostId, PostTagId};
use rusqlite::{params, Connection, OptionalExtension, Transaction};

pub async fn get_post_urls(
Expand Down Expand Up @@ -73,13 +73,13 @@ pub async fn sync_posts(
config: &Config,
creator: &SyncedCreator,
posts: Vec<Post>,
fanbox_and_free_tag: (u32, u32),
fanbox_and_free_tag: (PostTagId, PostTagId),
) -> Result<(), Box<dyn std::error::Error>> {
let total_posts = posts.len();
let mut synced_posts = 0;

let mut all_files = vec![];
let author = creator.author().id.raw();
let author = creator.author().id;
let mut tx = conn.transaction()?;
for post in posts {
info!(" syncing {}", post.title());
Expand Down Expand Up @@ -110,9 +110,9 @@ pub async fn sync_posts(

fn sync_post(
tx: &mut Transaction,
author: u32,
author: AuthorId,
post: Post,
fanbox_and_free_tag: (u32, u32),
fanbox_and_free_tag: (PostTagId, PostTagId),
) -> Result<Vec<SyncedFile>, Box<dyn std::error::Error>> {
let post_id = sync_post_meta(tx, author, &post, fanbox_and_free_tag)?;
let body = post.body();
Expand All @@ -128,10 +128,10 @@ pub async fn sync_posts(

fn sync_post_meta(
tx: &mut Transaction,
author: u32,
author: AuthorId,
post: &Post,
(fanbox_tag, free_tag): (u32, u32),
) -> Result<u32, Box<dyn std::error::Error>> {
(fanbox_tag, free_tag): (PostTagId, PostTagId),
) -> Result<PostId, Box<dyn std::error::Error>> {
let mut select_post_stmt = tx.prepare_cached("SELECT id FROM posts WHERE source = ?")?;
let mut update_post_stmt =
tx.prepare_cached("UPDATE posts SET updated = ? WHERE id = ?")?;
Expand All @@ -145,7 +145,7 @@ pub async fn sync_posts(
let updated = post.updated_datetime;
let published = post.published_datetime;

let post_id: u32 = match select_post_stmt
let post_id: PostId = match select_post_stmt
.query_row(params![source], |row| row.get(0))
.optional()
.unwrap()
Expand Down Expand Up @@ -174,7 +174,7 @@ pub async fn sync_posts(

fn sync_post_content(
tx: &mut Transaction,
post_id: u32,
post_id: PostId,
content: Vec<Content>,
) -> Result<(), Box<dyn std::error::Error>> {
let mut insert_post_stmt =
Expand All @@ -189,21 +189,21 @@ pub async fn sync_posts(
fn sync_files(
tx: &mut Transaction,
post_body: &PostBody,
author: u32,
post: u32,
author: AuthorId,
post: PostId,
) -> Result<Vec<SyncedFile>, Box<dyn std::error::Error>> {
let mut insert_file_stmt = tx.prepare_cached(
"INSERT INTO file_metas (filename,author,post,mime,extra) VALUES (?,?,?,?,?) RETURNING id",
)?;
let files = post_body.files(AuthorId::from(author), PostId::from(post));
let mut collect = vec![];
for file in files {
let id: u32 = insert_file_stmt
let id: FileMetaId = insert_file_stmt
.query_row(
params![
&file.filename,
file.author.raw(),
file.post.raw(),
file.author,
file.post,
&file.mime,
serde_json::to_string(&file.extra).unwrap(),
],
Expand Down Expand Up @@ -261,7 +261,7 @@ async fn download_files(
Ok(())
}

pub fn get_or_insert_tag(conn: &mut Connection, name: &str) -> Result<u32, rusqlite::Error> {
pub fn get_or_insert_tag(conn: &mut Connection, name: &str) -> Result<PostTagId, rusqlite::Error> {
match conn
.query_row("SELECT id FROM tags WHERE name = ?", [name], |row| {
row.get(0)
Expand All @@ -286,5 +286,5 @@ pub struct SyncedFile {
pub path: PathBuf,
pub url: String,
pub raw_id: String,
pub id: u32,
pub id: FileMetaId,
}

0 comments on commit 447e8ca

Please sign in to comment.