From 2e6fd031d15a9502e7a7a4536febfe49780c0697 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Tue, 20 Jun 2017 09:55:43 -0400 Subject: Add GET /teams/invite/{invite_id} endpoint for v4 (#6685) --- api4/context.go | 11 +++++++++++ api4/params.go | 5 +++++ api4/team.go | 25 +++++++++++++++++++++++++ api4/team_test.go | 21 +++++++++++++++++++++ model/client4.go | 10 ++++++++++ 5 files changed, 72 insertions(+) diff --git a/api4/context.go b/api4/context.go index 7a908c588..35f343b7a 100644 --- a/api4/context.go +++ b/api4/context.go @@ -350,6 +350,17 @@ func (c *Context) RequireTeamId() *Context { return c } +func (c *Context) RequireInviteId() *Context { + if c.Err != nil { + return c + } + + if len(c.Params.InviteId) != 26 { + c.SetInvalidUrlParam("invite_id") + } + return c +} + func (c *Context) RequireChannelId() *Context { if c.Err != nil { return c diff --git a/api4/params.go b/api4/params.go index d952eb65a..999bf8e77 100644 --- a/api4/params.go +++ b/api4/params.go @@ -19,6 +19,7 @@ const ( type ApiParams struct { UserId string TeamId string + InviteId string ChannelId string PostId string FileId string @@ -55,6 +56,10 @@ func ApiParamsFromRequest(r *http.Request) *ApiParams { params.TeamId = val } + if val, ok := props["invite_id"]; ok { + params.InviteId = val + } + if val, ok := props["channel_id"]; ok { params.ChannelId = val } diff --git a/api4/team.go b/api4/team.go index 57a715937..00a16d5c1 100644 --- a/api4/team.go +++ b/api4/team.go @@ -49,6 +49,7 @@ func InitTeam() { BaseRoutes.Team.Handle("/import", ApiSessionRequired(importTeam)).Methods("POST") BaseRoutes.Team.Handle("/invite/email", ApiSessionRequired(inviteUsersToTeam)).Methods("POST") + BaseRoutes.Teams.Handle("/invite/{invite_id:[A-Za-z0-9]+}", ApiHandler(getInviteInfo)).Methods("GET") } func createTeam(c *Context, w http.ResponseWriter, r *http.Request) { @@ -687,3 +688,27 @@ func inviteUsersToTeam(c *Context, w http.ResponseWriter, r *http.Request) { ReturnStatusOK(w) } + +func getInviteInfo(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequireInviteId() + if c.Err != nil { + return + } + + if team, err := app.GetTeamByInviteId(c.Params.InviteId); err != nil { + c.Err = err + return + } else { + if !(team.Type == model.TEAM_OPEN) { + c.Err = model.NewAppError("getInviteInfo", "api.team.get_invite_info.not_open_team", nil, "id="+c.Params.InviteId, http.StatusForbidden) + return + } + + result := map[string]string{} + result["display_name"] = team.DisplayName + result["description"] = team.Description + result["name"] = team.Name + result["id"] = team.Id + w.Write([]byte(model.MapToJson(result))) + } +} diff --git a/api4/team_test.go b/api4/team_test.go index 2aee4ba5f..78ddc8e84 100644 --- a/api4/team_test.go +++ b/api4/team_test.go @@ -1453,3 +1453,24 @@ func TestInviteUsersToTeam(t *testing.T) { } } } + +func TestGetTeamInviteInfo(t *testing.T) { + th := Setup().InitBasic() + defer TearDown() + Client := th.Client + team := th.BasicTeam + + team, resp := Client.GetTeamInviteInfo(team.InviteId) + CheckNoError(t, resp) + + if team.DisplayName == "" { + t.Fatal("should not be empty") + } + + if team.Email != "" { + t.Fatal("should be empty") + } + + _, resp = Client.GetTeamInviteInfo("junk") + CheckBadRequestStatus(t, resp) +} diff --git a/model/client4.go b/model/client4.go index efe12a3b9..be8a689f2 100644 --- a/model/client4.go +++ b/model/client4.go @@ -1204,6 +1204,16 @@ func (c *Client4) InviteUsersToTeam(teamId string, userEmails []string) (bool, * } } +// GetTeamInviteInfo returns a team object from an invite id containing sanitized information. +func (c *Client4) GetTeamInviteInfo(inviteId string) (*Team, *Response) { + if r, err := c.DoApiGet(c.GetTeamsRoute()+"/invite/"+inviteId, ""); err != nil { + return nil, &Response{StatusCode: r.StatusCode, Error: err} + } else { + defer closeBody(r) + return TeamFromJson(r.Body), BuildResponse(r) + } +} + // Channel Section // CreateChannel creates a channel based on the provided channel struct. -- cgit v1.2.3-1-g7c22