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 ../signals/types
import chat/chat_item import chat/chat_item
import chat/chat_message import chat/chat_message
import tables
export chat_item export chat_item
export chat_message export chat_message
@ -20,11 +21,13 @@ type
ChatModel* = ref object ChatModel* = ref object
events*: EventEmitter events*: EventEmitter
channels*: HashSet[string] channels*: HashSet[string]
filters*: Table[string, string]
proc newChatModel*(): ChatModel = proc newChatModel*(): ChatModel =
result = ChatModel() result = ChatModel()
result.events = createEventEmitter() result.events = createEventEmitter()
result.channels = initHashSet[string]() result.channels = initHashSet[string]()
result.filters = initTable[string, string]()
proc delete*(self: ChatModel) = proc delete*(self: ChatModel) =
discard discard
@ -57,6 +60,8 @@ proc join*(self: ChatModel, chatId: string, isNewChat: bool = true) =
if (($topicObj["chatId"]).strip(chars = {'"'}) == chatId): if (($topicObj["chatId"]).strip(chars = {'"'}) == chatId):
topics.add(($topicObj["topic"]).strip(chars = {'"'})) topics.add(($topicObj["topic"]).strip(chars = {'"'}))
if(not self.filters.hasKey(chatId)): self.filters[chatId] = topicObj["filterId"].getStr
if (topics.len == 0): if (topics.len == 0):
warn "No topic found for the chat. Cannot load past messages" warn "No topic found for the chat. Cannot load past messages"
else: else:
@ -69,9 +74,12 @@ proc load*(self: ChatModel) =
self.events.emit("chatsLoaded", ChatArgs(chats: chatList)) self.events.emit("chatsLoaded", ChatArgs(chats: chatList))
proc leave*(self: ChatModel, chatId: string) = proc leave*(self: ChatModel, chatId: string) =
let oneToOne = isOneToOneChat(chatId) status_chat.removeFilters(chatId, self.filters[chatId])
discard status_chat.removeFilters(chatId = chatId, oneToOne = oneToOne) status_chat.inactivateChat(chatId)
# TODO: other calls (if any) # TODO: REMOVE MAILSERVER TOPIC
# TODO: REMOVE HISTORY
self.filters.del(chatId)
self.channels.excl(chatId) self.channels.excl(chatId)
proc sendMessage*(self: ChatModel, chatId: string, msg: string): string = 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 = proc removeFilters*(chatId: string, filterId: string) =
result = callPrivateRPC("removeFilters".prefix, %* [ discard callPrivateRPC("removeFilters".prefix, %* [
[{ [{
"ChatID": chatId, # identifier of the chat "ChatID": chatId,
"FilterID": filterId, # whisper filter id generated "FilterID": filterId
"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
}] }]
]) ])
proc saveChat*(chatId: string, oneToOne = false) = proc saveChat*(chatId: string, oneToOne: bool = false, active: bool = true) =
discard callPrivateRPC("saveChat".prefix, %* [ discard callPrivateRPC("saveChat".prefix, %* [
{ {
"lastClockValue": 0, # TODO: "lastClockValue": 0, # TODO:
"color": "#51d0f0", # TODO: "color": "#51d0f0", # TODO:
"name": chatId, "name": chatId,
"lastMessage": nil, # TODO: "lastMessage": nil, # TODO:
"active": true, # TODO: "active": active,
"id": chatId, "id": chatId,
"unviewedMessagesCount": 0, # TODO: "unviewedMessagesCount": 0, # TODO:
# TODO use constants for those too or use the Date # 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] = proc loadChats*(): seq[Chat] =
result = @[] result = @[]
let jsonResponse = parseJson($callPrivateRPC("chats".prefix)) let jsonResponse = parseJson($callPrivateRPC("chats".prefix))
if jsonResponse["result"].kind != JNull: if jsonResponse["result"].kind != JNull:
for jsonChat in jsonResponse{"result"}: for jsonChat in jsonResponse{"result"}:
result.add(jsonChat.toChat) let chat = jsonChat.toChat
if chat.active: result.add(jsonChat.toChat)
proc chatMessages*(chatId: string) = proc chatMessages*(chatId: string) =
discard callPrivateRPC("chatMessages".prefix, %* [chatId, nil, 20]) discard callPrivateRPC("chatMessages".prefix, %* [chatId, nil, 20])