Skip to content

Commit

Permalink
fixed the test app (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
levb authored Aug 16, 2022
1 parent 9dd1155 commit 49ab502
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
53 changes: 21 additions & 32 deletions apps/appclient/mattermost_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,26 @@ type Client struct {
userID string
}

func as(id, token string, cc apps.Context) *Client {
return NewClient(id, token, cc.MattermostSiteURL)
func as(token string, cc apps.Context) *Client {
return NewClient(token, cc.MattermostSiteURL)
}

func AsBot(cc apps.Context) *Client {
return as(cc.BotUserID, cc.BotAccessToken, cc)
client := as(cc.BotAccessToken, cc)
client.userID = cc.BotUserID
return client
}

func AsActingUser(cc apps.Context) *Client {
return as(cc.ActingUser.Id, cc.ActingUserAccessToken, cc)
client := as(cc.ActingUserAccessToken, cc)
if cc.ActingUser != nil {
client.userID = cc.ActingUser.Id
}
return client
}

func NewClient(userID, token, mattermostSiteURL string) *Client {
func NewClient(token, mattermostSiteURL string) *Client {
c := Client{
userID: userID,
ClientPP: NewAppsPluginAPIClient(mattermostSiteURL),
Client4: model.NewAPIv4Client(mattermostSiteURL),
}
Expand Down Expand Up @@ -170,8 +175,6 @@ func (c *Client) Call(creq apps.CallRequest) (*apps.CallResponse, error) {
}

func (c *Client) CreatePost(post *model.Post) (*model.Post, error) {
post.UserId = c.userID

createdPost, res, err := c.Client4.CreatePost(post)
if res.StatusCode != http.StatusCreated {
if err != nil {
Expand All @@ -185,37 +188,23 @@ func (c *Client) CreatePost(post *model.Post) (*model.Post, error) {
}

func (c *Client) DM(userID string, format string, args ...interface{}) (*model.Post, error) {
channel, err := c.getDirectChannelWith(userID)
if err != nil {
return nil, errors.Wrap(err, "failed to get direct channel to post DM")
}

post := &model.Post{
ChannelId: channel.Id,
Message: fmt.Sprintf(format, args...),
}
return c.CreatePost(post)
return c.DMPost(userID, &model.Post{
Message: fmt.Sprintf(format, args...),
})
}

func (c *Client) DMPost(userID string, post *model.Post) (*model.Post, error) {
channel, err := c.getDirectChannelWith(userID)
if err != nil {
return nil, errors.Wrap(err, "failed to get direct channel")
if c.userID == "" {
return nil, errors.New("empty sender user_id, perhaps Call does not expand acting_user")
}

post.ChannelId = channel.Id
return c.CreatePost(post)
}

func (c *Client) getDirectChannelWith(userID string) (*model.Channel, error) {
channel, res, err := c.CreateDirectChannel(c.userID, userID)
if res.StatusCode != http.StatusCreated && res.StatusCode != http.StatusOK {
if err != nil {
return nil, err
if err == nil {
err = errors.Errorf("API returned status %d", res.StatusCode)
}

return nil, errors.Errorf("returned with status %d", res.StatusCode)
return nil, errors.Wrap(err, "failed to get direct channel")
}

return channel, nil
post.ChannelId = channel.Id
return c.CreatePost(post)
}
2 changes: 1 addition & 1 deletion cmd/appsctl/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func getMattermostClient() (*appclient.Client, error) {
return nil, errors.New("MM_SERVICESETTINGS_SITEURL and MM_ADMIN_TOKEN must be set")
}

return appclient.NewClient("", adminToken, siteURL), nil
return appclient.NewClient(adminToken, siteURL), nil
}

func updateMattermost(m apps.Manifest, deployType apps.DeployType, installApp bool) error {
Expand Down
4 changes: 3 additions & 1 deletion test/app/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,13 @@ func handleSubscription(creq *apps.CallRequest, subscribe bool) apps.CallRespons

switch subject {
case apps.SubjectUserJoinedChannel,
apps.SubjectBotLeftChannel /*, apps.SubjectPostCreated*/ :
apps.SubjectUserLeftChannel:
sub.ChannelID = creq.Context.Channel.Id

case apps.SubjectUserJoinedTeam,
apps.SubjectUserLeftTeam,
apps.SubjectBotJoinedChannel,
apps.SubjectBotLeftChannel,
apps.SubjectChannelCreated:
sub.TeamID = creq.Context.Team.Id
}
Expand Down

0 comments on commit 49ab502

Please sign in to comment.