scala-news-reader

rss/atom news reader in scala

git clone https://9o.is/git/scala-news-reader.git

commit 8887cf1a58d9c76d37181e4b583c79d9f7fe72be
parent 9a9ec4e905550fc0f4f3dab084e82ffd069b5f86
Author: Jul <jul@9o.is>
Date:   Sat,  3 Aug 2013 11:44:07 -0400

able to save (and share [not completely]) articles

Diffstat:
Msrc/main/scala/com/joereader/model/User.scala | 17+++++++++++++++++
Msrc/main/scala/com/joereader/snippet/ArticleSnip.scala | 72+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 84 insertions(+), 5 deletions(-)

diff --git a/src/main/scala/com/joereader/model/User.scala b/src/main/scala/com/joereader/model/User.scala @@ -156,7 +156,24 @@ class User private() extends ProtoAuthUser[User] with ObjectIdPk[User] { } object saved extends MongoListField[User, String](this) + + def saveArticle(article: Article) = { + if (saved.get.exists(_ == article.id)) this + else saved(article.id :: saved.get) + } + + def unSaveArticle(article: Article) = + saved(saved.get.filterNot(_ == article.id)) + object shared extends MongoListField[User, String](this) + + def shareArticle(article: Article) = { + if (shared.get.exists(_ == article.id)) this + else shared(article.id :: shared.get) + } + + def unShareArticle(article: Article) = + shared(shared.get.filterNot(_ == article.id)) } object User extends User with ProtoAuthUserMeta[User] with Loggable { diff --git a/src/main/scala/com/joereader/snippet/ArticleSnip.scala b/src/main/scala/com/joereader/snippet/ArticleSnip.scala @@ -1,12 +1,19 @@ package com.joereader.snippet -import net.liftweb.util._ +import net.liftweb._ +import common._ +import http._ +import SHtml._ +import js._, JsCmds._ +import util._ import Helpers._ import com.joereader._ import lib.rss._ import model._ +import xml._ + /** * Mix this in to list articles. */ @@ -17,10 +24,58 @@ trait ArticleSnip { f => val (entry, bwu) = f - ".article-save [style]" #> ("color:"+bwu.color) & - ".article-share [style]" #> ("color:"+bwu.color) & - ".article-inner [style]" #> ("border-right:3px solid "+bwu.color) & - ".article-key [class+]" #> bwu.id & + def article = bwu.blog.map(Article(_, entry)) + def articleId = article.map(_.id).getOrElse("") + def shareId = article.map(_.idHash+"-share").getOrElse("") + def saveId = article.map(_.idHash+"-save").getOrElse("") + def color = "color:" + bwu.color + + def saved: Boolean = User.currentUser.exists( + _.saved.get.exists(_ == articleId)) + + def shared: Boolean = User.currentUser.exists( + _.shared.get.exists(_ == articleId)) + + def saveLink: NodeSeq = + a(() => save, Text("Save"), "id" -> saveId, "style" -> color) + + def unSaveLink: NodeSeq = + a(() => unSave, Text("Undo Save"), "id" -> saveId, "style" -> color) + + def shareLink: NodeSeq = + a(() => share, Text("Share"), "id" -> shareId, "style" -> color) + + def unShareLink: NodeSeq = + a(() => unShare, Text("Undo Share"), "id" -> shareId, "style" -> color) + + def save: JsCmd = { + for (u <- User.currentUser; a <- article) + u.saveArticle(a).update + Replace(saveId, unSaveLink) + } + + def unSave: JsCmd = { + for (u <- User.currentUser; a <- article) + u.unSaveArticle(a).update + Replace(saveId, saveLink) + } + + def share: JsCmd = { + for (u <- User.currentUser; a <- article) + u.shareArticle(a).update + Replace(shareId, unShareLink) + } + + def unShare: JsCmd = { + for (u <- User.currentUser; a <- article) + u.unShareArticle(a).update + Replace(shareId, shareLink) + } + + ".article-save" #> (if (saved) unSaveLink else saveLink) & + ".article-share" #> (if (shared) unShareLink else shareLink) & + ".article-inner [style]" #> ("border-right:3px solid " + bwu.color) & + ".article-key [class+]" #> bwu.id & ".article-user-link [href]" #> bwu.link & "#article-user-image [src]" #> bwu.image & "#article-user-name *" #> bwu.name & @@ -42,3 +97,10 @@ trait ArticleSnip { } } } + +case class Article(blog: Blog, entry: FeedEntry) { + def id: String = blog.id.get + "~" + entry.id + def idHash = id.hashCode +} + +