-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
92 lines (73 loc) · 2.38 KB
/
client.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"net/url"
"path"
"strings"
fb_client "github.com/mattermost/mattermost-server/server/v8/boards/client"
fb_model "github.com/mattermost/mattermost-server/server/v8/boards/model"
mm_model "github.com/mattermost/mattermost-server/server/v8/model"
)
type Client struct {
FBclient *fb_client.Client
MMclient *mm_model.Client4
user *mm_model.User
}
func NewClient(siteURL string, username string, password string) (*Client, error) {
mmclient := mm_model.NewAPIv4Client(siteURL)
user, _, err := mmclient.Login(username, password)
if err != nil {
return nil, err
}
fbclient := fb_client.NewClient(siteURL, mmclient.AuthToken)
u, err := url.Parse(siteURL)
if err != nil {
return nil, fmt.Errorf("Invalid site URL: %w", err)
}
u.Path = path.Join("/plugins/boards/", fb_client.APIURLSuffix)
fbclient.APIURL = u.String()
me2, resp := fbclient.GetMe()
if resp.Error != nil {
return nil, fmt.Errorf("cannot fetch FocalBoard user %s: %w", username, resp.Error)
}
if me2.ID != user.Id {
return nil, fmt.Errorf("user ids don't match %s != %s: %w", user.Id, me2.ID, err)
}
return &Client{
FBclient: fbclient,
MMclient: mmclient,
user: user,
}, nil
}
func (c *Client) InsertBoard(board *fb_model.Board) (*fb_model.Board, error) {
boardNew, resp := c.FBclient.CreateBoard(board)
return boardNew, resp.Error
}
func (c *Client) InsertBlocks(boardID string, blocks []*fb_model.Block) ([]*fb_model.Block, error) {
blocks, resp := c.FBclient.InsertBlocks(boardID, blocks, true)
return blocks, resp.Error
}
// CreateChannel creates a new channel in an idempotent manner.
func (c *Client) CreateChannel(channelName string, teamId string) (*mm_model.Channel, error) {
displayName := channelName
channelName = strings.ToLower(channelName)
channelName = strings.ReplaceAll(channelName, ".", "-")
channelName = strings.ReplaceAll(channelName, " ", "")
channel, _, _ := c.MMclient.GetChannelByName(channelName, teamId, "")
if channel != nil {
return channel, nil
}
channelNew := &mm_model.Channel{
TeamId: teamId,
Type: mm_model.ChannelTypeOpen,
Name: channelName,
DisplayName: displayName,
Header: "A channel created by FBSC.",
CreatorId: c.user.Id,
}
channel, _, err := c.MMclient.CreateChannel(channelNew)
if err != nil {
return nil, fmt.Errorf("cannot create channel %s: %w", channelName, err)
}
return channel, nil
}