a mini social media app for small communities
1module entity
2
3import db.pg
4import time
5import util
6
7pub struct User {
8pub mut:
9 id int @[primary; sql: serial]
10 username string @[unique]
11 nickname ?string
12
13 password string
14 password_salt string
15
16 muted bool
17 admin bool
18 automated bool
19
20 theme string
21
22 bio string
23 pronouns string
24
25 created_at time.Time = time.now()
26}
27
28// get_name returns the user's nickname if it is not none, if so then their
29// username is returned.
30@[inline]
31pub fn (user User) get_name() string {
32 return user.nickname or { user.username }
33}
34
35// to_str_without_sensitive_data returns the stringified data for the user with
36// their password and salt censored.
37@[inline]
38pub fn (user User) to_str_without_sensitive_data() string {
39 return user.str()
40 .replace(user.password, '*'.repeat(16))
41 .replace(user.password_salt, '*'.repeat(16))
42}
43
44// User.from_row creates a user object from the given database row.
45// see src/database/user.v#search_for_users for usage.
46@[inline]
47pub fn User.from_row(row pg.Row) User {
48 // this throws a cgen error when put in User{}
49 //todo: report this
50 created_at := time.parse(util.or_throw[string](row.vals[10])) or { panic(err) }
51
52 return User{
53 id: util.or_throw[string](row.vals[0]).int()
54 username: util.or_throw[string](row.vals[1])
55 nickname: if row.vals[2] == none { ?string(none) } else {
56 util.or_throw[string](row.vals[2])
57 }
58 password: 'haha lol, nope'
59 password_salt: 'haha lol, nope'
60 muted: util.map_or_throw[string, bool](row.vals[5], |it| it.bool())
61 admin: util.map_or_throw[string, bool](row.vals[6], |it| it.bool())
62 theme: util.or_throw[string](row.vals[7])
63 bio: util.or_throw[string](row.vals[8])
64 pronouns: util.or_throw[string](row.vals[9])
65 created_at: created_at
66 }
67}