Skip to content

Commit

Permalink
Merge 83aa62b into 19ec4c4
Browse files Browse the repository at this point in the history
  • Loading branch information
saturninoabril committed Mar 8, 2017
2 parents 19ec4c4 + 83aa62b commit 2acca57
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 7 deletions.
22 changes: 22 additions & 0 deletions api4/channel.go
Expand Up @@ -18,6 +18,8 @@ func InitChannel() {
BaseRoutes.Channels.Handle("", ApiSessionRequired(createChannel)).Methods("POST")
BaseRoutes.Channels.Handle("/direct", ApiSessionRequired(createDirectChannel)).Methods("POST")

BaseRoutes.Team.Handle("/channels", ApiSessionRequired(getPublicChannelsForTeam)).Methods("GET")

BaseRoutes.Channel.Handle("", ApiSessionRequired(getChannel)).Methods("GET")
BaseRoutes.ChannelByName.Handle("", ApiSessionRequired(getChannelByName)).Methods("GET")
BaseRoutes.ChannelByNameForTeamName.Handle("", ApiSessionRequired(getChannelByNameForTeamName)).Methods("GET")
Expand Down Expand Up @@ -115,6 +117,26 @@ func getChannel(c *Context, w http.ResponseWriter, r *http.Request) {
}
}

func getPublicChannelsForTeam(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireTeamId()
if c.Err != nil {
return
}

if !app.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_LIST_TEAM_CHANNELS) {
c.SetPermissionError(model.PERMISSION_LIST_TEAM_CHANNELS)
return
}

if channels, err := app.GetPublicChannelsForTeam(c.Params.TeamId, c.Params.Page, c.Params.PerPage); err != nil {
c.Err = err
return
} else {
w.Write([]byte(channels.ToJson()))
return
}
}

func getChannelByName(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireTeamId().RequireChannelName()
if c.Err != nil {
Expand Down
80 changes: 80 additions & 0 deletions api4/channel_test.go
Expand Up @@ -254,6 +254,86 @@ func TestGetChannel(t *testing.T) {
CheckNotFoundStatus(t, resp)
}

func TestGetPublicChannelsForTeam(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
Client := th.Client
team := th.BasicTeam
publicChannel1 := th.BasicChannel
publicChannel2 := th.BasicChannel2

channels, resp := Client.GetPublicChannelsForTeam(team.Id, 0, 100, "")
CheckNoError(t, resp)
if len(*channels) != 4 {
t.Fatal("wrong length")
}

for i, c := range *channels {
if c.Type != model.CHANNEL_OPEN {
t.Fatal("should include open channel only")
}

// only check the created 2 public channels
if i < 2 && !(c.DisplayName == publicChannel1.DisplayName || c.DisplayName == publicChannel2.DisplayName) {
t.Logf("channel %v: %v", i, c.DisplayName)
t.Fatal("should match public channel display name only")
}
}

privateChannel := th.CreatePrivateChannel()
channels, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "")
CheckNoError(t, resp)
if len(*channels) != 4 {
t.Fatal("wrong length")
}

for _, c := range *channels {
if c.Type != model.CHANNEL_OPEN {
t.Fatal("should not include private channel")
}

if c.DisplayName == privateChannel.DisplayName {
t.Fatal("should not match private channel display name")
}
}

channels, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 1, "")
CheckNoError(t, resp)
if len(*channels) != 1 {
t.Fatal("should be one channel per page")
}

channels, resp = Client.GetPublicChannelsForTeam(team.Id, 1, 1, "")
CheckNoError(t, resp)
if len(*channels) != 1 {
t.Fatal("should be one channel per page")
}

channels, resp = Client.GetPublicChannelsForTeam(team.Id, 10000, 100, "")
CheckNoError(t, resp)
if len(*channels) != 0 {
t.Fatal("should be no channel")
}

_, resp = Client.GetPublicChannelsForTeam("junk", 0, 100, "")
CheckBadRequestStatus(t, resp)

_, resp = Client.GetPublicChannelsForTeam(model.NewId(), 0, 100, "")
CheckForbiddenStatus(t, resp)

Client.Logout()
_, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "")
CheckUnauthorizedStatus(t, resp)

user := th.CreateUser()
Client.Login(user.Email, user.Password)
_, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "")
CheckForbiddenStatus(t, resp)

_, resp = th.SystemAdminClient.GetPublicChannelsForTeam(team.Id, 0, 100, "")
CheckNoError(t, resp)
}

func TestGetChannelByName(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
Expand Down
8 changes: 8 additions & 0 deletions app/channel.go
Expand Up @@ -646,6 +646,14 @@ func GetChannelsUserNotIn(teamId string, userId string, offset int, limit int) (
}
}

