summaryrefslogtreecommitdiffstats
path: root/api/post.go
diff options
context:
space:
mode:
authorhmhealey <harrisonmhealey@gmail.com>2016-02-02 10:52:18 -0500
committerhmhealey <harrisonmhealey@gmail.com>2016-02-04 11:38:56 -0500
commitfcb92fa1b53a2b67323a881e7cb03965d3ec24d1 (patch)
treef9e9c4470fdb98c558ba6db4e5ebb08fc2a4a617 /api/post.go
parentdb13b7a3ca3d3099392be557e47f838a7851a69a (diff)
downloadchat-fcb92fa1b53a2b67323a881e7cb03965d3ec24d1.tar.gz
chat-fcb92fa1b53a2b67323a881e7cb03965d3ec24d1.tar.bz2
chat-fcb92fa1b53a2b67323a881e7cb03965d3ec24d1.zip
Added ephemeral messages sent when a user mentions someone not in the channel
Diffstat (limited to 'api/post.go')
-rw-r--r--api/post.go45
1 files changed, 40 insertions, 5 deletions
diff --git a/api/post.go b/api/post.go
index b72af7c48..ee2b9a4b2 100644
--- a/api/post.go
+++ b/api/post.go
@@ -15,6 +15,7 @@ import (
"net/url"
"path/filepath"
"regexp"
+ "sort"
"strconv"
"strings"
"time"
@@ -249,7 +250,7 @@ func handlePostEventsAndForget(c *Context, post *model.Post, triggerWebhooks boo
}
sendNotificationsAndForget(c, post, team, channel)
- go checkForOutOfChannelMentions(post, channel)
+ go checkForOutOfChannelMentions(c, post, channel)
var user *model.User
if result := <-uchan; result.Err != nil {
@@ -729,18 +730,52 @@ func updateMentionCountAndForget(channelId, userId string) {
}()
}
-func checkForOutOfChannelMentions(post *model.Post, channel *model.Channel) {
+func checkForOutOfChannelMentions(c *Context, post *model.Post, channel *model.Channel) {
// don't check for out of channel mentions in direct channels
if channel.Type == model.CHANNEL_DIRECT {
return
}
mentioned := getOutOfChannelMentions(post, channel.TeamId)
+ if len(mentioned) == 0 {
+ return
+ }
- // TODO come up with a way to alert the client of these
- for _, user := range mentioned {
- l4g.Debug("%v was mentioned and wasn't in the channel", user.Username)
+ usernames := make([]string, len(mentioned))
+ for i, user := range mentioned {
+ usernames[i] = user.Username
+ }
+ sort.Strings(usernames)
+
+ var messageText string
+ if len(usernames) == 1 {
+ messageText = c.T("api.post.check_for_out_of_channel_mentions.message.one", map[string]interface{}{
+ "Username": usernames[0],
+ })
+ } else {
+ messageText = c.T("api.post.check_for_out_of_channel_mentions.message.multiple", map[string]interface{}{
+ "Usernames": strings.Join(usernames[:len(usernames)-1], ", "),
+ "LastUsername": usernames[len(usernames)-1],
+ })
}
+
+ // create an ephemeral post that will be sent only to the sender of this original post and not stored in the DB
+ warningPost := model.Post{
+ Id: model.NewId(),
+ ChannelId: post.ChannelId,
+ Message: messageText,
+ Type: model.POST_OUT_OF_CHANNEL_MENTION,
+ CreateAt: post.CreateAt + 1,
+ Ephemeral: true,
+ Props: model.StringInterface{},
+ Filenames: []string{},
+ }
+
+ message := model.NewMessage(c.Session.TeamId, channel.Id, post.UserId, model.ACTION_EPHEMERAL_MESSAGE)
+ message.Add("post", warningPost.ToJson())
+ message.Add("channel_type", channel.Type)
+
+ PublishAndForget(message)
}
// Gets a list of users that were mentioned in a given post that aren't in the channel that the post was made in