Skip to content

Implement v2 chat system with two-way chats #138

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

Open
wants to merge 47 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
78dc739
PoC in memory two way messaging
jeffyanta May 15, 2024
413bc99
Move chat event stream notification into an async worker
jeffyanta May 17, 2024
b4ef04f
Ensure chat event streams are cleaned up after being closed
jeffyanta May 17, 2024
e645a76
Add support for thank you messages
jeffyanta May 21, 2024
d3f001f
Merge branch 'main' into two-way-chats
jeffyanta May 21, 2024
69aba1b
Rename struct
jeffyanta May 21, 2024
368854e
Fill out missing log message
jeffyanta May 21, 2024
73bb704
Add basic push support for user messages
jeffyanta May 22, 2024
51ea27c
Use separate context for pushing user chat messages
jeffyanta May 22, 2024
92dfbd3
Merge branch 'main' into two-way-chats
jeffyanta May 27, 2024
310dc3f
Fix build
jeffyanta May 27, 2024
9ecf67b
Merge branch 'main' into two-way-chats
jeffyanta Jun 5, 2024
de3d9af
Merge branch 'main' into two-way-chats
jeffyanta Jun 7, 2024
67928e6
Move existing chat stuff to v1 in prep for v2
jeffyanta Jun 7, 2024
a8103da
Add skeleton for v2 gRPC chat service
jeffyanta Jun 7, 2024
3b0c6af
Define chat v2 models
jeffyanta Jun 7, 2024
66dfce4
Implement RPCs that operate on chat member state
jeffyanta Jun 7, 2024
f0f1b91
Implement StreamChatEvents with integration of pointer events
jeffyanta Jun 7, 2024
c491d72
Implement GetMessages RPC
jeffyanta Jun 7, 2024
9e6b74e
Add reminder to add a flush on StreamChatEvents
jeffyanta Jun 7, 2024
a18a725
Add a flush on StreamChatEvents stream open
jeffyanta Jun 10, 2024
ccda13e
Fix todo comment
jeffyanta Jun 10, 2024
d6f3e54
Add missing query parameters to flush
jeffyanta Jun 10, 2024
9740697
Implement the SendMessage RPC without consideration for other chat fe…
jeffyanta Jun 10, 2024
38149d7
Skeleton for minimal memory PoC chat v2 data store
jeffyanta Jun 10, 2024
695ee4e
Implement chat v2 memory data store (WIP)
jeffyanta Jun 10, 2024
fa485f1
Implement remaining chat memory store defined methods
jeffyanta Jun 11, 2024
170ead3
Merge branch 'main' into two-way-chats
jeffyanta Jun 11, 2024
a2cda00
Add missing result codes and update/comment on flush
jeffyanta Jun 11, 2024
f2f99d6
Flush pointers on chat event stream open
jeffyanta Jun 11, 2024
1ed4862
Address todos relating to copying pointer values in chat store
jeffyanta Jun 11, 2024
0b8ec7b
Fix result codes in GetMessages
jeffyanta Jun 11, 2024
c7a4b53
Setup a temporary mock chat for testing
jeffyanta Jun 11, 2024
f2135f0
Fix message ID sorting
jeffyanta Jun 12, 2024
b0f1836
flushMessages doesn't need a cursor value for the DB query
jeffyanta Jun 12, 2024
c203be6
Merge branch 'main' into two-way-chats
jeffyanta Jun 13, 2024
b48020f
Implement GetChats RPC limited to anonymous chat membership
jeffyanta Jun 13, 2024
febc5f2
Remove addressed todo
jeffyanta Jun 13, 2024
26bf2b7
Have chat v2 store indicate if pointer was advanced
jeffyanta Jun 13, 2024
74852ba
Fix unread count to not count messages sent by the reader
jeffyanta Jun 13, 2024
8366e10
Fix build with refactor changes to chat protos
jeffyanta Jun 17, 2024
f62c762
Initial implementation of StartChat that always starts a new chat
jeffyanta Jun 18, 2024
5f39c72
Initial implementation of the RevealIdentity RPC
jeffyanta Jun 18, 2024
c61f7b0
More refactors of chat stuff
jeffyanta Jun 19, 2024
9e74ca3
Incorporate small tweaks to chat APIs
jeffyanta Jun 20, 2024
54aa95f
Pull official proto API release with v2 chat service
jeffyanta Jun 20, 2024
e29881f
Merge branch 'main' into two-way-chats
jeffyanta Jul 3, 2024
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
firebase.google.com/go/v4 v4.8.0
github.com/aws/aws-sdk-go-v2 v0.17.0
github.com/bits-and-blooms/bloom/v3 v3.1.0
github.com/code-payments/code-protobuf-api v1.16.6
github.com/code-payments/code-protobuf-api v1.17.0
github.com/emirpasic/gods v1.12.0
github.com/envoyproxy/protoc-gen-validate v1.0.4
github.com/golang-jwt/jwt/v5 v5.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWH
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/code-payments/code-protobuf-api v1.16.6 h1:QCot0U+4Ar5SdSX4v955FORMsd3Qcf0ZgkoqlGJZzu0=
github.com/code-payments/code-protobuf-api v1.16.6/go.mod h1:pHQm75vydD6Cm2qHAzlimW6drysm489Z4tVxC2zHSsU=
github.com/code-payments/code-protobuf-api v1.17.0 h1:zqLrhm54purzsKYb+5CZ3fJZCIVzmuZ31yeARUkuyWE=
github.com/code-payments/code-protobuf-api v1.17.0/go.mod h1:pHQm75vydD6Cm2qHAzlimW6drysm489Z4tVxC2zHSsU=
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6 h1:NmTXa/uVnDyp0TY5MKi197+3HWcnYWfnHGyaFthlnGw=
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6/go.mod h1:GL3xCUCBDV3CZiTSEKksMWbLE66hEyuu9qyDOOqM47Y=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
Expand Down
6 changes: 3 additions & 3 deletions pkg/code/async/geyser/external_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/account"
"github.com/code-payments/code-server/pkg/code/data/balance"
"github.com/code-payments/code-server/pkg/code/data/chat"
chat_v1 "github.com/code-payments/code-server/pkg/code/data/chat/v1"
"github.com/code-payments/code-server/pkg/code/data/deposit"
"github.com/code-payments/code-server/pkg/code/data/fulfillment"
"github.com/code-payments/code-server/pkg/code/data/intent"
Expand Down Expand Up @@ -299,7 +299,7 @@ func processPotentialExternalDeposit(ctx context.Context, conf *conf, data code_
chatMessage,
)
}
case chat.ErrMessageAlreadyExists:
case chat_v1.ErrMessageAlreadyExists:
default:
return errors.Wrap(err, "error sending chat message")
}
Expand Down Expand Up @@ -772,7 +772,7 @@ func delayedUsdcDepositProcessing(
chatMessage,
)
}
case chat.ErrMessageAlreadyExists:
case chat_v1.ErrMessageAlreadyExists:
default:
return
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/code/async/geyser/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/account"
"github.com/code-payments/code-server/pkg/code/data/chat"
chat_v1 "github.com/code-payments/code-server/pkg/code/data/chat/v1"
"github.com/code-payments/code-server/pkg/code/push"
"github.com/code-payments/code-server/pkg/code/thirdparty"
"github.com/code-payments/code-server/pkg/database/query"
Expand Down Expand Up @@ -169,13 +169,13 @@ func processPotentialBlockchainMessage(ctx context.Context, data code_data.Provi
ctx,
data,
asciiBaseDomain,
chat.ChatTypeExternalApp,
chat_v1.ChatTypeExternalApp,
true,
recipientOwner,
chatMessage,
false,
)
if err != nil && err != chat.ErrMessageAlreadyExists {
if err != nil && err != chat_v1.ErrMessageAlreadyExists {
return errors.Wrap(err, "error persisting chat message")
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/code/chat/message_cash_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/chat"
chat_v1 "github.com/code-payments/code-server/pkg/code/data/chat/v1"
"github.com/code-payments/code-server/pkg/code/data/intent"
)

Expand Down Expand Up @@ -93,9 +93,9 @@ func SendCashTransactionsExchangeMessage(ctx context.Context, data code_data.Pro
return errors.Wrap(err, "error getting original gift card issued intent")
}

chatId := chat.GetChatId(CashTransactionsName, giftCardIssuedIntentRecord.InitiatorOwnerAccount, true)
chatId := chat_v1.GetChatId(CashTransactionsName, giftCardIssuedIntentRecord.InitiatorOwnerAccount, true)

err = data.DeleteChatMessage(ctx, chatId, giftCardIssuedIntentRecord.IntentId)
err = data.DeleteChatMessageV1(ctx, chatId, giftCardIssuedIntentRecord.IntentId)
if err != nil {
return errors.Wrap(err, "error deleting chat message")
}
Expand Down Expand Up @@ -152,13 +152,13 @@ func SendCashTransactionsExchangeMessage(ctx context.Context, data code_data.Pro
ctx,
data,
CashTransactionsName,
chat.ChatTypeInternal,
chat_v1.ChatTypeInternal,
true,
receiver,
protoMessage,
true,
)
if err != nil && err != chat.ErrMessageAlreadyExists {
if err != nil && err != chat_v1.ErrMessageAlreadyExists {
return errors.Wrap(err, "error persisting chat message")
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/code/chat/message_code_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/chat"
chat_v1 "github.com/code-payments/code-server/pkg/code/data/chat/v1"
"github.com/code-payments/code-server/pkg/code/data/intent"
"github.com/code-payments/code-server/pkg/code/localization"
)
Expand All @@ -20,7 +20,7 @@ func SendCodeTeamMessage(ctx context.Context, data code_data.Provider, receiver
ctx,
data,
CodeTeamName,
chat.ChatTypeInternal,
chat_v1.ChatTypeInternal,
true,
receiver,
chatMessage,
Expand Down Expand Up @@ -48,8 +48,8 @@ func newIncentiveMessage(localizedTextKey string, intentRecord *intent.Record) (

content := []*chatpb.Content{
{
Type: &chatpb.Content_Localized{
Localized: &chatpb.LocalizedContent{
Type: &chatpb.Content_ServerLocalized{
ServerLocalized: &chatpb.ServerLocalizedContent{
KeyOrText: localizedTextKey,
},
},
Expand Down
20 changes: 10 additions & 10 deletions pkg/code/chat/message_kin_purchases.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (

"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/chat"
chat_v1 "github.com/code-payments/code-server/pkg/code/data/chat/v1"
"github.com/code-payments/code-server/pkg/code/localization"
)

// GetKinPurchasesChatId returns the chat ID for the Kin Purchases chat for a
// given owner account
func GetKinPurchasesChatId(owner *common.Account) chat.ChatId {
return chat.GetChatId(KinPurchasesName, owner.PublicKey().ToBase58(), true)
func GetKinPurchasesChatId(owner *common.Account) chat_v1.ChatId {
return chat_v1.GetChatId(KinPurchasesName, owner.PublicKey().ToBase58(), true)
}

// SendKinPurchasesMessage sends a message to the Kin Purchases chat.
Expand All @@ -27,7 +27,7 @@ func SendKinPurchasesMessage(ctx context.Context, data code_data.Provider, recei
ctx,
data,
KinPurchasesName,
chat.ChatTypeInternal,
chat_v1.ChatTypeInternal,
true,
receiver,
chatMessage,
Expand All @@ -40,8 +40,8 @@ func SendKinPurchasesMessage(ctx context.Context, data code_data.Provider, recei
func ToUsdcDepositedMessage(signature string, ts time.Time) (*chatpb.ChatMessage, error) {
content := []*chatpb.Content{
{
Type: &chatpb.Content_Localized{
Localized: &chatpb.LocalizedContent{
Type: &chatpb.Content_ServerLocalized{
ServerLocalized: &chatpb.ServerLocalizedContent{
KeyOrText: localization.ChatMessageUsdcDeposited,
},
},
Expand All @@ -60,8 +60,8 @@ func NewUsdcBeingConvertedMessage(ts time.Time) (*chatpb.ChatMessage, error) {

content := []*chatpb.Content{
{
Type: &chatpb.Content_Localized{
Localized: &chatpb.LocalizedContent{
Type: &chatpb.Content_ServerLocalized{
ServerLocalized: &chatpb.ServerLocalizedContent{
KeyOrText: localization.ChatMessageUsdcBeingConverted,
},
},
Expand All @@ -79,8 +79,8 @@ func ToKinAvailableForUseMessage(signature string, ts time.Time, purchases ...*t

content := []*chatpb.Content{
{
Type: &chatpb.Content_Localized{
Localized: &chatpb.LocalizedContent{
Type: &chatpb.Content_ServerLocalized{
ServerLocalized: &chatpb.ServerLocalizedContent{
KeyOrText: localization.ChatMessageKinAvailableForUse,
},
},
Expand Down
14 changes: 7 additions & 7 deletions pkg/code/chat/message_merchant.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/action"
"github.com/code-payments/code-server/pkg/code/data/chat"
chat_v1 "github.com/code-payments/code-server/pkg/code/data/chat/v1"
"github.com/code-payments/code-server/pkg/code/data/intent"
)

Expand All @@ -36,7 +36,7 @@ func SendMerchantExchangeMessage(ctx context.Context, data code_data.Provider, i
// the merchant. Representation in the UI may differ (ie. 2 and 3 are grouped),
// but this is the most flexible solution with the chat model.
chatTitle := PaymentsName
chatType := chat.ChatTypeInternal
chatType := chat_v1.ChatTypeInternal
isVerifiedChat := false

exchangeData, ok := getExchangeDataFromIntent(intentRecord)
Expand All @@ -59,7 +59,7 @@ func SendMerchantExchangeMessage(ctx context.Context, data code_data.Provider, i

if paymentRequestRecord.Domain != nil {
chatTitle = *paymentRequestRecord.Domain
chatType = chat.ChatTypeExternalApp
chatType = chat_v1.ChatTypeExternalApp
isVerifiedChat = paymentRequestRecord.IsVerified
}

Expand Down Expand Up @@ -87,7 +87,7 @@ func SendMerchantExchangeMessage(ctx context.Context, data code_data.Provider, i
// and will have merchant payments appear in the verified merchant
// chat.
chatTitle = *destinationAccountInfoRecord.RelationshipTo
chatType = chat.ChatTypeExternalApp
chatType = chat_v1.ChatTypeExternalApp
isVerifiedChat = true
verbAndExchangeDataByMessageReceiver[intentRecord.SendPrivatePaymentMetadata.DestinationOwnerAccount] = &verbAndExchangeData{
verb: chatpb.ExchangeDataContent_RECEIVED,
Expand All @@ -107,7 +107,7 @@ func SendMerchantExchangeMessage(ctx context.Context, data code_data.Provider, i
// and will have merchant payments appear in the verified merchant
// chat.
chatTitle = *destinationAccountInfoRecord.RelationshipTo
chatType = chat.ChatTypeExternalApp
chatType = chat_v1.ChatTypeExternalApp
isVerifiedChat = true
verbAndExchangeDataByMessageReceiver[intentRecord.SendPublicPaymentMetadata.DestinationOwnerAccount] = &verbAndExchangeData{
verb: chatpb.ExchangeDataContent_RECEIVED,
Expand All @@ -126,7 +126,7 @@ func SendMerchantExchangeMessage(ctx context.Context, data code_data.Provider, i
// and will have merchant payments appear in the verified merchant
// chat.
chatTitle = *destinationAccountInfoRecord.RelationshipTo
chatType = chat.ChatTypeExternalApp
chatType = chat_v1.ChatTypeExternalApp
isVerifiedChat = true
verbAndExchangeDataByMessageReceiver[intentRecord.ExternalDepositMetadata.DestinationOwnerAccount] = &verbAndExchangeData{
verb: chatpb.ExchangeDataContent_RECEIVED,
Expand Down Expand Up @@ -171,7 +171,7 @@ func SendMerchantExchangeMessage(ctx context.Context, data code_data.Provider, i
protoMessage,
verbAndExchangeData.verb != chatpb.ExchangeDataContent_RECEIVED || !isVerifiedChat,
)
if err != nil && err != chat.ErrMessageAlreadyExists {
if err != nil && err != chat_v1.ErrMessageAlreadyExists {
return nil, errors.Wrap(err, "error persisting chat message")
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/code/chat/message_tips.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/chat"
chat_v1 "github.com/code-payments/code-server/pkg/code/data/chat/v1"
"github.com/code-payments/code-server/pkg/code/data/intent"
)

Expand Down Expand Up @@ -70,13 +70,13 @@ func SendTipsExchangeMessage(ctx context.Context, data code_data.Provider, inten
ctx,
data,
TipsName,
chat.ChatTypeInternal,
chat_v1.ChatTypeInternal,
true,
receiver,
protoMessage,
verb != chatpb.ExchangeDataContent_RECEIVED_TIP,
)
if err != nil && err != chat.ErrMessageAlreadyExists {
if err != nil && err != chat_v1.ErrMessageAlreadyExists {
return nil, errors.Wrap(err, "error persisting chat message")
}

Expand Down
20 changes: 10 additions & 10 deletions pkg/code/chat/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/chat"
chat_v1 "github.com/code-payments/code-server/pkg/code/data/chat/v1"
)

// SendChatMessage sends a chat message to a receiving owner account.
Expand All @@ -24,13 +24,13 @@ func SendChatMessage(
ctx context.Context,
data code_data.Provider,
chatTitle string,
chatType chat.ChatType,
chatType chat_v1.ChatType,
isVerifiedChat bool,
receiver *common.Account,
protoMessage *chatpb.ChatMessage,
isSilentMessage bool,
) (canPushMessage bool, err error) {
chatId := chat.GetChatId(chatTitle, receiver.PublicKey().ToBase58(), isVerifiedChat)
chatId := chat_v1.GetChatId(chatTitle, receiver.PublicKey().ToBase58(), isVerifiedChat)

if protoMessage.Cursor != nil {
// Let the utilities and GetMessages RPC handle cursors
Expand Down Expand Up @@ -58,13 +58,13 @@ func SendChatMessage(
canPersistMessage := true
canPushMessage = !isSilentMessage

existingChatRecord, err := data.GetChatById(ctx, chatId)
existingChatRecord, err := data.GetChatByIdV1(ctx, chatId)
switch err {
case nil:
canPersistMessage = !existingChatRecord.IsUnsubscribed
canPushMessage = canPushMessage && canPersistMessage && !existingChatRecord.IsMuted
case chat.ErrChatNotFound:
chatRecord := &chat.Chat{
case chat_v1.ErrChatNotFound:
chatRecord := &chat_v1.Chat{
ChatId: chatId,
ChatType: chatType,
ChatTitle: chatTitle,
Expand All @@ -79,16 +79,16 @@ func SendChatMessage(
CreatedAt: time.Now(),
}

err = data.PutChat(ctx, chatRecord)
if err != nil && err != chat.ErrChatAlreadyExists {
err = data.PutChatV1(ctx, chatRecord)
if err != nil && err != chat_v1.ErrChatAlreadyExists {
return false, err
}
default:
return false, err
}

if canPersistMessage {
messageRecord := &chat.Message{
messageRecord := &chat_v1.Message{
ChatId: chatId,

MessageId: base58.Encode(messageId),
Expand All @@ -100,7 +100,7 @@ func SendChatMessage(
Timestamp: ts.AsTime(),
}

err = data.PutChatMessage(ctx, messageRecord)
err = data.PutChatMessageV1(ctx, messageRecord)
if err != nil {
return false, err
}
Expand Down
Loading
Loading