diff options
Diffstat (limited to 'api/post.go')
-rw-r--r-- | api/post.go | 85 |
1 files changed, 74 insertions, 11 deletions
diff --git a/api/post.go b/api/post.go index ffb69f382..5fbacc906 100644 --- a/api/post.go +++ b/api/post.go @@ -23,6 +23,7 @@ func InitPost(r *mux.Router) { l4g.Debug("Initializing post api routes") r.Handle("/posts/search", ApiUserRequired(searchPosts)).Methods("GET") + r.Handle("/posts/{post_id}", ApiUserRequired(getPostById)).Methods("GET") sr := r.PathPrefix("/channels/{id:[A-Za-z0-9]+}").Subrouter() sr.Handle("/create", ApiUserRequired(createPost)).Methods("POST") @@ -152,9 +153,6 @@ func CreateWebhookPost(c *Context, channelId, text, overrideUsername, overrideIc linkWithTextRegex := regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`) text = linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})") - linkRegex := regexp.MustCompile(`<\s*(\S*)\s*>`) - text = linkRegex.ReplaceAllString(text, "${1}") - post := &model.Post{UserId: c.Session.UserId, ChannelId: channelId, Message: text, Type: postType} post.AddProp("from_webhook", "true") @@ -176,7 +174,21 @@ func CreateWebhookPost(c *Context, channelId, text, overrideUsername, overrideIc if len(props) > 0 { for key, val := range props { - if key != "override_icon_url" && key != "override_username" && key != "from_webhook" { + if key == "attachments" { + if list, success := val.([]interface{}); success { + // parse attachment links into Markdown format + for i, aInt := range list { + attachment := aInt.(map[string]interface{}) + if _, ok := attachment["text"]; ok { + aText := attachment["text"].(string) + aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") + attachment["text"] = aText + list[i] = attachment + } + } + post.AddProp(key, list) + } + } else if key != "override_icon_url" && key != "override_username" && key != "from_webhook" { post.AddProp(key, val) } } @@ -406,9 +418,9 @@ func sendNotificationsAndForget(c *Context, post *model.Post, team *model.Team, } // Add @all to keywords if user has them turned on - if profile.NotifyProps["all"] == "true" { - keywordMap["@all"] = append(keywordMap["@all"], profile.Id) - } + // if profile.NotifyProps["all"] == "true" { + // keywordMap["@all"] = append(keywordMap["@all"], profile.Id) + // } // Add @channel to keywords if user has them turned on if profile.NotifyProps["channel"] == "true" { @@ -535,7 +547,7 @@ func sendNotificationsAndForget(c *Context, post *model.Post, team *model.Team, l4g.Error("Failed to send mention email successfully email=%v err=%v", profileMap[id].Email, err) } - if len(utils.Cfg.EmailSettings.ApplePushServer) > 0 { + if *utils.Cfg.EmailSettings.SendPushNotifications { sessionChan := Srv.Store.Session().GetSessions(id) if result := <-sessionChan; result.Err != nil { l4g.Error("Failed to retrieve sessions in notifications id=%v, err=%v", id, result.Err) @@ -544,11 +556,27 @@ func sendNotificationsAndForget(c *Context, post *model.Post, team *model.Team, alreadySeen := make(map[string]string) for _, session := range sessions { - if len(session.DeviceId) > 0 && alreadySeen[session.DeviceId] == "" { - + if len(session.DeviceId) > 0 && alreadySeen[session.DeviceId] == "" && strings.HasPrefix(session.DeviceId, "apple:") { alreadySeen[session.DeviceId] = session.DeviceId - utils.SendAppleNotifyAndForget(session.DeviceId, subjectPage.Render(), 1) + msg := model.PushNotification{} + msg.Platform = model.PUSH_NOTIFY_APPLE + msg.Badge = 1 + msg.DeviceId = strings.TrimPrefix(session.DeviceId, "apple:") + msg.ServerId = utils.CfgDiagnosticId + + if channel.Type == model.CHANNEL_DIRECT { + msg.Message = channelName + " sent you a direct message" + } else { + msg.Message = profileMap[id].FirstName + " mentioned you in " + channelName + } + + httpClient := http.Client{} + request, _ := http.NewRequest("POST", *utils.Cfg.EmailSettings.PushNotificationServer+"/api/v1/send_push", strings.NewReader(msg.ToJson())) + + if _, err := httpClient.Do(request); err != nil { + l4g.Error("Failed to send push notificationid=%v, err=%v", id, err) + } } } } @@ -768,6 +796,41 @@ func getPost(c *Context, w http.ResponseWriter, r *http.Request) { } } +func getPostById(c *Context, w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + + postId := params["post_id"] + if len(postId) != 26 { + c.SetInvalidParam("getPostById", "postId") + return + } + + if result := <-Srv.Store.Post().Get(postId); result.Err != nil { + c.Err = result.Err + return + } else { + list := result.Data.(*model.PostList) + + if len(list.Order) != 1 { + c.Err = model.NewAppError("getPostById", "Unable to get post", "") + return + } + post := list.Posts[list.Order[0]] + + cchan := Srv.Store.Channel().CheckPermissionsTo(c.Session.TeamId, post.ChannelId, c.Session.UserId) + if !c.HasPermissionsToChannel(cchan, "getPostById") { + return + } + + if HandleEtag(list.Etag(), w, r) { + return + } + + w.Header().Set(model.HEADER_ETAG_SERVER, list.Etag()) + w.Write([]byte(list.ToJson())) + } +} + func deletePost(c *Context, w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) |