feat: Join private groups

This commit is contained in:
Richard Ramos 2020-06-10 14:23:18 -04:00 committed by Iuri Matias
parent 8cca18d7f9
commit 761be22bfa
10 changed files with 102 additions and 23 deletions

View File

@ -33,12 +33,11 @@ proc handleChatEvents(self: ChatController) =
self.status.events.on("messagesLoaded") do(e:Args):
self.view.pushMessages(MsgsLoadedArgs(e).messages)
self.status.events.on("messageSent") do(e: Args):
var sentMessage = MsgArgs(e)
var chatMessage = sentMessage.payload.toChatMessage()
chatMessage.message = sentMessage.message
chatMessage.isCurrentUser = true
self.view.pushMessage(chatMessage)
self.status.events.on("pushMessage") do(e: Args):
var evArgs = PushMessageArgs(e)
for chat in evArgs.chats:
self.view.updateChat(chat)
self.view.pushMessages(evArgs.messages)
self.status.events.on("channelJoined") do(e: Args):
var channel = ChannelArgs(e)

View File

@ -113,6 +113,9 @@ QtObject:
proc joinChat*(self: ChatsView, channel: string, chatTypeInt: int): int {.slot.} =
self.status.chat.join(channel, ChatType(chatTypeInt))
proc joinGroup*(self: ChatsView) {.slot.} =
self.status.chat.confirmJoiningGroup(self.activeChannel.id)
proc messagesLoaded*(self: ChatsView) {.signal.}
proc loadMoreMessages*(self: ChatsView) {.slot.} =

View File

@ -1,5 +1,6 @@
import NimQml, Tables
import ../../../status/chat
import ../../../signals/types
type
ChatMessageRoles {.pure.} = enum
@ -13,6 +14,8 @@ type
FromAuthor = UserRole + 8
Clock = UserRole + 9
ChatId = UserRole + 10
SectionIdentifier = UserRole + 11
QtObject:
type
ChatMessageList* = ref object of QAbstractListModel
@ -29,7 +32,7 @@ QtObject:
proc chatIdentifier(self: ChatMessageList, chatId:string): ChatMessage =
result = newChatMessage();
result.contentType = -1;
result.contentType = ContentType.ChatIdentifier;
result.chatId = chatId
@ -41,6 +44,13 @@ QtObject:
method rowCount(self: ChatMessageList, index: QModelIndex = nil): int =
return self.messages.len
proc sectionIdentifier(message: ChatMessage): string =
result = message.fromAuthor
# Force section change, because group status messages are sent with the
# same fromAuthor, and ends up causing the header to not be shown
if message.contentType == ContentType.Group:
result = "GroupChatMessage"
method data(self: ChatMessageList, index: QModelIndex, role: int): QVariant =
if not index.isValid:
return
@ -55,10 +65,12 @@ QtObject:
of ChatMessageRoles.Clock: result = newQVariant($message.clock)
of ChatMessageRoles.Identicon: result = newQVariant(message.identicon)
of ChatMessageRoles.IsCurrentUser: result = newQVariant(message.isCurrentUser)
of ChatMessageRoles.ContentType: result = newQVariant(message.contentType)
of ChatMessageRoles.ContentType: result = newQVariant(message.contentType.int)
of ChatMessageRoles.Sticker: result = newQVariant(message.sticker)
of ChatMessageRoles.FromAuthor: result = newQVariant(message.fromAuthor)
of ChatMessageRoles.ChatId: result = newQVariant(message.chatId)
of ChatMessageRoles.SectionIdentifier: result = newQVariant(sectionIdentifier(message))
method roleNames(self: ChatMessageList): Table[int, string] =
{
@ -71,7 +83,8 @@ QtObject:
ChatMessageRoles.ContentType.int:"contentType",
ChatMessageRoles.Sticker.int:"sticker",
ChatMessageRoles.FromAuthor.int:"fromAuthor",
ChatMessageRoles.ChatId.int:"chatId"
ChatMessageRoles.ChatId.int:"chatId",
ChatMessageRoles.SectionIdentifier.int: "sectionIdentifier"
}.toTable
proc add*(self: ChatMessageList, message: ChatMessage) =

View File

@ -85,7 +85,7 @@ proc toMessage*(jsonMsg: JsonNode): Message =
alias: jsonMsg{"alias"}.getStr,
chatId: jsonMsg{"localChatId"}.getStr,
clock: jsonMsg{"clock"}.getInt,
contentType: jsonMsg{"contentType"}.getInt,
contentType: ContentType(jsonMsg{"contentType"}.getInt),
ensName: jsonMsg{"ensName"}.getStr,
fromAuthor: jsonMsg{"from"}.getStr,
id: jsonMsg{"identicon"}.getStr,
@ -104,7 +104,7 @@ proc toMessage*(jsonMsg: JsonNode): Message =
stickerHash: ""
)
if result.contentType == 2:
if result.contentType == ContentType.Sticker:
result.stickerHash = jsonMsg["sticker"]["hash"].getStr

View File

@ -17,12 +17,22 @@ type NodeSignal* = ref object of Signal
type WalletSignal* = ref object of Signal
content*: string
type ContentType* {.pure.} = enum
ChatIdentifier = -1,
Unknown = 0,
Message = 1,
Sticker = 2,
Status = 3,
Emoji = 4,
Transaction = 5,
Group = 6
type Message* = object
alias*: string
chatId*: string
clock*: int
# commandParameters*: # ???
contentType*: int # ???
contentType*: ContentType # ???
ensName*: string # ???
fromAuthor*: string
id*: string
@ -47,7 +57,7 @@ type Message* = object
method onSignal*(self: SignalSubscriber, data: Signal) {.base.} =
error "onSignal must be overriden in controller. Signal is unhandled"
type ChatType* = enum
type ChatType* {.pure.}= enum
Unknown = 0,
OneToOne = 1,
Public = 2,
@ -102,3 +112,8 @@ proc findIndexById*(self: seq[Chat], id: string): int =
result = idx
break
proc isMember*(self: Chat, pubKey: string): bool =
for member in self.members:
if member.id == pubKey and member.joined: return true
return false

