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