tweeter.nim 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. discard """
  2. action: compile
  3. """
  4. import asyncdispatch, times
  5. import jester
  6. import database, views/user, views/general
  7. proc userLogin(db: Database, request: Request, user: var User): bool =
  8. if request.cookies.hasKey("username"):
  9. if not db.findUser(request.cookies["username"], user):
  10. user = User(username: request.cookies["username"], following: @[])
  11. db.create(user)
  12. return true
  13. else:
  14. return false
  15. let db = newDatabase()
  16. routes:
  17. get "/":
  18. var user: User
  19. if db.userLogin(request, user):
  20. let messages = db.findMessages(user.following & user.username)
  21. resp renderMain(renderTimeline(user.username, messages))
  22. else:
  23. resp renderMain(renderLogin())
  24. get "/@name":
  25. cond '.' notin @"name"
  26. var user: User
  27. if not db.findUser(@"name", user):
  28. halt "User not found"
  29. let messages = db.findMessages(@[user.username])
  30. var currentUser: User
  31. if db.userLogin(request, currentUser):
  32. resp renderMain(renderUser(user, currentUser) & renderMessages(messages))
  33. else:
  34. resp renderMain(renderUser(user) & renderMessages(messages))
  35. post "/follow":
  36. var follower: User
  37. var target: User
  38. if not db.findUser(@"follower", follower):
  39. halt "Follower not found"
  40. if not db.findUser(@"target", target):
  41. halt "Follow target not found"
  42. db.follow(follower, target)
  43. redirect(uri("/" & @"target"))
  44. post "/login":
  45. setCookie("username", @"username", getTime().getGMTime() + 2.hours)
  46. redirect("/")
  47. post "/createMessage":
  48. let message = Message(
  49. username: @"username",
  50. time: getTime(),
  51. msg: @"message"
  52. )
  53. db.post(message)
  54. redirect("/")
  55. runForever()