func GetPublicChannelsForTeam(teamId string, offset int, limit int) (*model.ChannelList, *model.AppError) {
if result := <-Srv.Store.Channel().GetPublicChannelsForTeam(teamId, offset, limit); result.Err != nil {
return nil, result.Err
} else {
return result.Data.(*model.ChannelList), nil
}
}

func GetChannelMember(channelId string, userId string) (*model.ChannelMember, *model.AppError) {
if result := <-Srv.Store.Channel().GetMember(channelId, userId); result.Err != nil {
return nil, result.Err
Expand Down
14 changes: 7 additions & 7 deletions config/config.json
Expand Up @@ -55,10 +55,10 @@
"CustomDescriptionText": "",
"RestrictDirectMessage": "any",
"RestrictTeamInvite": "all",
"RestrictPublicChannelCreation": "all",
"RestrictPrivateChannelCreation": "all",
"RestrictPublicChannelManagement": "all",
"RestrictPrivateChannelManagement": "all",
"RestrictPublicChannelCreation": "all",
"RestrictPrivateChannelCreation": "all",
"RestrictPublicChannelDeletion": "all",
"RestrictPrivateChannelDeletion": "all",
"UserStatusAwayTimeout": 300,
Expand All @@ -72,7 +72,7 @@
"MaxIdleConns": 20,
"MaxOpenConns": 300,
"Trace": false,
"AtRestEncryptKey": ""
"AtRestEncryptKey": "sf1o5wnxhoiu9ajtnb86jqsjpj3yyoux"
},
"LogSettings": {
"EnableConsole": true,
Expand All @@ -96,7 +96,7 @@
"DriverName": "local",
"Directory": "./data/",
"EnablePublicLink": false,
"PublicLinkSalt": "",
"PublicLinkSalt": "af4tn57mopyqbwwbcf3fzyeujrsgz8jk",
"ThumbnailWidth": 120,
"ThumbnailHeight": 100,
"PreviewWidth": 1024,
Expand Down Expand Up @@ -125,8 +125,8 @@
"SMTPServer": "dockerhost",
"SMTPPort": "2500",
"ConnectionSecurity": "",
"InviteSalt": "",
"PasswordResetSalt": "",
"InviteSalt": "fam3uw4b9uhq8a7qkygri1yurzqjkrfx",
"PasswordResetSalt": "ceafp53xaqpb33kjnpkb9c1wnnpz7hff",
"SendPushNotifications": false,
"PushNotificationServer": "",
"PushNotificationContents": "generic",
Expand Down Expand Up @@ -260,4 +260,4 @@
"TurnUsername": "",
"TurnSharedKey": ""
}
}
}
4 changes: 4 additions & 0 deletions i18n/en.json
Expand Up @@ -4699,6 +4699,10 @@
"id": "store.sql_channel.get_more_channels.get.app_error",
"translation": "We couldn't get the channels"
},
{
"id": "store.sql_channel.get_public_channels.get.app_error",
"translation": "We couldn't get public channels"
},
{
"id": "store.sql_channel.increment_mention_count.app_error",
"translation": "We couldn't increment the mention count"
Expand Down
15 changes: 15 additions & 0 deletions model/client4.go
Expand Up @@ -98,6 +98,10 @@ func (c *Client4) GetChannelsRoute() string {
return fmt.Sprintf("/channels")
}

func (c *Client4) GetPublicChannelsForTeamRoute(teamId string) string {
return fmt.Sprintf(c.GetTeamRoute(teamId) + "/channels")
}

func (c *Client4) GetChannelRoute(channelId string) string {
return fmt.Sprintf(c.GetChannelsRoute()+"/%v", channelId)
}
Expand Down Expand Up @@ -698,6 +702,17 @@ func (c *Client4) GetChannel(channelId, etag string) (*Channel, *Response) {
}
}

// GetPublicChannelsForTeam returns a channel based on the provided team id string.
func (c *Client4) GetPublicChannelsForTeam(teamId string, page int, perPage int, etag string) (*ChannelList, *Response) {
query := fmt.Sprintf("?page=%v&per_page=%v", page, perPage)
if r, err := c.DoApiGet(c.GetPublicChannelsForTeamRoute(teamId)+query, etag); err != nil {
return nil, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return ChannelListFromJson(r.Body), BuildResponse(r)
}
}

