Setting channel topic & renaming
This commit is contained in:
+12
-4
@@ -50,7 +50,7 @@ func main() {
|
||||
}
|
||||
|
||||
if text[:1] == "/" {
|
||||
cmdSplit := strings.Split(text, " ")
|
||||
cmdSplit := strings.SplitN(text, " ", 2)
|
||||
switch cmdSplit[0] {
|
||||
case "/join":
|
||||
if len(cmdSplit) < 2 {
|
||||
@@ -58,7 +58,7 @@ func main() {
|
||||
} else {
|
||||
joinCh(conn, cmdSplit[1])
|
||||
chanId = cmdSplit[1]
|
||||
openCh(conn, chanId)
|
||||
msgHistory(conn, chanId)
|
||||
}
|
||||
case "/part":
|
||||
partCh(conn, chanId)
|
||||
@@ -67,11 +67,15 @@ func main() {
|
||||
case "/list":
|
||||
listCh(conn)
|
||||
|
||||
case "/raw":
|
||||
sendRaw(conn, cmdSplit[1])
|
||||
|
||||
default:
|
||||
println("Unknown command, try:")
|
||||
println(" /join <channel-id>")
|
||||
println(" /part")
|
||||
println(" /list")
|
||||
println(" /raw <raw json to send>")
|
||||
}
|
||||
} else {
|
||||
sendMsg(conn, text, chanId)
|
||||
@@ -119,8 +123,12 @@ func partCh(conn *websocket.Conn, channelId string) {
|
||||
should(conn.WriteMessage(websocket.TextMessage, pb))
|
||||
}
|
||||
|
||||
func openCh(conn *websocket.Conn, channelId string) {
|
||||
pb, err := json.Marshal(incoming.Payload{ChannelOpen: &incoming.ChannelOpen{ChannelID: channelId}})
|
||||
func msgHistory(conn *websocket.Conn, channelId string) {
|
||||
pb, err := json.Marshal(incoming.Payload{MessageHistory: &incoming.MessageHistory{ChannelID: channelId}})
|
||||
should(err)
|
||||
should(conn.WriteMessage(websocket.TextMessage, pb))
|
||||
}
|
||||
|
||||
func sendRaw(conn *websocket.Conn, msg string) {
|
||||
should(conn.WriteMessage(websocket.TextMessage, []byte(msg)))
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ func (svc message) Find(ctx context.Context, filter *types.MessageFilter) ([]*ty
|
||||
// @todo get user from context
|
||||
var currentUserID uint64 = 0
|
||||
|
||||
// @todo verify if current user can access & write to this channel
|
||||
// @todo verify if current user can access & read from this channel
|
||||
_ = currentUserID
|
||||
_ = filter.ChannelID
|
||||
|
||||
|
||||
@@ -22,6 +22,8 @@ func (s *Session) dispatch(raw []byte) (err error) {
|
||||
return s.messageUpdate(ctx, p.MessageUpdate)
|
||||
case p.MessageDelete != nil:
|
||||
return s.messageDelete(ctx, p.MessageDelete)
|
||||
case p.MessageHistory != nil:
|
||||
return s.messageHistory(ctx, p.MessageHistory)
|
||||
|
||||
// channel actions
|
||||
case p.ChannelJoin != nil:
|
||||
@@ -30,8 +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.ChannelOpen != nil:
|
||||
return s.channelOpen(ctx, p.ChannelOpen)
|
||||
case p.ChannelRename != nil:
|
||||
return s.channelRename(ctx, p.ChannelRename)
|
||||
case p.ChannelChangeTopic != nil:
|
||||
return s.channelChangeTopic(ctx, p.ChannelChangeTopic)
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -11,9 +11,13 @@ type (
|
||||
ChannelID string `json:"cid"`
|
||||
}
|
||||
|
||||
ChannelOpen struct {
|
||||
ChannelRename struct {
|
||||
ChannelID string `json:"cid"`
|
||||
Since string `json:"since,omitempty"`
|
||||
Until string `json:"until,omitempty"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
ChannelChangeTopic struct {
|
||||
ChannelID string `json:"cid"`
|
||||
Topic string `json:"topic"`
|
||||
}
|
||||
)
|
||||
|
||||
@@ -15,4 +15,10 @@ type (
|
||||
ChannelID string `json:"cid"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
MessageHistory struct {
|
||||
ChannelID string `json:"cid"`
|
||||
FromID string `json:"fid,omitempty"`
|
||||
UntilID string `json:"uid,omitempty"`
|
||||
}
|
||||
)
|
||||
|
||||
@@ -6,8 +6,11 @@ type Payload struct {
|
||||
*ChannelJoin `json:"chjoin"`
|
||||
*ChannelPart `json:"chpart"`
|
||||
|
||||
*ChannelChangeTopic `json:"chct"`
|
||||
*ChannelRename `json:"chrn"`
|
||||
|
||||
// Get channel message history
|
||||
*ChannelOpen `json:"chopen"`
|
||||
*MessageHistory `json:"chopen"`
|
||||
|
||||
// Message actions
|
||||
*MessageCreate `json:"msgcre"`
|
||||
|
||||
@@ -4,7 +4,6 @@ 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"
|
||||
)
|
||||
@@ -52,18 +51,44 @@ func (s *Session) channelList(ctx context.Context, p *incoming.ChannelList) erro
|
||||
return s.sendReply(payloadFromChannels(channels))
|
||||
}
|
||||
|
||||
func (s *Session) channelOpen(ctx context.Context, p *incoming.ChannelOpen) error {
|
||||
var (
|
||||
filter = &types.MessageFilter{
|
||||
ChannelID: parseUInt64(p.ChannelID),
|
||||
FromMessageID: parseUInt64(p.Since),
|
||||
}
|
||||
)
|
||||
|
||||
messages, err := service.Message().Find(ctx, filter)
|
||||
func (s *Session) channelRename(ctx context.Context, p *incoming.ChannelRename) error {
|
||||
ch, err := service.Channel().FindByID(ctx, parseUInt64(p.ChannelID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.sendReply(payloadFromMessages(messages))
|
||||
if ch.Name == p.Name {
|
||||
// No changes, ignore
|
||||
return nil
|
||||
}
|
||||
|
||||
ch.Name = p.Name
|
||||
|
||||
ch, err = service.Channel().Update(ctx, ch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.sendToAllSubscribers(payloadFromChannel(ch), p.ChannelID)
|
||||
}
|
||||
|
||||
func (s *Session) channelChangeTopic(ctx context.Context, p *incoming.ChannelChangeTopic) error {
|
||||
ch, err := service.Channel().FindByID(ctx, parseUInt64(p.ChannelID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ch.Topic == p.Topic {
|
||||
// No changes, ignore
|
||||
return nil
|
||||
}
|
||||
|
||||
ch.Topic = p.Topic
|
||||
|
||||
ch, err = service.Channel().Update(ctx, ch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.sendToAllSubscribers(payloadFromChannel(ch), p.ChannelID)
|
||||
}
|
||||
|
||||
@@ -56,3 +56,23 @@ func (s *Session) messageDelete(ctx context.Context, p *incoming.MessageDelete)
|
||||
|
||||
return s.sendToAllSubscribers(&outgoing.MessageDelete{ID: p.ID}, p.ChannelID)
|
||||
}
|
||||
|
||||
func (s *Session) messageHistory(ctx context.Context, p *incoming.MessageHistory) error {
|
||||
var (
|
||||
filter = &types.MessageFilter{
|
||||
ChannelID: parseUInt64(p.ChannelID),
|
||||
FromMessageID: parseUInt64(p.FromID),
|
||||
UntilMessageID: parseUInt64(p.UntilID),
|
||||
|
||||
// Max no. of messages we will return
|
||||
Limit: 50,
|
||||
}
|
||||
)
|
||||
|
||||
messages, err := service.Message().Find(ctx, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.sendReply(payloadFromMessages(messages))
|
||||
}
|
||||
|
||||
@@ -23,8 +23,10 @@ type (
|
||||
|
||||
Channel struct {
|
||||
// Channel to part (nil) for ALL channels
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Topic string `json:"topic"`
|
||||
LastMessageID string `json:"lastMessageId"`
|
||||
}
|
||||
|
||||
Channels []*Channel
|
||||
|
||||
@@ -3,17 +3,16 @@ package websocket
|
||||
import (
|
||||
"github.com/crusttech/crust/sam/types"
|
||||
"github.com/crusttech/crust/sam/websocket/outgoing"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func payloadFromMessage(msg *types.Message) *outgoing.Message {
|
||||
return &outgoing.Message{
|
||||
Message: msg.Message,
|
||||
ID: strconv.FormatUint(msg.ID, 10),
|
||||
ChannelID: strconv.FormatUint(msg.ChannelID, 10),
|
||||
ID: uint64toa(msg.ID),
|
||||
ChannelID: uint64toa(msg.ChannelID),
|
||||
Type: msg.Type,
|
||||
UserID: strconv.FormatUint(msg.UserID, 10),
|
||||
ReplyTo: strconv.FormatUint(msg.ReplyTo, 10),
|
||||
UserID: uint64toa(msg.UserID),
|
||||
ReplyTo: uint64toa(msg.ReplyTo),
|
||||
|
||||
CreatedAt: msg.CreatedAt,
|
||||
UpdatedAt: msg.UpdatedAt,
|
||||
@@ -31,8 +30,10 @@ func payloadFromMessages(msg []*types.Message) *outgoing.Messages {
|
||||
|
||||
func payloadFromChannel(ch *types.Channel) *outgoing.Channel {
|
||||
return &outgoing.Channel{
|
||||
ID: strconv.FormatUint(ch.ID, 10),
|
||||
Name: ch.Name,
|
||||
ID: uint64toa(ch.ID),
|
||||
Name: ch.Name,
|
||||
LastMessageID: uint64toa(ch.LastMessageID),
|
||||
Topic: ch.Topic,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user