Skip to content

Commit

Permalink
Misskey 2023.11.0-beta.4 compatible (only aid)
Browse files Browse the repository at this point in the history
  • Loading branch information
caipira113 committed Oct 29, 2023
1 parent e599598 commit 773f009
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ This tool is designed to migrate data from PostgreSQL to Meilisearch and is inte

![Screenshot](assets/Screenshot_2.png)

## Warning
If you have Misskey 2023.11.0-beta.4 or earlier, please set the `idtype` in your configuration to `null`.

**Currently, we only support AID.**

## Docker Image
```docker
asia-docker.pkg.dev/libnare/mk-meili-mgn/main:latest
Expand Down
1 change: 1 addition & 0 deletions config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"index": "undefined"
},
"option": {
"idtype": "aid",
"localonly": false,
"limit": null
}
Expand Down
13 changes: 13 additions & 0 deletions src/aid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::time::{Duration, SystemTime};
use chrono::{DateTime, Utc};

const TIME2000: i64 = 946_684_800_000;

pub(crate) fn parse(id: String) -> DateTime<Utc> {
let time = i64::from_str_radix(&id[..8], 36).unwrap() + TIME2000;
let system_time = SystemTime::UNIX_EPOCH
.checked_add(Duration::from_millis(time as u64))
.ok_or("Failed to parse AID: Invalid Date").unwrap();

DateTime::<Utc>::from(system_time)
}
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct MeiliConfig {

#[derive(Debug, Deserialize)]
pub struct OptionConfig {
pub idtype: Option<String>,
pub localonly: bool,
pub limit: Option<i64>,
}
Expand Down
57 changes: 42 additions & 15 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use tokio_postgres::{Client, NoTls};

use crate::config::config;
use crate::r#struct::Notes;
use crate::aid;

pub async fn connect_db() -> Result<Client, Box<dyn Error>> {
let config = config()?;
Expand Down Expand Up @@ -33,7 +34,13 @@ pub async fn query_notes(db: &Client) -> Result<Vec<Notes>, Box<dyn Error>> {
let config = config()?;

let mut query = std::string::String::from("
SELECT id, \"createdAt\", \"userId\", \"userHost\", \"channelId\", cw, text, tags
SELECT id, ");

if config.option.idtype.is_none() {
query.push_str("\"createdAt\", ");
}

query.push_str("\"userId\", \"userHost\", \"channelId\", cw, text, tags
FROM note
WHERE COALESCE(text, cw) IS NOT NULL
AND visibility IN ('home', 'public')
Expand All @@ -51,20 +58,40 @@ pub async fn query_notes(db: &Client) -> Result<Vec<Notes>, Box<dyn Error>> {

let mut data_vec = Vec::new();

for row in rows {
let created_at: DateTime<Utc> = row.get("createdAt");
let notes = Notes {
id: row.get("id"),
created_at: created_at.timestamp() * 1000 + created_at.timestamp_subsec_millis() as i64,
user_id: row.get("userId"),
user_host: row.get("userHost"),
channel_id: row.get("channelId"),
cw: row.get("cw"),
text: row.get("text"),
tags: row.get("tags"),
};

data_vec.push(notes);
if config.option.idtype.is_none() {
for row in rows {
let created_at: DateTime<Utc> = row.get("createdAt");
let notes = Notes {
id: row.get("id"),
created_at: created_at.timestamp() * 1000 + created_at.timestamp_subsec_millis() as i64,
user_id: row.get("userId"),
user_host: row.get("userHost"),
channel_id: row.get("channelId"),
cw: row.get("cw"),
text: row.get("text"),
tags: row.get("tags"),
};

data_vec.push(notes);
}
} else if config.option.idtype.as_ref().unwrap() == "aid" {
for row in rows {
let created_at: DateTime<Utc> = aid::parse(row.get("id"));
let notes = Notes {
id: row.get("id"),
created_at: created_at.timestamp() * 1000 + created_at.timestamp_subsec_millis() as i64,
user_id: row.get("userId"),
user_host: row.get("userHost"),
channel_id: row.get("channelId"),
cw: row.get("cw"),
text: row.get("text"),
tags: row.get("tags"),
};

data_vec.push(notes);
}
} else {
panic!("Invalid idtype: {}", config.option.idtype.as_ref().unwrap());
}

Ok(data_vec)
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod database;
mod config;
mod r#struct;
mod meili;
mod aid;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
Expand Down

0 comments on commit 773f009

Please sign in to comment.