// GetChannelByName returns a channel based on the provided channel name and team id strings.
func (c *Client4) GetChannelByName(channelName, teamId string, etag string) (*Channel, *Response) {
if r, err := c.DoApiGet(c.GetChannelByNameRoute(channelName, teamId), etag); err != nil {
Expand Down
34 changes: 34 additions & 0 deletions store/sql_channel_store.go
Expand Up @@ -524,6 +524,40 @@ func (s SqlChannelStore) GetMoreChannels(teamId string, userId string, offset in
return storeChannel
}

func (s SqlChannelStore) GetPublicChannelsForTeam(teamId string, offset int, limit int) StoreChannel {
storeChannel := make(StoreChannel, 1)

go func() {
result := StoreResult{}

data := &model.ChannelList{}
_, err := s.GetReplica().Select(data,
`SELECT
*
FROM
Channels
WHERE
TeamId = :TeamId
AND Type = 'O'
AND DeleteAt = 0
ORDER BY DisplayName
LIMIT :Limit
OFFSET :Offset`,
map[string]interface{}{"TeamId": teamId, "Limit": limit, "Offset": offset})

if err != nil {
result.Err = model.NewLocAppError("SqlChannelStore.GetPublicChannelsForTeam", "store.sql_channel.get_public_channels.get.app_error", nil, "teamId="+teamId+", err="+err.Error())
} else {
result.Data = data
}

storeChannel <- result
close(storeChannel)
}()

return storeChannel
}

type channelIdWithCountAndUpdateAt struct {
Id string
TotalMsgCount int64
Expand Down
85 changes: 85 additions & 0 deletions store/sql_channel_store_test.go
Expand Up @@ -775,6 +775,91 @@ func TestChannelStoreGetMoreChannels(t *testing.T) {
}
}

func TestChannelStoreGetPublicChannelsForTeam(t *testing.T) {
Setup()

o1 := model.Channel{}
o1.TeamId = model.NewId()
o1.DisplayName = "OpenChannel1Team1"
o1.Name = "a" + model.NewId() + "b"
o1.Type = model.CHANNEL_OPEN
Must(store.Channel().Save(&o1))

o2 := model.Channel{}
o2.TeamId = model.NewId()
o2.DisplayName = "OpenChannel1Team2"
o2.Name = "a" + model.NewId() + "b"
o2.Type = model.CHANNEL_OPEN
Must(store.Channel().Save(&o2))

o3 := model.Channel{}
o3.TeamId = o1.TeamId
o3.DisplayName = "PrivateChannel1Team1"
o3.Name = "a" + model.NewId() + "b"
o3.Type = model.CHANNEL_PRIVATE
Must(store.Channel().Save(&o3))

cresult := <-store.Channel().GetPublicChannelsForTeam(o1.TeamId, 0, 100)
if cresult.Err != nil {
t.Fatal(cresult.Err)
}
list := cresult.Data.(*model.ChannelList)

if len(*list) != 1 {
t.Fatal("wrong list")
}

if (*list)[0].Name != o1.Name {
t.Fatal("missing channel")
}

o4 := model.Channel{}
o4.TeamId = o1.TeamId
o4.DisplayName = "OpenChannel2Team1"
o4.Name = "a" + model.NewId() + "b"
o4.Type = model.CHANNEL_OPEN
Must(store.Channel().Save(&o4))

cresult = <-store.Channel().GetPublicChannelsForTeam(o1.TeamId, 0, 100)
list = cresult.Data.(*model.ChannelList)

if len(*list) != 2 {
t.Fatal("wrong list length")
}

cresult = <-store.Channel().GetPublicChannelsForTeam(o1.TeamId, 0, 1)
list = cresult.Data.(*model.ChannelList)

if len(*list) != 1 {
t.Fatal("wrong list length")
}

cresult = <-store.Channel().GetPublicChannelsForTeam(o1.TeamId, 1, 1)
list = cresult.Data.(*model.ChannelList)

if len(*list) != 1 {
t.Fatal("wrong list length")
}

if r1 := <-store.Channel().AnalyticsTypeCount(o1.TeamId, model.CHANNEL_OPEN); r1.Err != nil {
t.Fatal(r1.Err)
} else {
if r1.Data.(int64) != 2 {
t.Log(r1.Data)
t.Fatal("wrong value")
}
}

if r1 := <-store.Channel().AnalyticsTypeCount(o1.TeamId, model.CHANNEL_PRIVATE); r1.Err != nil {
t.Fatal(r1.Err)
} else {
if r1.Data.(int64) != 1 {
t.Log(r1.Data)
t.Fatal("wrong value")
}
}
}

func TestChannelStoreGetChannelCounts(t *testing.T) {
Setup()

Expand Down
1 change: 1 addition & 0 deletions store/store.go
Expand Up @@ -101,6 +101,7 @@ type ChannelStore interface {
GetDeletedByName(team_id string, name string) StoreChannel
GetChannels(teamId string, userId string) StoreChannel
GetMoreChannels(teamId string, userId string, offset int, limit int) StoreChannel
GetPublicChannelsForTeam(teamId string, offset int, limit int) StoreChannel
GetChannelCounts(teamId string, userId string) StoreChannel
GetTeamChannels(teamId string) StoreChannel
GetAll(teamId string) StoreChannel
Expand Down

0 comments on commit 2acca57

Please sign in to comment.