Skip to content

Commit

Permalink
everything is basically working...on to formatting and bugfixing
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelcm committed Aug 15, 2011
1 parent 9f0139f commit 0d139a9
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 103 deletions.
34 changes: 23 additions & 11 deletions src/main/scala/com/retwis/model/User.scala
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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) = {
Expand Down
39 changes: 39 additions & 0 deletions 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))
}
}
}
14 changes: 13 additions & 1 deletion src/main/scala/com/retwis/snippet/IndexPage.scala
Expand Up @@ -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 = ""
Expand Down
11 changes: 7 additions & 4 deletions src/main/scala/com/retwis/snippet/TimelineSnippet.scala
Expand Up @@ -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
}
Expand Down
51 changes: 0 additions & 51 deletions src/main/scala/com/retwis/snippet/UserPage.scala

This file was deleted.

35 changes: 11 additions & 24 deletions src/main/scala/com/retwis/snippet/UserSnippet.scala
Expand Up @@ -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 &+ <a href={"follow?u=" + uid} class="button">Follow this user</a>
else result &+ <a href={"follow?u=" + uid} class="button">Stop following</a>
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
}
}
}
8 changes: 4 additions & 4 deletions src/main/webapp/templates-hidden/_LoggedInHome.html
Expand Up @@ -19,11 +19,11 @@
<h2>Your latest tweets:</h2>
</div>
<lift:UserSnippet.latestTweets >
<div class="post">
<latestTweets:usertimeline>
<usertimeline.tweet /><br />
<i><usertimeline.time /></i>
<div class="post">
<p><usertimeline:tweet /><br />
<i>posted <usertimeline:time /> ago</i></p>
</div>
</latestTweets:usertimeline>
</div>
</lift:UserSnippet.latestTweets>
</lift:children>
4 changes: 2 additions & 2 deletions src/main/webapp/templates-hidden/default.html
Expand Up @@ -71,8 +71,8 @@ <h1 class="alt"><img style="border:none" src="/images/logo.png" width="192" heig
<a href="index">home</a>
| <a href="timeline">timeline</a>
<lift:test_cond.loggedin>
<span id="logoutlink">| <a href="logout">logout</a></span>
</lift:test_cond.loggedin>
| <lift:IndexPage.logout><logout:logoutButton /></lift:IndexPage.logout>
</lift:test_cond.loggedin>
<br /><div class="ui-widget">
<form name="input" action="user" method="get">
<label for="usersearch">User Search: </label>
Expand Down
4 changes: 2 additions & 2 deletions src/main/webapp/timeline.html
Expand Up @@ -12,11 +12,11 @@
<div id="main" class="lift:surround?with=default;at=content">
<h2>Global Timeline</h2>
<i>Latest registered users (an example of SORT command!)</i><br />
<div class="lift:Timeline.latestRegisteredUsers">
<div class="lift:TimelineSnippet.latestRegisteredUsers">
<lift:TimelineSnippet.latestRegisteredUsers />
</div>
<i>Latest 50 messages from users around the world!</i><br />
<div class="lift:Timeline.latestTweets">
<div class="lift:TimelineSnippet.latestTweets">
<lift:TimelineSnippet.latestTweets />
</div>

Expand Down
10 changes: 6 additions & 4 deletions src/main/webapp/user.html
Expand Up @@ -10,15 +10,17 @@

<body class="lift:content_id=main">
<div id="main" class="lift:surround?with=default;at=content">
<div class="lift:UserPage.userTitle">
<div class="lift:UserSnippet.userTitle">
<lift:UserSnippet.userTitle />
</div>
<div class="lift:UserPage.userPosts">
<div class="lift:UserSnippet.userPosts">
<lift:UserSnippet.userPosts />
</div>
<div class="lift:UserPage.followButton">
<lift:UserSnippet.followButton />
<lift:FollowSnippet.followButton>
<div class="followButton">
<follow:button />
</div>
</lift:FollowSnippet.followButton>
</div>
</body>
</html>

0 comments on commit 0d139a9

Please sign in to comment.