PLT-6403: Interactive messages (#7274)

* wip

* finish first pass

* requested changes

* add DoPostAction to Client4
Этот коммит содержится в:
Chris
2017-08-29 16:14:59 -05:00
коммит произвёл GitHub
родитель 59798c1375
Коммит 213a072b38
18 изменённых файлов: 366 добавлений и 11 удалений

Просмотреть файл

@@ -4,8 +4,11 @@
package app
import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"
l4g "github.com/alecthomas/log4go"
"github.com/dyatlov/go-opengraph/opengraph"
@@ -210,7 +213,7 @@ func parseSlackLinksToMarkdown(text string) string {
return linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})")
}
func SendEphemeralPost(teamId, userId string, post *model.Post) *model.Post {
func SendEphemeralPost(userId string, post *model.Post) *model.Post {
post.Type = model.POST_EPHEMERAL
// fill in fields which haven't been specified which have sensible defaults
@@ -638,3 +641,65 @@ func GetOpenGraphMetadata(url string) *opengraph.OpenGraph {
return og
}
func DoPostAction(postId string, actionId string, userId string) *model.AppError {
pchan := Srv.Store.Post().GetSingle(postId)
var post *model.Post
if result := <-pchan; result.Err != nil {
return result.Err
} else {
post = result.Data.(*model.Post)
}
action := post.GetAction(actionId)
if action == nil || action.Integration == nil {
return model.NewAppError("DoPostAction", "api.post.do_action.action_id.app_error", nil, fmt.Sprintf("action=%v", action), http.StatusNotFound)
}
request := &model.PostActionIntegrationRequest{
UserId: userId,
Context: action.Integration.Context,
}
req, _ := http.NewRequest("POST", action.Integration.URL, strings.NewReader(request.ToJson()))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := utils.HttpClient(false).Do(req)
if err != nil {
return model.NewAppError("DoPostAction", "api.post.do_action.action_integration.app_error", nil, "err="+err.Error(), http.StatusBadRequest)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return model.NewAppError("DoPostAction", "api.post.do_action.action_integration.app_error", nil, fmt.Sprintf("status=%v", resp.StatusCode), http.StatusBadRequest)
}
var response model.PostActionIntegrationResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return model.NewAppError("DoPostAction", "api.post.do_action.action_integration.app_error", nil, "err="+err.Error(), http.StatusBadRequest)
}
if response.Update != nil {
response.Update.Id = postId
response.Update.AddProp("from_webhook", "true")
if _, err := UpdatePost(response.Update, false); err != nil {
return err
}
}
if response.EphemeralText != "" {
ephemeralPost := &model.Post{}
ephemeralPost.Message = parseSlackLinksToMarkdown(response.EphemeralText)
ephemeralPost.ChannelId = post.ChannelId
ephemeralPost.RootId = post.RootId
if ephemeralPost.RootId == "" {
ephemeralPost.RootId = post.Id
}
ephemeralPost.UserId = userId
ephemeralPost.AddProp("from_webhook", "true")
SendEphemeralPost(userId, ephemeralPost)
}
return nil
}