Put it all in one dataspace
This commit is contained in:
parent
85648f4956
commit
c71ad44e3d
|
@ -9,80 +9,55 @@ import ./simplex_bot_actor/[message_types, simple_types, websockets]
|
||||||
type
|
type
|
||||||
Value = Preserve[void]
|
Value = Preserve[void]
|
||||||
|
|
||||||
Args {.preservesDictionary.} = object
|
|
||||||
dataspace: Cap
|
|
||||||
url: string
|
|
||||||
|
|
||||||
ContactSubscription {.preservesDictionary.} = object
|
|
||||||
contact: Attributes
|
|
||||||
|
|
||||||
Internal* {.preservesRecord: "internal".} = object
|
|
||||||
dataspace: Cap
|
|
||||||
|
|
||||||
Contact = ref object
|
|
||||||
# TODO: does a Contact get its own facet
|
|
||||||
dataspace: Cap
|
|
||||||
capHandle, summaryHandle, profileHandle, chatItemHandle: Handle
|
|
||||||
|
|
||||||
Group = ref object
|
|
||||||
dataspace: Cap
|
|
||||||
capHandle, infoHandle: Handle
|
|
||||||
|
|
||||||
# ContactAssertion = simple_types.ContactAssertion[Cap]
|
|
||||||
ContactAssertion {.preservesRecord: "contact".} = object
|
ContactAssertion {.preservesRecord: "contact".} = object
|
||||||
id: int
|
id: int
|
||||||
cap: Cap
|
info: Attributes
|
||||||
|
|
||||||
GroupAssertion {.preservesRecord: "group".} = object
|
GroupAssertion {.preservesRecord: "group".} = object
|
||||||
id: int
|
id: int
|
||||||
cap: Cap
|
info: Attributes
|
||||||
|
|
||||||
proc updateAttrs(contact: Contact; turn: var Turn; attrs: Attributes) =
|
ChatItemAssertion {.preservesRecord: "chat-item".} = object
|
||||||
replace(turn, contact.dataspace, contact.summaryHandle, attrs)
|
id: int
|
||||||
var profile = attrs.getOrDefault(Symbol"profile")
|
info: Attributes
|
||||||
if not profile.isFalse:
|
|
||||||
replace(turn, contact.dataspace, contact.profileHandle, profile)
|
|
||||||
|
|
||||||
proc updateAttrs(group: Group; turn: var Turn; info: Attributes) =
|
ContactSubscription {.preservesDictionary.} = object
|
||||||
replace(turn, group.dataspace, group.infoHandle, info)
|
contact: Attributes
|
||||||
|
ChatItemMeta {.preservesDictionary.} = object
|
||||||
|
itemId: int
|
||||||
|
|
||||||
proc `%`(bindings: sink openArray[(string, Pattern)]): Pattern =
|
proc `%`(bindings: sink openArray[(string, Pattern)]): Pattern =
|
||||||
## Sugar for creating dictionary patterns.
|
## Sugar for creating dictionary patterns.
|
||||||
patterns.grabDictionary(bindings)
|
patterns.grabDictionary(bindings)
|
||||||
|
|
||||||
proc bootContact(turn: var Turn; intern: Cap; contactId: int): Contact =
|
proc grabResp(obj: Pattern): Pattern =
|
||||||
let contact = Contact(dataspace: newDataspace(turn))
|
grabRecord("recv", %{ "resp": obj })
|
||||||
block:
|
|
||||||
let pat = grabRecord("recv", %{"resp": %{"contactUpdated": %{
|
|
||||||
"fromContact": %{"contactId": grab(contactId)},
|
|
||||||
"toContact": grab(),
|
|
||||||
}}})
|
|
||||||
onMessage(turn, intern, pat) do (attrs: Attributes):
|
|
||||||
updateAttrs(contact, turn, attrs)
|
|
||||||
|
|
||||||
block:
|
proc bootClient(turn: var Turn; extern, intern: Cap) =
|
||||||
let pat = grabRecord("recv", %{"resp": %{"chatItem": %{
|
|
||||||
"chatInfo": %{"contact": %{"contactId": grab(contactId)}},
|
|
||||||
"chatItem": grab(),
|
|
||||||
}}}) # TODO: could update contact profiles from these messages
|
|
||||||
onMessage(turn, intern, pat) do (attrs: Attributes):
|
|
||||||
var
|
|
||||||
msgId = cast[seq[byte]](base64.decode(attrs[Symbol"meta"]["itemSharedMsgId".toSymbol].string))
|
|
||||||
msg = initRecord("message", Preserve[void](), msgId.toPreserve, attrs[Symbol"content"])
|
|
||||||
debugEcho "publish message ", msg
|
|
||||||
contact.chatItemHandle = publish(turn, contact.dataspace, msg)
|
|
||||||
|
|
||||||
contact
|
var contacts = initTable[int, Handle]()
|
||||||
|
proc updateContact(turn: var Turn; attrs: Attributes) =
|
||||||
|
var ass: ContactAssertion
|
||||||
|
if ass.id.fromPreserve(attrs.getOrDefault(Symbol"contactId")):
|
||||||
|
ass.info = attrs
|
||||||
|
contacts[ass.id] = replace(turn, extern, contacts.getOrDefault(ass.id), ass)
|
||||||
|
|
||||||
proc bootGroup(turn: var Turn; intern: Cap; groupId: int): Group =
|
var groups = newTable[int, Handle]()
|
||||||
let group = Group(dataspace: newDataspace(turn))
|
proc updateGroup(turn: var Turn; attrs: Attributes) =
|
||||||
group
|
var ass: GroupAssertion
|
||||||
|
if ass.id.fromPreserve(attrs.getOrDefault(Symbol"groupId")):
|
||||||
|
ass.info = attrs
|
||||||
|
groups[ass.id] = replace(turn, extern, groups.getOrDefault(ass.id), ass)
|
||||||
|
|
||||||
proc bootClient(turn: var Turn; ds, intern: Cap) =
|
var chatItems = newTable[int, Handle]()
|
||||||
var
|
proc updateChatItem(turn: var Turn; attrs: Attributes) =
|
||||||
contacts = initTable[int, Contact]()
|
var
|
||||||
groups = initTable[int, Group]()
|
ass: ChatItemAssertion
|
||||||
# mapping of contactId to Contact data
|
meta: ChatItemMeta
|
||||||
|
if meta.fromPreserve(attrs.getOrDefault(Symbol"meta")):
|
||||||
|
ass.id = meta.itemId
|
||||||
|
ass.info = attrs
|
||||||
|
chatItems[ass.id] = replace(turn, extern, chatItems.getOrDefault(ass.id), ass)
|
||||||
|
|
||||||
block:
|
block:
|
||||||
let dumpStream = openFileStream("/tmp/simplex_bot_actor.log", fmWrite)
|
let dumpStream = openFileStream("/tmp/simplex_bot_actor.log", fmWrite)
|
||||||
|
@ -92,40 +67,38 @@ proc bootClient(turn: var Turn; ds, intern: Cap) =
|
||||||
write(dumpStream, '\n')
|
write(dumpStream, '\n')
|
||||||
flush(dumpStream)
|
flush(dumpStream)
|
||||||
|
|
||||||
block: # concats
|
block: # contacts
|
||||||
let pat = grabRecord("recv", %{"resp": %{
|
let pat = grabResp(%{
|
||||||
"contactSubscriptions": grab(),
|
"contactSubscriptions": grab(),
|
||||||
"type": grab"contactSubSummary",
|
"type": grab"contactSubSummary",
|
||||||
}})
|
})
|
||||||
|
debugEcho "grab contacts with ", pat
|
||||||
onMessage(turn, intern, pat) do (subs: seq[ContactSubscription]):
|
onMessage(turn, intern, pat) do (subs: seq[ContactSubscription]):
|
||||||
for e in subs:
|
for sub in subs: updateContact(turn, sub.contact)
|
||||||
var id: int
|
|
||||||
if id.fromPreserve(e.contact[Symbol"contactId"]):
|
|
||||||
var contact = contacts.getOrDefault(id)
|
|
||||||
if contact.isNil:
|
|
||||||
contact = bootContact(turn, intern, id)
|
|
||||||
contacts[id] = contact
|
|
||||||
contact.capHandle = publish(turn, ds,
|
|
||||||
ContactAssertion(id: id, cap: contact.dataspace))
|
|
||||||
updateAttrs(contact, turn, e.contact)
|
|
||||||
|
|
||||||
block: # groups
|
block: # groups
|
||||||
let pat = grabRecord("recv", %{"resp": %{"groupInfo": grab()}})
|
let pat = grabResp(%{ "groupInfo": grab() })
|
||||||
onMessage(turn, intern, pat) do (info: Attributes):
|
onMessage(turn, intern, pat) do (groupInfo: Attributes):
|
||||||
var id: int
|
updateGroup(turn, groupInfo)
|
||||||
if id.fromPreserve(info[Symbol"groupId"]):
|
block:
|
||||||
var group = groups.getOrDefault(id)
|
let pat = grabResp(%{ "chatItem": %{ "chatInfo":
|
||||||
if group.isNil:
|
%{ "groupInfo": grab() }}})
|
||||||
group = bootGroup(turn, intern, id)
|
onMessage(turn, intern, pat) do (groupInfo: Attributes):
|
||||||
groups[id] = group
|
updateGroup(turn, groupInfo)
|
||||||
group.capHandle = publish(turn, ds,
|
|
||||||
GroupAssertion(id: id, cap: group.dataspace))
|
|
||||||
updateAttrs(group, turn, info)
|
|
||||||
|
|
||||||
onPublish(turn, ds, ?ContactAssertion) do (contactId: int; cap: Cap):
|
block: # messages
|
||||||
onPublish(turn, cap, %{"localDisplayName": grab()}) do (name: string):
|
let pat = grabResp(%{ "chatItem": %{ "chatInfo": %{ "chatItem": grab() }}})
|
||||||
onPublish(turn, cap, %{"image": ?MIMEData}) do (typ: Symbol, data: seq[byte]):
|
onMessage(turn, intern, pat) do (chatItem: Attributes):
|
||||||
debugEcho "contact ", name, " has an image of ", data.len, " bytes"
|
updateChatItem(turn, chatItem)
|
||||||
|
|
||||||
|
onPublish(turn, extern, ContactAssertion ? {0: grab()}) do (contactId: int):
|
||||||
|
onPublish(turn, extern, ContactAssertion ? {
|
||||||
|
0: grab(contactId), 1: %{ "localDisplayName": grab() }}) do (name: string):
|
||||||
|
debugEcho "contact ", contactId, " is ", name
|
||||||
|
|
||||||
|
type Args {.preservesDictionary.} = object
|
||||||
|
dataspace: Cap
|
||||||
|
url: string
|
||||||
|
|
||||||
runActor("eris_actor") do (root: Cap; turn: var Turn):
|
runActor("eris_actor") do (root: Cap; turn: var Turn):
|
||||||
# connectStdio(root, turn)
|
# connectStdio(root, turn)
|
||||||
|
|
|
@ -50,6 +50,7 @@ proc spawnWebsocketJsonActor*(turn: var Turn; ds: Cap): Actor {.discardable.} =
|
||||||
of Pong, Cont:
|
of Pong, Cont:
|
||||||
discard
|
discard
|
||||||
of Close:
|
of Close:
|
||||||
|
stderr.writeLine "closed connection with ", url
|
||||||
retract(turn, handle)
|
retract(turn, handle)
|
||||||
stop(turn)
|
stop(turn)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue