diff --git a/codegen/auth/rest/handlers/index.php b/codegen/auth/rest/handlers/index.php deleted file mode 100644 index 7aa5983f3..000000000 --- a/codegen/auth/rest/handlers/index.php +++ /dev/null @@ -1,36 +0,0 @@ - function($name, $api) { - return strtolower($name) . ".go"; - }, -); - -foreach ($templates as $template => $fn) -foreach ($apis as $api) { - $name = ucfirst($api['interface']); - $filename = $dirname . "/" . $fn($name, $api); - - $tpl->load($template); - $tpl->assign($common); - $tpl->assign("package", basename(__DIR__)); - $tpl->assign("name", $name); - $tpl->assign("api", $api); - $tpl->assign("apis", $apis); - $tpl->assign("self", strtolower(substr($name, 0, 1))); - $tpl->assign("structs", $api['struct']); - $imports = array(); - if (is_array($api['struct'])) - foreach ($api['struct'] as $struct) { - if (isset($struct['imports'])) - foreach ($struct['imports'] as $import) { - $imports[] = $import; - } - } - $tpl->assign("imports", $imports); - $tpl->assign("calls", $api['apis']); - $contents = str_replace("\n\n}", "\n}", $tpl->get()); - - file_put_contents($filename, $contents); - echo $filename . "\n"; -} diff --git a/codegen/auth/rest/index.php b/codegen/auth/rest/index.php deleted file mode 100644 index fc5eabcd9..000000000 --- a/codegen/auth/rest/index.php +++ /dev/null @@ -1,30 +0,0 @@ -load("http_.tpl"); - $tpl->assign($common); - $tpl->assign("package", basename(__DIR__)); - $tpl->assign("name", $name); - $tpl->assign("api", $api); - $tpl->assign("self", strtolower(substr($name, 0, 1))); - $tpl->assign("structs", $api['struct']); - $imports = array(); - if (is_array($api['struct'])) - foreach ($api['struct'] as $struct) { - if (isset($struct['imports'])) - foreach ($struct['imports'] as $import) { - $imports[] = $import; - } - } - $tpl->assign("imports", $imports); - $tpl->assign("calls", $api['apis']); - $contents = str_replace("\n\n}", "\n}", $tpl->get()); - - if (!file_exists($filename)) { - file_put_contents($filename, $contents); - echo $filename . "\n"; - } -} diff --git a/codegen/auth/rest/request/index.php b/codegen/auth/rest/request/index.php deleted file mode 100644 index c98c0349b..000000000 --- a/codegen/auth/rest/request/index.php +++ /dev/null @@ -1,36 +0,0 @@ - function($name, $api) { - return strtolower($name) . ".go"; - }, -); - -foreach ($templates as $template => $fn) -foreach ($apis as $api) { - $name = ucfirst($api['interface']); - $filename = $dirname . "/" . $fn($name, $api); - - $tpl->load($template); - $tpl->assign($common); - $tpl->assign("package", basename(__DIR__)); - $tpl->assign("name", $name); - $tpl->assign("api", $api); - $tpl->assign("apis", $apis); - $tpl->assign("self", strtolower(substr($name, 0, 1))); - $tpl->assign("structs", $api['struct']); - $imports = array(); - if (is_array($api['struct'])) - foreach ($api['struct'] as $struct) { - if (isset($struct['imports'])) - foreach ($struct['imports'] as $import) { - $imports[] = $import; - } - } - $tpl->assign("imports", $imports); - $tpl->assign("calls", $api['apis']); - $contents = str_replace("\n\n}", "\n}", $tpl->get()); - - file_put_contents($filename, $contents); - echo $filename . "\n"; -} diff --git a/codegen/auth/types/index.php b/codegen/auth/types/index.php deleted file mode 100644 index 0f41ab172..000000000 --- a/codegen/auth/types/index.php +++ /dev/null @@ -1,37 +0,0 @@ - function($name, $api) { - return strtolower($name) . ".go"; - }, -); - -foreach ($templates as $template => $fn) -foreach ($apis as $api) { - if (is_array($api['struct'])) { - $name = ucfirst($api['interface']); - $filename = $dirname . "/" . $fn($name, $api); - - $tpl->load($template); - $tpl->assign($common); - $tpl->assign("package", basename(__DIR__)); - $tpl->assign("name", $name); - $tpl->assign("api", $api); - $tpl->assign("apis", $apis); - $tpl->assign("self", strtolower(substr($name, 0, 1))); - $tpl->assign("structs", $api['struct']); - $imports = array(); - foreach ($api['struct'] as $struct) { - if (isset($struct['imports'])) - foreach ($struct['imports'] as $import) { - $imports[] = $import; - } - } - $tpl->assign("imports", $imports); - $tpl->assign("calls", $api['apis']); - $contents = str_replace("\n\n}", "\n}", $tpl->get()); - - file_put_contents($filename, $contents); - echo $filename . "\n"; - } -} diff --git a/sam/chat/chat.pb.go b/sam/chat/chat.pb.go index da064b362..41d1c9ac7 100644 --- a/sam/chat/chat.pb.go +++ b/sam/chat/chat.pb.go @@ -1174,7 +1174,7 @@ func init() { proto.RegisterType((*StreamResponse_Join)(nil), "chat.StreamResponse.Join") proto.RegisterType((*StreamResponse_Part)(nil), "chat.StreamResponse.Part") proto.RegisterType((*StreamResponse_Names)(nil), "chat.StreamResponse.Names") - proto.RegisterType((*StreamResponse_Users)(nil), "chat.StreamResponse.Users") + proto.RegisterType((*StreamResponse_Users)(nil), "chat.StreamResponse.UserSet") proto.RegisterType((*StreamResponse_User)(nil), "chat.StreamResponse.User") proto.RegisterType((*StreamResponse_Message)(nil), "chat.StreamResponse.Message") proto.RegisterType((*StreamResponse_Shutdown)(nil), "chat.StreamResponse.Shutdown") diff --git a/sam/rest/router.go b/sam/rest/router.go index df2e2d8cb..7ba1e678a 100644 --- a/sam/rest/router.go +++ b/sam/rest/router.go @@ -6,7 +6,7 @@ import ( "github.com/go-chi/chi" ) -func MountRoutes(jwtAuth auth.TokenEncoder) func(chi.Router) { +func MountRoutes() func(chi.Router) { // Initialize handers & controllers. return func(r chi.Router) { handlers.NewAttachmentDownloadable(Attachment{}.New()).MountRoutes(r) diff --git a/sam/rest/user.go b/sam/rest/user.go index 10ba609e9..8bc46a956 100644 --- a/sam/rest/user.go +++ b/sam/rest/user.go @@ -2,38 +2,22 @@ package rest import ( "context" - - authService "github.com/crusttech/crust/auth/service" - authTypes "github.com/crusttech/crust/auth/types" "github.com/crusttech/crust/sam/rest/request" - "github.com/crusttech/crust/sam/service" - "github.com/crusttech/crust/sam/types" "github.com/pkg/errors" ) var _ = errors.Wrap -type ( - User struct { - svc struct { - user authService.UserService - message service.MessageService - } - } -) +type User struct{} func (User) New() *User { - ctrl := &User{} - ctrl.svc.user = authService.DefaultUser - ctrl.svc.message = service.DefaultMessage - return ctrl + return &User{} } -// Searches the users table in the database to find users by matching (by-prefix) their.Username func (ctrl *User) Search(ctx context.Context, r *request.UserSearch) (interface{}, error) { - return ctrl.svc.user.With(ctx).Find(&authTypes.UserFilter{Query: r.Query}) + return nil, errors.New("Not implemented: User.search") } func (ctrl *User) Message(ctx context.Context, r *request.UserMessage) (interface{}, error) { - return ctrl.svc.message.With(ctx).Direct(r.UserID, &types.Message{Message: r.Message}) + return nil, errors.New("Not implemented: User.message") } diff --git a/sam/service/attachment.go b/sam/service/attachment.go index 0609c3ec2..79210bb36 100644 --- a/sam/service/attachment.go +++ b/sam/service/attachment.go @@ -5,11 +5,10 @@ import ( "fmt" "io" "log" - "path" - "strings" - "net/http" "net/url" + "path" + "strings" "github.com/titpetric/factory" diff --git a/sam/service/message.go b/sam/service/message.go index 0440d1bbc..9497467e9 100644 --- a/sam/service/message.go +++ b/sam/service/message.go @@ -3,6 +3,7 @@ package service import ( "context" + authService "github.com/crusttech/crust/auth/service" "github.com/pkg/errors" "github.com/titpetric/factory" @@ -19,6 +20,7 @@ type ( message repository.MessageRepository reaction repository.ReactionRepository + usr authService.UserService att AttachmentService } @@ -46,7 +48,10 @@ type ( ) func Message() MessageService { - return (&message{}).With(context.Background()) + return (&message{ + usr: authService.DefaultUser, + att: DefaultAttachment, + }).With(context.Background()) } func (svc *message) With(ctx context.Context) MessageService { @@ -54,7 +59,8 @@ func (svc *message) With(ctx context.Context) MessageService { return &message{ db: db, ctx: ctx, - att: DefaultAttachment.With(ctx), + att: svc.att.With(ctx), + usr: svc.usr.With(ctx), channel: repository.Channel(ctx, db), message: repository.Message(ctx, db), reaction: repository.Reaction(ctx, db), @@ -74,6 +80,11 @@ func (svc *message) Find(filter *types.MessageFilter) (mm types.MessageSet, err return nil, err } + mm.Walk(func(i *types.Message) (err error) { + i.User, err = svc.usr.FindByID(i.UserID) + return + }) + return mm, svc.att.LoadFromMessages(mm) } diff --git a/sam/start.go b/sam/start.go index f6f2ac423..8ed8c6add 100644 --- a/sam/start.go +++ b/sam/start.go @@ -77,7 +77,7 @@ func Start() error { // Only protect application routes with JWT r.Group(func(r chi.Router) { r.Use(jwtAuth.Verifier(), jwtAuth.Authenticator()) - mountRoutes(r, flags.http, rest.MountRoutes(jwtAuth), websocket.MountRoutes(ctx.AsContext(deadline), flags.repository)) + mountRoutes(r, flags.http, rest.MountRoutes(), websocket.MountRoutes(ctx.AsContext(deadline), flags.repository)) }) printRoutes(r, flags.http) diff --git a/sam/types/message.go b/sam/types/message.go index 8a1dc1ee1..a6ff65011 100644 --- a/sam/types/message.go +++ b/sam/types/message.go @@ -3,20 +3,23 @@ package types import ( "database/sql/driver" "time" + + authTypes "github.com/crusttech/crust/auth/types" ) type ( Message struct { - ID uint64 `json:"id" db:"id"` - Type MessageType `json:"type" db:"type"` - Message string `json:"message" db:"message"` - UserID uint64 `json:"userId" db:"rel_user"` - ChannelID uint64 `json:"channelId" db:"rel_channel"` - ReplyTo uint64 `json:"replyTo" db:"reply_to"` - CreatedAt time.Time `json:"createdAt,omitempty" db:"created_at"` - UpdatedAt *time.Time `json:"updatedAt,omitempty" db:"updated_at"` - DeletedAt *time.Time `json:"deletedAt,omitempty" db:"deleted_at"` - Attachment *Attachment `json:"attachment,omitempty"` + ID uint64 `json:"id" db:"id"` + Type MessageType `json:"type" db:"type"` + Message string `json:"message" db:"message"` + UserID uint64 `json:"userId" db:"rel_user"` + ChannelID uint64 `json:"channelId" db:"rel_channel"` + ReplyTo uint64 `json:"replyTo" db:"reply_to"` + CreatedAt time.Time `json:"createdAt,omitempty" db:"created_at"` + UpdatedAt *time.Time `json:"updatedAt,omitempty" db:"updated_at"` + DeletedAt *time.Time `json:"deletedAt,omitempty" db:"deleted_at"` + Attachment *Attachment `json:"attachment,omitempty"` + User *authTypes.User `json:"user,omitempty"` } MessageSet []*Message diff --git a/sam/types/user.go b/sam/types/user.go deleted file mode 100644 index 98796bedb..000000000 --- a/sam/types/user.go +++ /dev/null @@ -1,49 +0,0 @@ -package types - -import ( - "encoding/json" - "golang.org/x/crypto/bcrypt" - "time" -) - -type ( - User struct { - ID uint64 `json:"id" db:"id"` - Email string `json:"email" db:"email"` - Username string `json:"username" db:"username"` - Meta json.RawMessage `json:"-" db:"meta"` - OrganisationID uint64 `json:"organisationId" db:"rel_organisation"` - Password []byte `json:"-" db:"password"` - CreatedAt time.Time `json:"createdAt,omitempty" db:"created_at"` - UpdatedAt *time.Time `json:"updatedAt,omitempty" db:"updated_at"` - SuspendedAt *time.Time `json:"suspendedAt,omitempty" db:"suspended_at"` - DeletedAt *time.Time `json:"deletedAt,omitempty" db:"deleted_at"` - } - - UserFilter struct { - Query string - MembersOfChannel uint64 - } -) - -func (u *User) Valid() bool { - return u.ID > 0 && u.SuspendedAt == nil && u.DeletedAt == nil -} - -func (u *User) Identity() uint64 { - return u.ID -} - -func (u *User) ValidatePassword(password string) bool { - return bcrypt.CompareHashAndPassword(u.Password, []byte(password)) == nil -} - -func (u *User) GeneratePassword(password string) error { - pwd, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) - if err != nil { - return err - } - - u.Password = pwd - return nil -} diff --git a/sam/websocket/event_queue.go b/sam/websocket/event_queue.go index ca412a8a9..b7dd76775 100644 --- a/sam/websocket/event_queue.go +++ b/sam/websocket/event_queue.go @@ -3,6 +3,7 @@ package websocket import ( "context" + "github.com/davecgh/go-spew/spew" "github.com/titpetric/factory" "github.com/crusttech/crust/sam/repository" @@ -108,6 +109,7 @@ func (eq *eventQueue) feedSessions(ctx context.Context, config *repository.Flags case <-ctx.Done(): return ctx.Err() case <-newMessageEvent: + spew.Println("Received newMessageEvent, polling...") if err := poll(); err != nil { return err } diff --git a/sam/websocket/outgoing/attachment.go b/sam/websocket/outgoing/attachment.go index 8967d70f6..645188783 100644 --- a/sam/websocket/outgoing/attachment.go +++ b/sam/websocket/outgoing/attachment.go @@ -6,14 +6,16 @@ import ( type ( Attachment struct { - ID string `json:"id"` - UserID string `json:"uid"` + ID string `json:"ID"` + UserID string `json:"userID"` Url string `json:"url"` - PreviewUrl string `json:"prw"` - Size int64 `json:"sze"` - Mimetype string `json:"typ"` - Name string `json:"nme"` - CreatedAt time.Time `json:"cat,omitempty"` - UpdatedAt *time.Time `json:"uat,omitempty"` + PreviewUrl string `json:"previewUrl"` + Size int64 `json:"size"` + Mimetype string `json:"mimetype"` + Name string `json:"name"` + CreatedAt time.Time `json:"createdAt,omitempty"` + UpdatedAt *time.Time `json:"updatedAt,omitempty"` } + + AttachmentSet []*Attachment ) diff --git a/sam/websocket/outgoing/channel.go b/sam/websocket/outgoing/channel.go index bf7323087..104b03d53 100644 --- a/sam/websocket/outgoing/channel.go +++ b/sam/websocket/outgoing/channel.go @@ -31,15 +31,15 @@ type ( Channel struct { // Channel to part (nil) for ALL channels - ID string `json:"id"` - Name string `json:"name"` - Topic string `json:"topic"` - Type string `json:"type"` - LastMessageID string `json:"lastMessageId"` - Members *Users `json:"members,omitempty"` + ID string `json:"ID"` + Name string `json:"name"` + Topic string `json:"topic"` + Type string `json:"type"` + LastMessageID string `json:"lastMessageID"` + Members *UserSet `json:"members,omitempty"` } - Channels []*Channel + ChannelSet []*Channel ) func (p *ChannelJoin) EncodeMessage() ([]byte, error) { @@ -58,6 +58,6 @@ func (p *Channel) EncodeMessage() ([]byte, error) { return json.Marshal(Payload{Channel: p}) } -func (p *Channels) EncodeMessage() ([]byte, error) { - return json.Marshal(Payload{Channels: p}) +func (p *ChannelSet) EncodeMessage() ([]byte, error) { + return json.Marshal(Payload{ChannelSet: p}) } diff --git a/sam/websocket/outgoing/message.go b/sam/websocket/outgoing/message.go index a3d5f6677..71c5dc6fd 100644 --- a/sam/websocket/outgoing/message.go +++ b/sam/websocket/outgoing/message.go @@ -7,30 +7,30 @@ import ( type ( Message struct { - ID string `json:"id"` - Type string `json:"t"` - Message string `json:"m"` - UserID string `json:"uid"` - ChannelID string `json:"cid"` - ReplyTo string `json:"rid"` + ID string `json:"ID"` + Type string `json:"type"` + Message string `json:"message"` + ChannelID string `json:"channelID"` + ReplyTo string `json:"replyID"` + User *User `json:"user"` Attachment *Attachment `json:"att,omitempty"` - CreatedAt time.Time `json:"cat,omitempty"` - UpdatedAt *time.Time `json:"uat,omitempty"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt *time.Time `json:"updatedAt,omitempty"` } - Messages []*Message + MessageSet []*Message MessageUpdate struct { - ID string `json:"id"` - Message string `json:"m"` + ID string `json:"ID"` + Message string `json:"message"` - UpdatedAt time.Time `json:"uat,omitempty"` + UpdatedAt time.Time `json:"updatedAt,omitempty"` } MessageDelete struct { - ID string `json:"id"` + ID string `json:"ID"` } ) @@ -38,8 +38,8 @@ func (p *Message) EncodeMessage() ([]byte, error) { return json.Marshal(Payload{Message: p}) } -func (p *Messages) EncodeMessage() ([]byte, error) { - return json.Marshal(Payload{Messages: p}) +func (p *MessageSet) EncodeMessage() ([]byte, error) { + return json.Marshal(Payload{MessageSet: p}) } func (p *MessageUpdate) EncodeMessage() ([]byte, error) { diff --git a/sam/websocket/outgoing/payload.go b/sam/websocket/outgoing/payload.go index 4da6cd292..8598bd2ab 100644 --- a/sam/websocket/outgoing/payload.go +++ b/sam/websocket/outgoing/payload.go @@ -10,16 +10,16 @@ type ( *Message `json:"message,omitempty"` *MessageDelete `json:"messageDeleted,omitempty"` *MessageUpdate `json:"messageUpdated,omitempty"` - *Messages `json:"messages,omitempty"` + *MessageSet `json:"messages,omitempty"` *ChannelJoin `json:"channelJoin,omitempty"` *ChannelPart `json:"channelPart,omitempty"` *ChannelDeleted `json:"channelDeleted,omitempty"` *Channel `json:"channel,omitempty"` - *Channels `json:"channels,omitempty"` + *ChannelSet `json:"channels,omitempty"` - *User `json:"user,omitempty"` - *Users `json:"users,omitempty"` + *User `json:"user,omitempty"` + *UserSet `json:"users,omitempty"` } // This is same-same but different as using the json.Marshaler diff --git a/sam/websocket/outgoing/user.go b/sam/websocket/outgoing/user.go index 89d5447ef..52470c24a 100644 --- a/sam/websocket/outgoing/user.go +++ b/sam/websocket/outgoing/user.go @@ -7,19 +7,20 @@ import ( type ( User struct { // Channel to part (nil) for ALL channels - ID string `json:"id"` + ID string `json:"ID"` Name string `json:"name"` Username string `json:"username"` + Handle string `json:"handle"` Connections uint `json:"connections"` } - Users []*User + UserSet []*User ) func (p *User) EncodeMessage() ([]byte, error) { return json.Marshal(Payload{User: p}) } -func (p *Users) EncodeMessage() ([]byte, error) { - return json.Marshal(Payload{Users: p}) +func (p *UserSet) EncodeMessage() ([]byte, error) { + return json.Marshal(Payload{UserSet: p}) } diff --git a/sam/websocket/payload.go b/sam/websocket/payload.go index d701ed359..9bbbdf903 100644 --- a/sam/websocket/payload.go +++ b/sam/websocket/payload.go @@ -12,9 +12,9 @@ func payloadFromMessage(msg *samTypes.Message) *outgoing.Message { ID: uint64toa(msg.ID), ChannelID: uint64toa(msg.ChannelID), Type: string(msg.Type), - UserID: uint64toa(msg.UserID), ReplyTo: uint64toa(msg.ReplyTo), + User: payloadFromUser(msg.User), Attachment: payloadFromAttachment(msg.Attachment), CreatedAt: msg.CreatedAt, @@ -22,12 +22,12 @@ func payloadFromMessage(msg *samTypes.Message) *outgoing.Message { } } -func payloadFromMessages(msg samTypes.MessageSet) *outgoing.Messages { +func payloadFromMessages(msg samTypes.MessageSet) *outgoing.MessageSet { msgs := make([]*outgoing.Message, len(msg)) for k, m := range msg { msgs[k] = payloadFromMessage(m) } - retval := outgoing.Messages(msgs) + retval := outgoing.MessageSet(msgs) return &retval } @@ -41,23 +41,27 @@ func payloadFromChannel(ch *samTypes.Channel) *outgoing.Channel { } } -func payloadFromChannels(channels []*samTypes.Channel) *outgoing.Channels { +func payloadFromChannels(channels []*samTypes.Channel) *outgoing.ChannelSet { cc := make([]*outgoing.Channel, len(channels)) for k, c := range channels { cc[k] = payloadFromChannel(c) } - retval := outgoing.Channels(cc) + retval := outgoing.ChannelSet(cc) return &retval } func payloadFromUser(user *authTypes.User) *outgoing.User { + if user == nil { + return nil + } + return &outgoing.User{ ID: uint64toa(user.ID), Username: user.Username, } } -func payloadFromUsers(users []*authTypes.User) *outgoing.Users { +func payloadFromUsers(users []*authTypes.User) *outgoing.UserSet { uu := make([]*outgoing.User, len(users)) for k, u := range users { uu[k] = payloadFromUser(u) @@ -71,7 +75,7 @@ func payloadFromUsers(users []*authTypes.User) *outgoing.Users { }) } - retval := outgoing.Users(uu) + retval := outgoing.UserSet(uu) return &retval }