3
0

Data structure naming clenaup

This commit is contained in:
Denis Arh
2018-09-26 16:47:10 +02:00
parent 2a357b1c93
commit 17a3f9461e
19 changed files with 91 additions and 273 deletions

View File

@@ -1,36 +0,0 @@
<?php
$templates = array(
"http_handlers_inline.tpl" => 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";
}

View File

@@ -1,30 +0,0 @@
<?php
foreach ($apis as $api) {
$name = ucfirst($api['interface']);
$filename = $dirname . "/" . str_replace("..", ".", strtolower($name) . ".go");
$tpl->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";
}
}

View File

@@ -1,36 +0,0 @@
<?php
$templates = array(
"http_request_inline.tpl" => 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";
}

View File

@@ -1,37 +0,0 @@
<?php
$templates = array(
"http_structs.tpl" => 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";
}
}

View File

@@ -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")

View File

@@ -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)

View File

@@ -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")
}

View File

@@ -5,11 +5,10 @@ import (
"fmt"
"io"
"log"
"path"
"strings"
"net/http"
"net/url"
"path"
"strings"
"github.com/titpetric/factory"

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
)

View File

@@ -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})
}

View File

@@ -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) {

View File

@@ -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

View File

@@ -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})
}

View File

@@ -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
}