Skip to content

Commit d3f7c65

Browse files
committed
Add functionality to un/follow buttons
The router_utils savePref function didn't work in my testing, so I am setting the cookie directly through Jester's setCookie procedure.
1 parent e248dcc commit d3f7c65

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/routes/follow.nim

+33-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
1-
import jester
2-
import asyncdispatch, strutils, options, router_utils
3-
import ".."/[prefs, types, utils, redis_cache]
1+
import jester, asyncdispatch, strutils, sequtils
2+
import router_utils
3+
import ../types
44

55
export follow
66

7+
proc addUserToFollowing*(following, toAdd: string): string =
8+
var updated = following.split(",")
9+
if updated == @[""]:
10+
return toAdd
11+
else:
12+
updated = concat(updated, @[toAdd])
13+
result = updated.join(",")
14+
15+
proc removeUserFromFollowing*(following, remove: string): string =
16+
var updated = following.split(",")
17+
if updated == @[""]:
18+
return ""
19+
else:
20+
updated = filter(updated, proc(x: string): bool = x != remove)
21+
result = updated.join(",")
22+
723
proc createFollowRouter*(cfg: Config) =
824
router follow:
9-
post "/follow":
25+
post "/follow/@name":
26+
let
27+
following = cookiePrefs().following
28+
toAdd = @"name"
29+
updated = addUserToFollowing(following, toAdd)
30+
setCookie("following", updated, daysForward(360),
31+
httpOnly=true, secure=cfg.useHttps, path="/")
1032
redirect(refPath())
11-
post "/unfollow":
33+
post "/unfollow/@name":
34+
let
35+
following = cookiePrefs().following
36+
remove = @"name"
37+
updated = removeUserFromFollowing(following, remove)
38+
setCookie("following", updated, daysForward(360),
39+
httpOnly=true, secure=cfg.useHttps, path="/")
1240
redirect(refPath())

src/views/profile.nim

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ proc renderProfileCard*(profile: Profile; prefs: Prefs, path: string): VNode =
2727
linkUser(profile, class="profile-card-username")
2828
let following = isFollowing(profile.username, prefs.following)
2929
if not following:
30-
buttonReferer "/follow", "Follow", path, "profile-card-follow-button"
30+
buttonReferer "/follow/" & profile.username, "Follow", path, "profile-card-follow-button"
3131
else:
32-
buttonReferer "/unfollow", "Unfollow", path, "profile-card-follow-button"
32+
buttonReferer "/unfollow/" & profile.username, "Unfollow", path, "profile-card-follow-button"
3333

3434
tdiv(class="profile-card-extra"):
3535
if profile.bio.len > 0:

0 commit comments

Comments
 (0)