Implement basic logic for /status endpoint
Currently it gets a list of connected users from websocket session store
and popuplates array of statuses:
[{"userID":"83947155491913829","status":"online","icon":"","message":""},
{"userID":"85008081418715239","status":"online","icon":"","message":""}]
This commit is contained in:
@@ -27,6 +27,7 @@ func MountRoutes() func(chi.Router) {
|
||||
handlers.NewChannel(Channel{}.New()).MountRoutes(r)
|
||||
handlers.NewMessage(Message{}.New()).MountRoutes(r)
|
||||
handlers.NewSearch(Search{}.New()).MountRoutes(r)
|
||||
handlers.NewStatus(Status{}.New()).MountRoutes(r)
|
||||
handlers.NewCommands(Commands{}.New()).MountRoutes(r)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/crusttech/crust/messaging/rest/request"
|
||||
"github.com/crusttech/crust/messaging/websocket"
|
||||
)
|
||||
|
||||
var _ = errors.Wrap
|
||||
@@ -19,7 +20,22 @@ func (Status) New() *Status {
|
||||
}
|
||||
|
||||
func (ctrl *Status) List(ctx context.Context, r *request.StatusList) (interface{}, error) {
|
||||
return nil, errors.New("Not implemented: Status.list")
|
||||
type userStatus struct {
|
||||
UserID uint64 `json:"userID,string"`
|
||||
Status string `json:"present"`
|
||||
Icon string `json:"icon"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
out := []userStatus{}
|
||||
for _, userID := range websocket.GetConnectedUsers() {
|
||||
out = append(out, userStatus{
|
||||
UserID: userID,
|
||||
Status: "online",
|
||||
})
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (ctrl *Status) Set(ctx context.Context, r *request.StatusSet) (interface{}, error) {
|
||||
|
||||
@@ -61,3 +61,18 @@ func (s *Store) Delete(id uint64) {
|
||||
defer s.Unlock()
|
||||
delete(s.Sessions, id)
|
||||
}
|
||||
|
||||
func GetConnectedUsers() []uint64 {
|
||||
var chk = map[uint64]bool{}
|
||||
|
||||
store.Walk(func(session *Session) {
|
||||
chk[session.user.Identity()] = true
|
||||
})
|
||||
|
||||
var out = make([]uint64, 0)
|
||||
for ID := range chk {
|
||||
out = append(out, ID)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user