Make message handling more synchronous and fill history on portal create

This commit is contained in:
Tulir Asokan
2019-05-21 23:44:14 +03:00
parent adc7257490
commit 6f2a51410f
14 changed files with 198 additions and 64 deletions

View File

@ -142,6 +142,11 @@ func (ext *ExtendedConn) handleMessageChatUpdate(message []byte) {
if !ok {
continue
}
go chatUpdateHandler.HandleChatUpdate(event)
if ext.shouldCallSynchronously(chatUpdateHandler) {
chatUpdateHandler.HandleChatUpdate(event)
} else {
go chatUpdateHandler.HandleChatUpdate(event)
}
}
}

View File

@ -59,6 +59,11 @@ func (ext *ExtendedConn) handleMessageCommand(message []byte) {
if !ok {
continue
}
go commandHandler.HandleCommand(event)
if ext.shouldCallSynchronously(commandHandler) {
commandHandler.HandleCommand(event)
} else {
go commandHandler.HandleCommand(event)
}
}
}

View File

@ -55,6 +55,11 @@ func (ext *ExtendedConn) handleMessageConn(message []byte) {
if !ok {
continue
}
connInfoHandler.HandleConnInfo(event)
if ext.shouldCallSynchronously(connInfoHandler) {
connInfoHandler.HandleConnInfo(event)
} else {
go connInfoHandler.HandleConnInfo(event)
}
}
}

View File

@ -96,7 +96,12 @@ func (ext *ExtendedConn) HandleJsonMessage(message string) {
if !ok {
continue
}
ujmHandler.HandleUnhandledJSONMessage(message)
if ext.shouldCallSynchronously(ujmHandler) {
ujmHandler.HandleUnhandledJSONMessage(message)
} else {
go ujmHandler.HandleUnhandledJSONMessage(message)
}
}
}
}

View File

@ -85,6 +85,11 @@ func (ext *ExtendedConn) handleMessageMsgInfo(msgType JSONMessageType, message [
if !ok {
continue
}
go msgInfoHandler.HandleMsgInfo(event)
if ext.shouldCallSynchronously(msgInfoHandler) {
msgInfoHandler.HandleMsgInfo(event)
} else {
go msgInfoHandler.HandleMsgInfo(event)
}
}
}

View File

@ -62,6 +62,11 @@ func (ext *ExtendedConn) handleMessagePresence(message []byte) {
if !ok {
continue
}
go presenceHandler.HandlePresence(event)
if ext.shouldCallSynchronously(presenceHandler) {
presenceHandler.HandlePresence(event)
} else {
go presenceHandler.HandlePresence(event)
}
}
}

View File

@ -63,6 +63,11 @@ func (ext *ExtendedConn) handleMessageProps(message []byte) {
if !ok {
continue
}
go protocolPropsHandler.HandleProtocolProps(event)
if ext.shouldCallSynchronously(protocolPropsHandler) {
protocolPropsHandler.HandleProtocolProps(event)
} else {
go protocolPropsHandler.HandleProtocolProps(event)
}
}
}

View File

@ -35,7 +35,7 @@ type MessageRevocation struct {
func (ext *ExtendedConn) HandleRawMessage(message *proto.WebMessageInfo) {
protoMsg := message.GetMessage().GetProtocolMessage()
if protoMsg.GetType() == proto.ProtocolMessage_REVOKE {
if protoMsg != nil && protoMsg.GetType() == proto.ProtocolMessage_REVOKE {
key := protoMsg.GetKey()
deletedMessage := MessageRevocation{
Id: key.GetId(),
@ -48,7 +48,12 @@ func (ext *ExtendedConn) HandleRawMessage(message *proto.WebMessageInfo) {
if !ok {
continue
}
mrHandler.HandleMessageRevoke(deletedMessage)
if ext.shouldCallSynchronously(mrHandler) {
mrHandler.HandleMessageRevoke(deletedMessage)
} else {
go mrHandler.HandleMessageRevoke(deletedMessage)
}
}
}
}

View File

@ -58,6 +58,11 @@ func (ext *ExtendedConn) handleMessageStream(message []json.RawMessage) {
if !ok {
continue
}
go streamHandler.HandleStreamEvent(event)
if ext.shouldCallSynchronously(streamHandler) {
streamHandler.HandleStreamEvent(event)
} else {
go streamHandler.HandleStreamEvent(event)
}
}
}

View File

@ -46,6 +46,15 @@ func ExtendConn(conn *whatsapp.Conn) *ExtendedConn {
return ext
}
func (ext *ExtendedConn) shouldCallSynchronously(handler whatsapp.Handler) bool {
sh, ok := handler.(whatsapp.SyncHandler)
return ok && sh.ShouldCallSynchronously()
}
func (ext *ExtendedConn) ShouldCallSynchronously() bool {
return true
}
type GroupInfo struct {
JID string `json:"jid"`
OwnerJID string `json:"owner"`