Skip to main content

max / makenotwork

4.3 KB · 130 lines History Blame Raw
1 //! The seeded people: three fixed harness accounts and the ordinary users
2 //! everything else is written as.
3
4 use mt_core::types::CommunityRole;
5 use sqlx::PgPool;
6 use uuid::Uuid;
7
8 pub(super) struct SeedUser {
9 pub(super) id: Uuid,
10 }
11
12 /// A harness account: the local row plus the roles it carries per community.
13 pub(super) struct HarnessUser {
14 pub(super) id: Uuid,
15 pub(super) rust_role: CommunityRole,
16 pub(super) music_role: CommunityRole,
17 }
18
19 /// The three accounts the browser harness logs in as.
20 ///
21 /// `mnw_account_id` is the join to MNW and these values are fixed on both
22 /// sides: the MNW example seed creates the same three ids in
23 /// `server/src/seed/harness.rs`, which is what lets roles be assigned here
24 /// before anyone has logged in. The two lists have to be edited together, and
25 /// the MNW side has a test pinning the literals to make that hard to forget.
26 ///
27 /// `is_fan_plus`/`is_creator` mirror the perks MNW will report at login. They
28 /// are denormalised onto the row for post rendering, and login overwrites them
29 /// from userinfo, so seeding them wrong is cosmetic rather than a privilege
30 /// question. Seed them right anyway: a pre-login render of a seeded post is one
31 /// of the things the harness looks at.
32 pub(super) async fn seed_harness_users(pool: &PgPool) -> Vec<HarnessUser> {
33 let harness_data = [
34 (
35 "00000000-0000-0000-0000-00000000f001",
36 "harness_fan",
37 "Harness Fan",
38 true,
39 false,
40 CommunityRole::Member,
41 CommunityRole::Member,
42 ),
43 (
44 "00000000-0000-0000-0000-00000000f002",
45 "harness_creator",
46 "Harness Creator",
47 false,
48 true,
49 CommunityRole::Member,
50 CommunityRole::Member,
51 ),
52 (
53 "00000000-0000-0000-0000-00000000f003",
54 "harness_owner",
55 "Harness Owner",
56 false,
57 false,
58 CommunityRole::Owner,
59 CommunityRole::Moderator,
60 ),
61 ];
62
63 let mut users = Vec::new();
64 for (uuid_str, username, display_name, is_fan_plus, is_creator, rust_role, music_role) in
65 harness_data
66 {
67 let id = Uuid::parse_str(uuid_str).unwrap();
68 sqlx::query(
69 "INSERT INTO users (mnw_account_id, username, display_name, is_fan_plus, is_creator)
70 VALUES ($1, $2, $3, $4, $5)
71 ON CONFLICT (mnw_account_id) DO UPDATE
72 SET username = EXCLUDED.username,
73 display_name = EXCLUDED.display_name,
74 is_fan_plus = EXCLUDED.is_fan_plus,
75 is_creator = EXCLUDED.is_creator",
76 )
77 .bind(id)
78 .bind(username)
79 .bind(display_name)
80 .bind(is_fan_plus)
81 .bind(is_creator)
82 .execute(pool)
83 .await
84 .expect("failed to seed harness user");
85 users.push(HarnessUser {
86 id,
87 rust_role,
88 music_role,
89 });
90 }
91 users
92 }
93
94 pub(super) async fn seed_users(pool: &PgPool) -> Vec<SeedUser> {
95 let user_data = [
96 ("00000000-0000-0000-0000-000000000001", "admin", "Admin"),
97 ("00000000-0000-0000-0000-000000000002", "maxmj", "Max"),
98 (
99 "00000000-0000-0000-0000-000000000003",
100 "synthwave99",
101 "Juno",
102 ),
103 ("00000000-0000-0000-0000-000000000004", "basshunter", "Erik"),
104 ("00000000-0000-0000-0000-000000000005", "tape_hiss", "Rae"),
105 ("00000000-0000-0000-0000-000000000006", "drumroom", "Cole"),
106 ("00000000-0000-0000-0000-000000000007", "patchwork", "Lina"),
107 ("00000000-0000-0000-0000-000000000008", "detuned", "Kai"),
108 ("00000000-0000-0000-0000-000000000009", "resampled", "Noor"),
109 ("00000000-0000-0000-0000-00000000000a", "clipgain", "Wren"),
110 ];
111
112 let mut users = Vec::new();
113 for (uuid_str, username, display_name) in &user_data {
114 let id = Uuid::parse_str(uuid_str).unwrap();
115 sqlx::query(
116 "INSERT INTO users (mnw_account_id, username, display_name)
117 VALUES ($1, $2, $3)
118 ON CONFLICT (mnw_account_id) DO NOTHING",
119 )
120 .bind(id)
121 .bind(username)
122 .bind(display_name)
123 .execute(pool)
124 .await
125 .expect("failed to seed user");
126 users.push(SeedUser { id });
127 }
128 users
129 }
130