2024-04-30 16:48:28 +02:00
use crate ::model ::{ addressing , config , credential , activity , object , user , Audience } ;
2024-03-26 04:20:35 +01:00
use openssl ::rsa ::Rsa ;
2024-03-21 20:36:28 +01:00
use sea_orm ::IntoActiveModel ;
2024-05-11 22:47:43 +02:00
pub async fn faker ( ctx : crate ::server ::Context , count : u64 ) -> Result < ( ) , sea_orm ::DbErr > {
2024-03-21 20:36:28 +01:00
use sea_orm ::{ EntityTrait , Set } ;
2024-05-11 22:47:43 +02:00
let domain = ctx . domain ( ) ;
let db = ctx . db ( ) ;
2024-03-26 04:20:35 +01:00
let key = Rsa ::generate ( 2048 ) . unwrap ( ) ;
2024-04-30 16:48:28 +02:00
let test_user = user ::Model {
2024-03-26 23:55:57 +01:00
id : format ! ( " {domain}/users/test " ) ,
2024-03-21 20:36:28 +01:00
name : Some ( " μpub " . into ( ) ) ,
2024-05-11 22:47:43 +02:00
domain : clean_domain ( domain ) ,
2024-03-26 23:53:30 +01:00
preferred_username : " test " . to_string ( ) ,
2024-03-21 20:36:28 +01:00
summary : Some ( " hello world! i'm manually generated but served dynamically from db! check progress at https://git.alemi.dev/upub.git " . to_string ( ) ) ,
following : None ,
2024-03-24 04:04:48 +01:00
following_count : 0 ,
2024-03-21 20:36:28 +01:00
followers : None ,
2024-03-24 04:04:48 +01:00
followers_count : 0 ,
2024-04-18 04:09:13 +02:00
statuses_count : count as i64 ,
2024-03-21 20:36:28 +01:00
icon : Some ( " https://cdn.alemi.dev/social/circle-square.png " . to_string ( ) ) ,
image : Some ( " https://cdn.alemi.dev/social/someriver-xs.jpg " . to_string ( ) ) ,
inbox : None ,
shared_inbox : None ,
outbox : None ,
2024-04-06 16:56:13 +02:00
actor_type : apb ::ActorType ::Person ,
2024-03-21 20:36:28 +01:00
created : chrono ::Utc ::now ( ) ,
updated : chrono ::Utc ::now ( ) ,
2024-03-26 04:20:35 +01:00
private_key : Some ( std ::str ::from_utf8 ( & key . private_key_to_pem ( ) . unwrap ( ) ) . unwrap ( ) . to_string ( ) ) ,
2024-03-21 20:36:28 +01:00
// TODO generate a fresh one every time
2024-03-26 04:20:35 +01:00
public_key : std ::str ::from_utf8 ( & key . public_key_to_pem ( ) . unwrap ( ) ) . unwrap ( ) . to_string ( ) ,
2024-03-21 20:36:28 +01:00
} ;
2024-03-26 23:53:30 +01:00
user ::Entity ::insert ( test_user . clone ( ) . into_active_model ( ) ) . exec ( db ) . await ? ;
2024-03-21 20:36:28 +01:00
2024-03-23 06:10:33 +01:00
config ::Entity ::insert ( config ::ActiveModel {
2024-03-26 23:53:30 +01:00
id : Set ( test_user . id . clone ( ) ) ,
2024-03-23 06:10:33 +01:00
accept_follow_requests : Set ( true ) ,
show_followers : Set ( true ) ,
show_following : Set ( true ) ,
show_following_count : Set ( true ) ,
show_followers_count : Set ( true ) ,
} ) . exec ( db ) . await ? ;
credential ::Entity ::insert ( credential ::ActiveModel {
2024-03-26 23:53:30 +01:00
id : Set ( test_user . id . clone ( ) ) ,
2024-03-23 06:10:33 +01:00
email : Set ( " mail@example.net " . to_string ( ) ) ,
2024-03-26 01:19:34 +01:00
password : Set ( sha256 ::digest ( " very-strong-password " ) ) ,
2024-03-23 06:10:33 +01:00
} ) . exec ( db ) . await ? ;
2024-03-21 20:36:28 +01:00
let context = uuid ::Uuid ::new_v4 ( ) . to_string ( ) ;
2024-03-26 00:53:07 +01:00
for i in ( 0 .. count ) . rev ( ) {
2024-03-21 20:36:28 +01:00
let oid = uuid ::Uuid ::new_v4 ( ) ;
let aid = uuid ::Uuid ::new_v4 ( ) ;
2024-04-17 22:08:41 +02:00
addressing ::Entity ::insert ( addressing ::ActiveModel {
actor : Set ( apb ::target ::PUBLIC . to_string ( ) ) ,
server : Set ( " www.w3.org " . to_string ( ) ) ,
2024-04-21 15:41:29 +02:00
activity : Set ( Some ( format! ( " {domain} /activities/ {aid} " ) ) ) ,
2024-04-17 22:08:41 +02:00
object : Set ( Some ( format! ( " {domain} /objects/ {oid} " ) ) ) ,
published : Set ( chrono ::Utc ::now ( ) ) ,
.. Default ::default ( )
} ) . exec ( db ) . await ? ;
2024-03-21 20:36:28 +01:00
object ::Entity ::insert ( object ::ActiveModel {
id : Set ( format! ( " {domain} /objects/ {oid} " ) ) ,
name : Set ( None ) ,
2024-04-06 16:56:13 +02:00
object_type : Set ( apb ::ObjectType ::Note ) ,
2024-03-26 23:53:30 +01:00
attributed_to : Set ( Some ( format! ( " {domain} /users/test " ) ) ) ,
2024-03-21 20:36:28 +01:00
summary : Set ( None ) ,
context : Set ( Some ( context . clone ( ) ) ) ,
2024-04-18 04:09:13 +02:00
in_reply_to : Set ( None ) ,
2024-03-21 20:36:28 +01:00
content : Set ( Some ( format! ( " [ {i} ] Tic(k). Quasiparticle of intensive multiplicity. Tics (or ticks) are intrinsically several components of autonomously numbering anorganic populations, propagating by contagion between segmentary divisions in the order of nature. Ticks - as nonqualitative differentially-decomposable counting marks - each designate a multitude comprehended as a singular variation in tic(k)-density. " ) ) ) ,
published : Set ( chrono ::Utc ::now ( ) - std ::time ::Duration ::from_secs ( 60 * i ) ) ,
2024-05-02 15:16:14 +02:00
updated : Set ( None ) ,
2024-03-22 05:34:08 +01:00
comments : Set ( 0 ) ,
likes : Set ( 0 ) ,
shares : Set ( 0 ) ,
2024-04-11 00:29:32 +02:00
to : Set ( Audience ( vec! [ apb ::target ::PUBLIC . to_string ( ) ] ) ) ,
2024-03-21 20:36:28 +01:00
bto : Set ( Audience ::default ( ) ) ,
2024-03-23 06:10:33 +01:00
cc : Set ( Audience ( vec! [ ] ) ) ,
2024-03-21 20:36:28 +01:00
bcc : Set ( Audience ::default ( ) ) ,
2024-05-12 01:58:51 +02:00
url : Set ( None ) ,
2024-04-24 05:21:04 +02:00
sensitive : Set ( false ) ,
2024-03-21 20:36:28 +01:00
} ) . exec ( db ) . await ? ;
activity ::Entity ::insert ( activity ::ActiveModel {
id : Set ( format! ( " {domain} /activities/ {aid} " ) ) ,
2024-04-06 16:56:13 +02:00
activity_type : Set ( apb ::ActivityType ::Create ) ,
2024-03-26 23:53:30 +01:00
actor : Set ( format! ( " {domain} /users/test " ) ) ,
2024-03-21 20:36:28 +01:00
object : Set ( Some ( format! ( " {domain} /objects/ {oid} " ) ) ) ,
target : Set ( None ) ,
published : Set ( chrono ::Utc ::now ( ) - std ::time ::Duration ::from_secs ( 60 * i ) ) ,
2024-04-11 00:29:32 +02:00
to : Set ( Audience ( vec! [ apb ::target ::PUBLIC . to_string ( ) ] ) ) ,
2024-03-21 20:36:28 +01:00
bto : Set ( Audience ::default ( ) ) ,
2024-03-23 06:10:33 +01:00
cc : Set ( Audience ( vec! [ ] ) ) ,
2024-03-21 20:36:28 +01:00
bcc : Set ( Audience ::default ( ) ) ,
} ) . exec ( db ) . await ? ;
}
Ok ( ( ) )
}
2024-04-11 00:29:32 +02:00
fn clean_domain ( domain : & str ) -> String {
domain
. replace ( " http:// " , " " )
. replace ( " https:// " , " " )
. replace ( '/' , " " )
}