Skip to content

Commit

Permalink
refactored a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelcm committed Aug 17, 2011
1 parent 6e3f6cb commit beb9d79
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 368 deletions.
8 changes: 7 additions & 1 deletion src/main/scala/bootstrap/liftweb/Boot.scala
Expand Up @@ -2,6 +2,7 @@
package bootstrap.liftweb

import com.retwis.model._
import com.retwis.api._
import net.liftweb._
import util._
import Helpers._
Expand Down Expand Up @@ -54,9 +55,14 @@ class Boot {
//Logged in/out test
LiftRules.loggedInTest = Full(
() => {
User.isLoggedIn()
RetwisAPI.isLoggedIn()
}
)

LiftRules.dispatch.append {
case Req("follow" :: "id" :: followId :: Nil, _, _) =>
RetwisAPI.follow(followId)
}

// Use HTML5 for rendering
/* //commented out so I can use mixed case in my snippets
Expand Down
245 changes: 245 additions & 0 deletions src/main/scala/com/retwis/api/RetwisAPI.scala
@@ -0,0 +1,245 @@
package com.retwis.api

import com.retwis.api._
import com.retwis.model._
import scala.collection.immutable.List
import net.liftweb.http._
import net.liftweb.http.provider._
import _root_.redis.clients.jedis._
import scala.collection.JavaConversions._
import compat.Platform
import java.security.MessageDigest
import java.security.SecureRandom
import java.math.BigInteger
import scala.collection.JavaConversions._
import _root_.scala.xml.{NodeSeq, Text, Group, NodeBuffer}


object RetwisAPI {
val pool = new JedisPool(new JedisPoolConfig(), "localhost");
private val random = new SecureRandom();
object auth extends SessionVar[String]("LoggedOut")

//render User HTML
def renderUserHTML(username: String): NodeSeq = {
<a class="username" href={ "user?u=" + username }>{username}</a><br />
}

//get last 50 users
def getLastUsers(): Array[User] = {
val jedis = pool.getResource

try {
val userIds = jedis.lrange("global:users", 0, 50)
var userArray = new Array[User](userIds.length)
var i = 0
for(id<-userIds) {
val username = jedis.get("uid:" + id + ":username")
userArray(i) = new User(id, username, "")
i += 1
}
return userArray
} catch {
case e => e.printStackTrace
} finally {
pool.returnResource(jedis)
}
return null
}

//return a random MD5 hash value (for session keys)
private def getRand(): String = {
val s = new BigInteger(130, random).toString(32)
return new String(MessageDigest.getInstance("MD5").digest(s.getBytes))
}

def createUser(username: String, password: String): Boolean = {
val jedis = pool.getResource

if(username != null && password != null) {
try {
if(jedis.get("username:" + username + ":uid") != null) return false
val nextUserId = jedis.incr("glabal:nextUserId")
jedis.set("username:" + username + ":uid", nextUserId.toString)
jedis.set("uid:" + nextUserId.toString + ":username", username)
jedis.set("uid:" + nextUserId.toString + ":password", password)
jedis.lpush("global:users", nextUserId.toString)
return true
} catch {
case e => e.printStackTrace
} finally {
pool.returnResource(jedis)
}
}
return false
}

//return TRUE and set AUTH hash if login info is value, otherwise return FALSE
def login(username: String, password: String): Boolean = {
val jedis = pool.getResource()
try {
val userid = jedis.get("username:" + username + ":uid")
if(userid != null && password == jedis.get("uid:" + userid + ":password")) {
val authToken = getRand()
jedis.set("uid:" + userid + ":auth", authToken)
jedis.set("auth:" + authToken, userid)
auth.set(authToken)
return true
}
} catch {
case e => e.printStackTrace()
} finally {
pool.returnResource(jedis)
}
return false
}

def logout(): Boolean = {
val jedis = pool.getResource()
try {
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")
return true
}
} catch {
case e => e.printStackTrace()
} finally {
pool.returnResource(jedis)
}
return false
}

//return TRUE if logged in
def isLoggedIn(): Boolean = {
val jedis = pool.getResource()
var retVal = false

try {
val userid = jedis.get("auth:" + auth.is)
if(userid != null && jedis.get("uid:" + userid + ":auth") == auth.is) {
retVal = true
}
} catch {
case e => e.printStackTrace()
} finally {
pool.returnResource(jedis)
}
return retVal
}

//get User object representing the logged in user
def getLoggedInUser(): User = {
val jedis = pool.getResource()

if(isLoggedIn) {
try {
val userid = jedis.get("auth:" + auth.is)
return getUserById(userid)
} catch {
case e => e.printStackTrace()
} finally {
pool.returnResource(jedis)
}
}
return null
}

//return a User object corresponding to userid
def getUserById(userid: String): User = {
val jedis = pool.getResource()
try {
var username = jedis.get("uid:" + userid + ":username")
var password = jedis.get("uid:" + userid + ":password")
if (username != null && password != null) {
return new User(userid, username, password)
}
} catch {
case e => e.printStackTrace()
} finally {
pool.returnResource(jedis)
}
return null
}

//return a User object corresponding to username
def getUserByName(username: String): User = {
val jedis = pool.getResource()
try {
val userid = jedis.get("username:" + username + ":uid")
val password = jedis.get("uid:" + userid + ":password")
if (userid != null && password != null) {
return new User(userid, username, password)
}
} catch {
case e => e.printStackTrace()
} finally {
pool.returnResource(jedis)
}
return null
}

//create a string showing the time elapsed
def strElapsed(time: Long): String = {
val elapsedSeconds = (Platform.currentTime - time) / 1000
if(elapsedSeconds < 60) return elapsedSeconds + " seconds"
if(elapsedSeconds < 3600) {
val m = elapsedSeconds / 60
return pluralize(m, " minute")
}
if(elapsedSeconds < 3600*24) {
val h = elapsedSeconds / 3600
return pluralize(h, " hour")
}
val d = elapsedSeconds / (3600*24)
return pluralize(d, " day")
}

private def pluralize(i: Long, s: String): String = {
if(i>1) return i + s + "s"
return i + s
}

//get Tweet object based on tweet id
def getTweet(id: String): Tweet = {
val jedis = pool.getResource()

try {
val time = jedis.get("pid:" + id + ":time")
val message = jedis.get("pid:" + id + ":message")
val authorId = jedis.get("pid:" + id + ":uid")
if (time != null && message != null) {
return new Tweet(id, time.toLong, message, authorId)
}
} catch {
case e => e.printStackTrace()
} finally {
pool.returnResource(jedis)
}

return null
}

//get last 50 tweets
def getLastTweets(): Array[Tweet] = {
val jedis = pool.getResource

try {
val tweetIds = jedis.lrange("global:timeline", 0, 50)
var tweets = new Array[Tweet](tweetIds.length)
var i = 0
for(id<-tweetIds) {
tweets(i) = new Tweet(id, jedis.get("pid:" + id + ":time").toLong, jedis.get("pid:" + id + ":message"), jedis.get("pid:" + id + ":uid"))
i += 1
}
return tweets
} catch {
case e => e.printStackTrace
} finally {
pool.returnResource(jedis)
}
return null
}
}
85 changes: 4 additions & 81 deletions src/main/scala/com/retwis/model/Tweet.scala
@@ -1,88 +1,11 @@
package com.retwis.model

import com.retwis.util._
import com.retwis.api._
import com.retwis.api._
import scala.collection.JavaConversions._
import compat.Platform
import _root_.scala.xml.{NodeSeq, Text, Group, NodeBuffer}

object Tweet {
//render Tweet HTML
/* delete this
def renderTweetHTML(username: String, message: String, time: Long) : NodeSeq = {
val elapsed = strElapsed(time)
<a id="user" href={ "user?u=" + username }>{username}</a>
<div class="post">{message}<br />
<i>posted {elapsed} ago via web</i></div>
}
def renderTweetHTML(tweet: Tweet) : NodeSeq = {
renderTweetHTML(tweet.getUsername, tweet.getMessage, tweet.getTime)
}
*/

//create a string showing the time elapsed
def strElapsed(time: Long): String = {
val elapsedSeconds = (Platform.currentTime - time) / 1000
if(elapsedSeconds < 60) return elapsedSeconds + " seconds"
if(elapsedSeconds < 3600) {
val m = elapsedSeconds / 60
return pluralize(m, " minute")
}
if(elapsedSeconds < 3600*24) {
val h = elapsedSeconds / 3600
return pluralize(h, " hour")
}
val d = elapsedSeconds / (3600*24)
return pluralize(d, " day")
}

private def pluralize(i: Long, s: String): String = {
if(i>1) return i + s + "s"
return i + s
}

//get Tweet object based on tweet id
def getTweet(id: String): Tweet = {
val jedis = RetwisDB.pool.getResource()

try {
val time = jedis.get("pid:" + id + ":time")
val message = jedis.get("pid:" + id + ":message")
val authorId = jedis.get("pid:" + id + ":uid")
if (time != null && message != null) {
return new Tweet(id, time.toLong, message, authorId)
}
} catch {
case e => e.printStackTrace()
} finally {
RetwisDB.pool.returnResource(jedis)
}

return null
}

//get last 50 tweets
def getLastTweets(): Array[Tweet] = {
val jedis = RetwisDB.pool.getResource

try {
val tweetIds = jedis.lrange("global:timeline", 0, 50)
var tweets = new Array[Tweet](tweetIds.length)
var i = 0
for(id<-tweetIds) {
tweets(i) = new Tweet(id, jedis.get("pid:" + id + ":time").toLong, jedis.get("pid:" + id + ":message"), jedis.get("pid:" + id + ":uid"))
i += 1
}
return tweets
} catch {
case e => e.printStackTrace
} finally {
RetwisDB.pool.returnResource(jedis)
}
return null
}
}

class Tweet(id: String, time: Long, message: String, authorId: String) {
/* Getters */
def getId(): String = return id
Expand All @@ -91,7 +14,7 @@ class Tweet(id: String, time: Long, message: String, authorId: String) {
def getAuthorId(): String = return authorId

def getAuthorLink(): NodeSeq = {
val username = User.getUserById(authorId).getUsername
return User.renderUserHTML(username)
val username = RetwisAPI.getUserById(authorId).getUsername
return RetwisAPI.renderUserHTML(username)
}
}

0 comments on commit beb9d79

Please sign in to comment.