Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nil reactions in comms react RPC to unreact #4606

Merged
merged 2 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions comms/internal/rpcz/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ func chatSendMessage(tx *sqlx.Tx, userId int32, chatId string, messageId string,
return err
}

func chatReactMessage(tx *sqlx.Tx, userId int32, messageId string, reaction string, messageTimestamp time.Time) error {
_, err := tx.Exec("insert into chat_message_reactions (user_id, message_id, reaction, created_at, updated_at) values ($1, $2, $3, $4, $4) on conflict (user_id, message_id) do update set reaction = $3, updated_at = $4", userId, messageId, reaction, messageTimestamp)
func chatReactMessage(tx *sqlx.Tx, userId int32, messageId string, reaction *string, messageTimestamp time.Time) error {
var err error
if reaction != nil {
_, err = tx.Exec("insert into chat_message_reactions (user_id, message_id, reaction, created_at, updated_at) values ($1, $2, $3, $4, $4) on conflict (user_id, message_id) do update set reaction = $3, updated_at = $4", userId, messageId, *reaction, messageTimestamp)
} else {
_, err = tx.Exec("delete from chat_message_reactions where user_id = $1 and message_id = $2", userId, messageId)
}
if err != nil {
return err
}
Expand Down
25 changes: 17 additions & 8 deletions comms/internal/rpcz/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ func TestChat(t *testing.T) {
assert.Equal(t, expected, unreadCount)
}

assertReaction := func(userId int, messageId string, expected string) {
assertReaction := func(userId int, messageId string, expected *string) {
var reaction string
err := tx.Get(&reaction, "select reaction from chat_message_reactions where user_id = $1 and message_id = $2", userId, messageId)
assert.NoError(t, err)
assert.Equal(t, expected, reaction)
if expected != nil {
assert.NoError(t, err)
assert.Equal(t, *expected, reaction)
} else {
assert.ErrorIs(t, err, sql.ErrNoRows)
}
}

// assert sender has no unread messages
Expand Down Expand Up @@ -119,14 +123,19 @@ func TestChat(t *testing.T) {
// 91 reacts to 92's message
reactTs := time.Now()
reaction := "fire"
err = chatReactMessage(tx, 91, replyMessageId, reaction, reactTs)
assertReaction(91, replyMessageId, reaction)
err = chatReactMessage(tx, 91, replyMessageId, &reaction, reactTs)
assertReaction(91, replyMessageId, &reaction)

// 91 changes reaction to 92's old message
// 91 changes reaction to 92's message
changedReactTs := time.Now()
newReaction := "heart"
err = chatReactMessage(tx, 91, replyMessageId, newReaction, changedReactTs)
assertReaction(91, replyMessageId, newReaction)
err = chatReactMessage(tx, 91, replyMessageId, &newReaction, changedReactTs)
assertReaction(91, replyMessageId, &newReaction)

// 91 removes reaction to 92's message
removedReactTs := time.Now()
err = chatReactMessage(tx, 91, replyMessageId, nil, removedReactTs)
assertReaction(91, replyMessageId, nil)

tx.Rollback()
}
6 changes: 3 additions & 3 deletions comms/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ type ChatReactRPC struct {
}

type ChatReactRPCParams struct {
ChatID string `json:"chat_id"`
MessageID string `json:"message_id"`
Reaction string `json:"reaction"`
ChatID string `json:"chat_id"`
MessageID string `json:"message_id"`
Reaction *string `json:"reaction,omitempty"`
}

type ChatReadRPC struct {
Expand Down
2 changes: 1 addition & 1 deletion comms/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type ChatReactRPC = {
params: {
chat_id: string
message_id: string
reaction: string
reaction?: string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want explicitly null, not allowing undefined. wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, added in 8800a10

}
}

Expand Down
2 changes: 1 addition & 1 deletion libs/src/sdk/api/chats/serverTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type ChatReactRPC = {
params: {
chat_id: string
message_id: string
reaction: string
reaction?: string
}
}

Expand Down