Use ULIDs instead of v4 UUIDs for identifiers

This commit is contained in:
silverpill 2021-12-01 23:26:59 +00:00
parent b2150f9259
commit 12c21d86f8
12 changed files with 45 additions and 11 deletions

18
Cargo.lock generated
View file

@ -562,11 +562,13 @@ version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [ dependencies = [
"js-sys",
"libc", "libc",
"num-integer", "num-integer",
"num-traits", "num-traits",
"serde", "serde",
"time 0.1.44", "time 0.1.44",
"wasm-bindgen",
"winapi 0.3.9", "winapi 0.3.9",
] ]
@ -1144,8 +1146,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
"js-sys",
"libc", "libc",
"wasi 0.10.0+wasi-snapshot-preview1", "wasi 0.10.0+wasi-snapshot-preview1",
"wasm-bindgen",
] ]
[[package]] [[package]]
@ -1717,6 +1721,7 @@ dependencies = [
"thiserror", "thiserror",
"tokio", "tokio",
"tokio-postgres", "tokio-postgres",
"ulid",
"url 2.2.2", "url 2.2.2",
"uuid", "uuid",
"web3", "web3",
@ -2374,6 +2379,7 @@ dependencies = [
"libc", "libc",
"rand_core 0.4.2", "rand_core 0.4.2",
"rdrand", "rdrand",
"wasm-bindgen",
"winapi 0.3.9", "winapi 0.3.9",
] ]
@ -3301,6 +3307,18 @@ dependencies = [
"static_assertions", "static_assertions",
] ]
[[package]]
name = "ulid"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e95a59b292ca0cf9b45be2e52294d1ca6cb24eb11b08ef4376f73f1a00c549"
dependencies = [
"chrono",
"lazy_static",
"rand 0.6.5",
"uuid",
]
[[package]] [[package]]
name = "unicase" name = "unicase"
version = "2.6.0" version = "2.6.0"

View file

