diff options
author | hmhealey <harrisonmhealey@gmail.com> | 2016-01-30 13:44:39 -0500 |
---|---|---|
committer | hmhealey <harrisonmhealey@gmail.com> | 2016-02-04 11:38:56 -0500 |
commit | b7d045844c4d00d8268da50f2388be1d12c91829 (patch) | |
tree | 7d8b9b80d9ce051c11ff46b62bf0b38b321f60ed /api/post.go | |
parent | 34547186bab624d48866ccd5d3f20b6de9683011 (diff) | |
download | chat-b7d045844c4d00d8268da50f2388be1d12c91829.tar.gz chat-b7d045844c4d00d8268da50f2388be1d12c91829.tar.bz2 chat-b7d045844c4d00d8268da50f2388be1d12c91829.zip |
Added serverside check for a post that @mentions someone who isn't in the channel
Diffstat (limited to 'api/post.go')
-rw-r--r-- | api/post.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/api/post.go b/api/post.go index e8345b5e5..b72af7c48 100644 --- a/api/post.go +++ b/api/post.go @@ -249,6 +249,7 @@ func handlePostEventsAndForget(c *Context, post *model.Post, triggerWebhooks boo } sendNotificationsAndForget(c, post, team, channel) + go checkForOutOfChannelMentions(post, channel) var user *model.User if result := <-uchan; result.Err != nil { @@ -728,6 +729,58 @@ func updateMentionCountAndForget(channelId, userId string) { }() } +func checkForOutOfChannelMentions(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) + + // 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) + } +} + +// Gets a list of users that were mentioned in a given post that aren't in the channel that the post was made in +func getOutOfChannelMentions(post *model.Post, teamId string) []*model.User { + pchan := Srv.Store.User().GetProfiles(teamId) + mchan := Srv.Store.Channel().GetMembers(post.ChannelId) + + var profiles map[string]*model.User + if result := <-pchan; result.Err != nil { + l4g.Error(utils.T("api.post.get_out_of_channel_mentions.retrieve_profiles.error"), teamId, result.Err) + return []*model.User{} + } else { + profiles = result.Data.(map[string]*model.User) + } + + // only keep profiles which aren't in the current channel + if result := <-mchan; result.Err != nil { + l4g.Error(utils.T("api.post.get_out_of_channel_mentions.retrieve_members.error"), post.ChannelId, result.Err) + return []*model.User{} + } else { + members := result.Data.([]model.ChannelMember) + + for _, member := range members { + delete(profiles, member.UserId) + } + } + + var mentioned []*model.User + + for _, profile := range profiles { + if pattern, err := regexp.Compile(`(\W|^)@` + regexp.QuoteMeta(profile.Username) + `(\W|$)`); err != nil { + l4g.Error(utils.T("api.post.get_out_of_channel_mentions.regex.error"), profile.Id, err) + } else if pattern.MatchString(post.Message) { + mentioned = append(mentioned, profile) + } + } + + return mentioned +} + func updatePost(c *Context, w http.ResponseWriter, r *http.Request) { post := model.PostFromJson(r.Body) |