Leave chats

This commit is contained in:
Richard Ramos 2020-05-29 10:24:58 -04:00 committed by Iuri Matias
parent 98d2c1df68
commit 02c9f1cc6a
2 changed files with 33 additions and 18 deletions

View File

@ -5,6 +5,7 @@ import chronicles
import ../signals/types
import chat/chat_item
import chat/chat_message
import tables
export chat_item
export chat_message
@ -20,11 +21,13 @@ type
ChatModel* = ref object
events*: EventEmitter
channels*: HashSet[string]
filters*: Table[string, string]
proc newChatModel*(): ChatModel =
result = ChatModel()
result.events = createEventEmitter()
result.channels = initHashSet[string]()
result.filters = initTable[string, string]()
proc delete*(self: ChatModel) =
discard
@ -57,6 +60,8 @@ proc join*(self: ChatModel, chatId: string, isNewChat: bool = true) =
if (($topicObj["chatId"]).strip(chars = {'"'}) == chatId):
topics.add(($topicObj["topic"]).strip(chars = {'"'}))
if(not self.filters.hasKey(chatId)): self.filters[chatId] = topicObj["filterId"].getStr
if (topics.len == 0):
warn "No topic found for the chat. Cannot load past messages"
else:
@ -69,9 +74,12 @@ proc load*(self: ChatModel) =
self.events.emit("chatsLoaded", ChatArgs(chats: chatList))
proc leave*(self: ChatModel, chatId: string) =
let oneToOne = isOneToOneChat(chatId)
discard status_chat.removeFilters(chatId = chatId, oneToOne = oneToOne)
# TODO: other calls (if any)
status_chat.removeFilters(chatId, self.filters[chatId])
status_chat.inactivateChat(chatId)
# TODO: REMOVE MAILSERVER TOPIC
# TODO: REMOVE HISTORY
self.filters.del(chatId)
self.channels.excl(chatId)
proc sendMessage*(self: ChatModel, chatId: string, msg: string): string =

View File

@ -23,30 +23,22 @@ proc loadFilters*(chatId: string, filterId: string = "", symKeyId: string = "",
}]
])
proc removeFilters*(chatId: string, filterId: string = "", symKeyId: string = "", oneToOne: bool = false, identity: string = "", topic: string = "", discovery: bool = false, negotiated: bool = false, listen: bool = true): string =
result = callPrivateRPC("removeFilters".prefix, %* [
proc removeFilters*(chatId: string, filterId: string) =
discard callPrivateRPC("removeFilters".prefix, %* [
[{
"ChatID": chatId, # identifier of the chat
"FilterID": filterId, # whisper filter id generated
"SymKeyID": symKeyId, # symmetric key id used for symmetric filters
"OneToOne": oneToOne, # if asymmetric encryption is used for this chat
"Identity": identity, # public key of the other recipient for non-public filters.
# FIXME: passing empty string to the topic makes it error
# "Topic": topic, # whisper topic
"Discovery": discovery,
"Negotiated": negotiated,
"Listen": listen # whether we are actually listening for messages on this chat, or the filter is only created in order to be able to post on the topic
"ChatID": chatId,
"FilterID": filterId
}]
])
proc saveChat*(chatId: string, oneToOne = false) =
proc saveChat*(chatId: string, oneToOne: bool = false, active: bool = true) =
discard callPrivateRPC("saveChat".prefix, %* [
{
"lastClockValue": 0, # TODO:
"color": "#51d0f0", # TODO:
"name": chatId,
"lastMessage": nil, # TODO:
"active": true, # TODO:
"active": active,
"id": chatId,
"unviewedMessagesCount": 0, # TODO:
# TODO use constants for those too or use the Date
@ -55,12 +47,27 @@ proc saveChat*(chatId: string, oneToOne = false) =
}
])
proc inactivateChat*(chatId: string) =
discard callPrivateRPC("saveChat".prefix, %* [
{
"lastClockValue": 0,
"color": "",
"name": chatId,
"lastMessage": nil,
"active": false,
"id": chatId,
"unviewedMessagesCount": 0,
"timestamp": 0
}
])
proc loadChats*(): seq[Chat] =
result = @[]
let jsonResponse = parseJson($callPrivateRPC("chats".prefix))
if jsonResponse["result"].kind != JNull:
for jsonChat in jsonResponse{"result"}:
result.add(jsonChat.toChat)
let chat = jsonChat.toChat
if chat.active: result.add(jsonChat.toChat)
proc chatMessages*(chatId: string) =
discard callPrivateRPC("chatMessages".prefix, %* [chatId, nil, 20])