tangled
alpha
login
or
join now
back
round
1
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
case "followers":
···
35
35
}
36
36
}
37
37
38
38
-
func (s *State) profilePage(w http.ResponseWriter, r *http.Request) {
38
38
+
type ProfilePageParams struct {
39
39
+
Id identity.Identity
40
40
+
LoggedInUser *oauth.User
41
41
+
Card pages.ProfileCard
42
42
+
}
43
43
+
44
44
+
func (s *State) profilePage(w http.ResponseWriter, r *http.Request) *ProfilePageParams {
39
45
didOrHandle := chi.URLParam(r, "user")
40
46
if didOrHandle == "" {
41
41
-
http.Error(w, "Bad request", http.StatusBadRequest)
42
42
-
return
47
47
+
http.Error(w, "bad request", http.StatusBadRequest)
48
48
+
return nil
43
49
}
44
50
45
51
ident, ok := r.Context().Value("resolvedId").(identity.Identity)
46
52
if !ok {
47
47
-
s.pages.Error404(w)
48
48
-
return
53
53
+
log.Printf("malformed middleware")
54
54
+
w.WriteHeader(http.StatusInternalServerError)
55
55
+
return nil
49
56
}
57
57
+
did := ident.DID.String()
50
58
51
51
-
profile, err := db.GetProfile(s.db, ident.DID.String())
59
59
+
profile, err := db.GetProfile(s.db, did)
52
60
if err != nil {
53
53
-
log.Printf("getting profile data for %s: %s", ident.DID.String(), err)
61
61
+
log.Printf("getting profile data for %s: %s", did, err)
54
62
}
55
63
64
64
+
followersCount, followingCount, err := db.GetFollowerFollowingCount(s.db, did)
65
65
+
if err != nil {
66
66
+
log.Printf("getting follow stats for %s: %s", did, err)
67
67
+
}
68
68
+
69
69
+
loggedInUser := s.oauth.GetUser(r)
70
70
+
followStatus := db.IsNotFollowing
71
71
+
if loggedInUser != nil {
72
72
+
followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, did)
73
73
+
}
74
74
+
75
75
+
return &ProfilePageParams{
76
76
+
Id: ident,
77
77
+
LoggedInUser: loggedInUser,
78
78
+
Card: pages.ProfileCard{
79
79
+
UserDid: did,
80
80
+
UserHandle: ident.Handle.String(),
81
81
+
Profile: profile,
82
82
+
FollowStatus: followStatus,
83
83
+
FollowersCount: followersCount,
84
84
+
FollowingCount: followingCount,
85
85
+
},
86
86
+
}
87
87
+
}
88
88
+
89
89
+
func (s *State) profileHomePage(w http.ResponseWriter, r *http.Request) {
90
90
+
pageWithProfile := s.profilePage(w, r)
91
91
+
if pageWithProfile == nil {
92
92
+
return
93
93
+
}
94
94
+
95
95
+
id := pageWithProfile.Id
56
96
repos, err := db.GetRepos(
57
97
s.db,
58
98
0,
59
59
-
db.FilterEq("did", ident.DID.String()),
99
99
+
db.FilterEq("did", id.DID),
60
100
)
61
101
if err != nil {
62
62
-
log.Printf("getting repos for %s: %s", ident.DID.String(), err)
102
102
+
log.Printf("getting repos for %s: %s", id.DID, err)
63
103
}
64
104
105
105
+
profile := pageWithProfile.Card.Profile
65
106
// filter out ones that are pinned
66
107
pinnedRepos := []db.Repo{}
67
108
for i, r := range repos {
···
76
117
}
77
118
}
78
119
79
79
-
collaboratingRepos, err := db.CollaboratingIn(s.db, ident.DID.String())
120
120
+
collaboratingRepos, err := db.CollaboratingIn(s.db, id.DID.String())
80
121
if err != nil {
81
81
-
log.Printf("getting collaborating repos for %s: %s", ident.DID.String(), err)
122
122
+
log.Printf("getting collaborating repos for %s: %s", id.DID, err)
82
123
}
83
124
84
125
pinnedCollaboratingRepos := []db.Repo{}
···
89
130
}
90
131
}
91
132
92
92
-
timeline, err := db.MakeProfileTimeline(s.db, ident.DID.String())
133
133
+
timeline, err := db.MakeProfileTimeline(s.db, id.DID.String())
93
134
if err != nil {
94
94
-
log.Printf("failed to create profile timeline for %s: %s", ident.DID.String(), err)
135
135
+
log.Printf("failed to create profile timeline for %s: %s", id.DID, err)
95
136
}
96
137
97
138
var didsToResolve []string
···
113
154
}
114
155
}
115
156
116
116
-
followers, following, err := db.GetFollowerFollowingCount(s.db, ident.DID.String())
117
117
-
if err != nil {
118
118
-
log.Printf("getting follow stats repos for %s: %s", ident.DID.String(), err)
119
119
-
}
120
120
-
121
121
-
loggedInUser := s.oauth.GetUser(r)
122
122
-
followStatus := db.IsNotFollowing
123
123
-
if loggedInUser != nil {
124
124
-
followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, ident.DID.String())
125
125
-
}
126
126
-
127
157
now := time.Now()
128
158
startOfYear := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.UTC)
129
159
punchcard, err := db.MakePunchcard(
130
160
s.db,
131
131
-
db.FilterEq("did", ident.DID.String()),
161
161
+
db.FilterEq("did", id.DID),
132
162
db.FilterGte("date", startOfYear.Format(time.DateOnly)),
133
163
db.FilterLte("date", now.Format(time.DateOnly)),
134
164
)
135
165
if err != nil {
136
136
-
log.Println("failed to get punchcard for did", "did", ident.DID.String(), "err", err)
166
166
+
log.Println("failed to get punchcard for did", "did", id.DID, "err", err)
137
167
}
138
168
139
139
-
s.pages.ProfilePage(w, pages.ProfilePageParams{
140
140
-
LoggedInUser: loggedInUser,
169
169
+
s.pages.ProfileHomePage(w, pages.ProfileHomePageParams{
170
170
+
LoggedInUser: pageWithProfile.LoggedInUser,
141
171
Repos: pinnedRepos,
142
172
CollaboratingRepos: pinnedCollaboratingRepos,
143
143
-
Card: pages.ProfileCard{
144
144
-
UserDid: ident.DID.String(),
145
145
-
UserHandle: ident.Handle.String(),
146
146
-
Profile: profile,
147
147
-
FollowStatus: followStatus,
148
148
-
FollowersCount: followers,
149
149
-
FollowingCount: following,
150
150
-
},
151
151
-
Punchcard: punchcard,
152
152
-
ProfileTimeline: timeline,
173
173
+
Card: pageWithProfile.Card,
174
174
+
Punchcard: punchcard,
175
175
+
ProfileTimeline: timeline,
153
176
})
154
177
}
155
178
156
179
func (s *State) reposPage(w http.ResponseWriter, r *http.Request) {
157
157
-
ident, ok := r.Context().Value("resolvedId").(identity.Identity)
158
158
-
if !ok {
159
159
-
s.pages.Error404(w)
180
180
+
pageWithProfile := s.profilePage(w, r)
181
181
+
if pageWithProfile == nil {
160
182
return
161
183
}
162
184
163
163
-
profile, err := db.GetProfile(s.db, ident.DID.String())
164
164
-
if err != nil {
165
165
-
log.Printf("getting profile data for %s: %s", ident.DID.String(), err)
166
166
-
}
167
167
-
185
185
+
id := pageWithProfile.Id
168
186
repos, err := db.GetRepos(
169
187
s.db,
170
188
0,
171
171
-
db.FilterEq("did", ident.DID.String()),
189
189
+
db.FilterEq("did", id.DID),
172
190
)
173
191
if err != nil {
174
174
-
log.Printf("getting repos for %s: %s", ident.DID.String(), err)
175
175
-
}
176
176
-
177
177
-
loggedInUser := s.oauth.GetUser(r)
178
178
-
followStatus := db.IsNotFollowing
179
179
-
if loggedInUser != nil {
180
180
-
followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, ident.DID.String())
181
181
-
}
182
182
-
183
183
-
followers, following, err := db.GetFollowerFollowingCount(s.db, ident.DID.String())
184
184
-
if err != nil {
185
185
-
log.Printf("getting follow stats repos for %s: %s", ident.DID.String(), err)
192
192
+
log.Printf("getting repos for %s: %s", id.DID, err)
186
193
}
187
194
188
195
s.pages.ReposPage(w, pages.ReposPageParams{
189
189
-
LoggedInUser: loggedInUser,
196
196
+
LoggedInUser: pageWithProfile.LoggedInUser,
190
197
Repos: repos,
191
191
-
Card: pages.ProfileCard{
192
192
-
UserDid: ident.DID.String(),
193
193
-
UserHandle: ident.Handle.String(),
194
194
-
Profile: profile,
195
195
-
FollowStatus: followStatus,
196
196
-
FollowersCount: followers,
197
197
-
FollowingCount: following,
198
198
-
},
198
198
+
Card: pageWithProfile.Card,
199
199
})
200
200
}
201
201
···
206
206
}
207
207
208
208
func (s *State) followPage(w http.ResponseWriter, r *http.Request, fetchFollows func(db.Execer, string) ([]db.Follow, error), extractDid func(db.Follow) string) *FollowsPageParams {
209
209
-
ident, ok := r.Context().Value("resolvedId").(identity.Identity)
210
210
-
if !ok {
211
211
-
s.pages.Error404(w)
209
209
+
pageWithProfile := s.profilePage(w, r)
210
210
+
if pageWithProfile == nil {
212
211
return nil
213
212
}
214
214
-
did := ident.DID.String()
215
215
-
216
216
-
profile, err := db.GetProfile(s.db, did)
217
217
-
if err != nil {
218
218
-
log.Printf("getting profile data for %s: %s", did, err)
219
219
-
}
220
213
221
221
-
loggedInUser := s.oauth.GetUser(r)
214
214
+
id := pageWithProfile.Id
222
215
223
223
-
follows, err := fetchFollows(s.db, did)
216
216
+
follows, err := fetchFollows(s.db, id.DID.String())
224
217
if err != nil {
225
225
-
log.Printf("getting followers for %s: %s", did, err)
218
218
+
log.Printf("getting followers for %s: %s", id.DID, err)
226
219
}
227
220
228
221
if len(follows) == 0 {
···
240
233
return nil
241
234
}
242
235
236
236
+
loggedInUser := pageWithProfile.LoggedInUser
243
237
var loggedInUserFollowing map[string]struct{}
244
238
if loggedInUser != nil {
245
239
following, err := db.GetFollowing(s.db, loggedInUser.Did)
···
284
278
})
285
279
}
286
280
287
287
-
followStatus := db.IsNotFollowing
288
288
-
if loggedInUser != nil {
289
289
-
followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, did)
290
290
-
}
291
291
-
292
292
-
followersCount, followingCount, err := db.GetFollowerFollowingCount(s.db, did)
293
293
-
if err != nil {
294
294
-
log.Printf("getting follow stats followers for %s: %s", did, err)
295
295
-
}
296
296
-
297
281
return &FollowsPageParams{
298
282
LoggedInUser: loggedInUser,
299
283
Follows: followCards,
300
300
-
Card: pages.ProfileCard{
301
301
-
UserDid: did,
302
302
-
UserHandle: ident.Handle.String(),
303
303
-
Profile: profile,
304
304
-
FollowStatus: followStatus,
305
305
-
FollowersCount: followersCount,
306
306
-
FollowingCount: followingCount,
307
307
-
},
284
284
+
Card: pageWithProfile.Card,
308
285
}
309
286
}
310
287