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"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [
"js-sys",
"libc",
"num-integer",
"num-traits",
"serde",
"time 0.1.44",
"wasm-bindgen",
"winapi 0.3.9",
]
@ -1144,8 +1146,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if 1.0.0",
"js-sys",
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"wasm-bindgen",
]
[[package]]
@ -1717,6 +1721,7 @@ dependencies = [
"thiserror",
"tokio",
"tokio-postgres",
"ulid",
"url 2.2.2",
"uuid",
"web3",
@ -2374,6 +2379,7 @@ dependencies = [
"libc",
"rand_core 0.4.2",
"rdrand",
"wasm-bindgen",
"winapi 0.3.9",
]
@ -3301,6 +3307,18 @@ dependencies = [
"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]]
name = "unicase"
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"
# Used to work with URLs
url = "2.2.2"
# Used to generate lexicographically sortable IDs
ulid = { version = "0.4.1", features = ["uuid"] }
# Used to work with UUIDs
uuid = { version = "0.8.2", features = ["serde", "v4"] }
# 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::users::types::User;
use crate::utils::files::get_file_url;
use crate::utils::id::new_uuid;
use super::actor::{get_local_actor, ActorKeyError};
use super::constants::{AP_CONTEXT, AP_PUBLIC};
use super::views::{get_actor_url, get_object_url};
@ -104,7 +105,7 @@ fn create_activity(
);
let activity_id = get_object_url(
instance_url,
&activity_uuid.unwrap_or(Uuid::new_v4()),
&activity_uuid.unwrap_or(new_uuid()),
);
Activity {
context: json!(AP_CONTEXT),
@ -455,7 +456,7 @@ mod tests {
username: "follower".to_string(),
..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 activity = create_activity_follow(
INSTANCE_URL,

View file

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

View file

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

View file

@ -2,6 +2,7 @@ use tokio_postgres::GenericClient;
use uuid::Uuid;
use crate::errors::DatabaseError;
use crate::utils::id::new_uuid;
use super::types::DbMediaAttachment;
pub async fn create_attachment(
@ -10,7 +11,7 @@ pub async fn create_attachment(
media_type: Option<String>,
file_name: String,
) -> Result<DbMediaAttachment, DatabaseError> {
let attachment_id = Uuid::new_v4();
let attachment_id = new_uuid();
let inserted_row = db_client.query_one(
"
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::types::DbActorProfile;
use crate::utils::id::new_uuid;
use super::types::{DbPost, Post, PostCreateData, Visibility};
pub async fn create_post(
@ -28,7 +29,7 @@ pub async fn create_post(
data: PostCreateData,
) -> Result<Post, DatabaseError> {
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());
// Reposting of other reposts or non-public posts is not allowed
let insert_statement = format!(

View file

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

View file

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

View file

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