* stash

* replace chats sql with store

* strip unused method
This commit is contained in:
Asim Aslam
2021-05-07 17:09:51 +01:00
committed by GitHub
parent 0ad35b9340
commit 542c105037
15 changed files with 410 additions and 315 deletions

View File

@@ -1,14 +1,16 @@
package handler package handler
import ( import (
"encoding/json" "context"
"fmt"
"sort"
"strings"
"time" "time"
pb "github.com/micro/services/chats/proto" 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" "github.com/micro/micro/v3/service/errors"
"google.golang.org/protobuf/types/known/timestamppb"
) )
var ( var (
@@ -21,13 +23,12 @@ var (
) )
type Chats struct { type Chats struct {
gorm.Helper
Time func() time.Time Time func() time.Time
} }
type Chat struct { type Chat struct {
ID string ID string
UserIDs string `gorm:"uniqueIndex"` // sorted json array UserIDs []string
CreatedAt time.Time CreatedAt time.Time
} }
@@ -45,17 +46,69 @@ func (m *Message) Serialize() *pb.Message {
AuthorId: m.AuthorID, AuthorId: m.AuthorID,
ChatId: m.ChatID, ChatId: m.ChatID,
Text: m.Text, 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 { func (c *Chat) Serialize() *pb.Chat {
var userIDs []string
json.Unmarshal([]byte(c.UserIDs), &userIDs)
return &pb.Chat{ return &pb.Chat{
Id: c.ID, Id: c.ID,
UserIds: userIDs, UserIds: c.UserIDs,
CreatedAt: timestamppb.New(c.CreatedAt), CreatedAt: c.CreatedAt.UnixNano(),
} }
} }

View File

@@ -2,38 +2,20 @@ package handler_test
import ( import (
"context" "context"
"database/sql"
"os"
"testing" "testing"
"time" "time"
"github.com/micro/micro/v3/service/auth" "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" "github.com/micro/services/chats/handler"
pb "github.com/micro/services/chats/proto" pb "github.com/micro/services/chats/proto"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/golang/protobuf/ptypes/timestamp"
) )
func testHandler(t *testing.T) *handler.Chats { func testHandler(t *testing.T) *handler.Chats {
// connect to the database store.DefaultStore = memory.NewStore()
addr := os.Getenv("POSTGRES_URL") return &handler.Chats{Time: func() time.Time { return time.Unix(1611327673, 0) }}
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
} }
func assertChatsMatch(t *testing.T, exp, act *pb.Chat) { 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) assert.Equal(t, exp.UserIds, act.UserIds)
if act.CreatedAt == nil { if act.CreatedAt == 0 {
t.Errorf("CreatedAt not set") t.Errorf("CreatedAt not set")
return return
} }
assert.True(t, microSecondTime(exp.CreatedAt).Equal(microSecondTime(act.CreatedAt))) assert.True(t, exp.CreatedAt == 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))
} }
func assertMessagesMatch(t *testing.T, exp, act *pb.Message) { 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.AuthorId, act.AuthorId)
assert.Equal(t, exp.ChatId, act.ChatId) assert.Equal(t, exp.ChatId, act.ChatId)
if act.SentAt == nil { if act.SentAt == 0 {
t.Errorf("SentAt not set") t.Errorf("SentAt not set")
return return
} }
assert.True(t, microSecondTime(exp.SentAt).Equal(microSecondTime(act.SentAt)))
assert.True(t, exp.SentAt == act.SentAt)
} }
func microAccountCtx() context.Context { func microAccountCtx() context.Context {

View File

@@ -2,8 +2,6 @@ package handler
import ( import (
"context" "context"
"encoding/json"
"regexp"
"sort" "sort"
"time" "time"
@@ -11,6 +9,7 @@ import (
"github.com/micro/micro/v3/service/auth" "github.com/micro/micro/v3/service/auth"
"github.com/micro/micro/v3/service/errors" "github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger" "github.com/micro/micro/v3/service/logger"
"github.com/micro/micro/v3/service/store"
pb "github.com/micro/services/chats/proto" pb "github.com/micro/services/chats/proto"
) )
@@ -26,43 +25,56 @@ func (c *Chats) CreateChat(ctx context.Context, req *pb.CreateChatRequest, rsp *
return ErrMissingUserIDs return ErrMissingUserIDs
} }
// sort the user ids and then marshal to json // sort the user ids
sort.Strings(req.UserIds) sort.Strings(req.UserIds)
bytes, err := json.Marshal(req.UserIds)
if err != nil { id := uuid.New().String()
logger.Errorf("Error mashaling user ids: %v", err) if len(req.Id) > 0 {
return errors.InternalServerError("ENCODING_ERROR", "Error encoding user ids") id = req.Id
} }
// construct the chat // construct the chat
chat := Chat{ chat := &Chat{
ID: uuid.New().String(), ID: id,
CreatedAt: time.Now(), CreatedAt: time.Now(),
UserIDs: string(bytes), UserIDs: req.UserIds,
} }
db, err := c.GetDBConn(ctx) // read the chat by the unique composition of ids
if err != nil { recs, err := store.Read(chat.Key(ctx), store.ReadLimit(1))
logger.Errorf("Error connecting to DB: %v", err) if err == nil && len(recs) == 1 {
return errors.InternalServerError("DB_ERROR", "Error connecting to DB") // found an existing record
} recs[0].Decode(&chat)
// write to the database, if we get a unique key error, the chat already exists
err = db.Create(&chat).Error
if err == nil {
rsp.Chat = chat.Serialize() rsp.Chat = chat.Serialize()
return nil 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) logger.Errorf("Error creating chat: %v", err)
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database") return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
} }
var existing Chat // write the user composite key
if err := db.Where(&Chat{UserIDs: chat.UserIDs}).First(&existing).Error; err != nil { newRec = store.NewRecord(chat.Index(ctx), chat)
logger.Errorf("Error reading chat: %v", err) if err := store.Write(newRec); err != nil {
logger.Errorf("Error creating chat: %v", err)
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database") return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
} }
rsp.Chat = existing.Serialize()
// return the record
rsp.Chat = chat.Serialize()
return nil return nil
} }

