tangled
alpha
login
or
join now
back
round
0
view raw
appview: state: dedup profile pages
#504
merged
opened by
ptr.pet
4 months ago
targeting
master
from
[deleted fork]
: followers-following-list
rename existing ProfilePage to ProfileHomePage
Signed-off-by: dusk
y.bera003.06@protonmail.com
options
unified
split
Changed files
+78
-101
appview
pages
pages.go
state
profile.go
+2
-2
appview/pages/pages.go
···
401
401
return p.execute("repo/fork", w, params)
402
402
}
403
403
404
404
-
type ProfilePageParams struct {
404
404
+
type ProfileHomePageParams struct {
405
405
LoggedInUser *oauth.User
406
406
Repos []db.Repo
407
407
CollaboratingRepos []db.Repo
···
420
420
Profile *db.Profile
421
421
}
422
422
423
423
-
func (p *Pages) ProfilePage(w io.Writer, params ProfilePageParams) error {
423
423
+
func (p *Pages) ProfileHomePage(w io.Writer, params ProfileHomePageParams) error {
424
424
return p.execute("user/profile", w, params)
425
425
}
426
426
+76
-99
appview/state/profile.go
···
25
25
tabVal := r.URL.Query().Get("tab")
26
26
switch tabVal {
27
27
case "":
28
28
-
s.profilePage(w, r)
28
28
+
s.profileHomePage(w, r)
29
29
case "repos":
30
30
s.reposPage(w, r)
31
31
}
32
32
}
33
33
34
34
-
func (s *State) profilePage(w http.ResponseWriter, r *http.Request) {
34
34
+
type ProfilePageParams struct {
35
35
+
Id identity.Identity
36
36
+
LoggedInUser *oauth.User
37
37
+
Card pages.ProfileCard
38
38
+
}
39
39
+
40
40
+
func (s *State) profilePage(w http.ResponseWriter, r *http.Request) *ProfilePageParams {
35
41
didOrHandle := chi.URLParam(r, "user")
36
42
if didOrHandle == "" {
37
37
-
http.Error(w, "Bad request", http.StatusBadRequest)
38
38
-
return
43
43
+
http.Error(w, "bad request", http.StatusBadRequest)
44
44
+
return nil
39
45
}
40
46
41
47
ident, ok := r.Context().Value("resolvedId").(identity.Identity)
42
48
if !ok {
43
43
-
s.pages.Error404(w)
44
44
-
return
49
49
+
log.Printf("malformed middleware")
50
50
+
w.WriteHeader(http.StatusInternalServerError)
51
51
+
return nil
45
52
}
53
53
+
did := ident.DID.String()
46
54
47
47
-
profile, err := db.GetProfile(s.db, ident.DID.String())
55
55
+
profile, err := db.GetProfile(s.db, did)
48
56
if err != nil {
49
49
-
log.Printf("getting profile data for %s: %s", ident.DID.String(), err)
57
57
+
log.Printf("getting profile data for %s: %s", did, err)
50
58
}
51
59
60
60
+
followersCount, followingCount, err := db.GetFollowerFollowingCount(s.db, did)
61
61
+
if err != nil {
62
62
+
log.Printf("getting follow stats for %s: %s", did, err)
63
63
+
}
64
64
+
65
65
+
loggedInUser := s.oauth.GetUser(r)
66
66
+
followStatus := db.IsNotFollowing
67
67
+
if loggedInUser != nil {
68
68
+
followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, did)
69
69
+
}
70
70
+
71
71
+
return &ProfilePageParams{
72
72
+
Id: ident,
73
73
+
LoggedInUser: loggedInUser,
74
74
+
Card: pages.ProfileCard{
75
75
+
UserDid: did,
76
76
+
UserHandle: ident.Handle.String(),
77
77
+
Profile: profile,
78
78
+
FollowStatus: followStatus,
79
79
+
FollowersCount: followersCount,
80
80
+
FollowingCount: followingCount,
81
81
+
},
82
82
+
}
83
83
+
}
84
84
+
85
85
+
func (s *State) profileHomePage(w http.ResponseWriter, r *http.Request) {
86
86
+
pageWithProfile := s.profilePage(w, r)
87
87
+
if pageWithProfile == nil {
88
88
+
return
89
89
+
}
90
90
+
91
91
+
id := pageWithProfile.Id
52
92
repos, err := db.GetRepos(
53
93
s.db,
54
94
0,
55
55
-
db.FilterEq("did", ident.DID.String()),
95
95
+
db.FilterEq("did", id.DID),
56
96
)
57
97
if err != nil {
58
58
-
log.Printf("getting repos for %s: %s", ident.DID.String(), err)
98
98
+
log.Printf("getting repos for %s: %s", id.DID, err)
59
99
}
60
100
101
101
+
profile := pageWithProfile.Card.Profile
61
102
// filter out ones that are pinned
62
103
pinnedRepos := []db.Repo{}
63
104
for i, r := range repos {
···
72
113
}
73
114
}
74
115
75
75
-
collaboratingRepos, err := db.CollaboratingIn(s.db, ident.DID.String())
116
116
+
collaboratingRepos, err := db.CollaboratingIn(s.db, id.DID.String())
76
117
if err != nil {
77
77
-
log.Printf("getting collaborating repos for %s: %s", ident.DID.String(), err)
118
118
+
log.Printf("getting collaborating repos for %s: %s", id.DID, err)
78
119
}
79
120
80
121
pinnedCollaboratingRepos := []db.Repo{}
···
85
126
}
86
127
}
87
128
88
88
-
timeline, err := db.MakeProfileTimeline(s.db, ident.DID.String())
129
129
+
timeline, err := db.MakeProfileTimeline(s.db, id.DID.String())
89
130
if err != nil {
90
90
-
log.Printf("failed to create profile timeline for %s: %s", ident.DID.String(), err)
131
131
+
log.Printf("failed to create profile timeline for %s: %s", id.DID, err)
91
132
}
92
133
93
134
var didsToResolve []string
···
109
150
}
110
151
}
111
152
112
112
-
followers, following, err := db.GetFollowerFollowingCount(s.db, ident.DID.String())
113
113
-
if err != nil {
114
114
-
log.Printf("getting follow stats repos for %s: %s", ident.DID.String(), err)
115
115
-
}
116
116
-
117
117
-
loggedInUser := s.oauth.GetUser(r)
118
118
-
followStatus := db.IsNotFollowing
119
119
-
if loggedInUser != nil {
120
120
-
followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, ident.DID.String())
121
121
-
}
122
122
-
123
153
now := time.Now()
124
154
startOfYear := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.UTC)
125
155
punchcard, err := db.MakePunchcard(
126
156
s.db,
127
127
-
db.FilterEq("did", ident.DID.String()),
157
157
+
db.FilterEq("did", id.DID),
128
158
db.FilterGte("date", startOfYear.Format(time.DateOnly)),
129
159
db.FilterLte("date", now.Format(time.DateOnly)),
130
160
)
131
161
if err != nil {
132
132
-
log.Println("failed to get punchcard for did", "did", ident.DID.String(), "err", err)
162
162
+
log.Println("failed to get punchcard for did", "did", id.DID, "err", err)
133
163
}
134
164
135
135
-
s.pages.ProfilePage(w, pages.ProfilePageParams{
136
136
-
LoggedInUser: loggedInUser,
165
165
+
s.pages.ProfileHomePage(w, pages.ProfileHomePageParams{
166
166
+
LoggedInUser: pageWithProfile.LoggedInUser,
137
167
Repos: pinnedRepos,
138
168
CollaboratingRepos: pinnedCollaboratingRepos,
139
139
-
Card: pages.ProfileCard{
140
140
-
UserDid: ident.DID.String(),
141
141
-
UserHandle: ident.Handle.String(),
142
142
-
Profile: profile,
143
143
-
FollowStatus: followStatus,
144
144
-
FollowersCount: followers,
145
145
-
FollowingCount: following,
146
146
-
},
147
147
-
Punchcard: punchcard,
148
148
-
ProfileTimeline: timeline,
169
169
+
Card: pageWithProfile.Card,
170
170
+
Punchcard: punchcard,
171
171
+
ProfileTimeline: timeline,
149
172
})
150
173
}
151
174
152
175
func (s *State) reposPage(w http.ResponseWriter, r *http.Request) {
153
153
-
ident, ok := r.Context().Value("resolvedId").(identity.Identity)
154
154
-
if !ok {
155
155
-
s.pages.Error404(w)
176
176
+
pageWithProfile := s.profilePage(w, r)
177
177
+
if pageWithProfile == nil {
156
178
return
157
179
}
158
180
159
159
-
profile, err := db.GetProfile(s.db, ident.DID.String())
160
160
-
if err != nil {
161
161
-
log.Printf("getting profile data for %s: %s", ident.DID.String(), err)
162
162
-
}
163
163
-
181
181
+
id := pageWithProfile.Id
164
182
repos, err := db.GetRepos(
165
183
s.db,
166
184
0,
167
167
-
db.FilterEq("did", ident.DID.String()),
185
185
+
db.FilterEq("did", id.DID),
168
186
)
169
187
if err != nil {
170
170
-
log.Printf("getting repos for %s: %s", ident.DID.String(), err)
171
171
-
}
172
172
-
173
173
-
loggedInUser := s.oauth.GetUser(r)
174
174
-
followStatus := db.IsNotFollowing
175
175
-
if loggedInUser != nil {
176
176
-
followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, ident.DID.String())
177
177
-
}
178
178
-
179
179
-
followers, following, err := db.GetFollowerFollowingCount(s.db, ident.DID.String())
180
180
-
if err != nil {
181
181
-
log.Printf("getting follow stats repos for %s: %s", ident.DID.String(), err)
188
188
+
log.Printf("getting repos for %s: %s", id.DID, err)
182
189
}
183
190
184
191
s.pages.ReposPage(w, pages.ReposPageParams{
185
185
-
LoggedInUser: loggedInUser,
192
192
+
LoggedInUser: pageWithProfile.LoggedInUser,
186
193
Repos: repos,
187
187
-
Card: pages.ProfileCard{
188
188
-
UserDid: ident.DID.String(),
189
189
-
UserHandle: ident.Handle.String(),
190
190
-
Profile: profile,
191
191
-
FollowStatus: followStatus,
192
192
-
FollowersCount: followers,
193
193
-
FollowingCount: following,
194
194
-
},
194
194
+
Card: pageWithProfile.Card,
195
195
})
196
196
}
197
197
···
202
202
}
203
203
204
204
func (s *State) followPage(w http.ResponseWriter, r *http.Request, fetchFollows func(db.Execer, string) ([]db.Follow, error), extractDid func(db.Follow) string) *FollowsPageParams {
205
205
-
ident, ok := r.Context().Value("resolvedId").(identity.Identity)
206
206
-
if !ok {
207
207
-
s.pages.Error404(w)
205
205
+
pageWithProfile := s.profilePage(w, r)
206
206
+
if pageWithProfile == nil {
208
207
return nil
209
208
}
210
210
-
did := ident.DID.String()
211
211
-
212
212
-
profile, err := db.GetProfile(s.db, did)
213
213
-
if err != nil {
214
214
-
log.Printf("getting profile data for %s: %s", did, err)
215
215
-
}
216
209
217
217
-
loggedInUser := s.oauth.GetUser(r)
210
210
+
id := pageWithProfile.Id
218
211
219
219
-
follows, err := fetchFollows(s.db, did)
212
212
+
follows, err := fetchFollows(s.db, id.DID.String())
220
213
if err != nil {
221
221
-
log.Printf("getting followers for %s: %s", did, err)
214
214
+
log.Printf("getting followers for %s: %s", id.DID, err)
222
215
}
223
216
224
217
if len(follows) == 0 {
···
236
229
return nil
237
230
}
238
231
232
232
+
loggedInUser := pageWithProfile.LoggedInUser
239
233
var loggedInUserFollowing map[string]struct{}
240
234
if loggedInUser != nil {
241
235
following, err := db.GetFollowing(s.db, loggedInUser.Did)
···
280
274
})
281
275
}
282
276
283
283
-
followStatus := db.IsNotFollowing
284
284
-
if loggedInUser != nil {
285
285
-
followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, did)
286
286
-
}
287
287
-
288
288
-
followersCount, followingCount, err := db.GetFollowerFollowingCount(s.db, did)
289
289
-
if err != nil {
290
290
-
log.Printf("getting follow stats followers for %s: %s", did, err)
291
291
-
}
292
292
-
293
277
return &FollowsPageParams{
294
278
LoggedInUser: loggedInUser,
295
279
Follows: followCards,
296
296
-
Card: pages.ProfileCard{
297
297
-
UserDid: did,
298
298
-
UserHandle: ident.Handle.String(),
299
299
-
Profile: profile,
300
300
-
FollowStatus: followStatus,
301
301
-
FollowersCount: followersCount,
302
302
-
FollowingCount: followingCount,
303
303
-
},
280
280
+
Card: pageWithProfile.Card,
304
281
}
305
282
}
306
283