diff --git a/auth/auth.go b/auth/auth.go index e90214c51..2742592f2 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -2,25 +2,25 @@ package auth type ( Identifiable interface { - GetID() uint64 + Identity() uint64 Valid() bool } Identity struct { - id uint64 + identity uint64 } ) func NewIdentity(id uint64) *Identity { return &Identity{ - id: id, + identity: id, } } -func (i Identity) GetID() uint64 { - return i.id +func (i Identity) Identity() uint64 { + return i.identity } func (i Identity) Valid() bool { - return i.id > 0 + return i.identity > 0 } diff --git a/auth/jwt.go b/auth/jwt.go index 6b634adb1..e2c35ef5c 100644 --- a/auth/jwt.go +++ b/auth/jwt.go @@ -30,7 +30,7 @@ func (t *jwt) Verifier() func(http.Handler) http.Handler { func (t *jwt) Encode(identity Identifiable) string { // @todo Set expiry claims := jwtauth.Claims{} - claims.Set("sub", strconv.FormatUint(identity.GetID(), 10)) + claims.Set("sub", strconv.FormatUint(identity.Identity(), 10)) claims.SetExpiryIn(time.Duration(config.jwtExpiry) * time.Minute) _, jwt, _ := t.tokenAuth.Encode(claims) diff --git a/codegen/crm/types/index.php b/codegen/crm/types/index.php deleted file mode 100644 index 5427926df..000000000 --- a/codegen/crm/types/index.php +++ /dev/null @@ -1,29 +0,0 @@ -load("http_structs.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(); - 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/templates/http_structs.tpl b/codegen/templates/http_structs.tpl deleted file mode 100644 index d97f5c7d4..000000000 --- a/codegen/templates/http_structs.tpl +++ /dev/null @@ -1,68 +0,0 @@ -package {package} - -{load warning.tpl} - -{if !empty($imports)} -import ( -{foreach ($imports as $import)} - "{import}" -{/foreach} -) -{/if} - - -{if !empty($structs)} -type ({foreach $structs as $struct} - -{if strpos($name, "Literal") !== false} - {foreach $struct.fields as $field} - {field}{newline} - {/foreach} -{else} - // {api.title} - {api.description} - {struct.name} struct { -{foreach $struct.fields as $field} - {field.name} {field.type} `{if $field.tag}{$field.tag} {/if}db:"{if $field.dbname}{$field.dbname}{else}{$field.name|decamel}{/if}"`{newline} -{/foreach} - - changed []string - } - -{/if}{/foreach} -) -{/if} - -{foreach $structs as $struct} -// New constructs a new instance of {struct.name} -func ({struct.name}) New() *{struct.name} { - return &{struct.name}{} -} -{/foreach} - -{foreach $structs as $struct} -{foreach $struct.fields as $field} -// Get the value of {field.name} -func ({self} *{struct.name}) Get{field.name}() {field.type} { - return {self}.{field.name} -} - -// Set the value of {field.name} -func ({self} *{struct.name}) Set{field.name}(value {field.type}) *{struct.name} {{if !$field.complex} - if {self}.{field.name} != value { - {self}.changed = append({self}.changed, "{field.name}") - {self}.{field.name} = value - } -{else} - {self}.changed = append({self}.changed, "{field.name}") - {self}.{field.name} = value -{/if} - return {self} -} -{/foreach} - -// Changes returns the names of changed fields -func ({self} *{struct.name}) Changes() []string { - return {self}.changed -} - -{/foreach} diff --git a/crm/repository/module.go b/crm/repository/module.go index f18d38ce4..cd55d4e28 100644 --- a/crm/repository/module.go +++ b/crm/repository/module.go @@ -37,22 +37,13 @@ func (r module) Find(ctx context.Context) ([]*types.Module, error) { } func (r module) Create(ctx context.Context, mod *types.Module) (*types.Module, error) { - db := factory.Database.MustGet() - mod.SetID(factory.Sonyflake.NextID()) - if err := db.With(ctx).Insert("crm_module", mod); err != nil { - return nil, ErrDatabaseError - } else { - return mod, nil - } + mod.ID = factory.Sonyflake.NextID() + return mod, factory.Database.MustGet().With(ctx).Insert("crm_module", mod) } func (r module) Update(ctx context.Context, mod *types.Module) (*types.Module, error) { - db := factory.Database.MustGet() - if err := db.With(ctx).Replace("crm_module", mod); err != nil { - return nil, ErrDatabaseError - } else { - return mod, nil - } + return mod, factory.Database.MustGet().With(ctx).Replace("crm_module", mod) + } func (r module) DeleteByID(ctx context.Context, id uint64) error { diff --git a/crm/rest/module.go b/crm/rest/module.go index 444ee84fb..40cf8198c 100644 --- a/crm/rest/module.go +++ b/crm/rest/module.go @@ -46,14 +46,12 @@ func (c *Module) Delete(ctx context.Context, r *server.ModuleDeleteRequest) (int } func (c *Module) Create(ctx context.Context, r *server.ModuleCreateRequest) (interface{}, error) { - m := types.Module{}.New() - m.SetName(r.Name) + m := &types.Module{Name: r.Name} return c.svc.Create(ctx, m) } func (c *Module) Edit(ctx context.Context, r *server.ModuleEditRequest) (interface{}, error) { - m := types.Module{}.New() - m.SetID(r.ID).SetName(r.Name) + m := &types.Module{ID: r.ID, Name: r.Name} return c.svc.Update(ctx, m) } diff --git a/crm/types/field.go b/crm/types/field.go index 1c1c71fac..764367d38 100644 --- a/crm/types/field.go +++ b/crm/types/field.go @@ -1,64 +1,9 @@ package types -/* - Hello! This file is auto-generated from `docs/src/spec.json`. - - For development: - In order to update the generated files, edit this file under the location, - add your struct fields, imports, API definitions and whatever you want, and: - - 1. run [spec](https://github.com/titpetric/spec) in the same folder, - 2. run `./_gen.php` in this folder. - - You may edit `field.go`, `field.util.go` or `field_test.go` to - implement your API calls, helper functions and tests. The file `field.go` - is only generated the first time, and will not be overwritten if it exists. -*/ - type ( // Fields - CRM input field definitions Field struct { Name string `json:"name" db:"name"` Type string `json:"type" db:"type"` - - changed []string } ) - -// New constructs a new instance of Field -func (Field) New() *Field { - return &Field{} -} - -// Get the value of Name -func (f *Field) GetName() string { - return f.Name -} - -// Set the value of Name -func (f *Field) SetName(value string) *Field { - if f.Name != value { - f.changed = append(f.changed, "Name") - f.Name = value - } - return f -} - -// Get the value of Type -func (f *Field) GetType() string { - return f.Type -} - -// Set the value of Type -func (f *Field) SetType(value string) *Field { - if f.Type != value { - f.changed = append(f.changed, "Type") - f.Type = value - } - return f -} - -// Changes returns the names of changed fields -func (f *Field) Changes() []string { - return f.changed -} diff --git a/crm/types/module.go b/crm/types/module.go index 4ef49a613..d8671afe7 100644 --- a/crm/types/module.go +++ b/crm/types/module.go @@ -1,20 +1,5 @@ package types -/* - Hello! This file is auto-generated from `docs/src/spec.json`. - - For development: - In order to update the generated files, edit this file under the location, - add your struct fields, imports, API definitions and whatever you want, and: - - 1. run [spec](https://github.com/titpetric/spec) in the same folder, - 2. run `./_gen.php` in this folder. - - You may edit `module.go`, `module.util.go` or `module_test.go` to - implement your API calls, helper functions and tests. The file `module.go` - is only generated the first time, and will not be overwritten if it exists. -*/ - import ( "github.com/jmoiron/sqlx/types" ) @@ -24,104 +9,12 @@ type ( Module struct { ID uint64 `db:"id"` Name string `db:"name"` - - changed []string } // Modules - CRM module definitions ModuleContentRow struct { - ID uint64 `db:"id" db:"id"` - ModuleID uint64 `db:"module_id" db:"module_id"` - Fields types.JSONText `db:"address" db:"fields"` - - changed []string + ID uint64 `db:"id"` + ModuleID uint64 `db:"module_id"` + Fields types.JSONText `db:"fields"` } ) - -// New constructs a new instance of Module -func (Module) New() *Module { - return &Module{} -} - -// New constructs a new instance of ModuleContentRow -func (ModuleContentRow) New() *ModuleContentRow { - return &ModuleContentRow{} -} - -// Get the value of ID -func (m *Module) GetID() uint64 { - return m.ID -} - -// Set the value of ID -func (m *Module) SetID(value uint64) *Module { - if m.ID != value { - m.changed = append(m.changed, "ID") - m.ID = value - } - return m -} - -// Get the value of Name -func (m *Module) GetName() string { - return m.Name -} - -// Set the value of Name -func (m *Module) SetName(value string) *Module { - if m.Name != value { - m.changed = append(m.changed, "Name") - m.Name = value - } - return m -} - -// Changes returns the names of changed fields -func (m *Module) Changes() []string { - return m.changed -} - -// Get the value of ID -func (m *ModuleContentRow) GetID() uint64 { - return m.ID -} - -// Set the value of ID -func (m *ModuleContentRow) SetID(value uint64) *ModuleContentRow { - if m.ID != value { - m.changed = append(m.changed, "ID") - m.ID = value - } - return m -} - -// Get the value of ModuleID -func (m *ModuleContentRow) GetModuleID() uint64 { - return m.ModuleID -} - -// Set the value of ModuleID -func (m *ModuleContentRow) SetModuleID(value uint64) *ModuleContentRow { - if m.ModuleID != value { - m.changed = append(m.changed, "ModuleID") - m.ModuleID = value - } - return m -} - -// Get the value of Fields -func (m *ModuleContentRow) GetFields() types.JSONText { - return m.Fields -} - -// Set the value of Fields -func (m *ModuleContentRow) SetFields(value types.JSONText) *ModuleContentRow { - m.changed = append(m.changed, "Fields") - m.Fields = value - return m -} - -// Changes returns the names of changed fields -func (m *ModuleContentRow) Changes() []string { - return m.changed -} diff --git a/sam/repository/attachment.go b/sam/repository/attachment.go index 508cf7d41..7c8741b68 100644 --- a/sam/repository/attachment.go +++ b/sam/repository/attachment.go @@ -44,25 +44,19 @@ func (r attachment) FindByRange(ctx context.Context, channelID, fromAttachmentID } func (r attachment) Create(ctx context.Context, mod *types.Attachment) (*types.Attachment, error) { - db := factory.Database.MustGet() + mod.ID = factory.Sonyflake.NextID() + mod.CreatedAt = time.Now() - mod.SetID(factory.Sonyflake.NextID()) - mod.SetCreatedAt(time.Now()) + mod.Attachment = coalesceJson(mod.Attachment, []byte("{}")) - if mod.Attachment == nil { - mod.SetAttachment([]byte("{}")) - } - - return mod, db.With(ctx).Insert("attachments", mod) + return mod, factory.Database.MustGet().With(ctx).Insert("attachments", mod) } func (r attachment) Update(ctx context.Context, mod *types.Attachment) (*types.Attachment, error) { - db := factory.Database.MustGet() + mod.UpdatedAt = timeNowPtr() + mod.Attachment = coalesceJson(mod.Attachment, []byte("{}")) - now := time.Now() - mod.SetUpdatedAt(&now) - - return mod, db.With(ctx).Replace("attachments", mod) + return mod, factory.Database.MustGet().With(ctx).Replace("attachments", mod) } func (r attachment) Delete(ctx context.Context, id uint64) error { diff --git a/sam/repository/attachment_test.go b/sam/repository/attachment_test.go index 7cab17952..ad9e1e433 100644 --- a/sam/repository/attachment_test.go +++ b/sam/repository/attachment_test.go @@ -16,11 +16,11 @@ func TestAttachment(t *testing.T) { rpo := Attachment() ctx := context.Background() - att := types.Attachment{}.New() + att := &types.Attachment{} var aa []*types.Attachment - att.SetChannelID(1) + att.ChannelID = 1 att, err = rpo.Create(ctx, att) must(t, err) @@ -28,7 +28,7 @@ func TestAttachment(t *testing.T) { t.Fatal("Changes were not stored") } - att.SetChannelID(2) + att.ChannelID = 2 att, err = rpo.Update(ctx, att) must(t, err) diff --git a/sam/repository/channel.go b/sam/repository/channel.go index 7f9293434..9950ebc32 100644 --- a/sam/repository/channel.go +++ b/sam/repository/channel.go @@ -49,25 +49,18 @@ func (r channel) Find(ctx context.Context, filter *types.ChannelFilter) ([]*type } func (r channel) Create(ctx context.Context, mod *types.Channel) (*types.Channel, error) { - db := factory.Database.MustGet() + mod.ID = factory.Sonyflake.NextID() + mod.CreatedAt = time.Now() + mod.Meta = coalesceJson(mod.Meta, []byte("{}")) - mod.SetID(factory.Sonyflake.NextID()) - mod.SetCreatedAt(time.Now()) - - if mod.Meta == nil { - mod.SetMeta([]byte("{}")) - } - - return mod, db.With(ctx).Insert("channels", mod) + return mod, factory.Database.MustGet().With(ctx).Insert("channels", mod) } func (r channel) Update(ctx context.Context, mod *types.Channel) (*types.Channel, error) { - db := factory.Database.MustGet() + mod.UpdatedAt = timeNowPtr() + mod.Meta = coalesceJson(mod.Meta, []byte("{}")) - now := time.Now() - mod.SetUpdatedAt(&now) - - return mod, db.With(ctx).Replace("channels", mod) + return mod, factory.Database.MustGet().With(ctx).Replace("channels", mod) } func (r channel) AddMember(ctx context.Context, channelID, userID uint64) error { diff --git a/sam/repository/channel_test.go b/sam/repository/channel_test.go index bfc2036d5..223c75dd1 100644 --- a/sam/repository/channel_test.go +++ b/sam/repository/channel_test.go @@ -16,13 +16,13 @@ func TestChannel(t *testing.T) { rpo := Channel() ctx := context.Background() - chn := types.Channel{}.New() + chn := &types.Channel{} var name1, name2 = "Test channel v1", "Test channel v2" - var aa []*types.Channel + var cc []*types.Channel - chn.SetName(name1) + chn.Name = name1 chn, err = rpo.Create(ctx, chn) must(t, err) @@ -30,7 +30,7 @@ func TestChannel(t *testing.T) { t.Fatal("Changes were not stored") } - chn.SetName(name2) + chn.Name = name2 chn, err = rpo.Update(ctx, chn) must(t, err) @@ -44,9 +44,9 @@ func TestChannel(t *testing.T) { t.Fatal("Changes were not stored") } - aa, err = rpo.Find(ctx, &types.ChannelFilter{Query: name2}) + cc, err = rpo.Find(ctx, &types.ChannelFilter{Query: name2}) must(t, err) - if len(aa) == 0 { + if len(cc) == 0 { t.Fatal("No results found") } @@ -66,11 +66,11 @@ func TestChannelMembers(t *testing.T) { rpo := Channel() ctx := context.Background() - chn := types.Channel{}.New() + chn := &types.Channel{} chn, err = rpo.Create(ctx, chn) must(t, err) - usr := types.User{}.New() + usr := &types.User{} usr, err = User().Create(ctx, usr) must(t, err) diff --git a/sam/repository/message.go b/sam/repository/message.go index 4c29d9d54..b426a5eb8 100644 --- a/sam/repository/message.go +++ b/sam/repository/message.go @@ -69,21 +69,16 @@ func (r message) Find(ctx context.Context, filter *types.MessageFilter) ([]*type } func (r message) Create(ctx context.Context, mod *types.Message) (*types.Message, error) { - db := factory.Database.MustGet() + mod.ID = factory.Sonyflake.NextID() + mod.CreatedAt = time.Now() - mod.SetID(factory.Sonyflake.NextID()) - mod.SetCreatedAt(time.Now()) - - return mod, db.With(ctx).Insert("messages", mod) + return mod, factory.Database.MustGet().With(ctx).Insert("messages", mod) } func (r message) Update(ctx context.Context, mod *types.Message) (*types.Message, error) { - db := factory.Database.MustGet() + mod.UpdatedAt = timeNowPtr() - now := time.Now() - mod.SetUpdatedAt(&now) - - return mod, db.With(ctx).Replace("messages", mod) + return mod, factory.Database.MustGet().With(ctx).Replace("messages", mod) } func (r message) Delete(ctx context.Context, id uint64) error { diff --git a/sam/repository/message_test.go b/sam/repository/message_test.go index 61513a5e0..26b01e76a 100644 --- a/sam/repository/message_test.go +++ b/sam/repository/message_test.go @@ -16,39 +16,39 @@ func TestMessage(t *testing.T) { rpo := Message() ctx := context.Background() - att := types.Message{}.New() + msg := &types.Message{} var msg1, msg2 = "Test message v1", "Test message v2" - var aa []*types.Message + var mm []*types.Message - att.SetMessage(msg1) + msg.Message = msg1 - att, err = rpo.Create(ctx, att) + msg, err = rpo.Create(ctx, msg) must(t, err) - if att.Message != msg1 { + if msg.Message != msg1 { t.Fatal("Changes were not stored") } - att.SetMessage(msg2) + msg.Message = msg2 - att, err = rpo.Update(ctx, att) + msg, err = rpo.Update(ctx, msg) must(t, err) - if att.Message != msg2 { + if msg.Message != msg2 { t.Fatal("Changes were not stored") } - att, err = rpo.FindByID(ctx, att.ID) + msg, err = rpo.FindByID(ctx, msg.ID) must(t, err) - if att.Message != msg2 { + if msg.Message != msg2 { t.Fatal("Changes were not stored") } - aa, err = rpo.Find(ctx, &types.MessageFilter{Query: msg2}) + mm, err = rpo.Find(ctx, &types.MessageFilter{Query: msg2}) must(t, err) - if len(aa) == 0 { + if len(mm) == 0 { t.Fatal("No results found") } - must(t, rpo.Delete(ctx, att.ID)) + must(t, rpo.Delete(ctx, msg.ID)) } diff --git a/sam/repository/organisation.go b/sam/repository/organisation.go index c39ba67b5..8f0bd9699 100644 --- a/sam/repository/organisation.go +++ b/sam/repository/organisation.go @@ -48,21 +48,16 @@ func (r organisation) Find(ctx context.Context, filter *types.OrganisationFilter } func (r organisation) Create(ctx context.Context, mod *types.Organisation) (*types.Organisation, error) { - db := factory.Database.MustGet() + mod.ID = factory.Sonyflake.NextID() + mod.CreatedAt = time.Now() - mod.SetID(factory.Sonyflake.NextID()) - mod.SetCreatedAt(time.Now()) - - return mod, db.With(ctx).Insert("organisations", mod) + return mod, factory.Database.MustGet().With(ctx).Insert("organisations", mod) } func (r organisation) Update(ctx context.Context, mod *types.Organisation) (*types.Organisation, error) { - db := factory.Database.MustGet() + mod.UpdatedAt = timeNowPtr() - now := time.Now() - mod.SetUpdatedAt(&now) - - return mod, db.With(ctx).Replace("organisations", mod) + return mod, factory.Database.MustGet().With(ctx).Replace("organisations", mod) } func (r organisation) Archive(ctx context.Context, id uint64) error { diff --git a/sam/repository/organisation_test.go b/sam/repository/organisation_test.go index aa96321ee..b949217c5 100644 --- a/sam/repository/organisation_test.go +++ b/sam/repository/organisation_test.go @@ -16,41 +16,41 @@ func TestOrganisation(t *testing.T) { rpo := Organisation() ctx := context.Background() - att := types.Organisation{}.New() + org := &types.Organisation{} var name1, name2 = "Test organisation v1", "Test organisation v2" - var aa []*types.Organisation + var oo []*types.Organisation - att.SetName(name1) + org.Name = name1 - att, err = rpo.Create(ctx, att) + org, err = rpo.Create(ctx, org) must(t, err) - if att.Name != name1 { + if org.Name != name1 { t.Fatal("Changes were not stored") } - att.SetName(name2) + org.Name = name2 - att, err = rpo.Update(ctx, att) + org, err = rpo.Update(ctx, org) must(t, err) - if att.Name != name2 { + if org.Name != name2 { t.Fatal("Changes were not stored") } - att, err = rpo.FindByID(ctx, att.ID) + org, err = rpo.FindByID(ctx, org.ID) must(t, err) - if att.Name != name2 { + if org.Name != name2 { t.Fatal("Changes were not stored") } - aa, err = rpo.Find(ctx, &types.OrganisationFilter{Query: name2}) + oo, err = rpo.Find(ctx, &types.OrganisationFilter{Query: name2}) must(t, err) - if len(aa) == 0 { + if len(oo) == 0 { t.Fatal("No results found") } - must(t, rpo.Archive(ctx, att.ID)) - must(t, rpo.Unarchive(ctx, att.ID)) - must(t, rpo.Delete(ctx, att.ID)) + must(t, rpo.Archive(ctx, org.ID)) + must(t, rpo.Unarchive(ctx, org.ID)) + must(t, rpo.Delete(ctx, org.ID)) } diff --git a/sam/repository/reaction.go b/sam/repository/reaction.go index ac5a0f64c..435c81a47 100644 --- a/sam/repository/reaction.go +++ b/sam/repository/reaction.go @@ -41,12 +41,10 @@ func (r reaction) FindByRange(ctx context.Context, channelID, fromReactionID, to } func (r reaction) Create(ctx context.Context, mod *types.Reaction) (*types.Reaction, error) { - db := factory.Database.MustGet() + mod.ID = factory.Sonyflake.NextID() + mod.CreatedAt = time.Now() - mod.SetID(factory.Sonyflake.NextID()) - mod.SetCreatedAt(time.Now()) - - return mod, db.With(ctx).Insert("reactions", mod) + return mod, factory.Database.MustGet().With(ctx).Insert("reactions", mod) } func (r reaction) Delete(ctx context.Context, id uint64) error { diff --git a/sam/repository/reaction_test.go b/sam/repository/reaction_test.go index f83486d66..8182332f2 100644 --- a/sam/repository/reaction_test.go +++ b/sam/repository/reaction_test.go @@ -16,23 +16,23 @@ func TestReaction(t *testing.T) { rpo := Reaction() ctx := context.Background() - att := types.Reaction{}.New() + react := &types.Reaction{} var reaction = ":laugh:" - att.SetReaction(reaction) + react.Reaction = reaction - att, err = rpo.Create(ctx, att) + react, err = rpo.Create(ctx, react) must(t, err) - if att.Reaction != reaction { + if react.Reaction != reaction { t.Fatal("Changes were not stored") } - att, err = rpo.FindByID(ctx, att.ID) + react, err = rpo.FindByID(ctx, react.ID) must(t, err) - if att.Reaction != reaction { + if react.Reaction != reaction { t.Fatal("Changes were not stored") } - must(t, rpo.Delete(ctx, att.ID)) + must(t, rpo.Delete(ctx, react.ID)) } diff --git a/sam/repository/team.go b/sam/repository/team.go index ec2654f1f..e85c2b9eb 100644 --- a/sam/repository/team.go +++ b/sam/repository/team.go @@ -49,21 +49,16 @@ func (r team) Find(ctx context.Context, filter *types.TeamFilter) ([]*types.Team } func (r team) Create(ctx context.Context, mod *types.Team) (*types.Team, error) { - db := factory.Database.MustGet() + mod.ID = factory.Sonyflake.NextID() + mod.CreatedAt = time.Now() - mod.SetID(factory.Sonyflake.NextID()) - mod.SetCreatedAt(time.Now()) - - return mod, db.With(ctx).Insert("teams", mod) + return mod, factory.Database.MustGet().With(ctx).Insert("teams", mod) } func (r team) Update(ctx context.Context, mod *types.Team) (*types.Team, error) { - db := factory.Database.MustGet() + mod.UpdatedAt = timeNowPtr() - now := time.Now() - mod.SetUpdatedAt(&now) - - return mod, db.With(ctx).Replace("teams", mod) + return mod, factory.Database.MustGet().With(ctx).Replace("teams", mod) } func (r team) Archive(ctx context.Context, id uint64) error { diff --git a/sam/repository/team_test.go b/sam/repository/team_test.go index 0a044721b..e791315fe 100644 --- a/sam/repository/team_test.go +++ b/sam/repository/team_test.go @@ -16,31 +16,31 @@ func TestTeam(t *testing.T) { rpo := Team() ctx := context.Background() - att := types.Team{}.New() + team := &types.Team{} var name1, name2 = "Test team v1", "Test team v2" var aa []*types.Team - att.SetName(name1) + team.Name = name1 - att, err = rpo.Create(ctx, att) + team, err = rpo.Create(ctx, team) must(t, err) - if att.Name != name1 { + if team.Name != name1 { t.Fatal("Changes were not stored") } - att.SetName(name2) + team.Name = name2 - att, err = rpo.Update(ctx, att) + team, err = rpo.Update(ctx, team) must(t, err) - if att.Name != name2 { + if team.Name != name2 { t.Fatal("Changes were not stored") } - att, err = rpo.FindByID(ctx, att.ID) + team, err = rpo.FindByID(ctx, team.ID) must(t, err) - if att.Name != name2 { + if team.Name != name2 { t.Fatal("Changes were not stored") } @@ -50,7 +50,7 @@ func TestTeam(t *testing.T) { t.Fatal("No results found") } - must(t, rpo.Archive(ctx, att.ID)) - must(t, rpo.Unarchive(ctx, att.ID)) - must(t, rpo.Delete(ctx, att.ID)) + must(t, rpo.Archive(ctx, team.ID)) + must(t, rpo.Unarchive(ctx, team.ID)) + must(t, rpo.Delete(ctx, team.ID)) } diff --git a/sam/repository/user.go b/sam/repository/user.go index 5c5b85b85..345e29013 100644 --- a/sam/repository/user.go +++ b/sam/repository/user.go @@ -62,25 +62,18 @@ func (r user) Find(ctx context.Context, filter *types.UserFilter) ([]*types.User } func (r user) Create(ctx context.Context, mod *types.User) (*types.User, error) { - db := factory.Database.MustGet() + mod.ID = factory.Sonyflake.NextID() + mod.CreatedAt = time.Now() + mod.Meta = coalesceJson(mod.Meta, []byte("{}")) - mod.SetID(factory.Sonyflake.NextID()) - mod.SetCreatedAt(time.Now()) - - if mod.Meta == nil { - mod.SetMeta([]byte("{}")) - } - - return mod, db.With(ctx).Insert("users", mod) + return mod, factory.Database.MustGet().With(ctx).Insert("users", mod) } func (r user) Update(ctx context.Context, mod *types.User) (*types.User, error) { - db := factory.Database.MustGet() + mod.UpdatedAt = timeNowPtr() + mod.Meta = coalesceJson(mod.Meta, []byte("{}")) - now := time.Now() - mod.SetUpdatedAt(&now) - - return mod, db.With(ctx).Replace("users", mod) + return mod, factory.Database.MustGet().With(ctx).Replace("users", mod) } func (r user) Suspend(ctx context.Context, id uint64) error { diff --git a/sam/repository/user_test.go b/sam/repository/user_test.go index 6aa9b916f..86a47cd2c 100644 --- a/sam/repository/user_test.go +++ b/sam/repository/user_test.go @@ -16,31 +16,31 @@ func TestUser(t *testing.T) { rpo := User() ctx := context.Background() - att := types.User{}.New() + usr := &types.User{} var name1, name2 = "Test user v1", "Test user v2" var aa []*types.User - att.SetUsername(name1) + usr.Username = name1 - att, err = rpo.Create(ctx, att) + usr, err = rpo.Create(ctx, usr) must(t, err) - if att.Username != name1 { + if usr.Username != name1 { t.Fatal("Changes were not stored") } - att.SetUsername(name2) + usr.Username = name2 - att, err = rpo.Update(ctx, att) + usr, err = rpo.Update(ctx, usr) must(t, err) - if att.Username != name2 { + if usr.Username != name2 { t.Fatal("Changes were not stored") } - att, err = rpo.FindByID(ctx, att.ID) + usr, err = rpo.FindByID(ctx, usr.ID) must(t, err) - if att.Username != name2 { + if usr.Username != name2 { t.Fatal("Changes were not stored") } @@ -50,5 +50,5 @@ func TestUser(t *testing.T) { t.Fatal("No results found") } - must(t, rpo.Delete(ctx, att.ID)) + must(t, rpo.Delete(ctx, usr.ID)) } diff --git a/sam/repository/generics.go b/sam/repository/util.go similarity index 78% rename from sam/repository/generics.go rename to sam/repository/util.go index 9b384f503..2ba6867f3 100644 --- a/sam/repository/generics.go +++ b/sam/repository/util.go @@ -2,8 +2,10 @@ package repository import ( "context" + "encoding/json" "fmt" "github.com/titpetric/factory" + "time" ) func simpleUpdate(ctx context.Context, tableName, columnName string, value interface{}, id uint64) (err error) { @@ -38,3 +40,18 @@ func isFound(err error, valid bool, nerr error) error { return nil } + +func timeNowPtr() *time.Time { + n := time.Now() + return &n +} + +func coalesceJson(vals ...json.RawMessage) json.RawMessage { + for _, val := range vals { + if val != nil { + return val + } + } + + return nil +} diff --git a/sam/rest/channel.go b/sam/rest/channel.go index 5aaae70ba..aa8dc6f95 100644 --- a/sam/rest/channel.go +++ b/sam/rest/channel.go @@ -3,11 +3,9 @@ package rest import ( "context" - "github.com/pkg/errors" - "github.com/titpetric/factory" - "github.com/crusttech/crust/sam/rest/server" "github.com/crusttech/crust/sam/types" + "github.com/pkg/errors" ) var _ = errors.Wrap @@ -36,21 +34,19 @@ func (Channel) New(channelSvc channelService) *Channel { } func (ctrl *Channel) Create(ctx context.Context, r *server.ChannelCreateRequest) (interface{}, error) { - channel := types.Channel{}. - New(). - SetName(r.Name). - SetTopic(r.Topic). - SetMeta([]byte("{}")). - SetID(factory.Sonyflake.NextID()) + channel := &types.Channel{ + Name: r.Name, + Topic: r.Topic, + } return ctrl.svc.Create(ctx, channel) } func (ctrl *Channel) Edit(ctx context.Context, r *server.ChannelEditRequest) (interface{}, error) { - channel := types.Channel{}. - New(). - SetName(r.Name). - SetTopic(r.Topic) + channel := &types.Channel{ + Name: r.Name, + Topic: r.Topic, + } return ctrl.svc.Update(ctx, channel) diff --git a/sam/rest/message.go b/sam/rest/message.go index fec60fc56..68f0b74b2 100644 --- a/sam/rest/message.go +++ b/sam/rest/message.go @@ -43,9 +43,10 @@ func (Message) New(messageSvc messageService) *Message { } func (ctrl *Message) Create(ctx context.Context, r *server.MessageCreateRequest) (interface{}, error) { - return ctrl.svc.Create(ctx, (&types.Message{}). - SetChannelID(r.ChannelID). - SetMessage(r.Message)) + return ctrl.svc.Create(ctx, &types.Message{ + ChannelID: r.ChannelID, + Message: r.Message, + }) } func (ctrl *Message) History(ctx context.Context, r *server.MessageHistoryRequest) (interface{}, error) { @@ -56,10 +57,11 @@ func (ctrl *Message) History(ctx context.Context, r *server.MessageHistoryReques } func (ctrl *Message) Edit(ctx context.Context, r *server.MessageEditRequest) (interface{}, error) { - return ctrl.svc.Update(ctx, (&types.Message{}). - SetID(r.MessageID). - SetChannelID(r.ChannelID). - SetMessage(r.Message)) + return ctrl.svc.Update(ctx, &types.Message{ + ID: r.MessageID, + ChannelID: r.ChannelID, + Message: r.Message, + }) } func (ctrl *Message) Delete(ctx context.Context, r *server.MessageDeleteRequest) (interface{}, error) { diff --git a/sam/rest/organisation.go b/sam/rest/organisation.go index 425352ed2..3f05b083b 100644 --- a/sam/rest/organisation.go +++ b/sam/rest/organisation.go @@ -41,18 +41,18 @@ func (ctrl *Organisation) List(ctx context.Context, r *server.OrganisationListRe } func (ctrl *Organisation) Create(ctx context.Context, r *server.OrganisationCreateRequest) (interface{}, error) { - org := types.Organisation{}. - New(). - SetName(r.Name) + org := &types.Organisation{ + Name: r.Name, + } return ctrl.svc.Create(ctx, org) } func (ctrl *Organisation) Edit(ctx context.Context, r *server.OrganisationEditRequest) (interface{}, error) { - org := types.Organisation{}. - New(). - SetID(r.ID). - SetName(r.Name) + org := &types.Organisation{ + ID: r.ID, + Name: r.Name, + } return ctrl.svc.Update(ctx, org) } diff --git a/sam/rest/team.go b/sam/rest/team.go index 2942cdc17..6f5af5367 100644 --- a/sam/rest/team.go +++ b/sam/rest/team.go @@ -43,18 +43,18 @@ func (ctrl *Team) List(ctx context.Context, r *server.TeamListRequest) (interfac } func (ctrl *Team) Create(ctx context.Context, r *server.TeamCreateRequest) (interface{}, error) { - org := types.Team{}. - New(). - SetName(r.Name) + org := &types.Team{ + Name: r.Name, + } return ctrl.svc.Create(ctx, org) } func (ctrl *Team) Edit(ctx context.Context, r *server.TeamEditRequest) (interface{}, error) { - org := types.Team{}. - New(). - SetID(r.TeamID). - SetName(r.Name) + org := &types.Team{ + ID: r.TeamID, + Name: r.Name, + } return ctrl.svc.Update(ctx, org) } diff --git a/sam/service/channel.go b/sam/service/channel.go index d979ebfb8..7a0d68ee5 100644 --- a/sam/service/channel.go +++ b/sam/service/channel.go @@ -42,11 +42,11 @@ func (svc channel) Create(ctx context.Context, mod *types.Channel) (*types.Chann // @todo: topic channelEvent/log entry // @todo: channel name cmessage/log entry // @todo: permission check if channel can add channel - return svc.repository.Create(ctx, mod) } func (svc channel) Update(ctx context.Context, mod *types.Channel) (*types.Channel, error) { + // @todo: load current entry and merge changes // @todo: topic change channelEvent/log entry // @todo: channel name change channelEvent/log entry // @todo: permission check if current user can edit channel diff --git a/sam/service/message.go b/sam/service/message.go index 719248fe8..09ef62e32 100644 --- a/sam/service/message.go +++ b/sam/service/message.go @@ -2,6 +2,7 @@ package service import ( "context" + "github.com/crusttech/crust/auth" "github.com/crusttech/crust/sam/repository" "github.com/crusttech/crust/sam/types" ) @@ -49,7 +50,7 @@ func Message() *message { func (svc message) Find(ctx context.Context, filter *types.MessageFilter) ([]*types.Message, error) { // @todo get user from context - var currentUserID uint64 = 0 + var currentUserID uint64 = auth.GetIdentityFromContext(ctx).Identity() // @todo verify if current user can access & read from this channel _ = currentUserID @@ -60,7 +61,7 @@ func (svc message) Find(ctx context.Context, filter *types.MessageFilter) ([]*ty func (svc message) Create(ctx context.Context, mod *types.Message) (*types.Message, error) { // @todo get user from context - var currentUserID uint64 = 0 + var currentUserID uint64 = auth.GetIdentityFromContext(ctx).Identity() // @todo verify if current user can access & write to this channel _ = currentUserID @@ -70,7 +71,7 @@ func (svc message) Create(ctx context.Context, mod *types.Message) (*types.Messa func (svc message) Update(ctx context.Context, mod *types.Message) (*types.Message, error) { // @todo get user from context - var currentUserID uint64 = 0 + var currentUserID uint64 = auth.GetIdentityFromContext(ctx).Identity() // @todo verify if current user can access & write to this channel _ = currentUserID @@ -84,7 +85,7 @@ func (svc message) Update(ctx context.Context, mod *types.Message) (*types.Messa func (svc message) Delete(ctx context.Context, id uint64) error { // @todo get user from context - var currentUserID uint64 = 0 + var currentUserID uint64 = auth.GetIdentityFromContext(ctx).Identity() // @todo verify if current user can access & write to this channel _ = currentUserID @@ -98,7 +99,7 @@ func (svc message) Delete(ctx context.Context, id uint64) error { func (svc message) React(ctx context.Context, messageID uint64, reaction string) error { // @todo get user from context - var currentUserID uint64 = 0 + var currentUserID uint64 = auth.GetIdentityFromContext(ctx).Identity() // @todo verify if current user can access & write to this channel var m *types.Message @@ -121,7 +122,7 @@ func (svc message) React(ctx context.Context, messageID uint64, reaction string) func (svc message) Unreact(ctx context.Context, messageID uint64, reaction string) error { // @todo get user from context - var currentUserID uint64 = 0 + var currentUserID uint64 = auth.GetIdentityFromContext(ctx).Identity() // @todo verify if current user can access & write to this channel _ = currentUserID @@ -134,7 +135,7 @@ func (svc message) Unreact(ctx context.Context, messageID uint64, reaction strin func (svc message) Pin(ctx context.Context, messageID uint64) error { // @todo get user from context - var currentUserID uint64 = 0 + var currentUserID uint64 = auth.GetIdentityFromContext(ctx).Identity() // @todo verify if current user can access & write to this channel _ = currentUserID @@ -144,7 +145,7 @@ func (svc message) Pin(ctx context.Context, messageID uint64) error { func (svc message) Unpin(ctx context.Context, messageID uint64) error { // @todo get user from context - var currentUserID uint64 = 0 + var currentUserID uint64 = auth.GetIdentityFromContext(ctx).Identity() // @todo verify if current user can access & write to this channel _ = currentUserID @@ -154,7 +155,7 @@ func (svc message) Unpin(ctx context.Context, messageID uint64) error { func (svc message) Flag(ctx context.Context, messageID uint64) error { // @todo get user from context - var currentUserID uint64 = 0 + var currentUserID uint64 = auth.GetIdentityFromContext(ctx).Identity() // @todo verify if current user can access & write to this channel _ = currentUserID @@ -164,7 +165,7 @@ func (svc message) Flag(ctx context.Context, messageID uint64) error { func (svc message) Unflag(ctx context.Context, messageID uint64) error { // @todo get user from context - var currentUserID uint64 = 0 + var currentUserID uint64 = auth.GetIdentityFromContext(ctx).Identity() // @todo verify if current user can access & write to this channel _ = currentUserID @@ -176,7 +177,7 @@ func (svc message) Attach(ctx context.Context) (*types.Attachment, error) { // @todo define func signature // @todo get user from context - var currentUserID uint64 = 0 + var currentUserID uint64 = auth.GetIdentityFromContext(ctx).Identity() // @todo verify if current user can access & write to this channel _ = currentUserID @@ -186,7 +187,7 @@ func (svc message) Attach(ctx context.Context) (*types.Attachment, error) { func (svc message) Detach(ctx context.Context, attachmentID uint64) error { // @todo get user from context - var currentUserID uint64 = 0 + var currentUserID uint64 = auth.GetIdentityFromContext(ctx).Identity() // @todo verify if current user can access & write to this channel _ = currentUserID diff --git a/sam/service/user.go b/sam/service/user.go index 7151ef710..91a6dbcd4 100644 --- a/sam/service/user.go +++ b/sam/service/user.go @@ -78,7 +78,7 @@ func (svc user) generatePassword(user *types.User, password string) error { return err } - user.SetPassword(pwd) + user.Password = pwd return nil } diff --git a/sam/types/attachment.go b/sam/types/attachment.go new file mode 100644 index 000000000..2ffa55520 --- /dev/null +++ b/sam/types/attachment.go @@ -0,0 +1,24 @@ +package types + +import ( + "encoding/json" + "time" +) + +type ( + Attachment struct { + ID uint64 `db:"id"` + UserID uint64 `db:"rel_user"` + MessageID uint64 `db:"rel_message"` + ChannelID uint64 `db:"rel_channel"` + Attachment json.RawMessage `db:"attachment"` + Url string `db:"url"` + PreviewUrl string `db:"preview_url"` + Size uint `db:"size"` + Mimetype string `db:"mimetype"` + Name string `db:"name"` + CreatedAt time.Time `json:"created_at,omitempty" db:"created_at"` + UpdatedAt *time.Time `json:"updated_at,omitempty" db:"updated_at"` + DeletedAt *time.Time `json:"deleted_at,omitempty" db:"deleted_at"` + } +) diff --git a/sam/types/auth.go b/sam/types/auth.go deleted file mode 100644 index dc640a349..000000000 --- a/sam/types/auth.go +++ /dev/null @@ -1,16 +0,0 @@ -package types - -/* - Hello! This file is auto-generated from `docs/src/spec.json`. - - For development: - In order to update the generated files, edit this file under the location, - add your struct fields, imports, API definitions and whatever you want, and: - - 1. run [spec](https://github.com/titpetric/spec) in the same folder, - 2. run `./_gen.php` in this folder. - - You may edit `auth.go`, `auth.util.go` or `auth_test.go` to - implement your API calls, helper functions and tests. The file `auth.go` - is only generated the first time, and will not be overwritten if it exists. -*/ diff --git a/sam/types/channel.go b/sam/types/channel.go index c0fb56c6c..b65758d21 100644 --- a/sam/types/channel.go +++ b/sam/types/channel.go @@ -1,27 +1,11 @@ package types -/* - Hello! This file is auto-generated from `docs/src/spec.json`. - - For development: - In order to update the generated files, edit this file under the location, - add your struct fields, imports, API definitions and whatever you want, and: - - 1. run [spec](https://github.com/titpetric/spec) in the same folder, - 2. run `./_gen.php` in this folder. - - You may edit `channel.go`, `channel.util.go` or `channel_test.go` to - implement your API calls, helper functions and tests. The file `channel.go` - is only generated the first time, and will not be overwritten if it exists. -*/ - import ( "encoding/json" "time" ) type ( - // Channels - A channel is a representation of a sequence of messages. It has meta data like channel subject. Channels may be public, private or direct (between two users). Channel struct { ID uint64 `db:"id"` Name string `db:"name"` @@ -32,133 +16,9 @@ type ( UpdatedAt *time.Time `json:"updated_at,omitempty" db:"updated_at"` ArchivedAt *time.Time `json:"archived_at,omitempty" db:"archived_at"` DeletedAt *time.Time `json:"deleted_at,omitempty" db:"deleted_at"` + } - changed []string + ChannelFilter struct { + Query string } ) - -// New constructs a new instance of Channel -func (Channel) New() *Channel { - return &Channel{} -} - -// Get the value of ID -func (c *Channel) GetID() uint64 { - return c.ID -} - -// Set the value of ID -func (c *Channel) SetID(value uint64) *Channel { - if c.ID != value { - c.changed = append(c.changed, "ID") - c.ID = value - } - return c -} - -// Get the value of Name -func (c *Channel) GetName() string { - return c.Name -} - -// Set the value of Name -func (c *Channel) SetName(value string) *Channel { - if c.Name != value { - c.changed = append(c.changed, "Name") - c.Name = value - } - return c -} - -// Get the value of Topic -func (c *Channel) GetTopic() string { - return c.Topic -} - -// Set the value of Topic -func (c *Channel) SetTopic(value string) *Channel { - if c.Topic != value { - c.changed = append(c.changed, "Topic") - c.Topic = value - } - return c -} - -// Get the value of Meta -func (c *Channel) GetMeta() json.RawMessage { - return c.Meta -} - -// Set the value of Meta -func (c *Channel) SetMeta(value json.RawMessage) *Channel { - c.changed = append(c.changed, "Meta") - c.Meta = value - return c -} - -// Get the value of LastMessageID -func (c *Channel) GetLastMessageID() uint64 { - return c.LastMessageID -} - -// Set the value of LastMessageID -func (c *Channel) SetLastMessageID(value uint64) *Channel { - if c.LastMessageID != value { - c.changed = append(c.changed, "LastMessageID") - c.LastMessageID = value - } - return c -} - -// Get the value of CreatedAt -func (c *Channel) GetCreatedAt() time.Time { - return c.CreatedAt -} - -// Set the value of CreatedAt -func (c *Channel) SetCreatedAt(value time.Time) *Channel { - c.changed = append(c.changed, "CreatedAt") - c.CreatedAt = value - return c -} - -// Get the value of UpdatedAt -func (c *Channel) GetUpdatedAt() *time.Time { - return c.UpdatedAt -} - -// Set the value of UpdatedAt -func (c *Channel) SetUpdatedAt(value *time.Time) *Channel { - c.changed = append(c.changed, "UpdatedAt") - c.UpdatedAt = value - return c -} - -// Get the value of ArchivedAt -func (c *Channel) GetArchivedAt() *time.Time { - return c.ArchivedAt -} - -// Set the value of ArchivedAt -func (c *Channel) SetArchivedAt(value *time.Time) *Channel { - c.changed = append(c.changed, "ArchivedAt") - c.ArchivedAt = value - return c -} - -// Get the value of DeletedAt -func (c *Channel) GetDeletedAt() *time.Time { - return c.DeletedAt -} - -// Set the value of DeletedAt -func (c *Channel) SetDeletedAt(value *time.Time) *Channel { - c.changed = append(c.changed, "DeletedAt") - c.DeletedAt = value - return c -} - -// Changes returns the names of changed fields -func (c *Channel) Changes() []string { - return c.changed -} diff --git a/sam/types/channel.util.go b/sam/types/channel.util.go deleted file mode 100644 index 6b3b977ea..000000000 --- a/sam/types/channel.util.go +++ /dev/null @@ -1,7 +0,0 @@ -package types - -type ( - ChannelFilter struct { - Query string - } -) diff --git a/sam/types/message.go b/sam/types/message.go index 64fc1b810..73b5e0224 100644 --- a/sam/types/message.go +++ b/sam/types/message.go @@ -1,27 +1,10 @@ package types -/* - Hello! This file is auto-generated from `docs/src/spec.json`. - - For development: - In order to update the generated files, edit this file under the location, - add your struct fields, imports, API definitions and whatever you want, and: - - 1. run [spec](https://github.com/titpetric/spec) in the same folder, - 2. run `./_gen.php` in this folder. - - You may edit `message.go`, `message.util.go` or `message_test.go` to - implement your API calls, helper functions and tests. The file `message.go` - is only generated the first time, and will not be overwritten if it exists. -*/ - import ( - "encoding/json" "time" ) type ( - // Messages - Message struct { ID uint64 `db:"id"` Type string `db:"type"` @@ -32,444 +15,13 @@ type ( CreatedAt time.Time `json:"created_at,omitempty" db:"created_at"` UpdatedAt *time.Time `json:"updated_at,omitempty" db:"updated_at"` DeletedAt *time.Time `json:"deleted_at,omitempty" db:"deleted_at"` - - changed []string } - // Messages - - Reaction struct { - ID uint64 `db:"id"` - UserID uint64 `db:"rel_user"` - MessageID uint64 `db:"rel_message"` - ChannelID uint64 `db:"rel_channel"` - Reaction string `db:"reaction"` - CreatedAt time.Time `json:"created_at,omitempty" db:"created_at"` - - changed []string - } - - // Messages - - Attachment struct { - ID uint64 `db:"id"` - UserID uint64 `db:"rel_user"` - MessageID uint64 `db:"rel_message"` - ChannelID uint64 `db:"rel_channel"` - Attachment json.RawMessage `db:"attachment"` - Url string `db:"url"` - PreviewUrl string `db:"preview_url"` - Size uint `db:"size"` - Mimetype string `db:"mimetype"` - Name string `db:"name"` - CreatedAt time.Time `json:"created_at,omitempty" db:"created_at"` - UpdatedAt *time.Time `json:"updated_at,omitempty" db:"updated_at"` - DeletedAt *time.Time `json:"deleted_at,omitempty" db:"deleted_at"` - - changed []string + MessageFilter struct { + Query string + ChannelID uint64 + FromMessageID uint64 + UntilMessageID uint64 + Limit uint } ) - -// New constructs a new instance of Message -func (Message) New() *Message { - return &Message{} -} - -// New constructs a new instance of Reaction -func (Reaction) New() *Reaction { - return &Reaction{} -} - -// New constructs a new instance of Attachment -func (Attachment) New() *Attachment { - return &Attachment{} -} - -// Get the value of ID -func (m *Message) GetID() uint64 { - return m.ID -} - -// Set the value of ID -func (m *Message) SetID(value uint64) *Message { - if m.ID != value { - m.changed = append(m.changed, "ID") - m.ID = value - } - return m -} - -// Get the value of Type -func (m *Message) GetType() string { - return m.Type -} - -// Set the value of Type -func (m *Message) SetType(value string) *Message { - if m.Type != value { - m.changed = append(m.changed, "Type") - m.Type = value - } - return m -} - -// Get the value of Message -func (m *Message) GetMessage() string { - return m.Message -} - -// Set the value of Message -func (m *Message) SetMessage(value string) *Message { - if m.Message != value { - m.changed = append(m.changed, "Message") - m.Message = value - } - return m -} - -// Get the value of UserID -func (m *Message) GetUserID() uint64 { - return m.UserID -} - -// Set the value of UserID -func (m *Message) SetUserID(value uint64) *Message { - if m.UserID != value { - m.changed = append(m.changed, "UserID") - m.UserID = value - } - return m -} - -// Get the value of ChannelID -func (m *Message) GetChannelID() uint64 { - return m.ChannelID -} - -// Set the value of ChannelID -func (m *Message) SetChannelID(value uint64) *Message { - if m.ChannelID != value { - m.changed = append(m.changed, "ChannelID") - m.ChannelID = value - } - return m -} - -// Get the value of ReplyTo -func (m *Message) GetReplyTo() uint64 { - return m.ReplyTo -} - -// Set the value of ReplyTo -func (m *Message) SetReplyTo(value uint64) *Message { - if m.ReplyTo != value { - m.changed = append(m.changed, "ReplyTo") - m.ReplyTo = value - } - return m -} - -// Get the value of CreatedAt -func (m *Message) GetCreatedAt() time.Time { - return m.CreatedAt -} - -// Set the value of CreatedAt -func (m *Message) SetCreatedAt(value time.Time) *Message { - m.changed = append(m.changed, "CreatedAt") - m.CreatedAt = value - return m -} - -// Get the value of UpdatedAt -func (m *Message) GetUpdatedAt() *time.Time { - return m.UpdatedAt -} - -// Set the value of UpdatedAt -func (m *Message) SetUpdatedAt(value *time.Time) *Message { - m.changed = append(m.changed, "UpdatedAt") - m.UpdatedAt = value - return m -} - -// Get the value of DeletedAt -func (m *Message) GetDeletedAt() *time.Time { - return m.DeletedAt -} - -// Set the value of DeletedAt -func (m *Message) SetDeletedAt(value *time.Time) *Message { - m.changed = append(m.changed, "DeletedAt") - m.DeletedAt = value - return m -} - -// Changes returns the names of changed fields -func (m *Message) Changes() []string { - return m.changed -} - -// Get the value of ID -func (m *Reaction) GetID() uint64 { - return m.ID -} - -// Set the value of ID -func (m *Reaction) SetID(value uint64) *Reaction { - if m.ID != value { - m.changed = append(m.changed, "ID") - m.ID = value - } - return m -} - -// Get the value of UserID -func (m *Reaction) GetUserID() uint64 { - return m.UserID -} - -// Set the value of UserID -func (m *Reaction) SetUserID(value uint64) *Reaction { - if m.UserID != value { - m.changed = append(m.changed, "UserID") - m.UserID = value - } - return m -} - -// Get the value of MessageID -func (m *Reaction) GetMessageID() uint64 { - return m.MessageID -} - -// Set the value of MessageID -func (m *Reaction) SetMessageID(value uint64) *Reaction { - if m.MessageID != value { - m.changed = append(m.changed, "MessageID") - m.MessageID = value - } - return m -} - -// Get the value of ChannelID -func (m *Reaction) GetChannelID() uint64 { - return m.ChannelID -} - -// Set the value of ChannelID -func (m *Reaction) SetChannelID(value uint64) *Reaction { - if m.ChannelID != value { - m.changed = append(m.changed, "ChannelID") - m.ChannelID = value - } - return m -} - -// Get the value of Reaction -func (m *Reaction) GetReaction() string { - return m.Reaction -} - -// Set the value of Reaction -func (m *Reaction) SetReaction(value string) *Reaction { - if m.Reaction != value { - m.changed = append(m.changed, "Reaction") - m.Reaction = value - } - return m -} - -// Get the value of CreatedAt -func (m *Reaction) GetCreatedAt() time.Time { - return m.CreatedAt -} - -// Set the value of CreatedAt -func (m *Reaction) SetCreatedAt(value time.Time) *Reaction { - m.changed = append(m.changed, "CreatedAt") - m.CreatedAt = value - return m -} - -// Changes returns the names of changed fields -func (m *Reaction) Changes() []string { - return m.changed -} - -// Get the value of ID -func (m *Attachment) GetID() uint64 { - return m.ID -} - -// Set the value of ID -func (m *Attachment) SetID(value uint64) *Attachment { - if m.ID != value { - m.changed = append(m.changed, "ID") - m.ID = value - } - return m -} - -// Get the value of UserID -func (m *Attachment) GetUserID() uint64 { - return m.UserID -} - -// Set the value of UserID -func (m *Attachment) SetUserID(value uint64) *Attachment { - if m.UserID != value { - m.changed = append(m.changed, "UserID") - m.UserID = value - } - return m -} - -// Get the value of MessageID -func (m *Attachment) GetMessageID() uint64 { - return m.MessageID -} - -// Set the value of MessageID -func (m *Attachment) SetMessageID(value uint64) *Attachment { - if m.MessageID != value { - m.changed = append(m.changed, "MessageID") - m.MessageID = value - } - return m -} - -// Get the value of ChannelID -func (m *Attachment) GetChannelID() uint64 { - return m.ChannelID -} - -// Set the value of ChannelID -func (m *Attachment) SetChannelID(value uint64) *Attachment { - if m.ChannelID != value { - m.changed = append(m.changed, "ChannelID") - m.ChannelID = value - } - return m -} - -// Get the value of Attachment -func (m *Attachment) GetAttachment() json.RawMessage { - return m.Attachment -} - -// Set the value of Attachment -func (m *Attachment) SetAttachment(value json.RawMessage) *Attachment { - m.changed = append(m.changed, "Attachment") - m.Attachment = value - return m -} - -// Get the value of Url -func (m *Attachment) GetUrl() string { - return m.Url -} - -// Set the value of Url -func (m *Attachment) SetUrl(value string) *Attachment { - if m.Url != value { - m.changed = append(m.changed, "Url") - m.Url = value - } - return m -} - -// Get the value of PreviewUrl -func (m *Attachment) GetPreviewUrl() string { - return m.PreviewUrl -} - -// Set the value of PreviewUrl -func (m *Attachment) SetPreviewUrl(value string) *Attachment { - if m.PreviewUrl != value { - m.changed = append(m.changed, "PreviewUrl") - m.PreviewUrl = value - } - return m -} - -// Get the value of Size -func (m *Attachment) GetSize() uint { - return m.Size -} - -// Set the value of Size -func (m *Attachment) SetSize(value uint) *Attachment { - if m.Size != value { - m.changed = append(m.changed, "Size") - m.Size = value - } - return m -} - -// Get the value of Mimetype -func (m *Attachment) GetMimetype() string { - return m.Mimetype -} - -// Set the value of Mimetype -func (m *Attachment) SetMimetype(value string) *Attachment { - if m.Mimetype != value { - m.changed = append(m.changed, "Mimetype") - m.Mimetype = value - } - return m -} - -// Get the value of Name -func (m *Attachment) GetName() string { - return m.Name -} - -// Set the value of Name -func (m *Attachment) SetName(value string) *Attachment { - if m.Name != value { - m.changed = append(m.changed, "Name") - m.Name = value - } - return m -} - -// Get the value of CreatedAt -func (m *Attachment) GetCreatedAt() time.Time { - return m.CreatedAt -} - -// Set the value of CreatedAt -func (m *Attachment) SetCreatedAt(value time.Time) *Attachment { - m.changed = append(m.changed, "CreatedAt") - m.CreatedAt = value - return m -} - -// Get the value of UpdatedAt -func (m *Attachment) GetUpdatedAt() *time.Time { - return m.UpdatedAt -} - -// Set the value of UpdatedAt -func (m *Attachment) SetUpdatedAt(value *time.Time) *Attachment { - m.changed = append(m.changed, "UpdatedAt") - m.UpdatedAt = value - return m -} - -// Get the value of DeletedAt -func (m *Attachment) GetDeletedAt() *time.Time { - return m.DeletedAt -} - -// Set the value of DeletedAt -func (m *Attachment) SetDeletedAt(value *time.Time) *Attachment { - m.changed = append(m.changed, "DeletedAt") - m.DeletedAt = value - return m -} - -// Changes returns the names of changed fields -func (m *Attachment) Changes() []string { - return m.changed -} diff --git a/sam/types/message_filter.go b/sam/types/message_filter.go deleted file mode 100644 index 1e76634db..000000000 --- a/sam/types/message_filter.go +++ /dev/null @@ -1,11 +0,0 @@ -package types - -type ( - MessageFilter struct { - Query string - ChannelID uint64 - FromMessageID uint64 - UntilMessageID uint64 - Limit uint - } -) diff --git a/sam/types/organisation.go b/sam/types/organisation.go index c58e00285..226cb76bb 100644 --- a/sam/types/organisation.go +++ b/sam/types/organisation.go @@ -1,20 +1,5 @@ package types -/* - Hello! This file is auto-generated from `docs/src/spec.json`. - - For development: - In order to update the generated files, edit this file under the location, - add your struct fields, imports, API definitions and whatever you want, and: - - 1. run [spec](https://github.com/titpetric/spec) in the same folder, - 2. run `./_gen.php` in this folder. - - You may edit `organisation.go`, `organisation.util.go` or `organisation_test.go` to - implement your API calls, helper functions and tests. The file `organisation.go` - is only generated the first time, and will not be overwritten if it exists. -*/ - import ( "time" ) @@ -29,107 +14,9 @@ type ( UpdatedAt *time.Time `json:"updated_at,omitempty" db:"updated_at"` ArchivedAt *time.Time `json:"archived_at,omitempty" db:"archived_at"` DeletedAt *time.Time `json:"deleted_at,omitempty" db:"deleted_at"` + } - changed []string + OrganisationFilter struct { + Query string } ) - -// New constructs a new instance of Organisation -func (Organisation) New() *Organisation { - return &Organisation{} -} - -// Get the value of ID -func (o *Organisation) GetID() uint64 { - return o.ID -} - -// Set the value of ID -func (o *Organisation) SetID(value uint64) *Organisation { - if o.ID != value { - o.changed = append(o.changed, "ID") - o.ID = value - } - return o -} - -// Get the value of FQN -func (o *Organisation) GetFQN() string { - return o.FQN -} - -// Set the value of FQN -func (o *Organisation) SetFQN(value string) *Organisation { - if o.FQN != value { - o.changed = append(o.changed, "FQN") - o.FQN = value - } - return o -} - -// Get the value of Name -func (o *Organisation) GetName() string { - return o.Name -} - -// Set the value of Name -func (o *Organisation) SetName(value string) *Organisation { - if o.Name != value { - o.changed = append(o.changed, "Name") - o.Name = value - } - return o -} - -// Get the value of CreatedAt -func (o *Organisation) GetCreatedAt() time.Time { - return o.CreatedAt -} - -// Set the value of CreatedAt -func (o *Organisation) SetCreatedAt(value time.Time) *Organisation { - o.changed = append(o.changed, "CreatedAt") - o.CreatedAt = value - return o -} - -// Get the value of UpdatedAt -func (o *Organisation) GetUpdatedAt() *time.Time { - return o.UpdatedAt -} - -// Set the value of UpdatedAt -func (o *Organisation) SetUpdatedAt(value *time.Time) *Organisation { - o.changed = append(o.changed, "UpdatedAt") - o.UpdatedAt = value - return o -} - -// Get the value of ArchivedAt -func (o *Organisation) GetArchivedAt() *time.Time { - return o.ArchivedAt -} - -// Set the value of ArchivedAt -func (o *Organisation) SetArchivedAt(value *time.Time) *Organisation { - o.changed = append(o.changed, "ArchivedAt") - o.ArchivedAt = value - return o -} - -// Get the value of DeletedAt -func (o *Organisation) GetDeletedAt() *time.Time { - return o.DeletedAt -} - -// Set the value of DeletedAt -func (o *Organisation) SetDeletedAt(value *time.Time) *Organisation { - o.changed = append(o.changed, "DeletedAt") - o.DeletedAt = value - return o -} - -// Changes returns the names of changed fields -func (o *Organisation) Changes() []string { - return o.changed -} diff --git a/sam/types/organisation_filter.go b/sam/types/organisation_filter.go deleted file mode 100644 index 126eab30e..000000000 --- a/sam/types/organisation_filter.go +++ /dev/null @@ -1,7 +0,0 @@ -package types - -type ( - OrganisationFilter struct { - Query string - } -) diff --git a/sam/types/reaction.go b/sam/types/reaction.go new file mode 100644 index 000000000..e76416846 --- /dev/null +++ b/sam/types/reaction.go @@ -0,0 +1,16 @@ +package types + +import ( + "time" +) + +type ( + Reaction struct { + ID uint64 `db:"id"` + UserID uint64 `db:"rel_user"` + MessageID uint64 `db:"rel_message"` + ChannelID uint64 `db:"rel_channel"` + Reaction string `db:"reaction"` + CreatedAt time.Time `json:"created_at,omitempty" db:"created_at"` + } +) diff --git a/sam/types/team.go b/sam/types/team.go index 7c8c0f029..fcd150713 100644 --- a/sam/types/team.go +++ b/sam/types/team.go @@ -1,20 +1,5 @@ package types -/* - Hello! This file is auto-generated from `docs/src/spec.json`. - - For development: - In order to update the generated files, edit this file under the location, - add your struct fields, imports, API definitions and whatever you want, and: - - 1. run [spec](https://github.com/titpetric/spec) in the same folder, - 2. run `./_gen.php` in this folder. - - You may edit `team.go`, `team.util.go` or `team_test.go` to - implement your API calls, helper functions and tests. The file `team.go` - is only generated the first time, and will not be overwritten if it exists. -*/ - import ( "time" ) @@ -29,107 +14,9 @@ type ( UpdatedAt *time.Time `json:"updated_at,omitempty" db:"updated_at"` ArchivedAt *time.Time `json:"archived_at,omitempty" db:"archived_at"` DeletedAt *time.Time `json:"deleted_at,omitempty" db:"deleted_at"` + } - changed []string + TeamFilter struct { + Query string } ) - -// New constructs a new instance of Team -func (Team) New() *Team { - return &Team{} -} - -// Get the value of ID -func (t *Team) GetID() uint64 { - return t.ID -} - -// Set the value of ID -func (t *Team) SetID(value uint64) *Team { - if t.ID != value { - t.changed = append(t.changed, "ID") - t.ID = value - } - return t -} - -// Get the value of Name -func (t *Team) GetName() string { - return t.Name -} - -// Set the value of Name -func (t *Team) SetName(value string) *Team { - if t.Name != value { - t.changed = append(t.changed, "Name") - t.Name = value - } - return t -} - -// Get the value of Handle -func (t *Team) GetHandle() string { - return t.Handle -} - -// Set the value of Handle -func (t *Team) SetHandle(value string) *Team { - if t.Handle != value { - t.changed = append(t.changed, "Handle") - t.Handle = value - } - return t -} - -// Get the value of CreatedAt -func (t *Team) GetCreatedAt() time.Time { - return t.CreatedAt -} - -// Set the value of CreatedAt -func (t *Team) SetCreatedAt(value time.Time) *Team { - t.changed = append(t.changed, "CreatedAt") - t.CreatedAt = value - return t -} - -// Get the value of UpdatedAt -func (t *Team) GetUpdatedAt() *time.Time { - return t.UpdatedAt -} - -// Set the value of UpdatedAt -func (t *Team) SetUpdatedAt(value *time.Time) *Team { - t.changed = append(t.changed, "UpdatedAt") - t.UpdatedAt = value - return t -} - -// Get the value of ArchivedAt -func (t *Team) GetArchivedAt() *time.Time { - return t.ArchivedAt -} - -// Set the value of ArchivedAt -func (t *Team) SetArchivedAt(value *time.Time) *Team { - t.changed = append(t.changed, "ArchivedAt") - t.ArchivedAt = value - return t -} - -// Get the value of DeletedAt -func (t *Team) GetDeletedAt() *time.Time { - return t.DeletedAt -} - -// Set the value of DeletedAt -func (t *Team) SetDeletedAt(value *time.Time) *Team { - t.changed = append(t.changed, "DeletedAt") - t.DeletedAt = value - return t -} - -// Changes returns the names of changed fields -func (t *Team) Changes() []string { - return t.changed -} diff --git a/sam/types/team_filter.go b/sam/types/team_filter.go deleted file mode 100644 index 80266513a..000000000 --- a/sam/types/team_filter.go +++ /dev/null @@ -1,7 +0,0 @@ -package types - -type ( - TeamFilter struct { - Query string - } -) diff --git a/sam/types/user.go b/sam/types/user.go index b84747f35..f26491ba3 100644 --- a/sam/types/user.go +++ b/sam/types/user.go @@ -1,163 +1,33 @@ package types -/* - Hello! This file is auto-generated from `docs/src/spec.json`. - - For development: - In order to update the generated files, edit this file under the location, - add your struct fields, imports, API definitions and whatever you want, and: - - 1. run [spec](https://github.com/titpetric/spec) in the same folder, - 2. run `./_gen.php` in this folder. - - You may edit `user.go`, `user.util.go` or `user_test.go` to - implement your API calls, helper functions and tests. The file `user.go` - is only generated the first time, and will not be overwritten if it exists. -*/ - import ( + "encoding/json" "time" ) type ( - // Users - User struct { - ID uint64 `db:"id"` - Username string `db:"username"` - Meta interface{} `json:"-" db:"meta"` - OrganisationID uint64 `db:"rel_organisation"` - Password []byte `json:"-" db:"password"` - CreatedAt time.Time `json:"created_at,omitempty" db:"created_at"` - UpdatedAt *time.Time `json:"updated_at,omitempty" db:"updated_at"` - SuspendedAt *time.Time `json:"suspended_at,omitempty" db:"suspended_at"` - DeletedAt *time.Time `json:"deleted_at,omitempty" db:"deleted_at"` + ID uint64 `db:"id"` + Username string `db:"username"` + Meta json.RawMessage `json:"-" db:"meta"` + OrganisationID uint64 `db:"rel_organisation"` + Password []byte `json:"-" db:"password"` + CreatedAt time.Time `json:"created_at,omitempty" db:"created_at"` + UpdatedAt *time.Time `json:"updated_at,omitempty" db:"updated_at"` + SuspendedAt *time.Time `json:"suspended_at,omitempty" db:"suspended_at"` + DeletedAt *time.Time `json:"deleted_at,omitempty" db:"deleted_at"` + } - changed []string + UserFilter struct { + Query string + MembersOfChannel uint64 } ) -// New constructs a new instance of User -func (User) New() *User { - return &User{} +func (u *User) Valid() bool { + return u.ID > 0 && u.SuspendedAt == nil && u.DeletedAt == nil } -// Get the value of ID -func (u *User) GetID() uint64 { +func (u *User) Identity() uint64 { return u.ID } - -// Set the value of ID -func (u *User) SetID(value uint64) *User { - if u.ID != value { - u.changed = append(u.changed, "ID") - u.ID = value - } - return u -} - -// Get the value of Username -func (u *User) GetUsername() string { - return u.Username -} - -// Set the value of Username -func (u *User) SetUsername(value string) *User { - if u.Username != value { - u.changed = append(u.changed, "Username") - u.Username = value - } - return u -} - -// Get the value of Meta -func (u *User) GetMeta() interface{} { - return u.Meta -} - -// Set the value of Meta -func (u *User) SetMeta(value interface{}) *User { - if u.Meta != value { - u.changed = append(u.changed, "Meta") - u.Meta = value - } - return u -} - -// Get the value of OrganisationID -func (u *User) GetOrganisationID() uint64 { - return u.OrganisationID -} - -// Set the value of OrganisationID -func (u *User) SetOrganisationID(value uint64) *User { - if u.OrganisationID != value { - u.changed = append(u.changed, "OrganisationID") - u.OrganisationID = value - } - return u -} - -// Get the value of Password -func (u *User) GetPassword() []byte { - return u.Password -} - -// Set the value of Password -func (u *User) SetPassword(value []byte) *User { - u.changed = append(u.changed, "Password") - u.Password = value - return u -} - -// Get the value of CreatedAt -func (u *User) GetCreatedAt() time.Time { - return u.CreatedAt -} - -// Set the value of CreatedAt -func (u *User) SetCreatedAt(value time.Time) *User { - u.changed = append(u.changed, "CreatedAt") - u.CreatedAt = value - return u -} - -// Get the value of UpdatedAt -func (u *User) GetUpdatedAt() *time.Time { - return u.UpdatedAt -} - -// Set the value of UpdatedAt -func (u *User) SetUpdatedAt(value *time.Time) *User { - u.changed = append(u.changed, "UpdatedAt") - u.UpdatedAt = value - return u -} - -// Get the value of SuspendedAt -func (u *User) GetSuspendedAt() *time.Time { - return u.SuspendedAt -} - -// Set the value of SuspendedAt -func (u *User) SetSuspendedAt(value *time.Time) *User { - u.changed = append(u.changed, "SuspendedAt") - u.SuspendedAt = value - return u -} - -// Get the value of DeletedAt -func (u *User) GetDeletedAt() *time.Time { - return u.DeletedAt -} - -// Set the value of DeletedAt -func (u *User) SetDeletedAt(value *time.Time) *User { - u.changed = append(u.changed, "DeletedAt") - u.DeletedAt = value - return u -} - -// Changes returns the names of changed fields -func (u *User) Changes() []string { - return u.changed -} diff --git a/sam/types/user.util.go b/sam/types/user.util.go deleted file mode 100644 index 6fa58d0ac..000000000 --- a/sam/types/user.util.go +++ /dev/null @@ -1,5 +0,0 @@ -package types - -func (u *User) Valid() bool { - return u.ID > 0 && u.SuspendedAt == nil && u.DeletedAt == nil -} diff --git a/sam/types/user_filter.go b/sam/types/user_filter.go deleted file mode 100644 index 73ec3a954..000000000 --- a/sam/types/user_filter.go +++ /dev/null @@ -1,8 +0,0 @@ -package types - -type ( - UserFilter struct { - Query string - MembersOfChannel uint64 - } -) diff --git a/sam/websocket/incoming_channel.go b/sam/websocket/incoming_channel.go index 963e12df2..00c5eae5e 100644 --- a/sam/websocket/incoming_channel.go +++ b/sam/websocket/incoming_channel.go @@ -16,7 +16,7 @@ func (s *Session) channelJoin(ctx context.Context, p *incoming.ChannelJoin) erro // Telling all subscribers of the channel we're joining that we are joining. var chJoin = &outgoing.ChannelJoin{ ID: p.ChannelID, - UserID: uint64toa(auth.GetIdentityFromContext(ctx).GetID()), + UserID: uint64toa(auth.GetIdentityFromContext(ctx).Identity()), } // Send to all channel subscribers @@ -34,7 +34,7 @@ func (s *Session) channelPart(ctx context.Context, p *incoming.ChannelPart) erro // This payload will tell everyone that we're parting from ALL channels var chPart = &outgoing.ChannelPart{ ID: p.ChannelID, - UserID: uint64toa(auth.GetIdentityFromContext(ctx).GetID()), + UserID: uint64toa(auth.GetIdentityFromContext(ctx).Identity()), } s.sendToAllSubscribers(chPart, p.ChannelID) diff --git a/sam/websocket/websocket.go b/sam/websocket/websocket.go index ab2bf0100..7c22703de 100644 --- a/sam/websocket/websocket.go +++ b/sam/websocket/websocket.go @@ -62,11 +62,11 @@ func (ws Websocket) Open(w http.ResponseWriter, r *http.Request) { session := store.Save(Session{}.New(r.Context(), conn)) - session.sendToAll(&outgoing.Connected{UserID: uint64toa(identity.GetID())}) + session.sendToAll(&outgoing.Connected{UserID: uint64toa(identity.Identity())}) if err := session.Handle(); err != nil { // @todo: log error, because at this point we can't really write it to w } - session.sendToAll(&outgoing.Disconnected{UserID: uint64toa(identity.GetID())}) + session.sendToAll(&outgoing.Disconnected{UserID: uint64toa(identity.Identity())}) }