scala-news-reader

rss/atom news reader in scala

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

UserWriterSnipEdit.scala

(2928B)


      1 package com.joereader.snippet
      2 
      3 import com.joereader._
      4 import model._
      5 import config._
      6 import lib._
      7 import SnipHelpers._
      8 
      9 import scala.xml._
     10 
     11 import net.liftweb._
     12 import util.Helpers._
     13 import http._
     14 import js._
     15 import JsCmds._
     16 import SHtml._
     17 import common._
     18 
     19 /**
     20  * Snippets for currently logged in user (writer only)
     21  * to edit their information.
     22  * We subclass the View trait to fallback if access control test fails.
     23  */
     24 trait UserWriterSnipEdit extends UserWriterSnipView with BackgroundSnip {
     25 
     26   private def test = user.exists(u =>
     27     User.currentUser.exists(_.id.is == u.id.is) && u.isWriter)
     28 
     29   def about(html: NodeSeq) = serve(html) {
     30     user =>
     31       "*" #> ajaxTextarea(user.about.is, {
     32         s => user.about(s).update; Noop
     33       }, "placeholder" -> "About Me goes here")
     34   }(test, super.about)
     35 
     36   def uploadBgImg(html: NodeSeq): NodeSeq = serve(html) {
     37     uploadBgImg
     38   }(test, NodeSeq.Empty)
     39 
     40   def username(html: NodeSeq) = serve(html) {
     41     user =>
     42 
     43       def check(s: String): JsCmd = {
     44         val u = User.findByUsername(s)
     45         def isYou = u.exists(_.id.is == user.id.is)
     46 
     47         if (!s.matches("^[a-z0-9-]{2,}$"))
     48           S.error("User name can only contain " +
     49             "alphanumeric or dash characters")
     50         else if (isYou)
     51           Noop
     52         else if (u.isEmpty && Site.isAvailableMenu(s)) {
     53           user.username(s).update
     54           Noop
     55         } else S.error("Username is taken")
     56       }
     57 
     58       "*" #> ajaxText(user.username.is, {
     59         s: String => check(s.toLowerCase)
     60       })
     61   }(test, super.username)
     62 
     63   def otherVid(html: NodeSeq) = serve(html) {
     64     user =>
     65       var vids: List[String] = user.otherVid.is
     66 
     67       def addVideo(in: String): JsCmd = {
     68         val res =
     69           if (in.isEmpty) Nil
     70           else in.split(",").map(_.trim).toList
     71 
     72         val newVids: List[String] = res diff vids
     73         val sameVids: List[String] = res diff newVids
     74 
     75         user.otherVid(sameVids).update
     76         vids = sameVids
     77 
     78         var failed: List[String] = Nil
     79         newVids.map {
     80           id =>
     81             if (validVideo(id)) {
     82               user.otherVid.add(id).update
     83               vids = id :: vids
     84             } else failed = id :: failed
     85         }
     86 
     87         if (!failed.isEmpty)
     88           S.error("Video ids ~ " + failed.mkString(", ") +
     89             " ~ could not be found")
     90         else Noop
     91       }
     92 
     93       def validVideo(id: String): Boolean = {
     94         import VideoService._, dispatch._
     95         val time = Youtube.video.duration(id)()
     96         if (time.isRight) true else false
     97       }
     98 
     99       "*" #> ajaxText(vids.mkString(", "), addVideo)
    100   }(test, NodeSeq.Empty)
    101 
    102   def uploadButton(html: NodeSeq) = serve(html) {
    103     user =>
    104       "*" #> button("Upload Media", () => {},
    105         "href" -> "#settingsModal",
    106         "role" -> "button",
    107         "class" -> "btn btn-primary btn-large btn-bold up-separate",
    108         "data-toggle" -> "modal")
    109   }(test, NodeSeq.Empty)
    110 }