~sircmpwn/meta.sr.ht

ref: 0.56.17 meta.sr.ht/api/graph/model/user.go -rw-r--r-- 2.0 KiB
b1deacf0Nicolas Schodet Fix field name when validating the PGP key in RegisterAccount 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package model

import (
	"fmt"
	"time"

	"git.sr.ht/~sircmpwn/core-go/database"
)

type User struct {
	ID               int       `json:"id"`
	Created          time.Time `json:"created"`
	Updated          time.Time `json:"updated"`
	Username         string    `json:"username"`
	Email            string    `json:"email"`
	URL              *string   `json:"url"`
	Location         *string   `json:"location"`
	Bio              *string   `json:"bio"`
	SuspensionNotice *string   `json:"suspensionNotice"`

	UserTypeRaw string
	PGPKeyID    *int

	alias  string
	fields *database.ModelFields
}

func (User) IsEntity() {}

func (u *User) CanonicalName() string {
	return "~" + u.Username
}

func (u *User) UserType() UserType {
	switch u.UserTypeRaw {
	case "unconfirmed":
		return UserTypeUnconfirmed
	case "active_non_paying":
		return UserTypeActiveNonPaying
	case "active_free":
		return UserTypeActiveFree
	case "active_paying":
		return UserTypeActivePaying
	case "active_delinquent":
		return UserTypeActiveDelinquent
	case "admin":
		return UserTypeAdmin
	case "suspended":
		return UserTypeSuspended
	}
	panic(fmt.Errorf("Unknown user type '%s'", u.UserTypeRaw))
}

func (u *User) As(alias string) *User {
	u.alias = alias
	return u
}

func (u *User) Alias() string {
	return u.alias
}

func (u *User) Table() string {
	return `"user"`
}

func (u *User) Fields() *database.ModelFields {
	if u.fields != nil {
		return u.fields
	}
	u.fields = &database.ModelFields{
		Fields: []*database.FieldMap{
			{ "id", "id", &u.ID },
			{ "created", "created", &u.Created },
			{ "updated", "updated", &u.Updated },
			{ "username", "username", &u.Username },
			{ "email", "email", &u.Email },
			{ "url", "url", &u.URL },
			{ "location", "location", &u.Location },
			{ "bio", "bio", &u.Bio },
			{ "user_type", "userType", &u.UserTypeRaw },
			{ "suspension_notice", "suspensionNotice", &u.SuspensionNotice },

			// Always fetch:
			{ "id", "", &u.ID },
			{ "username", "", &u.Username },
			{ "pgp_key_id", "", &u.PGPKeyID },
		},
	}
	return u.fields
}