refactor(chat): add functions for 1-1 chats (#127)

This commit is contained in:
Jonathan Rainville 2021-12-08 10:15:08 -05:00 committed by saledjenic
parent 32269deb54
commit 2fab5ae9c4
2 changed files with 76 additions and 2 deletions

View File

@ -1,13 +1,66 @@
import json
import json, sequtils, sugar
import core, utils
import response_type
export response_type
proc saveChat*(
chatId: string,
chatType: int,
active: bool = true,
color: string = "#000000",
ensName: string = "",
profile: string = "",
joined: int64 = 0
): RpcResponse[JsonNode] {.raises: [Exception].} =
# TODO: ideally status-go/stimbus should handle some of these fields instead of having the client
# send them: lastMessage, unviewedMEssagesCount, timestamp, lastClockValue, name?
return callPrivateRPC("saveChat".prefix, %* [
{
"lastClockValue": 0, # TODO:
"color": color,
"name": (if ensName != "": ensName else: chatId),
"lastMessage": nil, # TODO:
"active": active,
"profile": profile,
"id": chatId,
"unviewedMessagesCount": 0, # TODO:
"chatType": chatType.int,
"timestamp": 1588940692659, # TODO:
"joined": joined
}
])
proc getChats*(): RpcResponse[JsonNode] {.raises: [Exception].} =
let payload = %* []
result = callPrivateRPC("chats".prefix, payload)
proc createPublicChat*(chatId: string): RpcResponse[JsonNode] {.raises: [Exception].} =
let payload = %* [{"ID": chatId}]
result = callPrivateRPC("createPublicChat".prefix, payload)
result = callPrivateRPC("createPublicChat".prefix, payload)
proc createOneToOneChat*(chatId: string): RpcResponse[JsonNode] {.raises: [Exception].} =
let payload = %* [{"ID": chatId}]
result = callPrivateRPC("createOneToOneChat".prefix, payload)
proc leaveGroupChat*(chatId: string): RpcResponse[JsonNode] {.raises: [Exception].} =
result = callPrivateRPC("leaveGroupChat".prefix, %* [nil, chatId, true])
proc deactivateChat*(chatId: string): RpcResponse[JsonNode] {.raises: [Exception].} =
callPrivateRPC("deactivateChat".prefix, %* [{ "ID": chatId }])
proc clearChatHistory*(chatId: string): RpcResponse[JsonNode] {.raises: [Exception].} =
callPrivateRPC("deleteMessagesByChatID".prefix, %* [chatId])
proc sendImages*(chatId: string, images: var seq[string]): RpcResponse[JsonNode] {.raises: [Exception].} =
let imagesJson = %* images.map(image => %*
{
"chatId": chatId,
"contentType": 7, # TODO how do we unhardcode this
"imagePath": image,
# TODO is this still needed
# "ensName": preferredUsername,
"text": "Update to latest version to see a nice image here!"
}
)
callPrivateRPC("sendChatMessages".prefix, %* [imagesJson])

View File

@ -0,0 +1,21 @@
import json
import core, utils
import response_type
proc acceptRequestAddressForTransaction*(messageId: string, address: string): RpcResponse[JsonNode] {.raises: [Exception].} =
callPrivateRPC("acceptRequestAddressForTransaction".prefix, %* [messageId, address])
proc declineRequestAddressForTransaction*(messageId: string): RpcResponse[JsonNode] {.raises: [Exception].} =
callPrivateRPC("declineRequestAddressForTransaction".prefix, %* [messageId])
proc declineRequestTransaction*(messageId: string): RpcResponse[JsonNode] {.raises: [Exception].} =
callPrivateRPC("declineRequestTransaction".prefix, %* [messageId])
proc requestAddressForTransaction*(chatId: string, fromAddress: string, amount: string, tokenAddress: string): RpcResponse[JsonNode] {.raises: [Exception].} =
callPrivateRPC("requestAddressForTransaction".prefix, %* [chatId, fromAddress, amount, tokenAddress])
proc requestTransaction*(chatId: string, fromAddress: string, amount: string, tokenAddress: string): RpcResponse[JsonNode] {.raises: [Exception].} =
callPrivateRPC("requestTransaction".prefix, %* [chatId, amount, tokenAddress, fromAddress])
proc acceptRequestTransaction*(transactionHash: string, messageId: string, signature: string): RpcResponse[JsonNode] {.raises: [Exception].} =
callPrivateRPC("acceptRequestTransaction".prefix, %* [transactionHash, messageId, signature])