Permalink
Browse files

Tweets are now serialized as JSON strings

  • Loading branch information...
1 parent 7d2019d commit b1d15f0372dcb2bdc31294385eaa9da7ec7ebb76 @raphaelcm committed Aug 22, 2011
View
@@ -18,9 +18,7 @@ uid:1000:posts (list of post ids)
UPDATES
incr global:nextPostId => 134
-pid:134:time => time (in mills)
-pid:134:message => message
-pid:134:uid => uid of author
+pid:134:tweet => Tweet (serialized as JSON string)
TIMELINE
View
@@ -16,7 +16,8 @@ libraryDependencies ++= {
Seq(
"net.liftweb" %% "lift-webkit" % liftVersion % "compile->default",
"net.liftweb" %% "lift-mapper" % liftVersion % "compile->default",
- "net.liftweb" %% "lift-wizard" % liftVersion % "compile->default"
+ "net.liftweb" %% "lift-wizard" % liftVersion % "compile->default",
+ "net.liftweb" %% "lift-json" % liftVersion % "compile->default"
)
}
@@ -219,11 +219,9 @@ object Retwis {
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)
+ val jsonTwt = jedis.get("pid:" + id + ":tweet")
+ if (jsonTwt != null) {
+ return Tweet.getTweetFromJson(jsonTwt)
}
} catch {
case e => e.printStackTrace()
@@ -243,7 +241,7 @@ object Retwis {
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"))
+ tweets(i) = Tweet.getTweetFromJson(jedis.get("pid:" + id + ":tweet"))
i += 1
}
return tweets
@@ -268,7 +266,7 @@ object Retwis {
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"))
+ tweets(i) = Tweet.getTweetFromJson(jedis.get("pid:" + id + ":tweet"))
i += 1
}
return tweets
@@ -294,9 +292,8 @@ object Retwis {
val uid = getLoggedInId
try {
val nextPostId = jedis.incr("global:nextPostId")
- jedis.set("pid:" + nextPostId + ":time", Platform.currentTime.toString)
- jedis.set("pid:" + nextPostId + ":message", message)
- jedis.set("pid:" + nextPostId + ":uid", uid)
+ val newTwt = new Tweet(nextPostId.toString, Platform.currentTime, message, uid)
+ jedis.set("pid:" + nextPostId + ":tweet", newTwt.toJson)
jedis.lpush("global:timeline", nextPostId.toString)
jedis.lpush("uid:" + uid + ":posts", nextPostId.toString)
} catch {
@@ -311,14 +308,10 @@ object Retwis {
try {
val id = getLoggedInId
if(isFollowing(targetId)) {
- println("SREM " + "uid:" + id + ":following " + targetId)
jedis.srem("uid:" + id + ":following", targetId)
- println("SREM " + "uid:" + targetId + ":following " + id)
jedis.srem("uid:" + targetId + ":followers", id)
} else {
- println("SADD " + "uid:" + id + ":following " + targetId)
jedis.sadd("uid:" + id + ":following", targetId)
- println("SADD " + "uid:" + targetId + ":followers " + id)
jedis.sadd("uid:" + targetId + ":followers", id)
}
} catch {
@@ -1,15 +1,30 @@
package com.retwis.model
-import com.retwis.api._
-import com.retwis.api._
-import scala.collection.JavaConversions._
-import compat.Platform
-import _root_.scala.xml.{NodeSeq, Text, Group, NodeBuffer}
+import net.liftweb.json._
+import net.liftweb.json.JsonAST._
+import net.liftweb.json.JsonDSL._
-class Tweet(id: String, time: Long, message: String, authorId: String) {
- /* Getters */
- def getId(): String = return id
- def getTime(): Long = return time
- def getMessage(): String = return message
- def getAuthorId(): String = return authorId
+object Tweet {
+ def getTweetFromJson(jsonStr: String):Tweet = {
+ implicit val formats = net.liftweb.json.DefaultFormats
+ val json = parse(jsonStr)
+ val id = (json \ "tweet" \ "id").extract[String]
+ val time = (json \ "tweet" \ "time").extract[Long]
+ val message = (json \ "tweet" \ "message").extract[String]
+ val authorId = (json \ "tweet" \ "authorId").extract[String]
+ return new Tweet(id, time, message, authorId)
+ }
+}
+
+class Tweet(val id: String, val time: Long, val message: String, val authorId: String) {
+ /* Serialize tweet to JSON string */
+ def toJson(): String = {
+ val json = ("tweet" ->
+ ("id" -> id) ~
+ ("time" -> time) ~
+ ("message" -> message) ~
+ ("authorId" -> authorId))
+
+ compact(JsonAST.render(json))
+ }
}
@@ -45,7 +45,7 @@ class TimelineSnippet {
def latestTweets (latestTweets: Array[Tweet], xhtml: NodeSeq) : NodeSeq = {
if(latestTweets == null) return xhtml
def bindTweets(template: NodeSeq): NodeSeq = {
- latestTweets.toList.flatMap{ t => bind("post", template, "authorlink" -> Retwis.getUserLink(t.getAuthorId), "message" -> t.getMessage, "elapsedtime" -> Retwis.strElapsed(t.getTime))}
+ latestTweets.toList.flatMap{ t => bind("post", template, "authorlink" -> Retwis.getUserLink(t.authorId), "message" -> t.message, "elapsedtime" -> Retwis.strElapsed(t.time))}
}
bind("latestTweets", xhtml, "timeline" -> bindTweets _)
}
@@ -17,13 +17,6 @@
| <a href="timeline">timeline</a>
<lift:test_cond.loggedin>
| <lift:IndexPage.logout><logout:logoutButton /></lift:IndexPage.logout>
- <br /><div class="ui-widget">
- <form name="input" action="user" method="get">
- <label for="usersearch">User Search: </label>
- <input id="usersearch" type="text" name="u" />
- <input type="submit" />
- </form>
- </div>
</lift:test_cond.loggedin>
</div>
</div>

0 comments on commit b1d15f0

Please sign in to comment.