@ -73,6 +73,8 @@ postgres-types = { version = "0.1.2", features = ["derive", "with-chrono-0_4", "
postgres-protocol = "0.5.3" postgres-protocol = "0.5.3"
# Used to work with URLs # Used to work with URLs
url = "2.2.2" url = "2.2.2"
# Used to generate lexicographically sortable IDs
ulid = { version = "0.4.1", features = ["uuid"] }
# Used to work with UUIDs # Used to work with UUIDs
uuid = { version = "0.8.2", features = ["serde", "v4"] } uuid = { version = "0.8.2", features = ["serde", "v4"] }
# Used to query ethereum node # Used to query ethereum node

View file

@ -7,6 +7,7 @@ use crate::models::posts::types::Post;
use crate::models::profiles::types::DbActorProfile; use crate::models::profiles::types::DbActorProfile;
use crate::models::users::types::User; use crate::models::users::types::User;
use crate::utils::files::get_file_url; use crate::utils::files::get_file_url;
use crate::utils::id::new_uuid;
use super::actor::{get_local_actor, ActorKeyError}; use super::actor::{get_local_actor, ActorKeyError};
use super::constants::{AP_CONTEXT, AP_PUBLIC}; use super::constants::{AP_CONTEXT, AP_PUBLIC};
use super::views::{get_actor_url, get_object_url}; use super::views::{get_actor_url, get_object_url};
@ -104,7 +105,7 @@ fn create_activity(
); );
let activity_id = get_object_url( let activity_id = get_object_url(
instance_url, instance_url,
&activity_uuid.unwrap_or(Uuid::new_v4()), &activity_uuid.unwrap_or(new_uuid()),
); );
Activity { Activity {
context: json!(AP_CONTEXT), context: json!(AP_CONTEXT),
@ -455,7 +456,7 @@ mod tests {
username: "follower".to_string(), username: "follower".to_string(),
..Default::default() ..Default::default()
}; };
let follow_request_id = Uuid::new_v4(); let follow_request_id = new_uuid();
let target_actor_id = "https://example.com/actor/test"; let target_actor_id = "https://example.com/actor/test";
let activity = create_activity_follow( let activity = create_activity_follow(
INSTANCE_URL, INSTANCE_URL,

View file

@ -430,6 +430,7 @@ pub async fn receive_activity(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json::json; use serde_json::json;
use crate::utils::id::new_uuid;
use super::*; use super::*;
const INSTANCE_URL: &str = "https://example.org"; const INSTANCE_URL: &str = "https://example.org";
@ -460,7 +461,7 @@ mod tests {
#[test] #[test]
fn test_parse_object_id() { fn test_parse_object_id() {
let expected_uuid = Uuid::new_v4(); let expected_uuid = new_uuid();
let object_id = format!( let object_id = format!(
"https://example.org/objects/{}", "https://example.org/objects/{}",
expected_uuid, expected_uuid,

View file

@ -204,11 +204,9 @@ mod tests {
..Default::default() ..Default::default()
}; };
let user = User { let user = User {
id: Uuid::new_v4(),
wallet_address: wallet_address.to_string(), wallet_address: wallet_address.to_string(),
password_hash: "".to_string(),
private_key: "".to_string(),
profile, profile,
..Default::default()
}; };
let account = Account::from_user(user, INSTANCE_URL); let account = Account::from_user(user, INSTANCE_URL);

View file

@ -2,6 +2,7 @@ use tokio_postgres::GenericClient;
use uuid::Uuid; use uuid::Uuid;
use crate::errors::DatabaseError; use crate::errors::DatabaseError;
use crate::utils::id::new_uuid;
use super::types::DbMediaAttachment; use super::types::DbMediaAttachment;
pub async fn create_attachment( pub async fn create_attachment(
@ -10,7 +11,7 @@ pub async fn create_attachment(
media_type: Option<String>, media_type: Option<String>,
file_name: String, file_name: String,
) -> Result<DbMediaAttachment, DatabaseError> { ) -> Result<DbMediaAttachment, DatabaseError> {
let attachment_id = Uuid::new_v4(); let attachment_id = new_uuid();
let inserted_row = db_client.query_one( let inserted_row = db_client.query_one(
" "
INSERT INTO media_attachment (id, owner_id, media_type, file_name) INSERT INTO media_attachment (id, owner_id, media_type, file_name)

View file

@ -20,6 +20,7 @@ use crate::models::notifications::queries::{
}; };
use crate::models::profiles::queries::update_post_count; use crate::models::profiles::queries::update_post_count;
use crate::models::profiles::types::DbActorProfile; use crate::models::profiles::types::DbActorProfile;
use crate::utils::id::new_uuid;
use super::types::{DbPost, Post, PostCreateData, Visibility}; use super::types::{DbPost, Post, PostCreateData, Visibility};
pub async fn create_post( pub async fn create_post(
@ -28,7 +29,7 @@ pub async fn create_post(
data: PostCreateData, data: PostCreateData,
) -> Result<Post, DatabaseError> { ) -> Result<Post, DatabaseError> {
let transaction = db_client.transaction().await?; let transaction = db_client.transaction().await?;
let post_id = uuid::Uuid::new_v4(); let post_id = new_uuid();
let created_at = data.created_at.unwrap_or(Utc::now()); let created_at = data.created_at.unwrap_or(Utc::now());
// Reposting of other reposts or non-public posts is not allowed // Reposting of other reposts or non-public posts is not allowed
let insert_statement = format!( let insert_statement = format!(

View file

@ -8,6 +8,7 @@ use crate::models::cleanup::{
find_orphaned_ipfs_objects, find_orphaned_ipfs_objects,
DeletionQueue, DeletionQueue,
}; };
use crate::utils::id::new_uuid;
use super::types::{ use super::types::{
ExtraFields, ExtraFields,
DbActorProfile, DbActorProfile,
@ -20,7 +21,7 @@ pub async fn create_profile(
db_client: &impl GenericClient, db_client: &impl GenericClient,
profile_data: &ProfileCreateData, profile_data: &ProfileCreateData,
) -> Result<DbActorProfile, DatabaseError> { ) -> Result<DbActorProfile, DatabaseError> {
let profile_id = Uuid::new_v4(); let profile_id = new_uuid();
let extra_fields = ExtraFields(profile_data.extra_fields.clone()); let extra_fields = ExtraFields(profile_data.extra_fields.clone());
let row = db_client.query_one( let row = db_client.query_one(
" "

View file

@ -5,6 +5,7 @@ use crate::database::catch_unique_violation;
use crate::errors::DatabaseError; use crate::errors::DatabaseError;
use crate::models::notifications::queries::create_reaction_notification; use crate::models::notifications::queries::create_reaction_notification;
use crate::models::posts::queries::{get_post_by_id, update_reaction_count}; use crate::models::posts::queries::{get_post_by_id, update_reaction_count};
use crate::utils::id::new_uuid;
pub async fn create_reaction( pub async fn create_reaction(
db_client: &mut impl GenericClient, db_client: &mut impl GenericClient,
@ -12,7 +13,7 @@ pub async fn create_reaction(
post_id: &Uuid, post_id: &Uuid,
) -> Result<(), DatabaseError> { ) -> Result<(), DatabaseError> {
let transaction = db_client.transaction().await?; let transaction = db_client.transaction().await?;
let reaction_id = Uuid::new_v4(); let reaction_id = new_uuid();
transaction.execute( transaction.execute(
" "
INSERT INTO post_reaction (id, author_id, post_id) INSERT INTO post_reaction (id, author_id, post_id)

View file

@ -10,6 +10,7 @@ use crate::models::profiles::queries::{
update_follower_count, update_follower_count,
update_following_count, update_following_count,
}; };
use crate::utils::id::new_uuid;
use super::types::{ use super::types::{
DbFollowRequest, DbFollowRequest,
FollowRequestStatus, FollowRequestStatus,
@ -143,7 +144,7 @@ pub async fn create_follow_request(
target_id: &Uuid, target_id: &Uuid,
) -> Result<DbFollowRequest, DatabaseError> { ) -> Result<DbFollowRequest, DatabaseError> {
let request = DbFollowRequest { let request = DbFollowRequest {
id: Uuid::new_v4(), id: new_uuid(),
source_id: source_id.to_owned(), source_id: source_id.to_owned(),
target_id: target_id.to_owned(), target_id: target_id.to_owned(),
request_status: FollowRequestStatus::Pending, request_status: FollowRequestStatus::Pending,

8
src/utils/id.rs Normal file
View file

@ -0,0 +1,8 @@
use ulid::Ulid;
use uuid::Uuid;
/// Produces new lexicographically sortable ID
pub fn new_uuid() -> Uuid {
let ulid = Ulid::new();
Uuid::from(ulid)
}

View file

@ -1,3 +1,4 @@
pub mod crypto; pub mod crypto;
pub mod files; pub mod files;
pub mod html; pub mod html;
pub mod id;