View File

@@ -2,18 +2,17 @@ package handler
import ( import (
"context" "context"
"strings"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/micro/micro/v3/service/auth" "github.com/micro/micro/v3/service/auth"
"github.com/micro/micro/v3/service/errors" "github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger" "github.com/micro/micro/v3/service/logger"
"github.com/micro/micro/v3/service/store"
pb "github.com/micro/services/chats/proto" pb "github.com/micro/services/chats/proto"
"gorm.io/gorm"
) )
// Create a message within a chat // 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) _, ok := auth.AccountFromContext(ctx)
if !ok { if !ok {
errors.Unauthorized("UNAUTHORIZED", "Unauthorized") errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
@@ -29,14 +28,12 @@ func (c *Chats) CreateMessage(ctx context.Context, req *pb.CreateMessageRequest,
return ErrMissingText return ErrMissingText
} }
db, err := c.GetDBConn(ctx) chat := &Chat{
if err != nil { ID: req.ChatId,
logger.Errorf("Error connecting to DB: %v", err)
return errors.InternalServerError("DB_ERROR", "Error connecting to DB")
} }
// lookup the chat
var conv Chat recs, err := store.Read(chat.Key(ctx), store.ReadLimit(1))
if err := db.Where(&Chat{ID: req.ChatId}).First(&conv).Error; err == gorm.ErrRecordNotFound { if err == store.ErrNotFound {
return ErrNotFound return ErrNotFound
} else if err != nil { } else if err != nil {
logger.Errorf("Error reading chat: %v", err) logger.Errorf("Error reading chat: %v", err)
@@ -46,28 +43,45 @@ func (c *Chats) CreateMessage(ctx context.Context, req *pb.CreateMessageRequest,
// create the message // create the message
msg := &Message{ msg := &Message{
ID: req.Id, ID: req.Id,
SentAt: c.Time(),
Text: req.Text, Text: req.Text,
AuthorID: req.AuthorId, AuthorID: req.AuthorId,
ChatID: req.ChatId, ChatID: req.ChatId,
SentAt: c.Time(),
} }
if len(msg.ID) == 0 { if len(msg.ID) == 0 {
msg.ID = uuid.New().String() 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() rsp.Message = msg.Serialize()
return nil 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) logger.Errorf("Error creating message: %v", err)
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database") return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
} }
// a message already exists with this id // otherwise write the record
var existing Message if err := store.Write(store.NewRecord(msg.Key(ctx), msg)); err != nil {
if err := db.Where(&Message{ID: msg.ID}).First(&existing).Error; err != nil {
logger.Errorf("Error creating message: %v", err) logger.Errorf("Error creating message: %v", err)
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database") 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 return nil
} }

View File

@@ -5,13 +5,12 @@ import (
"github.com/micro/services/chats/handler" "github.com/micro/services/chats/handler"
pb "github.com/micro/services/chats/proto" pb "github.com/micro/services/chats/proto"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestCreateMessage(t *testing.T) { func TestSendMessage(t *testing.T) {
h := testHandler(t) h := testHandler(t)
// seed some data // seed some data
@@ -82,8 +81,8 @@ func TestCreateMessage(t *testing.T) {
for _, tc := range tt { for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
var rsp pb.CreateMessageResponse var rsp pb.SendMessageResponse
err := h.CreateMessage(microAccountCtx(), &pb.CreateMessageRequest{ err := h.SendMessage(microAccountCtx(), &pb.SendMessageRequest{
AuthorId: tc.AuthorID, AuthorId: tc.AuthorID,
ChatId: tc.ChatID, ChatId: tc.ChatID,
Text: tc.Text, Text: tc.Text,
@@ -99,7 +98,7 @@ func TestCreateMessage(t *testing.T) {
assertMessagesMatch(t, &pb.Message{ assertMessagesMatch(t, &pb.Message{
AuthorId: tc.AuthorID, AuthorId: tc.AuthorID,
ChatId: tc.ChatID, ChatId: tc.ChatID,
SentAt: timestamppb.New(h.Time()), SentAt: h.Time().UnixNano(),
Text: tc.Text, Text: tc.Text,
Id: tc.ID, Id: tc.ID,
}, rsp.Message) }, rsp.Message)

View File

@@ -6,6 +6,7 @@ import (
"github.com/micro/micro/v3/service/auth" "github.com/micro/micro/v3/service/auth"
"github.com/micro/micro/v3/service/errors" "github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger" "github.com/micro/micro/v3/service/logger"
"github.com/micro/micro/v3/service/store"
pb "github.com/micro/services/chats/proto" pb "github.com/micro/services/chats/proto"
) )
@@ -23,33 +24,46 @@ func (c *Chats) ListMessages(ctx context.Context, req *pb.ListMessagesRequest, r
return ErrMissingChatID return ErrMissingChatID
} }
db, err := c.GetDBConn(ctx) message := &Message{
if err != nil { ChatID: req.ChatId,
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)
} }
// execute the query // default order is descending
var msgs []Message order := store.OrderDesc
if err := q.Find(&msgs).Error; err != nil { 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) logger.Errorf("Error reading messages: %v", err)
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database") return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
} }
// serialize the response // return all the messages
rsp.Messages = make([]*pb.Message, len(msgs)) for _, rec := range recs {
for i, m := range msgs { m := &Message{}
rsp.Messages[i] = m.Serialize() rec.Decode(&m)
if len(m.ID) == 0 || m.ChatID != req.ChatId {
continue
} }
rsp.Messages = append(rsp.Messages, m.Serialize())
}
return nil return nil
} }

View File

@@ -10,7 +10,6 @@ import (
"github.com/micro/services/chats/handler" "github.com/micro/services/chats/handler"
pb "github.com/micro/services/chats/proto" pb "github.com/micro/services/chats/proto"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/wrapperspb"
) )
func TestListMessages(t *testing.T) { func TestListMessages(t *testing.T) {
@@ -29,8 +28,8 @@ func TestListMessages(t *testing.T) {
msgs := make([]*pb.Message, 50) msgs := make([]*pb.Message, 50)
for i := 0; i < len(msgs); i++ { for i := 0; i < len(msgs); i++ {
var rsp pb.CreateMessageResponse var rsp pb.SendMessageResponse
err := h.CreateMessage(microAccountCtx(), &pb.CreateMessageRequest{ err := h.SendMessage(microAccountCtx(), &pb.SendMessageRequest{
ChatId: chatRsp.Chat.Id, ChatId: chatRsp.Chat.Id,
AuthorId: uuid.New().String(), AuthorId: uuid.New().String(),
Text: strconv.Itoa(i), Text: strconv.Itoa(i),
@@ -68,7 +67,7 @@ func TestListMessages(t *testing.T) {
var rsp pb.ListMessagesResponse var rsp pb.ListMessagesResponse
err := h.ListMessages(microAccountCtx(), &pb.ListMessagesRequest{ err := h.ListMessages(microAccountCtx(), &pb.ListMessagesRequest{
ChatId: chatRsp.Chat.Id, ChatId: chatRsp.Chat.Id,
Limit: &wrapperspb.Int32Value{Value: 10}, Limit: 10,
}, &rsp) }, &rsp)
assert.NoError(t, err) assert.NoError(t, err)
@@ -87,8 +86,8 @@ func TestListMessages(t *testing.T) {
var rsp pb.ListMessagesResponse var rsp pb.ListMessagesResponse
err := h.ListMessages(microAccountCtx(), &pb.ListMessagesRequest{ err := h.ListMessages(microAccountCtx(), &pb.ListMessagesRequest{
ChatId: chatRsp.Chat.Id, ChatId: chatRsp.Chat.Id,
Limit: &wrapperspb.Int32Value{Value: 5}, Limit: 5,
SentBefore: msgs[20].SentAt, Offset: 15,
}, &rsp) }, &rsp)
assert.NoError(t, err) 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)) t.Fatalf("Expected %v messages but got %v", 5, len(rsp.Messages))
return return
} }
expected := msgs[15:20] expected := msgs[30:35]
sortMessages(rsp.Messages) sortMessages(rsp.Messages)
for i, msg := range rsp.Messages { for i, msg := range rsp.Messages {
assertMessagesMatch(t, expected[i], msg) assertMessagesMatch(t, expected[i], msg)
@@ -107,9 +106,9 @@ func TestListMessages(t *testing.T) {
// sortMessages by the time they were sent // sortMessages by the time they were sent
func sortMessages(msgs []*pb.Message) { func sortMessages(msgs []*pb.Message) {
sort.Slice(msgs, func(i, j int) bool { 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 true
} }
return msgs[i].SentAt.AsTime().Before(msgs[j].SentAt.AsTime()) return msgs[i].SentAt < msgs[j].SentAt
}) })
} }

View File

@@ -1,21 +1,15 @@
package main package main
import ( import (
"database/sql"
"time" "time"
"github.com/micro/services/chats/handler" "github.com/micro/services/chats/handler"
pb "github.com/micro/services/chats/proto" pb "github.com/micro/services/chats/proto"
"github.com/micro/micro/v3/service" "github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/config"
"github.com/micro/micro/v3/service/logger" "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() { func main() {
// Create service // Create service
srv := service.New( srv := service.New(
@@ -23,19 +17,7 @@ func main() {
service.Version("latest"), 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 := &handler.Chats{Time: time.Now}
h.DBConn(sqlDB).Migrations(&handler.Chat{}, &handler.Message{})
// Register handler // Register handler
pb.RegisterChatsHandler(srv.Server(), h) pb.RegisterChatsHandler(srv.Server(), h)

View File

@@ -1,17 +1,14 @@
// Code generated by protoc-gen-go. DO NOT EDIT. // Code generated by protoc-gen-go. DO NOT EDIT.
// versions: // versions:
// protoc-gen-go v1.25.0 // protoc-gen-go v1.26.0
// protoc v3.15.5 // protoc v3.15.6
// source: proto/chats.proto // source: proto/chats.proto
package chats package chats
import ( import (
proto "github.com/golang/protobuf/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl" 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" reflect "reflect"
sync "sync" sync "sync"
) )
@@ -23,18 +20,17 @@ const (
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) _ = 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 { type Chat struct {
state protoimpl.MessageState state protoimpl.MessageState
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
// unique id of the chat
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` 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"` 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"` // unix nanosecond timestamp
CreatedAt int64 `protobuf:"varint,3,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"`
} }
func (x *Chat) Reset() { func (x *Chat) Reset() {
@@ -83,11 +79,11 @@ func (x *Chat) GetUserIds() []string {
return nil return nil
} }
func (x *Chat) GetCreatedAt() *timestamppb.Timestamp { func (x *Chat) GetCreatedAt() int64 {
if x != nil { if x != nil {
return x.CreatedAt return x.CreatedAt
} }
return nil return 0
} }
type Message struct { type Message struct {
@@ -95,11 +91,16 @@ type Message struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
// unique id of the message
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` 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"` 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"` 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"` 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"` // unix nanosecond timestamp
SentAt int64 `protobuf:"varint,5,opt,name=sent_at,json=sentAt,proto3" json:"sent_at,omitempty"`
} }
func (x *Message) Reset() { func (x *Message) Reset() {
@@ -162,19 +163,23 @@ func (x *Message) GetText() string {
return "" return ""
} }
func (x *Message) GetSentAt() *timestamppb.Timestamp { func (x *Message) GetSentAt() int64 {
if x != nil { if x != nil {
return x.SentAt return x.SentAt
} }
return nil return 0
} }
// Create a new chat between mulitple users
type CreateChatRequest struct { type CreateChatRequest struct {
state protoimpl.MessageState state protoimpl.MessageState
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields 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() { func (x *CreateChatRequest) Reset() {
@@ -209,6 +214,13 @@ func (*CreateChatRequest) Descriptor() ([]byte, []int) {
return file_proto_chats_proto_rawDescGZIP(), []int{2} 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 { func (x *CreateChatRequest) GetUserIds() []string {
if x != nil { if x != nil {
return x.UserIds return x.UserIds
@@ -263,7 +275,8 @@ func (x *CreateChatResponse) GetChat() *Chat {
return nil return nil
} }
type CreateMessageRequest struct { // Send a message to a chat room
type SendMessageRequest struct {
state protoimpl.MessageState state protoimpl.MessageState
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
@@ -274,8 +287,8 @@ type CreateMessageRequest struct {
Text string `protobuf:"bytes,4,opt,name=text,proto3" json:"text,omitempty"` Text string `protobuf:"bytes,4,opt,name=text,proto3" json:"text,omitempty"`
} }
func (x *CreateMessageRequest) Reset() { func (x *SendMessageRequest) Reset() {
*x = CreateMessageRequest{} *x = SendMessageRequest{}
if protoimpl.UnsafeEnabled { if protoimpl.UnsafeEnabled {
mi := &file_proto_chats_proto_msgTypes[4] mi := &file_proto_chats_proto_msgTypes[4]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 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) 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] mi := &file_proto_chats_proto_msgTypes[4]
if protoimpl.UnsafeEnabled && x != nil { if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
@@ -301,40 +314,40 @@ func (x *CreateMessageRequest) ProtoReflect() protoreflect.Message {
return mi.MessageOf(x) return mi.MessageOf(x)
} }
// Deprecated: Use CreateMessageRequest.ProtoReflect.Descriptor instead. // Deprecated: Use SendMessageRequest.ProtoReflect.Descriptor instead.
func (*CreateMessageRequest) Descriptor() ([]byte, []int) { func (*SendMessageRequest) Descriptor() ([]byte, []int) {
return file_proto_chats_proto_rawDescGZIP(), []int{4} return file_proto_chats_proto_rawDescGZIP(), []int{4}
} }
func (x *CreateMessageRequest) GetId() string { func (x *SendMessageRequest) GetId() string {
if x != nil { if x != nil {
return x.Id return x.Id
} }
return "" return ""
} }
func (x *CreateMessageRequest) GetChatId() string { func (x *SendMessageRequest) GetChatId() string {
if x != nil { if x != nil {
return x.ChatId return x.ChatId
} }
return "" return ""
} }
func (x *CreateMessageRequest) GetAuthorId() string { func (x *SendMessageRequest) GetAuthorId() string {
if x != nil { if x != nil {
return x.AuthorId return x.AuthorId
} }
return "" return ""
} }
func (x *CreateMessageRequest) GetText() string { func (x *SendMessageRequest) GetText() string {
if x != nil { if x != nil {
return x.Text return x.Text
} }
return "" return ""
} }
type CreateMessageResponse struct { type SendMessageResponse struct {
state protoimpl.MessageState state protoimpl.MessageState
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
@@ -342,8 +355,8 @@ type CreateMessageResponse struct {
Message *Message `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` Message *Message `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
} }
func (x *CreateMessageResponse) Reset() { func (x *SendMessageResponse) Reset() {
*x = CreateMessageResponse{} *x = SendMessageResponse{}
if protoimpl.UnsafeEnabled { if protoimpl.UnsafeEnabled {
mi := &file_proto_chats_proto_msgTypes[5] mi := &file_proto_chats_proto_msgTypes[5]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 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) 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] mi := &file_proto_chats_proto_msgTypes[5]
if protoimpl.UnsafeEnabled && x != nil { if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
@@ -369,26 +382,32 @@ func (x *CreateMessageResponse) ProtoReflect() protoreflect.Message {
return mi.MessageOf(x) return mi.MessageOf(x)
} }
// Deprecated: Use CreateMessageResponse.ProtoReflect.Descriptor instead. // Deprecated: Use SendMessageResponse.ProtoReflect.Descriptor instead.
func (*CreateMessageResponse) Descriptor() ([]byte, []int) { func (*SendMessageResponse) Descriptor() ([]byte, []int) {
return file_proto_chats_proto_rawDescGZIP(), []int{5} return file_proto_chats_proto_rawDescGZIP(), []int{5}
} }
func (x *CreateMessageResponse) GetMessage() *Message { func (x *SendMessageResponse) GetMessage() *Message {
if x != nil { if x != nil {
return x.Message return x.Message
} }
return nil return nil
} }
// List messages within a chat
type ListMessagesRequest struct { type ListMessagesRequest struct {
state protoimpl.MessageState state protoimpl.MessageState
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
// unique id of the chat
ChatId string `protobuf:"bytes,1,opt,name=chat_id,json=chatId,proto3" json:"chat_id,omitempty"` 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 the number of messages
Limit *wrapperspb.Int32Value `protobuf:"bytes,3,opt,name=limit,proto3" json:"limit,omitempty"` 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() { func (x *ListMessagesRequest) Reset() {
@@ -430,18 +449,25 @@ func (x *ListMessagesRequest) GetChatId() string {
return "" return ""
} }
func (x *ListMessagesRequest) GetSentBefore() *timestamppb.Timestamp { func (x *ListMessagesRequest) GetLimit() int64 {
if x != nil {
return x.SentBefore
}
return nil
}
func (x *ListMessagesRequest) GetLimit() *wrapperspb.Int32Value {
if x != nil { if x != nil {
return x.Limit 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 { type ListMessagesResponse struct {
@@ -495,75 +521,65 @@ var File_proto_chats_proto protoreflect.FileDescriptor
var file_proto_chats_proto_rawDesc = []byte{ var file_proto_chats_proto_rawDesc = []byte{
0x0a, 0x11, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x70, 0x72, 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, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x63, 0x68, 0x61, 0x74, 0x73, 0x22, 0x50, 0x0a, 0x04, 0x43, 0x68,
0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x74, 0x69, 0x6d, 0x65, 0x61, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02,
0x73, 0x74, 0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1e, 0x67, 0x6f, 0x6f, 0x69, 0x64, 0x12, 0x19, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x02,
0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x77, 0x72, 0x61, 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x73, 0x12, 0x1d, 0x0a,
0x70, 0x70, 0x65, 0x72, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x6c, 0x0a, 0x04, 0x43, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28,
0x68, 0x61, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x52, 0x09, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x22, 0x7c, 0x0a, 0x07,
0x02, 0x69, 0x64, 0x12, 0x19, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20,
0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x73, 0x12, 0x39, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1b, 0x0a, 0x09, 0x61, 0x75, 0x74, 0x68, 0x6f,
0x0a, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x03, 0x20, 0x01, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x61, 0x75, 0x74, 0x68,
0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x72, 0x49, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x63, 0x68, 0x61, 0x74, 0x5f, 0x69, 0x64, 0x18,
0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x09, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x68, 0x61, 0x74, 0x49, 0x64, 0x12, 0x12, 0x0a,
0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x22, 0x98, 0x01, 0x0a, 0x07, 0x4d, 0x65, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78,
0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x74, 0x12, 0x17, 0x0a, 0x07, 0x73, 0x65, 0x6e, 0x74, 0x5f, 0x61, 0x74, 0x18, 0x05, 0x20, 0x01,
0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1b, 0x0a, 0x09, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x5f, 0x28, 0x03, 0x52, 0x06, 0x73, 0x65, 0x6e, 0x74, 0x41, 0x74, 0x22, 0x3e, 0x0a, 0x11, 0x43, 0x72,
0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12,
0x49, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x63, 0x68, 0x61, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x03, 0x20, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12,
0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x68, 0x61, 0x74, 0x49, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x19, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28,
0x65, 0x78, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x12, 0x09, 0x52, 0x07, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x73, 0x22, 0x35, 0x0a, 0x12, 0x43, 0x72,
0x33, 0x0a, 0x07, 0x73, 0x65, 0x6e, 0x74, 0x5f, 0x61, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65,
0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x12, 0x1f, 0x0a, 0x04, 0x63, 0x68, 0x61, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b,
0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x06, 0x73, 0x65, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x43, 0x68, 0x61, 0x74, 0x52, 0x04, 0x63, 0x68, 0x61,
0x6e, 0x74, 0x41, 0x74, 0x22, 0x2e, 0x0a, 0x11, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x74, 0x22, 0x6e, 0x0a, 0x12, 0x53, 0x65, 0x6e, 0x64, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
0x61, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x19, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20,
0x72, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x75, 0x73, 0x65, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x63, 0x68, 0x61, 0x74, 0x5f,
0x72, 0x49, 0x64, 0x73, 0x22, 0x35, 0x0a, 0x12, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x68, 0x61, 0x74, 0x49, 0x64,
0x61, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1f, 0x0a, 0x04, 0x63, 0x68, 0x12, 0x1b, 0x0a, 0x09, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x03, 0x20,
0x61, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x01, 0x28, 0x09, 0x52, 0x08, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x49, 0x64, 0x12, 0x12, 0x0a,
0x2e, 0x43, 0x68, 0x61, 0x74, 0x52, 0x04, 0x63, 0x68, 0x61, 0x74, 0x22, 0x70, 0x0a, 0x14, 0x43, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78,
0x72, 0x65, 0x61, 0x74, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x71, 0x75, 0x74, 0x22, 0x3f, 0x0a, 0x13, 0x53, 0x65, 0x6e, 0x64, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x28, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73,
0x02, 0x69, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x63, 0x68, 0x61, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x63, 0x68, 0x61, 0x74,
0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x68, 0x61, 0x74, 0x49, 0x64, 0x12, 0x1b, 0x0a, 0x09, 0x73, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61,
0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x67, 0x65, 0x22, 0x72, 0x0a, 0x13, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67,
0x08, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x49, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x17, 0x0a, 0x07, 0x63, 0x68, 0x61,
0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x22, 0x41, 0x0a, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x68, 0x61, 0x74,
0x15, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x49, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28,
0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x28, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x03, 0x52, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x6f, 0x66, 0x66, 0x73,
0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x65, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74,
0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52,
0x22, 0x9e, 0x01, 0x0a, 0x13, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x05, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x22, 0x42, 0x0a, 0x14, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65,
0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x17, 0x0a, 0x07, 0x63, 0x68, 0x61, 0x74, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x2a,
0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x68, 0x61, 0x74, 0x49, 0x0a, 0x08, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b,
0x64, 0x12, 0x3b, 0x0a, 0x0b, 0x73, 0x65, 0x6e, 0x74, 0x5f, 0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x32, 0x0e, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x52, 0x08, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x32, 0xd9, 0x01, 0x0a, 0x05, 0x43,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x68, 0x61, 0x74, 0x73, 0x12, 0x41, 0x0a, 0x0a, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68,
0x6d, 0x70, 0x52, 0x0a, 0x73, 0x65, 0x6e, 0x74, 0x42, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x12, 0x31, 0x61, 0x74, 0x12, 0x18, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74,
0x0a, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x65, 0x43, 0x68, 0x61, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x63,
0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, 0x52,
0x49, 0x6e, 0x74, 0x33, 0x32, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x52, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x44, 0x0a, 0x0b, 0x53, 0x65, 0x6e, 0x64, 0x4d,
0x74, 0x22, 0x42, 0x0a, 0x14, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x19, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x53,
0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x2a, 0x0a, 0x08, 0x6d, 0x65, 0x73, 0x65, 0x6e, 0x64, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
0x73, 0x61, 0x67, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x63, 0x68, 0x74, 0x1a, 0x1a, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x53, 0x65, 0x6e, 0x64, 0x4d, 0x65,
0x61, 0x74, 0x73, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x08, 0x6d, 0x65, 0x73, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x47, 0x0a,
0x73, 0x61, 0x67, 0x65, 0x73, 0x32, 0xdf, 0x01, 0x0a, 0x05, 0x43, 0x68, 0x61, 0x74, 0x73, 0x12, 0x0c, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x12, 0x1a, 0x2e,
0x41, 0x0a, 0x0a, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, 0x12, 0x18, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67,
0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1b, 0x2e, 0x63, 0x68, 0x61, 0x74,
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x73, 0x2e, 0x73, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x52, 0x65,
0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x0f, 0x5a, 0x0d, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74,
0x73, 0x65, 0x12, 0x4a, 0x0a, 0x0d, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x6f, 0x3b, 0x63, 0x68, 0x61, 0x74, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
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,
} }
var ( var (
@@ -584,32 +600,26 @@ var file_proto_chats_proto_goTypes = []interface{}{
(*Message)(nil), // 1: chats.Message (*Message)(nil), // 1: chats.Message
(*CreateChatRequest)(nil), // 2: chats.CreateChatRequest (*CreateChatRequest)(nil), // 2: chats.CreateChatRequest
(*CreateChatResponse)(nil), // 3: chats.CreateChatResponse (*CreateChatResponse)(nil), // 3: chats.CreateChatResponse
(*CreateMessageRequest)(nil), // 4: chats.CreateMessageRequest (*SendMessageRequest)(nil), // 4: chats.SendMessageRequest
(*CreateMessageResponse)(nil), // 5: chats.CreateMessageResponse (*SendMessageResponse)(nil), // 5: chats.SendMessageResponse
(*ListMessagesRequest)(nil), // 6: chats.ListMessagesRequest (*ListMessagesRequest)(nil), // 6: chats.ListMessagesRequest
(*ListMessagesResponse)(nil), // 7: chats.ListMessagesResponse (*ListMessagesResponse)(nil), // 7: chats.ListMessagesResponse
(*timestamppb.Timestamp)(nil), // 8: google.protobuf.Timestamp
(*wrapperspb.Int32Value)(nil), // 9: google.protobuf.Int32Value
} }
var file_proto_chats_proto_depIdxs = []int32{ var file_proto_chats_proto_depIdxs = []int32{
8, // 0: chats.Chat.created_at:type_name -> google.protobuf.Timestamp 0, // 0: chats.CreateChatResponse.chat:type_name -> chats.Chat
8, // 1: chats.Message.sent_at:type_name -> google.protobuf.Timestamp 1, // 1: chats.SendMessageResponse.message:type_name -> chats.Message
0, // 2: chats.CreateChatResponse.chat:type_name -> chats.Chat 1, // 2: chats.ListMessagesResponse.messages:type_name -> chats.Message
1, // 3: chats.CreateMessageResponse.message:type_name -> chats.Message 2, // 3: chats.Chats.CreateChat:input_type -> chats.CreateChatRequest
8, // 4: chats.ListMessagesRequest.sent_before:type_name -> google.protobuf.Timestamp 4, // 4: chats.Chats.SendMessage:input_type -> chats.SendMessageRequest
9, // 5: chats.ListMessagesRequest.limit:type_name -> google.protobuf.Int32Value 6, // 5: chats.Chats.ListMessages:input_type -> chats.ListMessagesRequest
1, // 6: chats.ListMessagesResponse.messages:type_name -> chats.Message 3, // 6: chats.Chats.CreateChat:output_type -> chats.CreateChatResponse
2, // 7: chats.Chats.CreateChat:input_type -> chats.CreateChatRequest 5, // 7: chats.Chats.SendMessage:output_type -> chats.SendMessageResponse
4, // 8: chats.Chats.CreateMessage:input_type -> chats.CreateMessageRequest 7, // 8: chats.Chats.ListMessages:output_type -> chats.ListMessagesResponse
6, // 9: chats.Chats.ListMessages:input_type -> chats.ListMessagesRequest 6, // [6:9] is the sub-list for method output_type
3, // 10: chats.Chats.CreateChat:output_type -> chats.CreateChatResponse 3, // [3:6] is the sub-list for method input_type
5, // 11: chats.Chats.CreateMessage:output_type -> chats.CreateMessageResponse 3, // [3:3] is the sub-list for extension type_name
7, // 12: chats.Chats.ListMessages:output_type -> chats.ListMessagesResponse 3, // [3:3] is the sub-list for extension extendee
10, // [10:13] is the sub-list for method output_type 0, // [0:3] is the sub-list for field type_name
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
} }
func init() { file_proto_chats_proto_init() } 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{} { 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: case 0:
return &v.state return &v.state
case 1: 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{} { 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: case 0:
return &v.state return &v.state
case 1: case 1:

View File

@@ -6,8 +6,6 @@ package chats
import ( import (
fmt "fmt" fmt "fmt"
proto "github.com/golang/protobuf/proto" proto "github.com/golang/protobuf/proto"
_ "google.golang.org/protobuf/types/known/timestamppb"
_ "google.golang.org/protobuf/types/known/wrapperspb"
math "math" math "math"
) )
@@ -48,7 +46,7 @@ type ChatsService interface {
// chat will be returned // chat will be returned
CreateChat(ctx context.Context, in *CreateChatRequest, opts ...client.CallOption) (*CreateChatResponse, error) CreateChat(ctx context.Context, in *CreateChatRequest, opts ...client.CallOption) (*CreateChatResponse, error)
// Create a message within a chat // 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 // List the messages within a chat in reverse chronological order, using sent_before to
// offset as older messages need to be loaded // offset as older messages need to be loaded
ListMessages(ctx context.Context, in *ListMessagesRequest, opts ...client.CallOption) (*ListMessagesResponse, error) 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 return out, nil
} }
func (c *chatsService) CreateMessage(ctx context.Context, in *CreateMessageRequest, opts ...client.CallOption) (*CreateMessageResponse, error) { func (c *chatsService) SendMessage(ctx context.Context, in *SendMessageRequest, opts ...client.CallOption) (*SendMessageResponse, error) {
req := c.c.NewRequest(c.name, "Chats.CreateMessage", in) req := c.c.NewRequest(c.name, "Chats.SendMessage", in)
out := new(CreateMessageResponse) out := new(SendMessageResponse)
err := c.c.Call(ctx, req, out, opts...) err := c.c.Call(ctx, req, out, opts...)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -103,7 +101,7 @@ type ChatsHandler interface {
// chat will be returned // chat will be returned
CreateChat(context.Context, *CreateChatRequest, *CreateChatResponse) error CreateChat(context.Context, *CreateChatRequest, *CreateChatResponse) error
// Create a message within a chat // 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 // List the messages within a chat in reverse chronological order, using sent_before to
// offset as older messages need to be loaded // offset as older messages need to be loaded
ListMessages(context.Context, *ListMessagesRequest, *ListMessagesResponse) error ListMessages(context.Context, *ListMessagesRequest, *ListMessagesResponse) error
@@ -112,7 +110,7 @@ type ChatsHandler interface {
func RegisterChatsHandler(s server.Server, hdlr ChatsHandler, opts ...server.HandlerOption) error { func RegisterChatsHandler(s server.Server, hdlr ChatsHandler, opts ...server.HandlerOption) error {
type chats interface { type chats interface {
CreateChat(ctx context.Context, in *CreateChatRequest, out *CreateChatResponse) error 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 ListMessages(ctx context.Context, in *ListMessagesRequest, out *ListMessagesResponse) error
} }
type Chats struct { type Chats struct {
@@ -130,8 +128,8 @@ func (h *chatsHandler) CreateChat(ctx context.Context, in *CreateChatRequest, ou
return h.ChatsHandler.CreateChat(ctx, in, out) return h.ChatsHandler.CreateChat(ctx, in, out)
} }
func (h *chatsHandler) CreateMessage(ctx context.Context, in *CreateMessageRequest, out *CreateMessageResponse) error { func (h *chatsHandler) SendMessage(ctx context.Context, in *SendMessageRequest, out *SendMessageResponse) error {
return h.ChatsHandler.CreateMessage(ctx, in, out) return h.ChatsHandler.SendMessage(ctx, in, out)
} }
func (h *chatsHandler) ListMessages(ctx context.Context, in *ListMessagesRequest, out *ListMessagesResponse) error { func (h *chatsHandler) ListMessages(ctx context.Context, in *ListMessagesRequest, out *ListMessagesResponse) error {

View File

@@ -4,57 +4,73 @@ package chats;
option go_package = "./proto;chats"; option go_package = "./proto;chats";
import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";
service Chats { service Chats {
// Create a chat between two or more users, if a chat already exists for these users, the existing // Create a chat between two or more users, if a chat already exists for these users, the existing
// chat will be returned // chat will be returned
rpc CreateChat(CreateChatRequest) returns (CreateChatResponse); rpc CreateChat(CreateChatRequest) returns (CreateChatResponse);
// Create a message within a chat // 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 // List the messages within a chat in reverse chronological order, using sent_before to
// offset as older messages need to be loaded // offset as older messages need to be loaded
rpc ListMessages(ListMessagesRequest) returns (ListMessagesResponse); rpc ListMessages(ListMessagesRequest) returns (ListMessagesResponse);
} }
message Chat { message Chat {
// unique id of the chat
string id = 1; string id = 1;
// list of users in the chat
repeated string user_ids = 2; repeated string user_ids = 2;
google.protobuf.Timestamp created_at = 3; // unix nanosecond timestamp
int64 created_at = 3;
} }
message Message { message Message {
// unique id of the message
string id = 1; string id = 1;
// user id of the message
string author_id = 2; string author_id = 2;
// chat id the message belongs to
string chat_id = 3; string chat_id = 3;
// text within the message
string text = 4; 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 { 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 { message CreateChatResponse {
Chat chat = 1; Chat chat = 1;
} }
message CreateMessageRequest { // Send a message to a chat room
message SendMessageRequest {
string id = 1; string id = 1;
string chat_id = 2; string chat_id = 2;
string author_id = 3; string author_id = 3;
string text = 4; string text = 4;
} }
message CreateMessageResponse { message SendMessageResponse {
Message message = 1; Message message = 1;
} }
// List messages within a chat
message ListMessagesRequest { message ListMessagesRequest {
// unique id of the chat
string chat_id = 1; string chat_id = 1;
google.protobuf.Timestamp sent_before = 2; // limit the number of messages
google.protobuf.Int32Value limit = 3; int64 limit = 2;
// offset for the messages
int64 offset = 3;
// order "asc" or "desc" (defaults to reverse chronological)
string order = 4;
} }
message ListMessagesResponse { message ListMessagesResponse {

2
go.mod
View File

@@ -15,7 +15,7 @@ require (
github.com/hailocab/go-geoindex v0.0.0-20160127134810-64631bfe9711 github.com/hailocab/go-geoindex v0.0.0-20160127134810-64631bfe9711
github.com/jackc/pgx/v4 v4.10.1 github.com/jackc/pgx/v4 v4.10.1
github.com/micro/dev v0.0.0-20201117163752-d3cfc9788dfa 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/miekg/dns v1.1.31 // indirect
github.com/nats-io/nats-streaming-server v0.21.1 github.com/nats-io/nats-streaming-server v0.21.1
github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33 github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33

8
go.sum
View File

@@ -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.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 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.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.15/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.27/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/miekg/dns v1.1.27/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
github.com/miekg/dns v1.1.31 h1:sJFOl9BgwbYAWOGEwr61FU28pqsBNdpRBnhGXtO06Oo= github.com/miekg/dns v1.1.31 h1:sJFOl9BgwbYAWOGEwr61FU28pqsBNdpRBnhGXtO06Oo=

View File

@@ -1,2 +1,3 @@
package main package main
//go:generate make proto //go:generate make proto

View File

@@ -19,10 +19,22 @@ func FromContext(ctx context.Context) (string, bool) {
// FromAccount returns a tenant from // FromAccount returns a tenant from
func FromAccount(acc *auth.Account) string { func FromAccount(acc *auth.Account) string {
id := acc.ID
issuer := acc.Issuer
owner := acc.Metadata["apikey_owner"] 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) return fmt.Sprintf("%s/%s", acc.Issuer, owner)
} }