Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Node role info #63

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/main/scala/ApiImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class ApiImpl(dsl: GuardDsl, db: Db)(implicit ec: ExecutionContext) extends Api[
}
changes.addNodes.forall {
case node:Node.Content => allPostsWithAuthor.contains(node.id)
case node:Node.Info => allPostsWithAuthor.contains(node.id)
case _ => false
}
}
Expand Down
10 changes: 5 additions & 5 deletions graph/src/main/scala/Edge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sealed trait Edge {
}

object Edge {
sealed trait Content extends Edge

case class Member(userId: UserId, data: EdgeData.Member, channelId: NodeId) extends Edge {
def sourceId = userId
Expand All @@ -25,7 +26,7 @@ object Edge {
def targetId = nodeId
}

case class Parent(childId: NodeId, data: EdgeData.Parent, parentId: NodeId) extends Edge {
case class Parent(childId: NodeId, data: EdgeData.Parent, parentId: NodeId) extends Content {
def sourceId = childId
def targetId = parentId
}
Expand All @@ -40,22 +41,21 @@ object Edge {
def data = EdgeData.Expanded
}

case class Notify(nodeId: NodeId, userId: UserId)
extends Edge {
case class Notify(nodeId: NodeId, userId: UserId) extends Edge {
def sourceId = nodeId
def targetId = userId
def data = EdgeData.Notify
}

case class Label(sourceId: NodeId, data: EdgeData.Label, targetId: NodeId) extends Edge
case class Label(sourceId: NodeId, data: EdgeData.Label, targetId: NodeId) extends Content

case class Pinned(userId: UserId, nodeId: NodeId) extends Edge {
def sourceId = userId
def targetId = nodeId
def data = EdgeData.Pinned
}

case class Before(nodeId: NodeId, data: EdgeData.Before, afterId: NodeId) extends Edge {
case class Before(nodeId: NodeId, data: EdgeData.Before, afterId: NodeId) extends Content {
def sourceId = nodeId
def targetId = afterId
}
Expand Down
2 changes: 2 additions & 0 deletions graph/src/main/scala/GraphChanges.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import wust.util.collection.RichCollection

import scala.collection.breakOut

//TODO: only Edge.Content and Node.Content?
//TODO: not sets! what about an addnode with a succeeding edit, who wins?
case class GraphChanges(
addNodes: collection.Set[Node] = Set.empty,
addEdges: collection.Set[Edge] = Set.empty,
Expand Down
18 changes: 16 additions & 2 deletions graph/src/main/scala/Node.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,25 @@ sealed trait Node {
}

object Node {
// TODO: we cannot set the nodemeta here, but there is changeable data in the db class
//TODO: noderole/meta is not changeable for users, but is in db.
case class User(id: UserId, data: NodeData.User, meta: NodeMeta) extends Node {
@inline def name: String = data.name
def role: NodeRole = NodeRole.default
def role: NodeRole = NodeRole.Message
}

//TODO: noderole/meta is not changeable for infos, but is in db.
case class Info(id: NodeId, data: NodeData.Info, meta: NodeMeta) extends Node {
def role: NodeRole = NodeRole.Message
}
object Info {
@inline def apply(data: NodeData.Info): Info = {
new Info(NodeId.fresh, data, NodeMeta.default)
}
@inline def apply(id: NodeId, data: NodeData.Info): Info = {
new Info(id, data, NodeMeta.default)
}
}

case class Content(id: NodeId, data: NodeData.Content, role: NodeRole, meta: NodeMeta) extends Node
object Content {
@inline def apply(data: NodeData.Content, role: NodeRole, meta: NodeMeta): Content = {
Expand Down
10 changes: 10 additions & 0 deletions ids/src/main/scala/NodeData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ object NodeData {
}
object PlainText extends Named

sealed trait Info extends NodeData { def str = "Info" }
object Info {
case class EditNode(id: NodeId, oldData: NodeData, data: NodeData) extends Named with Info
object EditNode extends Named
case class AddParent(id: NodeId, parentId: NodeId) extends Named with Info
object AddParent extends Named
case class RemoveParent(id: NodeId, parentId: NodeId) extends Named with Info
object RemoveParent extends Named
}

object User extends Named
case class User(name: String, isImplicit: Boolean, revision: Int) extends Named with NodeData {
def str = name
Expand Down
2 changes: 2 additions & 0 deletions ids/src/main/scala/serialize/Circe.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ trait Circe {
implicit val postContentEncoder2: Encoder[NodeData.Content] = deriveEncoder[NodeData.Content]
implicit val postContentDecoder3: Decoder[NodeData.User] = deriveDecoder[NodeData.User]
implicit val postContentEncoder3: Encoder[NodeData.User] = deriveEncoder[NodeData.User]
implicit val postContentDecoder4: Decoder[NodeData.Info] = deriveDecoder[NodeData.Info]
implicit val postContentEncoder4: Encoder[NodeData.Info] = deriveEncoder[NodeData.Info]
implicit val postContentDecoder: Decoder[NodeData] = deriveDecoder[NodeData]
implicit val postContentEncoder: Encoder[NodeData] = deriveEncoder[NodeData]

Expand Down
42 changes: 42 additions & 0 deletions sdk/shared/src/main/scala/EventInterpreter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package wust.sdk

import akka.io.UdpConnected.Disconnect
import wust.api.ApiEvent
import wust.graph.{Edge, Graph, GraphChanges, Node}
import wust.ids.{NodeData, NodeId}

import scala.collection.mutable

object EventInterpreter {
import NodeData.Info._

def apply(graph: Graph, changes: GraphChanges): Array[NodeData.Info] = {
val c = changes.consistent
val builder = Array.newBuilder[NodeData.Info]
val newNodesBuilder = mutable.HashSet.newBuilder[NodeId]
c.addNodes.foreach {
case n: Node.Content =>
val oldNode = graph.nodesByIdGet(n.id)
oldNode match {
case None => newNodesBuilder += n.id
case Some(oldNode) => builder += EditNode(n.id, oldNode.data, n.data)
}
case _ =>
}

val newNodes = newNodesBuilder.result()

c.addEdges.foreach {
case e: Edge.Parent =>
if (!newNodes(e.childId)) builder += AddParent(e.childId, e.parentId)
case _ =>
}
c.delEdges.foreach {
case e: Edge.Parent =>
builder += RemoveParent(e.childId, e.parentId)
case _ =>
}

builder.result()
}
}
19 changes: 17 additions & 2 deletions sdk/shared/src/main/scala/EventProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import monix.reactive.{Observable, OverflowStrategy}
import monix.reactive.subjects.{PublishSubject, PublishToOneSubject}
import wust.api.ApiEvent._
import wust.api._
import wust.ids.NodeId
import wust.ids.{NodeId, NodeRole, NodeData}
import wust.graph._

import scala.concurrent.Future
Expand Down Expand Up @@ -96,7 +96,22 @@ class EventProcessor private (
val rawGraphWithInit = sharedRawGraph.startWith(Seq(Graph.empty))

val enrichedChanges = enriched.changes.withLatestFrom(rawGraphWithInit)(enrichChanges)
val allChanges = Observable(enrichedChanges, changes).merge
val allChanges = Observable(enrichedChanges, changes).merge.withLatestFrom(rawGraphWithInit) { (changes, graph) =>
val events = EventInterpreter(graph, changes)
val statusChanges = events.map {
case e: NodeData.Info.EditNode =>
val statusNode = Node.Info(e)
graph.parents(e.id).map(GraphChanges.addNodeWithParent(statusNode, _)).foldLeft(GraphChanges.empty)(_ merge _)
case e: NodeData.Info.AddParent =>
val statusNode = Node.Info(e)
GraphChanges.addNodeWithParent(statusNode, e.parentId)
case e: NodeData.Info.RemoveParent =>
val statusNode = Node.Info(e)
GraphChanges.addNodeWithParent(statusNode, e.parentId)
}

statusChanges.foldLeft(changes)(_ merge _)
}

val localChanges = allChanges.withLatestFrom(currentUser.startWith(Seq(initialAuth.user)))((g, u) => (g, u)).collect {
case (changes, user) if changes.nonEmpty => changes.consistent.withAuthor(user.id)
Expand Down
1 change: 1 addition & 0 deletions webApp/src/main/scala/views/Components.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ object Components {
case NodeData.Markdown(content) => markdownVNode(trimToMaxLength(content, maxLength))
case NodeData.PlainText(content) => div(trimToMaxLength(content, maxLength))
case user: NodeData.User => div(displayUserName(user))
case e: NodeData.Info => div(e.toString)
}

private val woostPathCurve = "m51.843 221.96c81.204 0-6.6913-63.86 18.402 13.37 25.093 77.23 58.666-26.098-7.029 21.633-65.695 47.73 42.949 47.73-22.746 0-65.695-47.731-32.122 55.597-7.029-21.633 25.093-77.23-62.802-13.37 18.402-13.37z"
Expand Down