diff --git a/src/main/scala/com/retwis/model/User.scala b/src/main/scala/com/retwis/model/User.scala index 25b4289..38f04c4 100644 --- a/src/main/scala/com/retwis/model/User.scala +++ b/src/main/scala/com/retwis/model/User.scala @@ -61,7 +61,7 @@ object User { jedis.set("username:" + username + ":uid", nextUserId.toString) jedis.set("uid:" + nextUserId.toString + ":username", username) jedis.set("uid:" + nextUserId.toString + ":password", password) - jedis.sadd("global:users", nextUserId.toString) + jedis.lpush("global:users", nextUserId.toString) return true } catch { case e => e.printStackTrace @@ -92,11 +92,11 @@ object User { return false } - def logout(username: String, password: String): Boolean = { + def logout(): Boolean = { val jedis = RetwisDB.pool.getResource() try { - val userid = jedis.get("username:" + username + ":uid") - if(userid != null && password == jedis.get("uid:" + userid + ":password")) { + val userid = jedis.get("auth:" + auth.is) + if(userid != null) { jedis.del("uid:" + userid + ":auth", auth.is) jedis.del("auth:" + auth.is, userid) auth.set("Logged Out") @@ -216,7 +216,7 @@ class User(id: String, username: String, password: String) { val postTime = jedis.get("pid:" + postId + ":time") val postMessage = jedis.get("pid:" + postId + ":message") tweets(i) = new Tweet(postId, postTime.toLong, postMessage, username) - i += 1 + i = i + 1 } return tweets } catch { @@ -233,12 +233,10 @@ class User(id: String, username: String, password: String) { try { val nextPostId = jedis.incr("global:nextPostId") - val setTimeResponse = jedis.set("pid:" + nextPostId + ":time", Platform.currentTime.toString) - val setMessageResponse = jedis.set("pid:" + nextPostId + ":message", message) - val setUsernameResponse = jedis.set("pid:" + nextPostId + ":username", username) - val setUserPostsResponse = jedis.rpush("uid:" + id + ":posts", nextPostId.toString) - if(setTimeResponse != "OK" || setMessageResponse != "OK" || setUsernameResponse != "OK" || setUserPostsResponse < 1) - throw new Exception("Response *not* OK. setTimeResponse=" + setTimeResponse + " setMessageResponse=" + setMessageResponse + " setUsernameResponse=" + setUsernameResponse + " setUserPostsResponse=" + setUserPostsResponse) + jedis.set("pid:" + nextPostId + ":time", Platform.currentTime.toString) + jedis.set("pid:" + nextPostId + ":message", message) + jedis.lpush("global:timeline", nextPostId.toString) + jedis.rpush("uid:" + id + ":posts", nextPostId.toString) } catch { case e => e.printStackTrace } finally { @@ -258,6 +256,20 @@ class User(id: String, username: String, password: String) { RetwisDB.pool.returnResource(jedis) } } + + //follow user by username + def followUsername(followeeName: String) = { + val jedis = RetwisDB.pool.getResource + + try { + val uid = jedis.get("username:" + followeeName + ":uid") + this.follow(uid) + } catch { + case e => e.printStackTrace + } finally { + RetwisDB.pool.returnResource(jedis) + } + } //stop following user def unFollow(followeeId: String) = { diff --git a/src/main/scala/com/retwis/snippet/FollowSnippet.scala b/src/main/scala/com/retwis/snippet/FollowSnippet.scala new file mode 100644 index 0000000..cbff304 --- /dev/null +++ b/src/main/scala/com/retwis/snippet/FollowSnippet.scala @@ -0,0 +1,39 @@ +package com.retwis.snippet + +import com.retwis.model._ +import _root_.net.liftweb._ +import http._ +import mapper._ +import S._ +import SHtml._ +import Req._ +import common._ +import util._ +import Helpers._ +import _root_.scala.xml.{NodeSeq, Text, Group, NodeBuffer} + +class FollowSnippet { + object targetUser extends RequestVar("") + + def followButton (xhtml : NodeSeq) : NodeSeq = { + val curUser = User.getLoggedInUser + targetUser(S.param("u").openTheBox) + + def processFollow () { + curUser.followUsername(targetUser.is) + } + + if(curUser == null || curUser.getUsername == targetUser.is) { + bind("follow", xhtml, + "button" -> "") + } + else { + var followButtonText = new NodeBuffer &+ "Follow" + if(curUser.isFollowing(targetUser.is)) + followButtonText = new NodeBuffer &+ "Unfollow" + + bind("follow", xhtml, + "button" -> SHtml.link("index", processFollow, followButtonText)) + } + } +} \ No newline at end of file diff --git a/src/main/scala/com/retwis/snippet/IndexPage.scala b/src/main/scala/com/retwis/snippet/IndexPage.scala index 0c145a1..3ebad84 100644 --- a/src/main/scala/com/retwis/snippet/IndexPage.scala +++ b/src/main/scala/com/retwis/snippet/IndexPage.scala @@ -39,7 +39,19 @@ class IndexPage { "password" -> SHtml.password(password, password = _), "submit" -> SHtml.submit("Login", processLogin)) } - + + def logout(xhtml : NodeSeq) : NodeSeq = { + def processLogout () { + User.logout + } + + val logout = new NodeBuffer &+ "logout" + + bind("logout", xhtml, + //"logoutButton" -> SHtml.submit("Logout", processLogout)) + "logoutButton" -> SHtml.link("index", processLogout, logout)) + } + def register(xhtml : NodeSeq) : NodeSeq = { var password = "" var passwordRepeat = "" diff --git a/src/main/scala/com/retwis/snippet/TimelineSnippet.scala b/src/main/scala/com/retwis/snippet/TimelineSnippet.scala index 59f3ef2..e586028 100644 --- a/src/main/scala/com/retwis/snippet/TimelineSnippet.scala +++ b/src/main/scala/com/retwis/snippet/TimelineSnippet.scala @@ -13,11 +13,14 @@ import _root_.scala.xml.{NodeSeq, Text, Group, NodeBuffer} class TimelineSnippet { def latestRegisteredUsers (content : NodeSeq) : NodeSeq = { - val uIter = User.getLastUsers.iterator val result = new NodeBuffer - while(uIter.hasNext) { - val user = uIter.next - result &+ User.renderUserHTML(user.getUsername) + val usrs = User.getLastUsers + if(usrs != null) { + val uIter = usrs.iterator + while(uIter.hasNext) { + val user = uIter.next + result &+ User.renderUserHTML(user.getUsername) + } } result } diff --git a/src/main/scala/com/retwis/snippet/UserPage.scala b/src/main/scala/com/retwis/snippet/UserPage.scala deleted file mode 100644 index 9e919ce..0000000 --- a/src/main/scala/com/retwis/snippet/UserPage.scala +++ /dev/null @@ -1,51 +0,0 @@ -package com.retwis.snippet - -import com.retwis.model._ -import _root_.net.liftweb._ -import http._ -import mapper._ -import S._ -import SHtml._ -import Req._ -import common._ -import util._ -import Helpers._ -import _root_.scala.xml.{NodeSeq, Text, Group, NodeBuffer} - -class UserPage { - def userTitle (content : NodeSeq) : NodeSeq = { - val result = new NodeBuffer - val userBox = S.param("u") - if(userBox.isEmpty) result &+

No user by that name.

- else { - val uname = userBox.openTheBox - result &+

{uname}

- } - result - } - - def userPosts (content : NodeSeq) : NodeSeq = { - val result = new NodeBuffer - val userBox = S.param("u") - if(!userBox.isEmpty) { - val u = User.getUserByName(userBox.openTheBox) - val tweets = u.getNRecentTweets(10) - println("tweets is " + tweets.length + " long.") - for(i <- 0 until tweets.length) result &+ Tweet.renderTweetHTML(tweets(i)) - } - result - } - - /* Fix This */ - def followButton (content : NodeSeq) : NodeSeq = { - val result = new NodeBuffer - val userBox = S.param("u") - if(!userBox.isEmpty && User.isLoggedIn()) { - val uid = userBox.openTheBox - val user = User.getLoggedInUser - if(user isFollowing uid) result &+ Follow this user - else result &+ Stop following - } - result - } -} \ No newline at end of file diff --git a/src/main/scala/com/retwis/snippet/UserSnippet.scala b/src/main/scala/com/retwis/snippet/UserSnippet.scala index 51014ad..bbcf523 100644 --- a/src/main/scala/com/retwis/snippet/UserSnippet.scala +++ b/src/main/scala/com/retwis/snippet/UserSnippet.scala @@ -45,29 +45,16 @@ class UserSnippet { val u = User.getLoggedInUser val followerCount = u.getFollowers.length val followingCount = u.getFollowing.length - bind("followInfo", xhtml, - "followers" -> followerCount.toString, - "following" -> followingCount.toString) - } - - def latestTweets( xhtml: NodeSeq ) : NodeSeq = { - val latestTweets = User.getLoggedInUser.getNRecentTweets(20).elements.toList - def bindTweets(template: NodeSeq): NodeSeq = { - latestTweets.flatMap{ case (t) => bind("usertimeline", template,"tweet" -> t.getMessage,"time" -> Tweet.strElapsed(t.getTime))} - } - bind("usertimeline",xhtml, "name" -> User.getLoggedInUser.getUsername, "tweets" -> bindTweets _) - } + bind("followInfo", xhtml, + "followers" -> followerCount.toString, + "following" -> followingCount.toString) + } - /* Fix This */ - def followButton (xhtml : NodeSeq) : NodeSeq = { - val result = new NodeBuffer - val userBox = S.param("u") - if(!userBox.isEmpty && User.isLoggedIn()) { - val uid = userBox.openTheBox - val user = User.getLoggedInUser - if(user isFollowing uid) result &+ Follow this user - else result &+ Stop following + def latestTweets( xhtml: NodeSeq ) : NodeSeq = { + val latestTweets = User.getLoggedInUser.getNRecentTweets(20).elements.toList + def bindTweets(template: NodeSeq): NodeSeq = { + latestTweets.flatMap{ t => bind("usertimeline", template, "tweet" -> t.getMessage, "time" -> Tweet.strElapsed(t.getTime))} + } + bind("latestTweets", xhtml, "usertimeline" -> bindTweets _) } - result - } -} \ No newline at end of file + } \ No newline at end of file diff --git a/src/main/webapp/templates-hidden/_LoggedInHome.html b/src/main/webapp/templates-hidden/_LoggedInHome.html index 67ab88d..04887c7 100644 --- a/src/main/webapp/templates-hidden/_LoggedInHome.html +++ b/src/main/webapp/templates-hidden/_LoggedInHome.html @@ -19,11 +19,11 @@

Your latest tweets:

-
-
- +
+


+ posted ago

+
-
\ No newline at end of file diff --git a/src/main/webapp/templates-hidden/default.html b/src/main/webapp/templates-hidden/default.html index 7661e73..a8d958a 100644 --- a/src/main/webapp/templates-hidden/default.html +++ b/src/main/webapp/templates-hidden/default.html @@ -71,8 +71,8 @@

home | timeline - | logout - + | +
diff --git a/src/main/webapp/timeline.html b/src/main/webapp/timeline.html index c066f51..b18a72c 100644 --- a/src/main/webapp/timeline.html +++ b/src/main/webapp/timeline.html @@ -12,11 +12,11 @@

Global Timeline

Latest registered users (an example of SORT command!)
-
+
Latest 50 messages from users around the world!
-
+
diff --git a/src/main/webapp/user.html b/src/main/webapp/user.html index 649783a..7b24c36 100644 --- a/src/main/webapp/user.html +++ b/src/main/webapp/user.html @@ -10,15 +10,17 @@
-
+
-
+
-
- + + +