2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
2017-01-13 13:53:37 -05:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
2017-01-25 09:32:42 -05:00
|
|
|
"net/http"
|
2017-01-13 13:53:37 -05:00
|
|
|
"regexp"
|
|
|
|
|
|
|
|
|
|
l4g "github.com/alecthomas/log4go"
|
2017-01-25 09:32:42 -05:00
|
|
|
"github.com/dyatlov/go-opengraph/opengraph"
|
2017-01-13 13:53:37 -05:00
|
|
|
"github.com/mattermost/platform/einterfaces"
|
|
|
|
|
"github.com/mattermost/platform/model"
|
|
|
|
|
"github.com/mattermost/platform/store"
|
|
|
|
|
"github.com/mattermost/platform/utils"
|
|
|
|
|
)
|
|
|
|
|
|
2017-05-31 16:34:05 +02:00
|
|
|
var linkWithTextRegex = regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`)
|
2017-03-20 21:20:42 +08:00
|
|
|
|
2017-04-04 11:54:52 -04:00
|
|
|
func CreatePostAsUser(post *model.Post) (*model.Post, *model.AppError) {
|
2017-01-25 09:32:42 -05:00
|
|
|
// Check that channel has not been deleted
|
|
|
|
|
var channel *model.Channel
|
|
|
|
|
if result := <-Srv.Store.Channel().Get(post.ChannelId, true); result.Err != nil {
|
|
|
|
|
err := model.NewLocAppError("CreatePostAsUser", "api.context.invalid_param.app_error", map[string]interface{}{"Name": "post.channel_id"}, result.Err.Error())
|
|
|
|
|
err.StatusCode = http.StatusBadRequest
|
|
|
|
|
return nil, err
|
|
|
|
|
} else {
|
|
|
|
|
channel = result.Data.(*model.Channel)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if channel.DeleteAt != 0 {
|
|
|
|
|
err := model.NewLocAppError("createPost", "api.post.create_post.can_not_post_to_deleted.error", nil, "")
|
|
|
|
|
err.StatusCode = http.StatusBadRequest
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 11:54:52 -04:00
|
|
|
if rp, err := CreatePost(post, channel.TeamId, true); err != nil {
|
2017-01-25 09:32:42 -05:00
|
|
|
if err.Id == "api.post.create_post.root_id.app_error" ||
|
|
|
|
|
err.Id == "api.post.create_post.channel_root_id.app_error" ||
|
|
|
|
|
err.Id == "api.post.create_post.parent_id.app_error" {
|
|
|
|
|
err.StatusCode = http.StatusBadRequest
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
} else {
|
|
|
|
|
// Update the LastViewAt only if the post does not have from_webhook prop set (eg. Zapier app)
|
|
|
|
|
if _, ok := post.Props["from_webhook"]; !ok {
|
|
|
|
|
if result := <-Srv.Store.Channel().UpdateLastViewedAt([]string{post.ChannelId}, post.UserId); result.Err != nil {
|
|
|
|
|
l4g.Error(utils.T("api.post.create_post.last_viewed.error"), post.ChannelId, post.UserId, result.Err)
|
|
|
|
|
}
|
2017-06-29 07:51:15 -07:00
|
|
|
|
2017-07-03 09:01:14 -04:00
|
|
|
if *utils.Cfg.ServiceSettings.EnableChannelViewedMessages {
|
2017-06-29 07:51:15 -07:00
|
|
|
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_VIEWED, "", "", post.UserId, nil)
|
|
|
|
|
message.Add("channel_id", post.ChannelId)
|
|
|
|
|
go Publish(message)
|
|
|
|
|
}
|
2017-01-25 09:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rp, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 11:54:52 -04:00
|
|
|
func CreatePost(post *model.Post, teamId string, triggerWebhooks bool) (*model.Post, *model.AppError) {
|
2017-01-13 13:53:37 -05:00
|
|
|
var pchan store.StoreChannel
|
|
|
|
|
if len(post.RootId) > 0 {
|
|
|
|
|
pchan = Srv.Store.Post().Get(post.RootId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify the parent/child relationships are correct
|
|
|
|
|
if pchan != nil {
|
|
|
|
|
if presult := <-pchan; presult.Err != nil {
|
|
|
|
|
return nil, model.NewLocAppError("createPost", "api.post.create_post.root_id.app_error", nil, "")
|
|
|
|
|
} else {
|
|
|
|
|
list := presult.Data.(*model.PostList)
|
|
|
|
|
if len(list.Posts) == 0 || !list.IsChannelId(post.ChannelId) {
|
|
|
|
|
return nil, model.NewLocAppError("createPost", "api.post.create_post.channel_root_id.app_error", nil, "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if post.ParentId == "" {
|
|
|
|
|
post.ParentId = post.RootId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if post.RootId != post.ParentId {
|
|
|
|
|
parent := list.Posts[post.ParentId]
|
|
|
|
|
if parent == nil {
|
|
|
|
|
return nil, model.NewLocAppError("createPost", "api.post.create_post.parent_id.app_error", nil, "")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
post.Hashtags, _ = model.ParseHashtags(post.Message)
|
|
|
|
|
|
|
|
|
|
var rpost *model.Post
|
|
|
|
|
if result := <-Srv.Store.Post().Save(post); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
rpost = result.Data.(*model.Post)
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-07 15:36:17 +01:00
|
|
|
esInterface := einterfaces.GetElasticsearchInterface()
|
2017-07-27 08:48:02 +01:00
|
|
|
if esInterface != nil && *utils.Cfg.ElasticsearchSettings.EnableIndexing {
|
2017-05-18 16:26:52 +01:00
|
|
|
go esInterface.IndexPost(rpost, teamId)
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 13:53:37 -05:00
|
|
|
if einterfaces.GetMetricsInterface() != nil {
|
|
|
|
|
einterfaces.GetMetricsInterface().IncrementPostCreate()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(post.FileIds) > 0 {
|
|
|
|
|
// There's a rare bug where the client sends up duplicate FileIds so protect against that
|
|
|
|
|
post.FileIds = utils.RemoveDuplicatesFromStringArray(post.FileIds)
|
|
|
|
|
|
|
|
|
|
for _, fileId := range post.FileIds {
|
|
|
|
|
if result := <-Srv.Store.FileInfo().AttachToPost(fileId, post.Id); result.Err != nil {
|
|
|
|
|
l4g.Error(utils.T("api.post.create_post.attach_files.error"), post.Id, post.FileIds, post.UserId, result.Err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if einterfaces.GetMetricsInterface() != nil {
|
|
|
|
|
einterfaces.GetMetricsInterface().IncrementPostFileAttachment(len(post.FileIds))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 11:54:52 -04:00
|
|
|
if err := handlePostEvents(rpost, teamId, triggerWebhooks); err != nil {
|
2017-01-13 13:53:37 -05:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rpost, nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 11:54:52 -04:00
|
|
|
func handlePostEvents(post *model.Post, teamId string, triggerWebhooks bool) *model.AppError {
|
2017-02-13 10:52:50 -05:00
|
|
|
var tchan store.StoreChannel
|
|
|
|
|
if len(teamId) > 0 {
|
|
|
|
|
tchan = Srv.Store.Team().Get(teamId)
|
|
|
|
|
}
|
2017-01-13 13:53:37 -05:00
|
|
|
cchan := Srv.Store.Channel().Get(post.ChannelId, true)
|
|
|
|
|
uchan := Srv.Store.User().Get(post.UserId)
|
|
|
|
|
|
|
|
|
|
var team *model.Team
|
2017-02-13 10:52:50 -05:00
|
|
|
if tchan != nil {
|
|
|
|
|
if result := <-tchan; result.Err != nil {
|
|
|
|
|
return result.Err
|
|
|
|
|
} else {
|
|
|
|
|
team = result.Data.(*model.Team)
|
|
|
|
|
}
|
2017-01-13 13:53:37 -05:00
|
|
|
} else {
|
2017-02-13 10:52:50 -05:00
|
|
|
// Blank team for DMs
|
|
|
|
|
team = &model.Team{}
|
2017-01-13 13:53:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var channel *model.Channel
|
|
|
|
|
if result := <-cchan; result.Err != nil {
|
|
|
|
|
return result.Err
|
|
|
|
|
} else {
|
|
|
|
|
channel = result.Data.(*model.Channel)
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 14:07:34 -05:00
|
|
|
InvalidateCacheForChannel(channel)
|
|
|
|
|
InvalidateCacheForChannelPosts(channel.Id)
|
|
|
|
|
|
2017-01-13 13:53:37 -05:00
|
|
|
var user *model.User
|
|
|
|
|
if result := <-uchan; result.Err != nil {
|
|
|
|
|
return result.Err
|
|
|
|
|
} else {
|
|
|
|
|
user = result.Data.(*model.User)
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 11:54:52 -04:00
|
|
|
if _, err := SendNotifications(post, team, channel, user); err != nil {
|
2017-01-20 09:43:14 -05:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 13:53:37 -05:00
|
|
|
if triggerWebhooks {
|
|
|
|
|
go func() {
|
2017-04-04 11:54:52 -04:00
|
|
|
if err := handleWebhookEvents(post, team, channel, user); err != nil {
|
2017-01-13 13:53:37 -05:00
|
|
|
l4g.Error(err.Error())
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method only parses and processes the attachments,
|
|
|
|
|
// all else should be set in the post which is passed
|
2017-03-08 04:15:33 -05:00
|
|
|
func parseSlackAttachment(post *model.Post, attachments []*model.SlackAttachment) {
|
2017-01-13 13:53:37 -05:00
|
|
|
post.Type = model.POST_SLACK_ATTACHMENT
|
|
|
|
|
|
2017-03-08 04:15:33 -05:00
|
|
|
for _, attachment := range attachments {
|
|
|
|
|
attachment.Text = parseSlackLinksToMarkdown(attachment.Text)
|
|
|
|
|
attachment.Pretext = parseSlackLinksToMarkdown(attachment.Pretext)
|
|
|
|
|
|
|
|
|
|
for _, field := range attachment.Fields {
|
|
|
|
|
if value, ok := field.Value.(string); ok {
|
|
|
|
|
field.Value = parseSlackLinksToMarkdown(value)
|
2017-01-13 13:53:37 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-08 04:15:33 -05:00
|
|
|
post.AddProp("attachments", attachments)
|
2017-01-13 13:53:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseSlackLinksToMarkdown(text string) string {
|
|
|
|
|
return linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SendEphemeralPost(teamId, userId string, post *model.Post) *model.Post {
|
|
|
|
|
post.Type = model.POST_EPHEMERAL
|
|
|
|
|
|
|
|
|
|
// fill in fields which haven't been specified which have sensible defaults
|
|
|
|
|
if post.Id == "" {
|
|
|
|
|
post.Id = model.NewId()
|
|
|
|
|
}
|
|
|
|
|
if post.CreateAt == 0 {
|
|
|
|
|
post.CreateAt = model.GetMillis()
|
|
|
|
|
}
|
|
|
|
|
if post.Props == nil {
|
|
|
|
|
post.Props = model.StringInterface{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_EPHEMERAL_MESSAGE, "", post.ChannelId, userId, nil)
|
|
|
|
|
message.Add("post", post.ToJson())
|
|
|
|
|
|
|
|
|
|
go Publish(message)
|
|
|
|
|
|
|
|
|
|
return post
|
|
|
|
|
}
|
2017-01-25 09:32:42 -05:00
|
|
|
|
2017-04-04 15:17:47 -04:00
|
|
|
func UpdatePost(post *model.Post, safeUpdate bool) (*model.Post, *model.AppError) {
|
2017-01-25 09:32:42 -05:00
|
|
|
var oldPost *model.Post
|
|
|
|
|
if result := <-Srv.Store.Post().Get(post.Id); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
oldPost = result.Data.(*model.PostList).Posts[post.Id]
|
|
|
|
|
|
2017-07-17 18:22:28 -04:00
|
|
|
if utils.IsLicensed {
|
|
|
|
|
if *utils.Cfg.ServiceSettings.AllowEditPost == model.ALLOW_EDIT_POST_NEVER && post.Message != oldPost.Message {
|
|
|
|
|
err := model.NewAppError("UpdatePost", "api.post.update_post.permissions_denied.app_error", nil, "", http.StatusForbidden)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-25 09:32:42 -05:00
|
|
|
if oldPost == nil {
|
2017-04-04 15:17:47 -04:00
|
|
|
err := model.NewAppError("UpdatePost", "api.post.update_post.find.app_error", nil, "id="+post.Id, http.StatusBadRequest)
|
2017-01-25 09:32:42 -05:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if oldPost.DeleteAt != 0 {
|
2017-04-04 15:17:47 -04:00
|
|
|
err := model.NewAppError("UpdatePost", "api.post.update_post.permissions_details.app_error", map[string]interface{}{"PostId": post.Id}, "", http.StatusBadRequest)
|
2017-01-25 09:32:42 -05:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 23:47:21 +05:30
|
|
|
if oldPost.IsSystemMessage() && !oldPost.IsUserActivitySystemMessage() {
|
|
|
|
|
err := model.NewAppError("updatePost", "api.post.update_post.system_message.app_error", nil, "id="+post.Id, http.StatusBadRequest)
|
2017-01-25 09:32:42 -05:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if utils.IsLicensed {
|
2017-07-14 13:10:20 -04:00
|
|
|
if *utils.Cfg.ServiceSettings.AllowEditPost == model.ALLOW_EDIT_POST_TIME_LIMIT && model.GetMillis() > oldPost.CreateAt+int64(*utils.Cfg.ServiceSettings.PostEditTimeLimit*1000) && post.Message != oldPost.Message {
|
2017-04-04 15:17:47 -04:00
|
|
|
err := model.NewAppError("UpdatePost", "api.post.update_post.permissions_time_limit.app_error", map[string]interface{}{"timeLimit": *utils.Cfg.ServiceSettings.PostEditTimeLimit}, "", http.StatusBadRequest)
|
2017-01-25 09:32:42 -05:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newPost := &model.Post{}
|
|
|
|
|
*newPost = *oldPost
|
|
|
|
|
|
2017-07-04 15:17:54 -04:00
|
|
|
if newPost.Message != post.Message {
|
|
|
|
|
newPost.Message = post.Message
|
|
|
|
|
newPost.EditAt = model.GetMillis()
|
|
|
|
|
newPost.Hashtags, _ = model.ParseHashtags(post.Message)
|
|
|
|
|
}
|
2017-04-04 15:17:47 -04:00
|
|
|
|
|
|
|
|
if !safeUpdate {
|
|
|
|
|
newPost.IsPinned = post.IsPinned
|
|
|
|
|
newPost.HasReactions = post.HasReactions
|
|
|
|
|
newPost.FileIds = post.FileIds
|
|
|
|
|
newPost.Props = post.Props
|
|
|
|
|
}
|
2017-01-25 09:32:42 -05:00
|
|
|
|
|
|
|
|
if result := <-Srv.Store.Post().Update(newPost, oldPost); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
rpost := result.Data.(*model.Post)
|
|
|
|
|
|
2017-07-07 15:36:17 +01:00
|
|
|
esInterface := einterfaces.GetElasticsearchInterface()
|
2017-07-27 08:48:02 +01:00
|
|
|
if esInterface != nil && *utils.Cfg.ElasticsearchSettings.EnableIndexing {
|
2017-05-18 16:26:52 +01:00
|
|
|
go func() {
|
|
|
|
|
if rchannel := <-Srv.Store.Channel().GetForPost(rpost.Id); rchannel.Err != nil {
|
2017-07-27 08:48:02 +01:00
|
|
|
l4g.Error("Couldn't get channel %v for post %v for Elasticsearch indexing.", rpost.ChannelId, rpost.Id)
|
2017-05-18 16:26:52 +01:00
|
|
|
} else {
|
|
|
|
|
esInterface.IndexPost(rpost, rchannel.Data.(*model.Channel).TeamId)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-30 00:06:51 +09:00
|
|
|
sendUpdatedPostEvent(rpost)
|
2017-01-25 09:32:42 -05:00
|
|
|
|
|
|
|
|
InvalidateCacheForChannelPosts(rpost.ChannelId)
|
|
|
|
|
|
|
|
|
|
return rpost, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-30 00:06:51 +09:00
|
|
|
func PatchPost(postId string, patch *model.PostPatch) (*model.Post, *model.AppError) {
|
|
|
|
|
post, err := GetSinglePost(postId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
post.Patch(patch)
|
|
|
|
|
|
2017-04-04 15:17:47 -04:00
|
|
|
updatedPost, err := UpdatePost(post, false)
|
2017-03-30 00:06:51 +09:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendUpdatedPostEvent(updatedPost)
|
|
|
|
|
InvalidateCacheForChannelPosts(updatedPost.ChannelId)
|
|
|
|
|
|
|
|
|
|
return updatedPost, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sendUpdatedPostEvent(post *model.Post) {
|
|
|
|
|
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_EDITED, "", post.ChannelId, "", nil)
|
|
|
|
|
message.Add("post", post.ToJson())
|
|
|
|
|
|
|
|
|
|
go Publish(message)
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 10:52:50 -05:00
|
|
|
func GetPostsPage(channelId string, page int, perPage int) (*model.PostList, *model.AppError) {
|
|
|
|
|
if result := <-Srv.Store.Post().GetPosts(channelId, page*perPage, perPage, true); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
return result.Data.(*model.PostList), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-25 09:32:42 -05:00
|
|
|
func GetPosts(channelId string, offset int, limit int) (*model.PostList, *model.AppError) {
|
|
|
|
|
if result := <-Srv.Store.Post().GetPosts(channelId, offset, limit, true); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
return result.Data.(*model.PostList), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetPostsEtag(channelId string) string {
|
|
|
|
|
return (<-Srv.Store.Post().GetEtag(channelId, true)).Data.(string)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetPostsSince(channelId string, time int64) (*model.PostList, *model.AppError) {
|
|
|
|
|
if result := <-Srv.Store.Post().GetPostsSince(channelId, time, true); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
return result.Data.(*model.PostList), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetSinglePost(postId string) (*model.Post, *model.AppError) {
|
|
|
|
|
if result := <-Srv.Store.Post().GetSingle(postId); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
return result.Data.(*model.Post), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetPostThread(postId string) (*model.PostList, *model.AppError) {
|
|
|
|
|
if result := <-Srv.Store.Post().Get(postId); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
return result.Data.(*model.PostList), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetFlaggedPosts(userId string, offset int, limit int) (*model.PostList, *model.AppError) {
|
|
|
|
|
if result := <-Srv.Store.Post().GetFlaggedPosts(userId, offset, limit); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
return result.Data.(*model.PostList), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-31 12:25:39 -04:00
|
|
|
func GetFlaggedPostsForTeam(userId, teamId string, offset int, limit int) (*model.PostList, *model.AppError) {
|
|
|
|
|
if result := <-Srv.Store.Post().GetFlaggedPostsForTeam(userId, teamId, offset, limit); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
return result.Data.(*model.PostList), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-06 05:18:23 +09:00
|
|
|
func GetFlaggedPostsForChannel(userId, channelId string, offset int, limit int) (*model.PostList, *model.AppError) {
|
|
|
|
|
if result := <-Srv.Store.Post().GetFlaggedPostsForChannel(userId, channelId, offset, limit); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
return result.Data.(*model.PostList), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 11:54:52 -04:00
|
|
|
func GetPermalinkPost(postId string, userId string) (*model.PostList, *model.AppError) {
|
2017-01-25 09:32:42 -05:00
|
|
|
if result := <-Srv.Store.Post().Get(postId); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
list := result.Data.(*model.PostList)
|
|
|
|
|
|
|
|
|
|
if len(list.Order) != 1 {
|
|
|
|
|
return nil, model.NewLocAppError("getPermalinkTmp", "api.post_get_post_by_id.get.app_error", nil, "")
|
|
|
|
|
}
|
|
|
|
|
post := list.Posts[list.Order[0]]
|
|
|
|
|
|
|
|
|
|
var channel *model.Channel
|
|
|
|
|
var err *model.AppError
|
|
|
|
|
if channel, err = GetChannel(post.ChannelId); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 11:54:52 -04:00
|
|
|
if err = JoinChannel(channel, userId); err != nil {
|
2017-01-25 09:32:42 -05:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 16:46:11 -04:00
|
|
|
func GetPostsBeforePost(channelId, postId string, page, perPage int) (*model.PostList, *model.AppError) {
|
|
|
|
|
if result := <-Srv.Store.Post().GetPostsBefore(channelId, postId, perPage, page*perPage); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
return result.Data.(*model.PostList), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetPostsAfterPost(channelId, postId string, page, perPage int) (*model.PostList, *model.AppError) {
|
|
|
|
|
if result := <-Srv.Store.Post().GetPostsAfter(channelId, postId, perPage, page*perPage); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
return result.Data.(*model.PostList), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-25 09:32:42 -05:00
|
|
|
func GetPostsAroundPost(postId, channelId string, offset, limit int, before bool) (*model.PostList, *model.AppError) {
|
|
|
|
|
var pchan store.StoreChannel
|
|
|
|
|
if before {
|
|
|
|
|
pchan = Srv.Store.Post().GetPostsBefore(channelId, postId, limit, offset)
|
|
|
|
|
} else {
|
|
|
|
|
pchan = Srv.Store.Post().GetPostsAfter(channelId, postId, limit, offset)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if result := <-pchan; result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
return result.Data.(*model.PostList), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DeletePost(postId string) (*model.Post, *model.AppError) {
|
|
|
|
|
if result := <-Srv.Store.Post().GetSingle(postId); result.Err != nil {
|
2017-02-21 07:36:52 -05:00
|
|
|
result.Err.StatusCode = http.StatusBadRequest
|
2017-01-25 09:32:42 -05:00
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
post := result.Data.(*model.Post)
|
|
|
|
|
|
|
|
|
|
if result := <-Srv.Store.Post().Delete(postId, model.GetMillis()); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_DELETED, "", post.ChannelId, "", nil)
|
|
|
|
|
message.Add("post", post.ToJson())
|
|
|
|
|
|
|
|
|
|
go Publish(message)
|
|
|
|
|
go DeletePostFiles(post)
|
|
|
|
|
go DeleteFlaggedPosts(post.Id)
|
|
|
|
|
|
2017-07-07 15:36:17 +01:00
|
|
|
esInterface := einterfaces.GetElasticsearchInterface()
|
2017-07-27 08:48:02 +01:00
|
|
|
if esInterface != nil && *utils.Cfg.ElasticsearchSettings.EnableIndexing {
|
2017-07-21 01:38:44 +01:00
|
|
|
go esInterface.DeletePost(post)
|
2017-05-18 16:26:52 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-25 09:32:42 -05:00
|
|
|
InvalidateCacheForChannelPosts(post.ChannelId)
|
|
|
|
|
|
|
|
|
|
return post, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DeleteFlaggedPosts(postId string) {
|
|
|
|
|
if result := <-Srv.Store.Preference().DeleteCategoryAndName(model.PREFERENCE_CATEGORY_FLAGGED_POST, postId); result.Err != nil {
|
|
|
|
|
l4g.Warn(utils.T("api.post.delete_flagged_post.app_error.warn"), result.Err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DeletePostFiles(post *model.Post) {
|
|
|
|
|
if len(post.FileIds) != 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if result := <-Srv.Store.FileInfo().DeleteForPost(post.Id); result.Err != nil {
|
|
|
|
|
l4g.Warn(utils.T("api.post.delete_post_files.app_error.warn"), post.Id, result.Err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SearchPostsInTeam(terms string, userId string, teamId string, isOrSearch bool) (*model.PostList, *model.AppError) {
|
|
|
|
|
paramsList := model.ParseSearchParams(terms)
|
|
|
|
|
|
2017-07-07 15:36:17 +01:00
|
|
|
esInterface := einterfaces.GetElasticsearchInterface()
|
2017-07-27 08:48:02 +01:00
|
|
|
if esInterface != nil && *utils.Cfg.ElasticsearchSettings.EnableSearching && utils.IsLicensed && *utils.License.Features.Elasticsearch {
|
2017-05-18 16:26:52 +01:00
|
|
|
finalParamsList := []*model.SearchParams{}
|
|
|
|
|
|
|
|
|
|
for _, params := range paramsList {
|
|
|
|
|
params.OrTerms = isOrSearch
|
|
|
|
|
// Don't allow users to search for "*"
|
|
|
|
|
if params.Terms != "*" {
|
|
|
|
|
// Convert channel names to channel IDs
|
|
|
|
|
for idx, channelName := range params.InChannels {
|
|
|
|
|
if channel, err := GetChannelByName(channelName, teamId); err != nil {
|
|
|
|
|
l4g.Error(err)
|
|
|
|
|
} else {
|
|
|
|
|
params.InChannels[idx] = channel.Id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert usernames to user IDs
|
|
|
|
|
for idx, username := range params.FromUsers {
|
|
|
|
|
if user, err := GetUserByUsername(username); err != nil {
|
|
|
|
|
l4g.Error(err)
|
|
|
|
|
} else {
|
|
|
|
|
params.FromUsers[idx] = user.Id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finalParamsList = append(finalParamsList, params)
|
|
|
|
|
}
|
2017-01-25 09:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
2017-05-18 16:26:52 +01:00
|
|
|
// We only allow the user to search in channels they are a member of.
|
|
|
|
|
userChannels, err := GetChannelsForUser(teamId, userId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
l4g.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-07 15:36:17 +01:00
|
|
|
postIds, err := einterfaces.GetElasticsearchInterface().SearchPosts(userChannels, finalParamsList)
|
2017-05-18 16:26:52 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the posts
|
|
|
|
|
postList := model.NewPostList()
|
|
|
|
|
if presult := <-Srv.Store.Post().GetPostsByIds(postIds); presult.Err != nil {
|
|
|
|
|
return nil, presult.Err
|
2017-01-25 09:32:42 -05:00
|
|
|
} else {
|
2017-05-18 16:26:52 +01:00
|
|
|
for _, p := range presult.Data.([]*model.Post) {
|
|
|
|
|
postList.AddPost(p)
|
|
|
|
|
postList.AddOrder(p.Id)
|
|
|
|
|
}
|
2017-01-25 09:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
2017-05-18 16:26:52 +01:00
|
|
|
return postList, nil
|
|
|
|
|
} else {
|
|
|
|
|
channels := []store.StoreChannel{}
|
|
|
|
|
|
|
|
|
|
for _, params := range paramsList {
|
|
|
|
|
params.OrTerms = isOrSearch
|
|
|
|
|
// don't allow users to search for everything
|
|
|
|
|
if params.Terms != "*" {
|
|
|
|
|
channels = append(channels, Srv.Store.Post().Search(teamId, userId, params))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posts := model.NewPostList()
|
|
|
|
|
for _, channel := range channels {
|
|
|
|
|
if result := <-channel; result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
data := result.Data.(*model.PostList)
|
|
|
|
|
posts.Extend(data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return posts, nil
|
|
|
|
|
}
|
2017-01-25 09:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
2017-02-20 13:40:32 -05:00
|
|
|
func GetFileInfosForPost(postId string, readFromMaster bool) ([]*model.FileInfo, *model.AppError) {
|
2017-02-13 10:52:50 -05:00
|
|
|
pchan := Srv.Store.Post().GetSingle(postId)
|
2017-02-20 13:40:32 -05:00
|
|
|
fchan := Srv.Store.FileInfo().GetForPost(postId, readFromMaster, true)
|
2017-01-25 09:32:42 -05:00
|
|
|
|
|
|
|
|
var infos []*model.FileInfo
|
|
|
|
|
if result := <-fchan; result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
infos = result.Data.([]*model.FileInfo)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(infos) == 0 {
|
|
|
|
|
// No FileInfos were returned so check if they need to be created for this post
|
|
|
|
|
var post *model.Post
|
|
|
|
|
if result := <-pchan; result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
2017-02-13 10:52:50 -05:00
|
|
|
post = result.Data.(*model.Post)
|
2017-01-25 09:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(post.Filenames) > 0 {
|
2017-02-07 12:36:37 -08:00
|
|
|
Srv.Store.FileInfo().InvalidateFileInfosForPostCache(postId)
|
2017-01-25 09:32:42 -05:00
|
|
|
// The post has Filenames that need to be replaced with FileInfos
|
|
|
|
|
infos = MigrateFilenamesToFileInfos(post)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return infos, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetOpenGraphMetadata(url string) *opengraph.OpenGraph {
|
|
|
|
|
og := opengraph.NewOpenGraph()
|
|
|
|
|
|
2017-05-31 16:34:05 +02:00
|
|
|
res, err := utils.HttpClient().Get(url)
|
2017-01-25 09:32:42 -05:00
|
|
|
if err != nil {
|
2017-04-12 13:02:31 -07:00
|
|
|
l4g.Error("GetOpenGraphMetadata request failed for url=%v with err=%v", url, err.Error())
|
2017-01-25 09:32:42 -05:00
|
|
|
return og
|
|
|
|
|
}
|
2017-02-07 20:56:14 +05:30
|
|
|
defer CloseBody(res)
|
2017-01-25 09:32:42 -05:00
|
|
|
|
|
|
|
|
if err := og.ProcessHTML(res.Body); err != nil {
|
2017-04-12 13:02:31 -07:00
|
|
|
l4g.Error("GetOpenGraphMetadata processing failed for url=%v with err=%v", url, err.Error())
|
2017-01-25 09:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return og
|
|
|
|
|
}
|