|
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 |
4 | 4 |
|
5 | 5 | export follow
|
6 | 6 |
|
| 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 | + |
7 | 23 | proc createFollowRouter*(cfg: Config) =
|
8 | 24 | 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="/") |
10 | 32 | 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="/") |
12 | 40 | redirect(refPath())
|
0 commit comments