Skip to content

Commit

Permalink
added fromArchive argument
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin committed Sep 5, 2023
1 parent f62d3f4 commit 7531dbe
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 46 deletions.
13 changes: 8 additions & 5 deletions _assets/generate_handlers/generate_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ type EnumType struct {

// MethodInfo holds information about a method
type MethodInfo struct {
ProtobufName string
MethodName string
EnumValue string
ProcessRaw bool
SyncMessage bool
ProtobufName string
MethodName string
EnumValue string
ProcessRaw bool
SyncMessage bool
FromArchiveArg bool
}

func main() {
Expand Down Expand Up @@ -67,6 +68,8 @@ func main() {
info.ProcessRaw = true
}

info.FromArchiveArg = protobufName == "ChatMessage" || protobufName == "PinMessage"

methodInfos = append(methodInfos, info)
}

Expand Down
8 changes: 4 additions & 4 deletions _assets/generate_handlers/generate_handlers_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
v1protocol "github.com/status-im/status-go/protocol/v1"
)

func (m *Messenger) dispatchToHandler(messageState *ReceivedMessageState, protoBytes []byte, msg *v1protocol.StatusMessage, filter transport.Filter) error {
func (m *Messenger) dispatchToHandler(messageState *ReceivedMessageState, protoBytes []byte, msg *v1protocol.StatusMessage, filter transport.Filter, fromArchive bool) error {
switch msg.Type {
{{ range .}}
case protobuf.ApplicationMetadataMessage_{{.EnumValue}}:
return m.{{.MethodName}}(messageState, protoBytes, msg, filter)
return m.{{.MethodName}}(messageState, protoBytes, msg, filter{{ if .FromArchiveArg }}, fromArchive{{ end }})
{{ end }}
default:
m.logger.Info("protobuf type not found", zap.String("type", string(msg.Type)))
Expand All @@ -30,7 +30,7 @@ func (m *Messenger) dispatchToHandler(messageState *ReceivedMessageState, protoB
}

{{ range . }}
func (m *Messenger) {{.MethodName}}(messageState *ReceivedMessageState, protoBytes []byte, msg *v1protocol.StatusMessage, filter transport.Filter) error {
func (m *Messenger) {{.MethodName}}(messageState *ReceivedMessageState, protoBytes []byte, msg *v1protocol.StatusMessage, filter transport.Filter{{ if .FromArchiveArg }}, fromArchive bool{{ end }}) error {
m.logger.Info("handling {{ .ProtobufName}}")
{{ if .SyncMessage }}
if !common.IsPubKeyEqual(messageState.CurrentMessageState.PublicKey, &m.identity.PublicKey) {
Expand All @@ -50,7 +50,7 @@ func (m *Messenger) {{.MethodName}}(messageState *ReceivedMessageState, protoByt

m.outputToCSV(msg.TransportMessage.Timestamp, msg.ID, messageState.CurrentMessageState.Contact.ID, filter.Topic, filter.ChatID, msg.Type, p)

return m.Handle{{.ProtobufName}}(messageState, p, msg)
return m.Handle{{.ProtobufName}}(messageState, p, msg{{ if .FromArchiveArg }}, fromArchive {{ end }})
{{ end }}
}

Expand Down
21 changes: 8 additions & 13 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3136,7 +3136,7 @@ func (m *Messenger) RetrieveAll() (*MessengerResponse, error) {
return nil, err
}

return m.handleRetrievedMessages(chatWithMessages, true)
return m.handleRetrievedMessages(chatWithMessages, true, false)
}

func (m *Messenger) GetStats() types.StatsSummary {
Expand Down Expand Up @@ -3413,21 +3413,16 @@ func (m *Messenger) handleImportedMessages(messagesToHandle map[transport.Filter
switch msg.Type {

case protobuf.ApplicationMetadataMessage_CHAT_MESSAGE:
logger.Debug("Handling ChatMessage")

protoMessage := &protobuf.ChatMessage{}
err := proto.Unmarshal(msg.UnwrappedPayload, protoMessage)
err = m.handleChatMessageProtobuf(messageState, msg.UnwrappedPayload, msg, filter, true)
if err != nil {
logger.Warn("failed to unmarshal ChatMessage", zap.Error(err))
logger.Warn("failed to handle ChatMessage", zap.Error(err))
continue
}

messageState.CurrentMessageState.Message = protoMessage
m.outputToCSV(msg.TransportMessage.Timestamp, msg.ID, senderID, filter.Topic, filter.ChatID, msg.Type, messageState.CurrentMessageState.Message)
err = m.HandleImportedChatMessage(messageState)
case protobuf.ApplicationMetadataMessage_PIN_MESSAGE:
err = m.handlePinMessageProtobuf(messageState, msg.UnwrappedPayload, msg, filter, true)
if err != nil {
logger.Warn("failed to handle ChatMessage", zap.Error(err))
continue
logger.Warn("failed to handle PinMessage", zap.Error(err))
}
}
}
Expand Down Expand Up @@ -3491,7 +3486,7 @@ func (m *Messenger) handleImportedMessages(messagesToHandle map[transport.Filter
return nil
}

func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filter][]*types.Message, storeWakuMessages bool) (*MessengerResponse, error) {
func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filter][]*types.Message, storeWakuMessages bool, fromArchive bool) (*MessengerResponse, error) {

m.handleMessagesMutex.Lock()
defer m.handleMessagesMutex.Unlock()
Expand Down Expand Up @@ -3596,7 +3591,7 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte

if msg.UnwrappedPayload != nil {

err := m.dispatchToHandler(messageState, msg.UnwrappedPayload, msg, filter)
err := m.dispatchToHandler(messageState, msg.UnwrappedPayload, msg, filter, fromArchive)
if err != nil {
allMessagesProcessed = false
logger.Warn("failed to process protobuf", zap.Error(err))
Expand Down
2 changes: 1 addition & 1 deletion protocol/messenger_contact_requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ func (s *MessengerContactRequestSuite) TestReceiveAcceptAndRetractContactRequest
}

response := state.Response
err = s.m.HandleChatMessage(state, &message, nil)
err = s.m.HandleChatMessage(state, &message, nil, false)
s.Require().NoError(err)
s.Require().Len(response.ActivityCenterNotifications(), 1)
contacts := s.m.Contacts()
Expand Down
30 changes: 14 additions & 16 deletions protocol/messenger_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (m *Messenger) HandleMembershipUpdate(messageState *ReceivedMessageState, c
}

if message.Message != nil {
return m.HandleChatMessage(messageState, message.Message, nil)
return m.HandleChatMessage(messageState, message.Message, nil, false)
} else if message.EmojiReaction != nil {
return m.HandleEmojiReaction(messageState, message.EmojiReaction, nil)
}
Expand Down Expand Up @@ -776,7 +776,7 @@ func (m *Messenger) HandleSyncChatMessagesRead(state *ReceivedMessageState, mess
return nil
}

func (m *Messenger) handlePinMessage(pinner *Contact, whisperTimestamp uint64, response *MessengerResponse, message *protobuf.PinMessage) error {
func (m *Messenger) handlePinMessage(pinner *Contact, whisperTimestamp uint64, response *MessengerResponse, message *protobuf.PinMessage, forceSeen bool) error {
logger := m.logger.With(zap.String("site", "HandlePinMessage"))

logger.Info("Handling pin message")
Expand Down Expand Up @@ -811,9 +811,6 @@ func (m *Messenger) handlePinMessage(pinner *Contact, whisperTimestamp uint64, r
return nil
}

// Set the LocalChatID for the message
pinMessage.LocalChatID = chat.ID

if c, ok := m.allChats.Load(chat.ID); ok {
chat = c
}
Expand All @@ -837,7 +834,7 @@ func (m *Messenger) handlePinMessage(pinner *Contact, whisperTimestamp uint64, r
if err != nil {
return err
}
message := &common.Message{
systemMessage := &common.Message{
ChatMessage: &protobuf.ChatMessage{
Clock: message.Clock,
Timestamp: whisperTimestamp,
Expand All @@ -851,7 +848,12 @@ func (m *Messenger) handlePinMessage(pinner *Contact, whisperTimestamp uint64, r
LocalChatID: chat.ID,
From: pinner.ID,
}
response.AddMessage(message)

if forceSeen {
systemMessage.Seen = true
}

response.AddMessage(systemMessage)
chat.UnviewedMessagesCount++
}

Expand All @@ -867,8 +869,8 @@ func (m *Messenger) handlePinMessage(pinner *Contact, whisperTimestamp uint64, r
return nil
}

func (m *Messenger) HandlePinMessage(state *ReceivedMessageState, message *protobuf.PinMessage, statusMessage *v1protocol.StatusMessage) error {
return m.handlePinMessage(state.CurrentMessageState.Contact, state.CurrentMessageState.WhisperTimestamp, state.Response, message)
func (m *Messenger) HandlePinMessage(state *ReceivedMessageState, message *protobuf.PinMessage, statusMessage *v1protocol.StatusMessage, fromArchive bool) error {
return m.handlePinMessage(state.CurrentMessageState.Contact, state.CurrentMessageState.WhisperTimestamp, state.Response, message, fromArchive)
}

func (m *Messenger) handleAcceptContactRequest(
Expand Down Expand Up @@ -1348,7 +1350,7 @@ func (m *Messenger) handleArchiveMessages(archiveMessages []*protobuf.WakuMessag
return nil, err
}

response, err := m.handleRetrievedMessages(otherMessages, false)
response, err := m.handleRetrievedMessages(otherMessages, false, true)
if err != nil {
m.communitiesManager.LogStdout("failed to write history archive messages to database", zap.Error(err))
return nil, err
Expand Down Expand Up @@ -2262,13 +2264,9 @@ func (m *Messenger) handleChatMessage(state *ReceivedMessageState, forceSeen boo
return nil
}

func (m *Messenger) HandleChatMessage(state *ReceivedMessageState, message *protobuf.ChatMessage, statusMessage *v1protocol.StatusMessage) error {
func (m *Messenger) HandleChatMessage(state *ReceivedMessageState, message *protobuf.ChatMessage, statusMessage *v1protocol.StatusMessage, fromArchive bool) error {
state.CurrentMessageState.Message = message
return m.handleChatMessage(state, false)
}

func (m *Messenger) HandleImportedChatMessage(state *ReceivedMessageState) error {
return m.handleChatMessage(state, true)
return m.handleChatMessage(state, fromArchive)
}

func (m *Messenger) addActivityCenterNotification(response *MessengerResponse, notification *ActivityCenterNotification) error {
Expand Down
14 changes: 7 additions & 7 deletions protocol/messenger_handlers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7531dbe

Please sign in to comment.