-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofile.go
136 lines (121 loc) · 3.27 KB
/
profile.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"unicode/utf8"
"github.com/go-chi/chi/v5"
"github.com/rkonfj/lln/config"
"github.com/rkonfj/lln/state"
"github.com/rkonfj/lln/tools"
)
type User struct {
state.User
Following bool `json:"following"`
Followers int64 `json:"followers"`
Followings int64 `json:"followings"`
Tweets int64 `json:"tweets"`
Disabled bool `json:"disabled"`
Admin bool `json:"admin"`
}
func profile(w http.ResponseWriter, r *http.Request) {
uniqueName, err := url.PathUnescape(chi.URLParam(r, tools.UniqueName))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
u := state.UserByUniqueName(uniqueName)
if u == nil {
w.WriteHeader(http.StatusNotFound)
return
}
user := currentSessionUser(r)
if user == nil || user.ID != u.ID {
// privacy
u.Email = ""
u.Locale = ""
}
json.NewEncoder(w).Encode(User{
User: *u,
Followers: u.Followers(),
Followings: u.Followings(),
Tweets: u.Tweets(),
Disabled: u.Disabled(),
Admin: tools.Contains(config.Conf.Admins, u.ID),
Following: u.FollowingBy(user)})
}
func followUser(w http.ResponseWriter, r *http.Request) {
uniqueName, err := url.PathUnescape(chi.URLParam(r, tools.UniqueName))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
err = state.FollowUser(currentSessionUser(r), uniqueName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
}
}
func modifyProfile(w http.ResponseWriter, r *http.Request) {
u := state.UserByID(currentSessionUser(r).ID)
if u == nil {
w.WriteHeader(http.StatusNotFound)
return
}
mu, err := checkArgsModifyProfile(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err.Error())
return
}
err = u.Modify(*mu)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
state.UserChanged <- u
state.DefaultSessionManager.Expire(currentSessionUser(r).ID)
}
func checkArgsModifyProfile(r *http.Request) (*state.ModifiableUser, error) {
mu := &state.ModifiableUser{}
err := json.NewDecoder(r.Body).Decode(mu)
if err != nil {
return nil, err
}
if len(mu.Bio) > 0 {
if utf8.RuneCountInString(mu.Bio) > 256 {
return nil, fmt.Errorf("bio: maximum 256 unicode characters")
}
}
if len(mu.UniqueName) > 0 {
if utf8.RuneCountInString(mu.UniqueName) > 12 {
return nil, fmt.Errorf("unique name: maximum 12 unicode characters")
}
if !state.UniqueNameRegex.MatchString(mu.UniqueName) {
return nil, fmt.Errorf("unique name: not match `%s`", state.UniqueNameRegex)
}
if tools.Contains(config.Conf.Model.Keywords, mu.UniqueName) {
return nil, fmt.Errorf("unique name: %s is a keyword", mu.UniqueName)
}
}
if len(mu.Name) > 0 {
if utf8.RuneCountInString(mu.Name) > 20 {
return nil, fmt.Errorf("name: maximum 20 unicode characters")
}
if len(strings.TrimSpace(mu.Name)) == 0 {
return nil, fmt.Errorf("name: can not be empty")
}
}
if len(mu.Picture) > 0 && len(mu.Picture) > 256 {
return nil, fmt.Errorf("picture: maximum 256 characters")
}
if len(mu.Locale) > 0 && len(mu.Locale) > 6 {
return nil, fmt.Errorf("locale: maximum 6 characters")
}
return mu, nil
}