diff --git a/chats/handler/chats.go b/chats/handler/chats.go index d2745b3..6ef3149 100644 --- a/chats/handler/chats.go +++ b/chats/handler/chats.go @@ -1,14 +1,16 @@ package handler import ( - "encoding/json" + "context" + "fmt" + "sort" + "strings" "time" pb "github.com/micro/services/chats/proto" - "github.com/micro/services/pkg/gorm" + "github.com/micro/services/pkg/tenant" "github.com/micro/micro/v3/service/errors" - "google.golang.org/protobuf/types/known/timestamppb" ) var ( @@ -21,13 +23,12 @@ var ( ) type Chats struct { - gorm.Helper Time func() time.Time } type Chat struct { ID string - UserIDs string `gorm:"uniqueIndex"` // sorted json array + UserIDs []string CreatedAt time.Time } @@ -45,17 +46,69 @@ func (m *Message) Serialize() *pb.Message { AuthorId: m.AuthorID, ChatId: m.ChatID, Text: m.Text, - SentAt: timestamppb.New(m.SentAt), + SentAt: m.SentAt.UnixNano(), } } +func (c *Chat) Index(ctx context.Context) string { + sort.Strings(c.UserIDs) + users := strings.Join(c.UserIDs, "-") + + key := fmt.Sprintf("chatByUserIDs:%s", users) + + t, ok := tenant.FromContext(ctx) + if !ok { + return key + } + + return fmt.Sprintf("%s/%s", t, key) +} + +func (c *Chat) Key(ctx context.Context) string { + key := fmt.Sprintf("chat:%s", c.ID) + + t, ok := tenant.FromContext(ctx) + if !ok { + return key + } + + return fmt.Sprintf("%s/%s", t, key) +} + +func (m *Message) Key(ctx context.Context) string { + key := fmt.Sprintf("message:%s:%s", m.ChatID, m.ID) + + t, ok := tenant.FromContext(ctx) + if !ok { + return key + } + + return fmt.Sprintf("%s/%s", t, key) +} + +func (m *Message) Index(ctx context.Context) string { + key := fmt.Sprintf("messagesByChatID:%s", m.ChatID) + + if !m.SentAt.IsZero() { + key = fmt.Sprintf("%s:%d", key, m.SentAt.UnixNano()) + + if len(m.ID) > 0 { + key = fmt.Sprintf("%s:%s", key, m.ID) + } + } + + t, ok := tenant.FromContext(ctx) + if !ok { + return key + } + + return fmt.Sprintf("%s/%s", t, key) +} + func (c *Chat) Serialize() *pb.Chat { - var userIDs []string - json.Unmarshal([]byte(c.UserIDs), &userIDs) - return &pb.Chat{ Id: c.ID, - UserIds: userIDs, - CreatedAt: timestamppb.New(c.CreatedAt), + UserIds: c.UserIDs, + CreatedAt: c.CreatedAt.UnixNano(), } } diff --git a/chats/handler/chats_test.go b/chats/handler/chats_test.go index 63ad3ae..ea83c3a 100644 --- a/chats/handler/chats_test.go +++ b/chats/handler/chats_test.go @@ -2,38 +2,20 @@ package handler_test import ( "context" - "database/sql" - "os" "testing" "time" "github.com/micro/micro/v3/service/auth" + "github.com/micro/micro/v3/service/store" + "github.com/micro/micro/v3/service/store/memory" "github.com/micro/services/chats/handler" pb "github.com/micro/services/chats/proto" "github.com/stretchr/testify/assert" - - "github.com/golang/protobuf/ptypes/timestamp" ) func testHandler(t *testing.T) *handler.Chats { - // connect to the database - addr := os.Getenv("POSTGRES_URL") - if len(addr) == 0 { - addr = "postgresql://postgres@localhost:5432/postgres?sslmode=disable" - } - sqlDB, err := sql.Open("pgx", addr) - if err != nil { - t.Fatalf("Failed to open connection to DB %s", err) - } - - // clean any data from a previous run - if _, err := sqlDB.Exec("DROP TABLE IF EXISTS micro_chats, micro_messages CASCADE"); err != nil { - t.Fatalf("Error cleaning database: %v", err) - } - - h := &handler.Chats{Time: func() time.Time { return time.Unix(1611327673, 0) }} - h.DBConn(sqlDB).Migrations(&handler.Chat{}, &handler.Message{}) - return h + store.DefaultStore = memory.NewStore() + return &handler.Chats{Time: func() time.Time { return time.Unix(1611327673, 0) }} } func assertChatsMatch(t *testing.T, exp, act *pb.Chat) { @@ -52,18 +34,12 @@ func assertChatsMatch(t *testing.T, exp, act *pb.Chat) { assert.Equal(t, exp.UserIds, act.UserIds) - if act.CreatedAt == nil { + if act.CreatedAt == 0 { t.Errorf("CreatedAt not set") return } - assert.True(t, microSecondTime(exp.CreatedAt).Equal(microSecondTime(act.CreatedAt))) -} - -// postgres has a resolution of 100microseconds so just test that it's accurate to the second -func microSecondTime(t *timestamp.Timestamp) time.Time { - tt := t.AsTime() - return time.Unix(tt.Unix(), int64(tt.Nanosecond()-tt.Nanosecond()%1000)) + assert.True(t, exp.CreatedAt == act.CreatedAt) } func assertMessagesMatch(t *testing.T, exp, act *pb.Message) { @@ -83,11 +59,12 @@ func assertMessagesMatch(t *testing.T, exp, act *pb.Message) { assert.Equal(t, exp.AuthorId, act.AuthorId) assert.Equal(t, exp.ChatId, act.ChatId) - if act.SentAt == nil { + if act.SentAt == 0 { t.Errorf("SentAt not set") return } - assert.True(t, microSecondTime(exp.SentAt).Equal(microSecondTime(act.SentAt))) + + assert.True(t, exp.SentAt == act.SentAt) } func microAccountCtx() context.Context { diff --git a/chats/handler/create_chat.go b/chats/handler/create_chat.go index 0c333d1..1bb631b 100644 --- a/chats/handler/create_chat.go +++ b/chats/handler/create_chat.go @@ -2,8 +2,6 @@ package handler import ( "context" - "encoding/json" - "regexp" "sort" "time" @@ -11,6 +9,7 @@ import ( "github.com/micro/micro/v3/service/auth" "github.com/micro/micro/v3/service/errors" "github.com/micro/micro/v3/service/logger" + "github.com/micro/micro/v3/service/store" pb "github.com/micro/services/chats/proto" ) @@ -26,43 +25,56 @@ func (c *Chats) CreateChat(ctx context.Context, req *pb.CreateChatRequest, rsp * return ErrMissingUserIDs } - // sort the user ids and then marshal to json + // sort the user ids sort.Strings(req.UserIds) - bytes, err := json.Marshal(req.UserIds) - if err != nil { - logger.Errorf("Error mashaling user ids: %v", err) - return errors.InternalServerError("ENCODING_ERROR", "Error encoding user ids") + + id := uuid.New().String() + if len(req.Id) > 0 { + id = req.Id } // construct the chat - chat := Chat{ - ID: uuid.New().String(), + chat := &Chat{ + ID: id, CreatedAt: time.Now(), - UserIDs: string(bytes), + UserIDs: req.UserIds, } - db, err := c.GetDBConn(ctx) - if err != nil { - logger.Errorf("Error connecting to DB: %v", err) - return errors.InternalServerError("DB_ERROR", "Error connecting to DB") - } - // write to the database, if we get a unique key error, the chat already exists - err = db.Create(&chat).Error - if err == nil { + // read the chat by the unique composition of ids + recs, err := store.Read(chat.Key(ctx), store.ReadLimit(1)) + if err == nil && len(recs) == 1 { + // found an existing record + recs[0].Decode(&chat) rsp.Chat = chat.Serialize() return nil } - if match, _ := regexp.MatchString(`idx_[\S]+_chats_user_ids`, err.Error()); !match { + // if not found check it exists by user index key + if err == store.ErrNotFound { + recs, err = store.Read(chat.Index(ctx), store.ReadLimit(1)) + if err == nil && len(recs) > 0 { + recs[0].Decode(&chat) + rsp.Chat = chat.Serialize() + return nil + } + } + + // ok otherwise we're creating an entirely new record + newRec := store.NewRecord(chat.Key(ctx), chat) + if err := store.Write(newRec); err != nil { logger.Errorf("Error creating chat: %v", err) return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database") } - var existing Chat - if err := db.Where(&Chat{UserIDs: chat.UserIDs}).First(&existing).Error; err != nil { - logger.Errorf("Error reading chat: %v", err) + // write the user composite key + newRec = store.NewRecord(chat.Index(ctx), chat) + if err := store.Write(newRec); err != nil { + logger.Errorf("Error creating chat: %v", err) return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database") } - rsp.Chat = existing.Serialize() + + // return the record + rsp.Chat = chat.Serialize() + return nil } diff --git a/chats/handler/create_message.go b/chats/handler/create_message.go index ed3fb97..9161347 100644 --- a/chats/handler/create_message.go +++ b/chats/handler/create_message.go @@ -2,18 +2,17 @@ package handler import ( "context" - "strings" "github.com/google/uuid" "github.com/micro/micro/v3/service/auth" "github.com/micro/micro/v3/service/errors" "github.com/micro/micro/v3/service/logger" + "github.com/micro/micro/v3/service/store" pb "github.com/micro/services/chats/proto" - "gorm.io/gorm" ) // Create a message within a chat -func (c *Chats) CreateMessage(ctx context.Context, req *pb.CreateMessageRequest, rsp *pb.CreateMessageResponse) error { +func (c *Chats) SendMessage(ctx context.Context, req *pb.SendMessageRequest, rsp *pb.SendMessageResponse) error { _, ok := auth.AccountFromContext(ctx) if !ok { errors.Unauthorized("UNAUTHORIZED", "Unauthorized") @@ -29,14 +28,12 @@ func (c *Chats) CreateMessage(ctx context.Context, req *pb.CreateMessageRequest, return ErrMissingText } - db, err := c.GetDBConn(ctx) - if err != nil { - logger.Errorf("Error connecting to DB: %v", err) - return errors.InternalServerError("DB_ERROR", "Error connecting to DB") + chat := &Chat{ + ID: req.ChatId, } - // lookup the chat - var conv Chat - if err := db.Where(&Chat{ID: req.ChatId}).First(&conv).Error; err == gorm.ErrRecordNotFound { + + recs, err := store.Read(chat.Key(ctx), store.ReadLimit(1)) + if err == store.ErrNotFound { return ErrNotFound } else if err != nil { logger.Errorf("Error reading chat: %v", err) @@ -46,28 +43,45 @@ func (c *Chats) CreateMessage(ctx context.Context, req *pb.CreateMessageRequest, // create the message msg := &Message{ ID: req.Id, - SentAt: c.Time(), Text: req.Text, AuthorID: req.AuthorId, ChatID: req.ChatId, + SentAt: c.Time(), } if len(msg.ID) == 0 { msg.ID = uuid.New().String() } - if err := db.Create(msg).Error; err == nil { + + // check if the message already exists + recs, err = store.Read(msg.Key(ctx), store.ReadLimit(1)) + if err == nil && len(recs) == 1 { + // return the existing message + msg = &Message{} + recs[0].Decode(&msg) rsp.Message = msg.Serialize() return nil - } else if !strings.Contains(err.Error(), "messages_pkey") { + } + + // if there's an error then return + if err != nil && err != store.ErrNotFound { logger.Errorf("Error creating message: %v", err) return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database") } - // a message already exists with this id - var existing Message - if err := db.Where(&Message{ID: msg.ID}).First(&existing).Error; err != nil { + // otherwise write the record + if err := store.Write(store.NewRecord(msg.Key(ctx), msg)); err != nil { logger.Errorf("Error creating message: %v", err) return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database") } - rsp.Message = existing.Serialize() + + // write the time based index + if err := store.Write(store.NewRecord(msg.Index(ctx), msg)); err == nil { + rsp.Message = msg.Serialize() + return nil + } else if err != nil { + logger.Errorf("Error creating message: %v", err) + return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database") + } + return nil } diff --git a/chats/handler/create_message_test.go b/chats/handler/create_message_test.go index c2caa43..0bd1f42 100644 --- a/chats/handler/create_message_test.go +++ b/chats/handler/create_message_test.go @@ -5,13 +5,12 @@ import ( "github.com/micro/services/chats/handler" pb "github.com/micro/services/chats/proto" - "google.golang.org/protobuf/types/known/timestamppb" "github.com/google/uuid" "github.com/stretchr/testify/assert" ) -func TestCreateMessage(t *testing.T) { +func TestSendMessage(t *testing.T) { h := testHandler(t) // seed some data @@ -82,8 +81,8 @@ func TestCreateMessage(t *testing.T) { for _, tc := range tt { t.Run(tc.Name, func(t *testing.T) { - var rsp pb.CreateMessageResponse - err := h.CreateMessage(microAccountCtx(), &pb.CreateMessageRequest{ + var rsp pb.SendMessageResponse + err := h.SendMessage(microAccountCtx(), &pb.SendMessageRequest{ AuthorId: tc.AuthorID, ChatId: tc.ChatID, Text: tc.Text, @@ -99,7 +98,7 @@ func TestCreateMessage(t *testing.T) { assertMessagesMatch(t, &pb.Message{ AuthorId: tc.AuthorID, ChatId: tc.ChatID, - SentAt: timestamppb.New(h.Time()), + SentAt: h.Time().UnixNano(), Text: tc.Text, Id: tc.ID, }, rsp.Message) diff --git a/chats/handler/list_messages.go b/chats/handler/list_messages.go index 8831d31..b4f6ec5 100644 --- a/chats/handler/list_messages.go +++ b/chats/handler/list_messages.go @@ -6,6 +6,7 @@ import ( "github.com/micro/micro/v3/service/auth" "github.com/micro/micro/v3/service/errors" "github.com/micro/micro/v3/service/logger" + "github.com/micro/micro/v3/service/store" pb "github.com/micro/services/chats/proto" ) @@ -23,33 +24,46 @@ func (c *Chats) ListMessages(ctx context.Context, req *pb.ListMessagesRequest, r return ErrMissingChatID } - db, err := c.GetDBConn(ctx) - if err != nil { - logger.Errorf("Error connecting to DB: %v", err) - return errors.InternalServerError("DB_ERROR", "Error connecting to DB") - } - // construct the query - q := db.Where(&Message{ChatID: req.ChatId}).Order("sent_at DESC") - if req.SentBefore != nil { - q = q.Where("sent_at < ?", req.SentBefore.AsTime()) - } - if req.Limit != nil { - q.Limit(int(req.Limit.Value)) - } else { - q.Limit(DefaultLimit) + message := &Message{ + ChatID: req.ChatId, } - // execute the query - var msgs []Message - if err := q.Find(&msgs).Error; err != nil { + // default order is descending + order := store.OrderDesc + if req.Order == "asc" { + order = store.OrderAsc + } + + opts := []store.ReadOption{ + store.ReadPrefix(), + store.ReadOrder(order), + } + + if req.Limit > 0 { + opts = append(opts, store.ReadLimit(uint(req.Limit))) + } else { + opts = append(opts, store.ReadLimit(uint(DefaultLimit))) + } + if req.Offset > 0 { + opts = append(opts, store.ReadOffset(uint(req.Offset))) + } + + // read all the records with the chat ID suffix + recs, err := store.Read(message.Index(ctx), opts...) + if err != nil { logger.Errorf("Error reading messages: %v", err) return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database") } - // serialize the response - rsp.Messages = make([]*pb.Message, len(msgs)) - for i, m := range msgs { - rsp.Messages[i] = m.Serialize() + // return all the messages + for _, rec := range recs { + m := &Message{} + rec.Decode(&m) + if len(m.ID) == 0 || m.ChatID != req.ChatId { + continue + } + rsp.Messages = append(rsp.Messages, m.Serialize()) } + return nil } diff --git a/chats/handler/list_messages_test.go b/chats/handler/list_messages_test.go index 2570e6e..0a5e3be 100644 --- a/chats/handler/list_messages_test.go +++ b/chats/handler/list_messages_test.go @@ -10,7 +10,6 @@ import ( "github.com/micro/services/chats/handler" pb "github.com/micro/services/chats/proto" "github.com/stretchr/testify/assert" - "google.golang.org/protobuf/types/known/wrapperspb" ) func TestListMessages(t *testing.T) { @@ -29,8 +28,8 @@ func TestListMessages(t *testing.T) { msgs := make([]*pb.Message, 50) for i := 0; i < len(msgs); i++ { - var rsp pb.CreateMessageResponse - err := h.CreateMessage(microAccountCtx(), &pb.CreateMessageRequest{ + var rsp pb.SendMessageResponse + err := h.SendMessage(microAccountCtx(), &pb.SendMessageRequest{ ChatId: chatRsp.Chat.Id, AuthorId: uuid.New().String(), Text: strconv.Itoa(i), @@ -68,7 +67,7 @@ func TestListMessages(t *testing.T) { var rsp pb.ListMessagesResponse err := h.ListMessages(microAccountCtx(), &pb.ListMessagesRequest{ ChatId: chatRsp.Chat.Id, - Limit: &wrapperspb.Int32Value{Value: 10}, + Limit: 10, }, &rsp) assert.NoError(t, err) @@ -86,9 +85,9 @@ func TestListMessages(t *testing.T) { t.Run("OffsetAndLimit", func(t *testing.T) { var rsp pb.ListMessagesResponse err := h.ListMessages(microAccountCtx(), &pb.ListMessagesRequest{ - ChatId: chatRsp.Chat.Id, - Limit: &wrapperspb.Int32Value{Value: 5}, - SentBefore: msgs[20].SentAt, + ChatId: chatRsp.Chat.Id, + Limit: 5, + Offset: 15, }, &rsp) assert.NoError(t, err) @@ -96,7 +95,7 @@ func TestListMessages(t *testing.T) { t.Fatalf("Expected %v messages but got %v", 5, len(rsp.Messages)) return } - expected := msgs[15:20] + expected := msgs[30:35] sortMessages(rsp.Messages) for i, msg := range rsp.Messages { assertMessagesMatch(t, expected[i], msg) @@ -107,9 +106,9 @@ func TestListMessages(t *testing.T) { // sortMessages by the time they were sent func sortMessages(msgs []*pb.Message) { sort.Slice(msgs, func(i, j int) bool { - if msgs[i].SentAt == nil || msgs[j].SentAt == nil { + if msgs[i].SentAt == 0 || msgs[j].SentAt == 0 { return true } - return msgs[i].SentAt.AsTime().Before(msgs[j].SentAt.AsTime()) + return msgs[i].SentAt < msgs[j].SentAt }) } diff --git a/chats/main.go b/chats/main.go index 93732fc..f260ad2 100644 --- a/chats/main.go +++ b/chats/main.go @@ -1,21 +1,15 @@ package main import ( - "database/sql" "time" "github.com/micro/services/chats/handler" pb "github.com/micro/services/chats/proto" "github.com/micro/micro/v3/service" - "github.com/micro/micro/v3/service/config" "github.com/micro/micro/v3/service/logger" - - _ "github.com/jackc/pgx/v4/stdlib" ) -var dbAddress = "postgresql://postgres:postgres@localhost:5432/chats?sslmode=disable" - func main() { // Create service srv := service.New( @@ -23,19 +17,7 @@ func main() { service.Version("latest"), ) - // Connect to the database - cfg, err := config.Get("chats.database") - if err != nil { - logger.Fatalf("Error loading config: %v", err) - } - addr := cfg.String(dbAddress) - sqlDB, err := sql.Open("pgx", addr) - if err != nil { - logger.Fatalf("Failed to open connection to DB %s", err) - } - h := &handler.Chats{Time: time.Now} - h.DBConn(sqlDB).Migrations(&handler.Chat{}, &handler.Message{}) // Register handler pb.RegisterChatsHandler(srv.Server(), h) diff --git a/chats/proto/chats.pb.go b/chats/proto/chats.pb.go index 898ffe3..e910e30 100644 --- a/chats/proto/chats.pb.go +++ b/chats/proto/chats.pb.go @@ -1,17 +1,14 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.25.0 -// protoc v3.15.5 +// protoc-gen-go v1.26.0 +// protoc v3.15.6 // source: proto/chats.proto package chats import ( - proto "github.com/golang/protobuf/proto" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" - timestamppb "google.golang.org/protobuf/types/known/timestamppb" - wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" reflect "reflect" sync "sync" ) @@ -23,18 +20,17 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) -// This is a compile-time assertion that a sufficiently up-to-date version -// of the legacy proto package is being used. -const _ = proto.ProtoPackageIsVersion4 - type Chat struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` - UserIds []string `protobuf:"bytes,2,rep,name=user_ids,json=userIds,proto3" json:"user_ids,omitempty"` - CreatedAt *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"` + // unique id of the chat + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` + // list of users in the chat + UserIds []string `protobuf:"bytes,2,rep,name=user_ids,json=userIds,proto3" json:"user_ids,omitempty"` + // unix nanosecond timestamp + CreatedAt int64 `protobuf:"varint,3,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"` } func (x *Chat) Reset() { @@ -83,11 +79,11 @@ func (x *Chat) GetUserIds() []string { return nil } -func (x *Chat) GetCreatedAt() *timestamppb.Timestamp { +func (x *Chat) GetCreatedAt() int64 { if x != nil { return x.CreatedAt } - return nil + return 0 } type Message struct { @@ -95,11 +91,16 @@ type Message struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` - AuthorId string `protobuf:"bytes,2,opt,name=author_id,json=authorId,proto3" json:"author_id,omitempty"` - ChatId string `protobuf:"bytes,3,opt,name=chat_id,json=chatId,proto3" json:"chat_id,omitempty"` - Text string `protobuf:"bytes,4,opt,name=text,proto3" json:"text,omitempty"` - SentAt *timestamppb.Timestamp `protobuf:"bytes,5,opt,name=sent_at,json=sentAt,proto3" json:"sent_at,omitempty"` + // unique id of the message + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` + // user id of the message + AuthorId string `protobuf:"bytes,2,opt,name=author_id,json=authorId,proto3" json:"author_id,omitempty"` + // chat id the message belongs to + ChatId string `protobuf:"bytes,3,opt,name=chat_id,json=chatId,proto3" json:"chat_id,omitempty"` + // text within the message + Text string `protobuf:"bytes,4,opt,name=text,proto3" json:"text,omitempty"` + // unix nanosecond timestamp + SentAt int64 `protobuf:"varint,5,opt,name=sent_at,json=sentAt,proto3" json:"sent_at,omitempty"` } func (x *Message) Reset() { @@ -162,19 +163,23 @@ func (x *Message) GetText() string { return "" } -func (x *Message) GetSentAt() *timestamppb.Timestamp { +func (x *Message) GetSentAt() int64 { if x != nil { return x.SentAt } - return nil + return 0 } +// Create a new chat between mulitple users type CreateChatRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - UserIds []string `protobuf:"bytes,1,rep,name=user_ids,json=userIds,proto3" json:"user_ids,omitempty"` + // The chat ID + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` + // List of users in the chat + UserIds []string `protobuf:"bytes,2,rep,name=user_ids,json=userIds,proto3" json:"user_ids,omitempty"` } func (x *CreateChatRequest) Reset() { @@ -209,6 +214,13 @@ func (*CreateChatRequest) Descriptor() ([]byte, []int) { return file_proto_chats_proto_rawDescGZIP(), []int{2} } +func (x *CreateChatRequest) GetId() string { + if x != nil { + return x.Id + } + return "" +} + func (x *CreateChatRequest) GetUserIds() []string { if x != nil { return x.UserIds @@ -263,7 +275,8 @@ func (x *CreateChatResponse) GetChat() *Chat { return nil } -type CreateMessageRequest struct { +// Send a message to a chat room +type SendMessageRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields @@ -274,8 +287,8 @@ type CreateMessageRequest struct { Text string `protobuf:"bytes,4,opt,name=text,proto3" json:"text,omitempty"` } -func (x *CreateMessageRequest) Reset() { - *x = CreateMessageRequest{} +func (x *SendMessageRequest) Reset() { + *x = SendMessageRequest{} if protoimpl.UnsafeEnabled { mi := &file_proto_chats_proto_msgTypes[4] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -283,13 +296,13 @@ func (x *CreateMessageRequest) Reset() { } } -func (x *CreateMessageRequest) String() string { +func (x *SendMessageRequest) String() string { return protoimpl.X.MessageStringOf(x) } -func (*CreateMessageRequest) ProtoMessage() {} +func (*SendMessageRequest) ProtoMessage() {} -func (x *CreateMessageRequest) ProtoReflect() protoreflect.Message { +func (x *SendMessageRequest) ProtoReflect() protoreflect.Message { mi := &file_proto_chats_proto_msgTypes[4] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -301,40 +314,40 @@ func (x *CreateMessageRequest) ProtoReflect() protoreflect.Message { return mi.MessageOf(x) } -// Deprecated: Use CreateMessageRequest.ProtoReflect.Descriptor instead. -func (*CreateMessageRequest) Descriptor() ([]byte, []int) { +// Deprecated: Use SendMessageRequest.ProtoReflect.Descriptor instead. +func (*SendMessageRequest) Descriptor() ([]byte, []int) { return file_proto_chats_proto_rawDescGZIP(), []int{4} } -func (x *CreateMessageRequest) GetId() string { +func (x *SendMessageRequest) GetId() string { if x != nil { return x.Id } return "" } -func (x *CreateMessageRequest) GetChatId() string { +func (x *SendMessageRequest) GetChatId() string { if x != nil { return x.ChatId } return "" } -func (x *CreateMessageRequest) GetAuthorId() string { +func (x *SendMessageRequest) GetAuthorId() string { if x != nil { return x.AuthorId } return "" } -func (x *CreateMessageRequest) GetText() string { +func (x *SendMessageRequest) GetText() string { if x != nil { return x.Text } return "" } -type CreateMessageResponse struct { +type SendMessageResponse struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields @@ -342,8 +355,8 @@ type CreateMessageResponse struct { Message *Message `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` } -func (x *CreateMessageResponse) Reset() { - *x = CreateMessageResponse{} +func (x *SendMessageResponse) Reset() { + *x = SendMessageResponse{} if protoimpl.UnsafeEnabled { mi := &file_proto_chats_proto_msgTypes[5] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -351,13 +364,13 @@ func (x *CreateMessageResponse) Reset() { } } -func (x *CreateMessageResponse) String() string { +func (x *SendMessageResponse) String() string { return protoimpl.X.MessageStringOf(x) } -func (*CreateMessageResponse) ProtoMessage() {} +func (*SendMessageResponse) ProtoMessage() {} -func (x *CreateMessageResponse) ProtoReflect() protoreflect.Message { +func (x *SendMessageResponse) ProtoReflect() protoreflect.Message { mi := &file_proto_chats_proto_msgTypes[5] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -369,26 +382,32 @@ func (x *CreateMessageResponse) ProtoReflect() protoreflect.Message { return mi.MessageOf(x) } -// Deprecated: Use CreateMessageResponse.ProtoReflect.Descriptor instead. -func (*CreateMessageResponse) Descriptor() ([]byte, []int) { +// Deprecated: Use SendMessageResponse.ProtoReflect.Descriptor instead. +func (*SendMessageResponse) Descriptor() ([]byte, []int) { return file_proto_chats_proto_rawDescGZIP(), []int{5} } -func (x *CreateMessageResponse) GetMessage() *Message { +func (x *SendMessageResponse) GetMessage() *Message { if x != nil { return x.Message } return nil } +// List messages within a chat type ListMessagesRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - ChatId string `protobuf:"bytes,1,opt,name=chat_id,json=chatId,proto3" json:"chat_id,omitempty"` - SentBefore *timestamppb.Timestamp `protobuf:"bytes,2,opt,name=sent_before,json=sentBefore,proto3" json:"sent_before,omitempty"` - Limit *wrapperspb.Int32Value `protobuf:"bytes,3,opt,name=limit,proto3" json:"limit,omitempty"` + // unique id of the chat + ChatId string `protobuf:"bytes,1,opt,name=chat_id,json=chatId,proto3" json:"chat_id,omitempty"` + // limit the number of messages + Limit int64 `protobuf:"varint,2,opt,name=limit,proto3" json:"limit,omitempty"` + // offset for the messages + Offset int64 `protobuf:"varint,3,opt,name=offset,proto3" json:"offset,omitempty"` + // order "asc" or "desc" (defaults to reverse chronological) + Order string `protobuf:"bytes,4,opt,name=order,proto3" json:"order,omitempty"` } func (x *ListMessagesRequest) Reset() { @@ -430,18 +449,25 @@ func (x *ListMessagesRequest) GetChatId() string { return "" } -func (x *ListMessagesRequest) GetSentBefore() *timestamppb.Timestamp { - if x != nil { - return x.SentBefore - } - return nil -} - -func (x *ListMessagesRequest) GetLimit() *wrapperspb.Int32Value { +func (x *ListMessagesRequest) GetLimit() int64 { if x != nil { return x.Limit } - return nil + return 0 +} + +func (x *ListMessagesRequest) GetOffset() int64 { + if x != nil { + return x.Offset + } + return 0 +} + +func (x *ListMessagesRequest) GetOrder() string { + if x != nil { + return x.Order + } + return "" } type ListMessagesResponse struct { @@ -495,75 +521,65 @@ var File_proto_chats_proto protoreflect.FileDescriptor var file_proto_chats_proto_rawDesc = []byte{ 0x0a, 0x11, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x63, 0x68, 0x61, 0x74, 0x73, 0x1a, 0x1f, 0x67, 0x6f, 0x6f, 0x67, - 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x74, 0x69, 0x6d, 0x65, - 0x73, 0x74, 0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1e, 0x67, 0x6f, 0x6f, - 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x77, 0x72, 0x61, - 0x70, 0x70, 0x65, 0x72, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x6c, 0x0a, 0x04, 0x43, - 0x68, 0x61, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x02, 0x69, 0x64, 0x12, 0x19, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x73, 0x18, - 0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x73, 0x12, 0x39, - 0x0a, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x03, 0x20, 0x01, - 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x09, - 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x22, 0x98, 0x01, 0x0a, 0x07, 0x4d, 0x65, - 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1b, 0x0a, 0x09, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x5f, - 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, - 0x49, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x63, 0x68, 0x61, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x03, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x68, 0x61, 0x74, 0x49, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x74, - 0x65, 0x78, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x12, - 0x33, 0x0a, 0x07, 0x73, 0x65, 0x6e, 0x74, 0x5f, 0x61, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, - 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, - 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x06, 0x73, 0x65, - 0x6e, 0x74, 0x41, 0x74, 0x22, 0x2e, 0x0a, 0x11, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, - 0x61, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x19, 0x0a, 0x08, 0x75, 0x73, 0x65, - 0x72, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x75, 0x73, 0x65, - 0x72, 0x49, 0x64, 0x73, 0x22, 0x35, 0x0a, 0x12, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, - 0x61, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1f, 0x0a, 0x04, 0x63, 0x68, - 0x61, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, - 0x2e, 0x43, 0x68, 0x61, 0x74, 0x52, 0x04, 0x63, 0x68, 0x61, 0x74, 0x22, 0x70, 0x0a, 0x14, 0x43, - 0x72, 0x65, 0x61, 0x74, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x02, 0x69, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x63, 0x68, 0x61, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x02, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x68, 0x61, 0x74, 0x49, 0x64, 0x12, 0x1b, 0x0a, 0x09, - 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x08, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x49, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, - 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x22, 0x41, 0x0a, - 0x15, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, - 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x28, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, - 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, - 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, - 0x22, 0x9e, 0x01, 0x0a, 0x13, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, - 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x17, 0x0a, 0x07, 0x63, 0x68, 0x61, 0x74, - 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x68, 0x61, 0x74, 0x49, - 0x64, 0x12, 0x3b, 0x0a, 0x0b, 0x73, 0x65, 0x6e, 0x74, 0x5f, 0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, - 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, - 0x6d, 0x70, 0x52, 0x0a, 0x73, 0x65, 0x6e, 0x74, 0x42, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x12, 0x31, - 0x0a, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1b, 0x2e, - 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, - 0x49, 0x6e, 0x74, 0x33, 0x32, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x52, 0x05, 0x6c, 0x69, 0x6d, 0x69, - 0x74, 0x22, 0x42, 0x0a, 0x14, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, - 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x2a, 0x0a, 0x08, 0x6d, 0x65, 0x73, - 0x73, 0x61, 0x67, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x63, 0x68, - 0x61, 0x74, 0x73, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x08, 0x6d, 0x65, 0x73, - 0x73, 0x61, 0x67, 0x65, 0x73, 0x32, 0xdf, 0x01, 0x0a, 0x05, 0x43, 0x68, 0x61, 0x74, 0x73, 0x12, - 0x41, 0x0a, 0x0a, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, 0x12, 0x18, 0x2e, - 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, - 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, - 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, - 0x73, 0x65, 0x12, 0x4a, 0x0a, 0x0d, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x4d, 0x65, 0x73, 0x73, - 0x61, 0x67, 0x65, 0x12, 0x1b, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x43, 0x72, 0x65, 0x61, - 0x74, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x1a, 0x1c, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x4d, - 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x47, - 0x0a, 0x0c, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x12, 0x1a, - 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, - 0x67, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1b, 0x2e, 0x63, 0x68, 0x61, - 0x74, 0x73, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x52, - 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x0f, 0x5a, 0x0d, 0x2e, 0x2f, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x3b, 0x63, 0x68, 0x61, 0x74, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x63, 0x68, 0x61, 0x74, 0x73, 0x22, 0x50, 0x0a, 0x04, 0x43, 0x68, + 0x61, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, + 0x69, 0x64, 0x12, 0x19, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x02, + 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x73, 0x12, 0x1d, 0x0a, + 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, + 0x03, 0x52, 0x09, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x22, 0x7c, 0x0a, 0x07, + 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1b, 0x0a, 0x09, 0x61, 0x75, 0x74, 0x68, 0x6f, + 0x72, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x61, 0x75, 0x74, 0x68, + 0x6f, 0x72, 0x49, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x63, 0x68, 0x61, 0x74, 0x5f, 0x69, 0x64, 0x18, + 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x68, 0x61, 0x74, 0x49, 0x64, 0x12, 0x12, 0x0a, + 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, + 0x74, 0x12, 0x17, 0x0a, 0x07, 0x73, 0x65, 0x6e, 0x74, 0x5f, 0x61, 0x74, 0x18, 0x05, 0x20, 0x01, + 0x28, 0x03, 0x52, 0x06, 0x73, 0x65, 0x6e, 0x74, 0x41, 0x74, 0x22, 0x3e, 0x0a, 0x11, 0x43, 0x72, + 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, + 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, + 0x19, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, + 0x09, 0x52, 0x07, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x73, 0x22, 0x35, 0x0a, 0x12, 0x43, 0x72, + 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, + 0x12, 0x1f, 0x0a, 0x04, 0x63, 0x68, 0x61, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, + 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x43, 0x68, 0x61, 0x74, 0x52, 0x04, 0x63, 0x68, 0x61, + 0x74, 0x22, 0x6e, 0x0a, 0x12, 0x53, 0x65, 0x6e, 0x64, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x63, 0x68, 0x61, 0x74, 0x5f, + 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x68, 0x61, 0x74, 0x49, 0x64, + 0x12, 0x1b, 0x0a, 0x09, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x03, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x08, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x49, 0x64, 0x12, 0x12, 0x0a, + 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, + 0x74, 0x22, 0x3f, 0x0a, 0x13, 0x53, 0x65, 0x6e, 0x64, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, + 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x28, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, + 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x63, 0x68, 0x61, 0x74, + 0x73, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, + 0x67, 0x65, 0x22, 0x72, 0x0a, 0x13, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x17, 0x0a, 0x07, 0x63, 0x68, 0x61, + 0x74, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x68, 0x61, 0x74, + 0x49, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, + 0x03, 0x52, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x6f, 0x66, 0x66, 0x73, + 0x65, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, + 0x12, 0x14, 0x0a, 0x05, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x05, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x22, 0x42, 0x0a, 0x14, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, + 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x2a, + 0x0a, 0x08, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, + 0x32, 0x0e, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, + 0x52, 0x08, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x32, 0xd9, 0x01, 0x0a, 0x05, 0x43, + 0x68, 0x61, 0x74, 0x73, 0x12, 0x41, 0x0a, 0x0a, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, + 0x61, 0x74, 0x12, 0x18, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, + 0x65, 0x43, 0x68, 0x61, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x63, + 0x68, 0x61, 0x74, 0x73, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x44, 0x0a, 0x0b, 0x53, 0x65, 0x6e, 0x64, 0x4d, + 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x19, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x53, + 0x65, 0x6e, 0x64, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x1a, 0x1a, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x53, 0x65, 0x6e, 0x64, 0x4d, 0x65, + 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x47, 0x0a, + 0x0c, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x12, 0x1a, 0x2e, + 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1b, 0x2e, 0x63, 0x68, 0x61, 0x74, + 0x73, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x0f, 0x5a, 0x0d, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x3b, 0x63, 0x68, 0x61, 0x74, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -580,36 +596,30 @@ func file_proto_chats_proto_rawDescGZIP() []byte { var file_proto_chats_proto_msgTypes = make([]protoimpl.MessageInfo, 8) var file_proto_chats_proto_goTypes = []interface{}{ - (*Chat)(nil), // 0: chats.Chat - (*Message)(nil), // 1: chats.Message - (*CreateChatRequest)(nil), // 2: chats.CreateChatRequest - (*CreateChatResponse)(nil), // 3: chats.CreateChatResponse - (*CreateMessageRequest)(nil), // 4: chats.CreateMessageRequest - (*CreateMessageResponse)(nil), // 5: chats.CreateMessageResponse - (*ListMessagesRequest)(nil), // 6: chats.ListMessagesRequest - (*ListMessagesResponse)(nil), // 7: chats.ListMessagesResponse - (*timestamppb.Timestamp)(nil), // 8: google.protobuf.Timestamp - (*wrapperspb.Int32Value)(nil), // 9: google.protobuf.Int32Value + (*Chat)(nil), // 0: chats.Chat + (*Message)(nil), // 1: chats.Message + (*CreateChatRequest)(nil), // 2: chats.CreateChatRequest + (*CreateChatResponse)(nil), // 3: chats.CreateChatResponse + (*SendMessageRequest)(nil), // 4: chats.SendMessageRequest + (*SendMessageResponse)(nil), // 5: chats.SendMessageResponse + (*ListMessagesRequest)(nil), // 6: chats.ListMessagesRequest + (*ListMessagesResponse)(nil), // 7: chats.ListMessagesResponse } var file_proto_chats_proto_depIdxs = []int32{ - 8, // 0: chats.Chat.created_at:type_name -> google.protobuf.Timestamp - 8, // 1: chats.Message.sent_at:type_name -> google.protobuf.Timestamp - 0, // 2: chats.CreateChatResponse.chat:type_name -> chats.Chat - 1, // 3: chats.CreateMessageResponse.message:type_name -> chats.Message - 8, // 4: chats.ListMessagesRequest.sent_before:type_name -> google.protobuf.Timestamp - 9, // 5: chats.ListMessagesRequest.limit:type_name -> google.protobuf.Int32Value - 1, // 6: chats.ListMessagesResponse.messages:type_name -> chats.Message - 2, // 7: chats.Chats.CreateChat:input_type -> chats.CreateChatRequest - 4, // 8: chats.Chats.CreateMessage:input_type -> chats.CreateMessageRequest - 6, // 9: chats.Chats.ListMessages:input_type -> chats.ListMessagesRequest - 3, // 10: chats.Chats.CreateChat:output_type -> chats.CreateChatResponse - 5, // 11: chats.Chats.CreateMessage:output_type -> chats.CreateMessageResponse - 7, // 12: chats.Chats.ListMessages:output_type -> chats.ListMessagesResponse - 10, // [10:13] is the sub-list for method output_type - 7, // [7:10] is the sub-list for method input_type - 7, // [7:7] is the sub-list for extension type_name - 7, // [7:7] is the sub-list for extension extendee - 0, // [0:7] is the sub-list for field type_name + 0, // 0: chats.CreateChatResponse.chat:type_name -> chats.Chat + 1, // 1: chats.SendMessageResponse.message:type_name -> chats.Message + 1, // 2: chats.ListMessagesResponse.messages:type_name -> chats.Message + 2, // 3: chats.Chats.CreateChat:input_type -> chats.CreateChatRequest + 4, // 4: chats.Chats.SendMessage:input_type -> chats.SendMessageRequest + 6, // 5: chats.Chats.ListMessages:input_type -> chats.ListMessagesRequest + 3, // 6: chats.Chats.CreateChat:output_type -> chats.CreateChatResponse + 5, // 7: chats.Chats.SendMessage:output_type -> chats.SendMessageResponse + 7, // 8: chats.Chats.ListMessages:output_type -> chats.ListMessagesResponse + 6, // [6:9] is the sub-list for method output_type + 3, // [3:6] is the sub-list for method input_type + 3, // [3:3] is the sub-list for extension type_name + 3, // [3:3] is the sub-list for extension extendee + 0, // [0:3] is the sub-list for field type_name } func init() { file_proto_chats_proto_init() } @@ -667,7 +677,7 @@ func file_proto_chats_proto_init() { } } file_proto_chats_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*CreateMessageRequest); i { + switch v := v.(*SendMessageRequest); i { case 0: return &v.state case 1: @@ -679,7 +689,7 @@ func file_proto_chats_proto_init() { } } file_proto_chats_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*CreateMessageResponse); i { + switch v := v.(*SendMessageResponse); i { case 0: return &v.state case 1: diff --git a/chats/proto/chats.pb.micro.go b/chats/proto/chats.pb.micro.go index c6fb2a8..3297be0 100644 --- a/chats/proto/chats.pb.micro.go +++ b/chats/proto/chats.pb.micro.go @@ -6,8 +6,6 @@ package chats import ( fmt "fmt" proto "github.com/golang/protobuf/proto" - _ "google.golang.org/protobuf/types/known/timestamppb" - _ "google.golang.org/protobuf/types/known/wrapperspb" math "math" ) @@ -48,7 +46,7 @@ type ChatsService interface { // chat will be returned CreateChat(ctx context.Context, in *CreateChatRequest, opts ...client.CallOption) (*CreateChatResponse, error) // Create a message within a chat - CreateMessage(ctx context.Context, in *CreateMessageRequest, opts ...client.CallOption) (*CreateMessageResponse, error) + SendMessage(ctx context.Context, in *SendMessageRequest, opts ...client.CallOption) (*SendMessageResponse, error) // List the messages within a chat in reverse chronological order, using sent_before to // offset as older messages need to be loaded ListMessages(ctx context.Context, in *ListMessagesRequest, opts ...client.CallOption) (*ListMessagesResponse, error) @@ -76,9 +74,9 @@ func (c *chatsService) CreateChat(ctx context.Context, in *CreateChatRequest, op return out, nil } -func (c *chatsService) CreateMessage(ctx context.Context, in *CreateMessageRequest, opts ...client.CallOption) (*CreateMessageResponse, error) { - req := c.c.NewRequest(c.name, "Chats.CreateMessage", in) - out := new(CreateMessageResponse) +func (c *chatsService) SendMessage(ctx context.Context, in *SendMessageRequest, opts ...client.CallOption) (*SendMessageResponse, error) { + req := c.c.NewRequest(c.name, "Chats.SendMessage", in) + out := new(SendMessageResponse) err := c.c.Call(ctx, req, out, opts...) if err != nil { return nil, err @@ -103,7 +101,7 @@ type ChatsHandler interface { // chat will be returned CreateChat(context.Context, *CreateChatRequest, *CreateChatResponse) error // Create a message within a chat - CreateMessage(context.Context, *CreateMessageRequest, *CreateMessageResponse) error + SendMessage(context.Context, *SendMessageRequest, *SendMessageResponse) error // List the messages within a chat in reverse chronological order, using sent_before to // offset as older messages need to be loaded ListMessages(context.Context, *ListMessagesRequest, *ListMessagesResponse) error @@ -112,7 +110,7 @@ type ChatsHandler interface { func RegisterChatsHandler(s server.Server, hdlr ChatsHandler, opts ...server.HandlerOption) error { type chats interface { CreateChat(ctx context.Context, in *CreateChatRequest, out *CreateChatResponse) error - CreateMessage(ctx context.Context, in *CreateMessageRequest, out *CreateMessageResponse) error + SendMessage(ctx context.Context, in *SendMessageRequest, out *SendMessageResponse) error ListMessages(ctx context.Context, in *ListMessagesRequest, out *ListMessagesResponse) error } type Chats struct { @@ -130,8 +128,8 @@ func (h *chatsHandler) CreateChat(ctx context.Context, in *CreateChatRequest, ou return h.ChatsHandler.CreateChat(ctx, in, out) } -func (h *chatsHandler) CreateMessage(ctx context.Context, in *CreateMessageRequest, out *CreateMessageResponse) error { - return h.ChatsHandler.CreateMessage(ctx, in, out) +func (h *chatsHandler) SendMessage(ctx context.Context, in *SendMessageRequest, out *SendMessageResponse) error { + return h.ChatsHandler.SendMessage(ctx, in, out) } func (h *chatsHandler) ListMessages(ctx context.Context, in *ListMessagesRequest, out *ListMessagesResponse) error { diff --git a/chats/proto/chats.proto b/chats/proto/chats.proto index 8d8343a..3d24ca0 100644 --- a/chats/proto/chats.proto +++ b/chats/proto/chats.proto @@ -4,57 +4,73 @@ package chats; option go_package = "./proto;chats"; -import "google/protobuf/timestamp.proto"; -import "google/protobuf/wrappers.proto"; - service Chats { // Create a chat between two or more users, if a chat already exists for these users, the existing // chat will be returned rpc CreateChat(CreateChatRequest) returns (CreateChatResponse); // Create a message within a chat - rpc CreateMessage(CreateMessageRequest) returns (CreateMessageResponse); + rpc SendMessage(SendMessageRequest) returns (SendMessageResponse); // List the messages within a chat in reverse chronological order, using sent_before to // offset as older messages need to be loaded rpc ListMessages(ListMessagesRequest) returns (ListMessagesResponse); } message Chat { + // unique id of the chat string id = 1; + // list of users in the chat repeated string user_ids = 2; - google.protobuf.Timestamp created_at = 3; + // unix nanosecond timestamp + int64 created_at = 3; } message Message { + // unique id of the message string id = 1; + // user id of the message string author_id = 2; + // chat id the message belongs to string chat_id = 3; + // text within the message string text = 4; - google.protobuf.Timestamp sent_at = 5; + // unix nanosecond timestamp + int64 sent_at = 5; } +// Create a new chat between mulitple users message CreateChatRequest { - repeated string user_ids = 1; + // The chat ID + string id = 1; + // List of users in the chat + repeated string user_ids = 2; } message CreateChatResponse { Chat chat = 1; } -message CreateMessageRequest { +// Send a message to a chat room +message SendMessageRequest { string id = 1; string chat_id = 2; string author_id = 3; string text = 4; } -message CreateMessageResponse { +message SendMessageResponse { Message message = 1; } +// List messages within a chat message ListMessagesRequest { + // unique id of the chat string chat_id = 1; - google.protobuf.Timestamp sent_before = 2; - google.protobuf.Int32Value limit = 3; + // limit the number of messages + int64 limit = 2; + // offset for the messages + int64 offset = 3; + // order "asc" or "desc" (defaults to reverse chronological) + string order = 4; } message ListMessagesResponse { diff --git a/go.mod b/go.mod index 39b9db2..641fb5b 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/hailocab/go-geoindex v0.0.0-20160127134810-64631bfe9711 github.com/jackc/pgx/v4 v4.10.1 github.com/micro/dev v0.0.0-20201117163752-d3cfc9788dfa - github.com/micro/micro/v3 v3.2.2-0.20210502173659-359ab425002e + github.com/micro/micro/v3 v3.2.2-0.20210507154259-3dd1cd29e797 github.com/miekg/dns v1.1.31 // indirect github.com/nats-io/nats-streaming-server v0.21.1 github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33 diff --git a/go.sum b/go.sum index 1f1b907..b95ee0d 100644 --- a/go.sum +++ b/go.sum @@ -400,6 +400,14 @@ github.com/micro/dev v0.0.0-20201117163752-d3cfc9788dfa/go.mod h1:j/8E+ezN/ij7a9 github.com/micro/micro/v3 v3.0.0-beta.6.0.20201016094841-ca8ffd563b2b/go.mod h1:RPJTp9meQAppzW/9jgQtfJmPpRJAySVPbz9uur4B3Ko= github.com/micro/micro/v3 v3.2.2-0.20210502173659-359ab425002e h1:o58+WWFR5l85vktSL/mQQFrGwG88PPyeuGlaQF348yU= github.com/micro/micro/v3 v3.2.2-0.20210502173659-359ab425002e/go.mod h1:3jH/R5iGtpMyeKH09iHt3KIw8gSFN7aW3W4iGOWUucE= +github.com/micro/micro/v3 v3.2.2-0.20210506130842-249062b573c2 h1:ZBp8IJ2fjWq1OlFjTEMBN4kVXoYEbHeXPMJ1z43Igl8= +github.com/micro/micro/v3 v3.2.2-0.20210506130842-249062b573c2/go.mod h1:3jH/R5iGtpMyeKH09iHt3KIw8gSFN7aW3W4iGOWUucE= +github.com/micro/micro/v3 v3.2.2-0.20210507151653-da4405f8310f h1:MUgQhZj5BrIEPVliGNwV5flQQoMSvCXyirgS3kzAAwI= +github.com/micro/micro/v3 v3.2.2-0.20210507151653-da4405f8310f/go.mod h1:3jH/R5iGtpMyeKH09iHt3KIw8gSFN7aW3W4iGOWUucE= +github.com/micro/micro/v3 v3.2.2-0.20210507153404-9b089a4eaf70 h1:OxjaaG+dR7kIRl5mpXTR6WMmuVYGgnwK57mKHIaV5uA= +github.com/micro/micro/v3 v3.2.2-0.20210507153404-9b089a4eaf70/go.mod h1:3jH/R5iGtpMyeKH09iHt3KIw8gSFN7aW3W4iGOWUucE= +github.com/micro/micro/v3 v3.2.2-0.20210507154259-3dd1cd29e797 h1:0j1z+nxdjv2ImkiEa4qvm3r39yqKKW0663G1JPz7eTw= +github.com/micro/micro/v3 v3.2.2-0.20210507154259-3dd1cd29e797/go.mod h1:3jH/R5iGtpMyeKH09iHt3KIw8gSFN7aW3W4iGOWUucE= github.com/miekg/dns v1.1.15/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.27/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/miekg/dns v1.1.31 h1:sJFOl9BgwbYAWOGEwr61FU28pqsBNdpRBnhGXtO06Oo= diff --git a/image/generate.go b/image/generate.go index 96f431a..7d9db91 100644 --- a/image/generate.go +++ b/image/generate.go @@ -1,2 +1,3 @@ package main + //go:generate make proto diff --git a/pkg/tenant/tenant.go b/pkg/tenant/tenant.go index 46cb708..42080aa 100644 --- a/pkg/tenant/tenant.go +++ b/pkg/tenant/tenant.go @@ -19,10 +19,22 @@ func FromContext(ctx context.Context) (string, bool) { // FromAccount returns a tenant from func FromAccount(acc *auth.Account) string { + id := acc.ID + issuer := acc.Issuer owner := acc.Metadata["apikey_owner"] - if len(owner) == 0 { - owner = acc.ID + + if len(id) == 0 { + id = "micro" } + + if len(issuer) == 0 { + issuer = "micro" + } + + if len(owner) == 0 { + owner = id + } + return fmt.Sprintf("%s/%s", acc.Issuer, owner) }