Guest User

Untitled

a guest
Jun 2nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 4.55 KB | None | 0 0
  1. package org.gmantra.ixchel.task
  2.  
  3. import akka.actor._
  4. import akka.event.Logging
  5. import akka.http.scaladsl._
  6. import akka.http.scaladsl.client._
  7. import akka.http.scaladsl.marshallers.argonaut._
  8. import akka.http.scaladsl.model._
  9. import akka.http.scaladsl.model.headers._
  10. import akka.stream._
  11. import akka.stream.scaladsl._
  12. import argonaut._, Argonaut._
  13. import com.typesafe.config.ConfigFactory
  14. import org.slf4j.LoggerFactory
  15. import scala.collection.immutable.Seq
  16. import com.github.ghik.silencer.silent
  17.  
  18. import org.gmantra.ixchel.actor._
  19. import org.gmantra.ixchel.helpers._
  20. import org.gmantra.ixchel.models._
  21. import org.gmantra.ixchel.models._, ix._
  22.  
  23. object FakeIt extends App with IxchelWorker with ArgonautSupport {
  24.  
  25.   implicit val system       = ActorSystem("fakeit")
  26.   implicit val ec           = system.dispatcher
  27.   implicit val materializer = ActorMaterializer()
  28.  
  29.   lazy val logger = LoggerFactory.getLogger(this.getClass)
  30.   val config      = ConfigFactory.load()
  31.  
  32.   /**
  33.     * Parse args and set user id, stream id, and stream item count
  34.     */
  35.   val (userId, streamId, count) = args.toList match {
  36.     case n :: Nil           ⇒ ("hackcave", "fakeit", n.toLong)
  37.     case s :: n :: Nil      ⇒ ("hackcave", s, n.toLong)
  38.     case u :: s :: n :: Nil ⇒ (u, s, n.toLong)
  39.     case _                  ⇒ ("hackcave", "fakeit", 10L)
  40.   }
  41.  
  42.   /**
  43.     * The user to create
  44.     */
  45.   val user = SignUp(
  46.     id = userId,
  47.     name = s"${userId}",
  48.     email = s"${userId}@dev.gmantra.net",
  49.     bio = s"${userId}: created using fakeit",
  50.     properties = jEmptyObject,
  51.     password = Option(userId.reverse)
  52.   )
  53.  
  54.   /**
  55.     * The stream to create
  56.     */
  57.   val stream =
  58.     Stream.Create(
  59.       id = streamId,
  60.       name = s"${streamId}: created using fakeIt",
  61.       properties = Map.empty
  62.     )
  63.  
  64.   /**
  65.     * The source of stream items generated from the fortune source
  66.     */
  67.   @silent
  68.   val itemSource =
  69.     Source
  70.       .actorPublisher[(String, List[String])](FortunePublisher.props)
  71.       .map {
  72.         case (cookie, tags)
  73.           StreamItem.Create(
  74.             source = Map(
  75.               "type"    → "textile",
  76.               "content" → cookie
  77.             ).asJson,
  78.             tags = tags,
  79.             keywords = Map.empty,
  80.             properties = jEmptyObject,
  81.             mentions = Nil,
  82.             actions = Nil,
  83.             published = true
  84.           )
  85.       }
  86.  
  87.   import RequestBuilding._
  88.  
  89.   /**
  90.     * The cookie used to authenticate
  91.     */
  92.   val (_, timestamp, auth) = Authenticate(userId)
  93.   val cookie = Cookie(
  94.     ("id", userId),
  95.     ("timestamp", s"${timestamp}"),
  96.     ("auth", auth)
  97.   )
  98.   val addAuth = addHeader(cookie)
  99.  
  100.   /**
  101.     *  The xsrf token
  102.     */
  103.   val xsrf = "42"
  104.   val addXSRF = addHeaders(
  105.     Cookie("XSRF-TOKEN", xsrf),
  106.     RawHeader("X-XSRF-TOKEN", xsrf)
  107.   )
  108.  
  109.   val userCreateRequest =
  110.     Post(s"/?method=account.create", user) ~>
  111.       addHeader(RawHeader("X-Captcha-Response", config.getString("recaptcha.dummyResponse")))
  112.  
  113.   val streamRequest =
  114.     Post(s"/${userId}", stream) ~> addAuth
  115.  
  116.   val itemRequestSource = itemSource.map { item ⇒
  117.     Post(s"/${userId}/${stream.id}/items", item) ~> addAuth
  118.   }
  119.  
  120.   /**
  121.     * Connect to http://localhost:7771
  122.     */
  123.   val http = Http().outgoingConnection("localhost", 7771)
  124.  
  125.   val createUser = Source.single(userCreateRequest)
  126.  
  127.   val createStream = Source.single(streamRequest)
  128.  
  129.   val createStreamItems = itemRequestSource.take(count)
  130.  
  131.   createUser
  132.   /*    .concat(createStream)
  133.     .concat(createStreamItems)*/
  134.     .map(addXSRF)
  135.     .map(
  136.       logRequest { req ⇒
  137.         logger.info(s"""${req.method.value} ${req.uri} ${req.headers.mkString(" ")}""")
  138.       }
  139.     )
  140.     .via(http)
  141.     .runWith {
  142.       Sink.foreach { resp ⇒
  143.         logger.info(s"""${resp.status.intValue} ${resp.headers.mkString(" ")}""")
  144.       }
  145.     }
  146.     .onComplete {
  147.   implicit val userIdentity = userId
  148.   implicit val driver       = IXDriver(null)
  149.   DO {
  150.     val crud = new UserCRUD {}
  151.         for {
  152.           resp ← runDB { crud.setActive(userIdentity) }
  153.         } yield resp
  154.   }
  155.    _ ⇒ system.terminate()
  156. }
  157.  
  158.   createStream
  159.     .concat(createStreamItems)
  160.     .map(addXSRF)
  161.     .map(
  162.       logRequest { req ⇒
  163.         logger.info(s"""${req.method.value} ${req.uri} ${req.headers.mkString(" ")}""")
  164.       }
  165.     )
  166.     .via(http)
  167.     .runWith {
  168.       Sink.foreach { resp ⇒
  169.         logger.info(s"""${resp.status.intValue} ${resp.headers.mkString(" ")}""")
  170.       }
  171.     }
  172.     .onComplete { _
  173.       system.terminate()
  174.     }
  175.  
  176. }
Add Comment
Please, Sign In to add comment