View File

@ -11,10 +11,9 @@ export chat_message
export Chat
type
MsgArgs* = ref object of Args
message*: string
chatId*: string
payload*: JsonNode
PushMessageArgs* = ref object of Args
chats*: seq[Chat]
messages*: seq[Message]
ChatIdArg* = ref object of Args
chatId*: string
@ -117,10 +116,21 @@ proc leave*(self: ChatModel, chatId: string) =
proc setActiveChannel*(self: ChatModel, chatId: string) =
self.events.emit("activeChannelChanged", ChatIdArg(chatId: chatId))
proc formatChatUpdate(response: JsonNode): (seq[Chat], seq[Message]) =
var chats: seq[Chat] = @[]
var messages: seq[Message] = @[]
if response["result"]{"chats"} != nil:
for jsonMsg in response["result"]["messages"]:
messages.add(jsonMsg.toMessage)
if response["result"]{"chats"} != nil:
for jsonChat in response["result"]["chats"]:
chats.add(jsonChat.toChat)
result = (chats, messages)
proc sendMessage*(self: ChatModel, chatId: string, msg: string): string =
var sentMessage = status_chat.sendChatMessage(chatId, msg)
var parsedMessage = parseJson(sentMessage)["result"]["chats"][0]["lastMessage"]
self.events.emit("messageSent", MsgArgs(message: msg, chatId: chatId, payload: parsedMessage))
var (chats, messages) = formatChatUpdate(parseJson(sentMessage))
self.events.emit("pushMessage", PushMessageArgs(messages: messages, chats: chats))
sentMessage
proc chatMessages*(self: ChatModel, chatId: string, initialLoad:bool = true) =
@ -138,3 +148,8 @@ proc chatMessages*(self: ChatModel, chatId: string, initialLoad:bool = true) =
proc markAllChannelMessagesRead*(self: ChatModel, chatId: string): JsonNode =
var response = status_chat.markAllRead(chatId)
result = parseJson(response)
proc confirmJoiningGroup*(self: ChatModel, chatId: string) =
var response = parseJson(status_chat.confirmJoiningGroup(chatId))
var (chats, messages) = formatChatUpdate(response)
self.events.emit("pushMessage", PushMessageArgs(messages: messages, chats: chats))

View File

@ -10,7 +10,7 @@ type ChatMessage* = ref object
clock*: int
identicon*: string
isCurrentUser*: bool
contentType*: int
contentType*: ContentType
sticker*: string
chatId*: string
@ -26,7 +26,7 @@ proc newChatMessage*(): ChatMessage =
result.timestamp = "0"
result.identicon = ""
result.isCurrentUser = false
result.contentType = 1
result.contentType = ContentType.Unknown
result.sticker = ""
result.chatId = ""
@ -40,7 +40,7 @@ proc toChatMessage*(payload: JsonNode): ChatMessage =
clock: payload["clock"].getInt,
identicon: payload["identicon"].str,
isCurrentUser: payload{"outgoingStatus"}.getStr == "sending",
contentType: payload["contentType"].getInt,
contentType: ContentType(payload["contentType"].getInt),
sticker: "" # TODO: implement when implementing stickers from user
)

View File

@ -108,3 +108,6 @@ proc sendChatMessage*(chatId: string, msg: string): string =
proc markAllRead*(chatId: string): string =
callPrivateRPC("markAllRead".prefix, %* [chatId])
proc confirmJoiningGroup*(chatId: string): string =
callPrivateRPC("confirmJoiningGroup".prefix, %* [chatId])

View File

@ -60,7 +60,7 @@ ScrollView {
model: messageListDelegate
section.property: "fromAuthor"
section.property: "sectionIdentifier"
section.criteria: ViewSection.FullString
}

View File

@ -83,6 +83,7 @@ Item {
}
Text {
id: channelName
wrapMode: Text.Wrap
text: {
if (chatsModel.activeChannel.chatType != Constants.chatTypePublic) {
@ -98,6 +99,29 @@ Item {
anchors.topMargin: 16
anchors.horizontalCenter: parent.horizontalCenter
}
Item {
visible: chatsModel.activeChannel.chatType == Constants.chatTypePrivateGroupChat // TODO: hide if the user is a member of the group (use chatsModel.activeChannel.isMember?)
anchors.top: channelName.bottom
anchors.topMargin: 16
id: joinOrDecline
Text {
text: qsTr("Join chat")
font.pixelSize: 20
color: Theme.blue
anchors.horizontalCenter: parent.horizontalCenter
MouseArea {
cursorShape: Qt.PointingHandCursor
anchors.fill: parent
onClicked: {
chatsModel.joinGroup()
}
}
}
}
}
@ -149,6 +173,13 @@ Item {
wrapMode: Text.WordWrap
selectByMouse: true
visible: isMessage && authorCurrentMsg != authorPrevMsg && !isCurrentUser
MouseArea {
cursorShape: Qt.PointingHandCursor
anchors.fill: parent
onClicked: {
profilePopup.open()
}
}
}
Rectangle {