Skip to content

Commit

Permalink
profile status updates
Browse files Browse the repository at this point in the history
  • Loading branch information
flexsurfer committed Oct 27, 2020
1 parent 7467ca7 commit b388002
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 39 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.62.13
0.62.14
20 changes: 20 additions & 0 deletions protocol/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
ChatTypeOneToOne ChatType = iota + 1
ChatTypePublic
ChatTypePrivateGroupChat
ChatTypeProfile
)

type Chat struct {
Expand Down Expand Up @@ -71,6 +72,9 @@ type Chat struct {

// Public key of administrator who created invitation link
InvitationAdmin string `json:"invitationAdmin,omitempty"`

// Public key of user profile
Profile string `json:"profile,omitempty"`
}

func (c *Chat) PublicKey() (*ecdsa.PublicKey, error) {
Expand All @@ -91,6 +95,10 @@ func (c *Chat) Public() bool {
return c.ChatType == ChatTypePublic
}

func (c *Chat) ProfileUpdates() bool {
return c.ChatType == ChatTypeProfile
}

func (c *Chat) OneToOne() bool {
return c.ChatType == ChatTypeOneToOne
}
Expand Down Expand Up @@ -267,6 +275,18 @@ func CreatePublicChat(name string, timesource TimeSource) Chat {
}
}

func CreateProfileChat(name string, profile string, timesource TimeSource) Chat {
return Chat{
ID: name,
Name: name,
Active: true,
Timestamp: int64(timesource.GetCurrentTime()),
Color: chatColors[rand.Intn(len(chatColors))],
ChatType: ChatTypeProfile,
Profile: profile,
}
}

func CreateGroupChat(timesource TimeSource) Chat {
return Chat{
Active: true,
Expand Down
5 changes: 5 additions & 0 deletions protocol/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ func (m *MessageHandler) HandleChatMessage(state *ReceivedMessageState) error {
return err // matchChatEntity returns a descriptive error message
}

// If profile updates check if author is the same as chat profile public key
if chat.ProfileUpdates() && receivedMessage.From != chat.Profile {
return nil
}

// If deleted-at is greater, ignore message
if chat.DeletedAtClockValue >= receivedMessage.Clock {
return nil
Expand Down
10 changes: 5 additions & 5 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ func (m *Messenger) Init() error {
continue
}
switch chat.ChatType {
case ChatTypePublic:
case ChatTypePublic, ChatTypeProfile:
publicChatIDs = append(publicChatIDs, chat.ID)
case ChatTypeOneToOne:
pk, err := chat.PublicKey()
Expand Down Expand Up @@ -673,7 +673,7 @@ func (m *Messenger) Join(chat Chat) error {
return err
}
return m.transport.JoinGroup(members)
case ChatTypePublic:
case ChatTypePublic, ChatTypeProfile:
return m.transport.JoinPublic(chat.ID)
default:
return errors.New("chat is neither public nor private")
Expand All @@ -696,7 +696,7 @@ func (m *Messenger) Leave(chat Chat) error {
return err
}
return m.transport.LeaveGroup(members)
case ChatTypePublic:
case ChatTypePublic, ChatTypeProfile:
return m.transport.LeavePublic(chat.Name)
default:
return errors.New("chat is neither public nor private")
Expand Down Expand Up @@ -1657,7 +1657,7 @@ func (m *Messenger) dispatchMessage(ctx context.Context, spec common.RawMessage)
return nil, err
}

case ChatTypePublic:
case ChatTypePublic, ChatTypeProfile:
logger.Debug("sending public message", zap.String("chatName", chat.Name))
id, err = m.processor.SendPublic(ctx, chat.ID, spec)
if err != nil {
Expand Down Expand Up @@ -3796,7 +3796,7 @@ func (m *Messenger) encodeChatEntity(chat *Chat, message common.ChatEntity) ([]b
if err != nil {
return nil, err
}
case ChatTypePublic:
case ChatTypePublic, ChatTypeProfile:
l.Debug("sending public message", zap.String("chatName", chat.Name))
message.SetMessageType(protobuf.MessageType_PUBLIC_GROUP)
encodedMessage, err = proto.Marshal(message.GetProtobuf())
Expand Down
29 changes: 28 additions & 1 deletion protocol/messenger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func buildTestMessage(chat Chat) *common.Message {
message.LocalChatID = chat.ID
message.ContentType = protobuf.ChatMessage_TEXT_PLAIN
switch chat.ChatType {
case ChatTypePublic:
case ChatTypePublic, ChatTypeProfile:
message.MessageType = protobuf.MessageType_PUBLIC_GROUP
case ChatTypeOneToOne:
message.MessageType = protobuf.MessageType_ONE_TO_ONE
Expand Down Expand Up @@ -373,6 +373,33 @@ func (s *MessengerSuite) TestSendPublic() {
s.Require().Equal(1, len(savedMessages), "it saves the message")
}

func (s *MessengerSuite) TestSendProfile() {
chat := CreateProfileChat("test-chat-profile", "0x"+hex.EncodeToString(crypto.FromECDSAPub(&s.privateKey.PublicKey)), s.m.transport)
chat.LastClockValue = uint64(100000000000000)
err := s.m.SaveChat(&chat)
s.NoError(err)
inputMessage := buildTestMessage(chat)
response, err := s.m.SendChatMessage(context.Background(), inputMessage)
s.NoError(err)

s.Require().Equal(1, len(response.Messages), "it returns the message")
outputMessage := response.Messages[0]

s.Require().Equal(uint64(100000000000001), outputMessage.Clock, "it correctly sets the clock")
s.Require().Equal(uint64(100000000000001), chat.LastClockValue, "it correctly sets the last-clock-value")
s.Require().NotEqual(uint64(0), chat.Timestamp, "it sets the timestamp")
s.Require().Equal("0x"+hex.EncodeToString(crypto.FromECDSAPub(&s.privateKey.PublicKey)), outputMessage.From, "it sets the From field")
s.Require().Equal(chat.Profile, outputMessage.From, "From equal to chat Profile")
s.Require().True(outputMessage.Seen, "it marks the message as seen")
s.Require().Equal(outputMessage.OutgoingStatus, common.OutgoingStatusSending, "it marks the message as sending")
s.Require().NotEmpty(outputMessage.ID, "it sets the ID field")
s.Require().Equal(protobuf.MessageType_PUBLIC_GROUP, outputMessage.MessageType)

savedMessages, _, err := s.m.MessageByChatID(chat.ID, "", 10)
s.Require().NoError(err)
s.Require().Equal(1, len(savedMessages), "it saves the message")
}

func (s *MessengerSuite) TestSendPrivateOneToOne() {
recipientKey, err := crypto.GenerateKey()
s.NoError(err)
Expand Down
Loading

0 comments on commit b388002

Please sign in to comment.