File size: 1,121 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package mockslack

import (
	"context"
	"net/http"
)

// GroupsCreate is used to create a channel.
func (st *API) GroupsCreate(ctx context.Context, opts ConversationCreateOpts) (*Channel, error) {
	err := checkPermission(ctx, "groups:write")
	if err != nil {
		return nil, err
	}

	if !opts.Validate {
		opts.Name = cleanChannelName(opts.Name)
	}
	err = validateChannelName(opts.Name)
	if err != nil {
		return nil, err
	}

	ch := Channel{
		ID:      st.gen.GroupID(),
		Name:    opts.Name,
		IsGroup: true,
	}

	st.mx.Lock()
	st.channels[ch.ID] = &channelState{Channel: ch}
	st.mx.Unlock()

	return &ch, nil
}

// ServeGroupsCreate serves a request to the `Groups.create` API call.
//
// https://api.slack.com/methods/Groups.create
func (s *Server) ServeGroupsCreate(w http.ResponseWriter, req *http.Request) {
	ch, err := s.API().GroupsCreate(req.Context(), ConversationCreateOpts{Name: req.FormValue("name"), Validate: req.FormValue("validate") == "true"})
	if respondErr(w, err) {
		return
	}

	var resp struct {
		response
		Group *Channel `json:"group"`
	}
	resp.OK = true
	resp.Group = ch

	respondWith(w, resp)
}