3
0

Support for channel add through WS

This commit is contained in:
Denis Arh 2018-08-07 21:11:42 +02:00
parent f61b706559
commit 636d2c05c7
6 changed files with 67 additions and 4 deletions

View File

@ -11,6 +11,11 @@ type (
ChannelID string `json:"cid"`
}
ChannelCreate struct {
Name string `json:"name"`
Topic string `json:"topic"`
}
ChannelRename struct {
ChannelID string `json:"cid"`
Name string `json:"name"`
@ -20,4 +25,8 @@ type (
ChannelID string `json:"cid"`
Topic string `json:"topic"`
}
ChannelDelete struct {
ChannelID string `json:"cid"`
}
)

View File

@ -8,6 +8,8 @@ type Payload struct {
*ChannelChangeTopic `json:"chct"`
*ChannelRename `json:"chrn"`
*ChannelCreate `json:"chcr"`
*ChannelDelete `json:"chdel"`
// Get channel message history
*MessageHistory `json:"chopen"`

View File

@ -21,6 +21,14 @@ type (
UserID string `json:"uid"`
}
ChannelDeleted struct {
// Channel that was deleted
ID string `json:"id"`
// Who deleted it
UserID string `json:"uid"`
}
Channel struct {
// Channel to part (nil) for ALL channels
ID string `json:"id"`
@ -40,6 +48,10 @@ func (p *ChannelPart) EncodeMessage() ([]byte, error) {
return json.Marshal(Payload{ChannelPart: p})
}
func (p *ChannelDeleted) EncodeMessage() ([]byte, error) {
return json.Marshal(Payload{ChannelDeleted: p})
}
func (p *Channel) EncodeMessage() ([]byte, error) {
return json.Marshal(Payload{Channel: p})
}

View File

@ -12,10 +12,11 @@ type (
*MessageUpdate `json:"messageUpdated,omitempty"`
*Messages `json:"messages,omitempty"`
*ChannelJoin `json:"channelJoin,omitempty"`
*ChannelPart `json:"channelPart,omitempty"`
*Channel `json:"channel,omitempty"`
*Channels `json:"channels,omitempty"`
*ChannelJoin `json:"channelJoin,omitempty"`
*ChannelPart `json:"channelPart,omitempty"`
*ChannelDeleted `json:"channelDeleted,omitempty"`
*Channel `json:"channel,omitempty"`
*Channels `json:"channels,omitempty"`
*User `json:"user,omitempty"`
*Users `json:"users,omitempty"`

View File

@ -32,6 +32,10 @@ func (s *Session) dispatch(raw []byte) (err error) {
return s.channelPart(ctx, p.ChannelPart)
case p.ChannelList != nil:
return s.channelList(ctx, p.ChannelList)
case p.ChannelCreate != nil:
return s.channelCreate(ctx, p.ChannelCreate)
case p.ChannelDelete != nil:
return s.channelDelete(ctx, p.ChannelDelete)
case p.ChannelRename != nil:
return s.channelRename(ctx, p.ChannelRename)
case p.ChannelChangeTopic != nil:

View File

@ -4,6 +4,7 @@ import (
"context"
"github.com/crusttech/crust/auth"
"github.com/crusttech/crust/sam/service"
"github.com/crusttech/crust/sam/types"
"github.com/crusttech/crust/sam/websocket/incoming"
"github.com/crusttech/crust/sam/websocket/outgoing"
)
@ -51,6 +52,40 @@ func (s *Session) channelList(ctx context.Context, p *incoming.ChannelList) erro
return s.sendReply(payloadFromChannels(channels))
}
func (s *Session) channelCreate(ctx context.Context, p *incoming.ChannelCreate) (err error) {
ch := &types.Channel{
Type: types.ChannelTypePublic,
Name: p.Name,
Topic: p.Topic,
}
ch, err = service.Channel().Create(ctx, ch)
if err != nil {
return err
}
pl := payloadFromChannel(ch)
if ch.Type == types.ChannelTypePublic {
return s.sendToAll(pl)
}
// By default, just send reply to user
return s.sendReply(pl)
}
func (s *Session) channelDelete(ctx context.Context, p *incoming.ChannelDelete) (err error) {
err = service.Channel().Delete(ctx, parseUInt64(p.ChannelID))
if err != nil {
return err
}
return s.sendToAllSubscribers(&outgoing.ChannelDeleted{
ID: p.ChannelID,
UserID: uint64toa(auth.GetIdentityFromContext(ctx).Identity()),
}, p.ChannelID)
}
func (s *Session) channelRename(ctx context.Context, p *incoming.ChannelRename) error {
ch, err := service.Channel().FindByID(ctx, parseUInt64(p.ChannelID))
if err != nil {