mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
GH-4187 Create direct channel during incoming webhook if not exists (#4206)
This commit is contained in:
committed by
Christopher Speller
parent
b1e2b23b88
commit
e7b25f4cd8
@@ -137,36 +137,19 @@ func createDirectChannel(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
func CreateDirectChannel(userId string, otherUserId string) (*model.Channel, *model.AppError) {
|
func CreateDirectChannel(userId string, otherUserId string) (*model.Channel, *model.AppError) {
|
||||||
uc := Srv.Store.User().Get(otherUserId)
|
uc := Srv.Store.User().Get(otherUserId)
|
||||||
|
|
||||||
channel := new(model.Channel)
|
|
||||||
|
|
||||||
channel.DisplayName = ""
|
|
||||||
channel.Name = model.GetDMNameFromIds(otherUserId, userId)
|
|
||||||
|
|
||||||
channel.Header = ""
|
|
||||||
channel.Type = model.CHANNEL_DIRECT
|
|
||||||
|
|
||||||
if uresult := <-uc; uresult.Err != nil {
|
if uresult := <-uc; uresult.Err != nil {
|
||||||
return nil, model.NewLocAppError("CreateDirectChannel", "api.channel.create_direct_channel.invalid_user.app_error", nil, otherUserId)
|
return nil, model.NewLocAppError("CreateDirectChannel", "api.channel.create_direct_channel.invalid_user.app_error", nil, otherUserId)
|
||||||
}
|
}
|
||||||
|
|
||||||
cm1 := &model.ChannelMember{
|
if result := <-Srv.Store.Channel().CreateDirectChannel(userId, otherUserId); result.Err != nil {
|
||||||
UserId: userId,
|
|
||||||
NotifyProps: model.GetDefaultChannelNotifyProps(),
|
|
||||||
Roles: model.ROLE_CHANNEL_USER.Id,
|
|
||||||
}
|
|
||||||
cm2 := &model.ChannelMember{
|
|
||||||
UserId: otherUserId,
|
|
||||||
NotifyProps: model.GetDefaultChannelNotifyProps(),
|
|
||||||
Roles: model.ROLE_CHANNEL_USER.Id,
|
|
||||||
}
|
|
||||||
|
|
||||||
if result := <-Srv.Store.Channel().SaveDirectChannel(channel, cm1, cm2); result.Err != nil {
|
|
||||||
if result.Err.Id == store.CHANNEL_EXISTS_ERROR {
|
if result.Err.Id == store.CHANNEL_EXISTS_ERROR {
|
||||||
return result.Data.(*model.Channel), nil
|
return result.Data.(*model.Channel), nil
|
||||||
} else {
|
} else {
|
||||||
return nil, result.Err
|
return nil, result.Err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
channel := result.Data.(*model.Channel)
|
||||||
|
|
||||||
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_DIRECT_ADDED, "", channel.Id, "", nil)
|
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_DIRECT_ADDED, "", channel.Id, "", nil)
|
||||||
message.Add("teammate_id", otherUserId)
|
message.Add("teammate_id", otherUserId)
|
||||||
go Publish(message)
|
go Publish(message)
|
||||||
|
|||||||
@@ -412,6 +412,7 @@ func incomingWebhook(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
var channel *model.Channel
|
var channel *model.Channel
|
||||||
var cchan store.StoreChannel
|
var cchan store.StoreChannel
|
||||||
|
var directUserId string
|
||||||
|
|
||||||
if len(channelName) != 0 {
|
if len(channelName) != 0 {
|
||||||
if channelName[0] == '@' {
|
if channelName[0] == '@' {
|
||||||
@@ -419,7 +420,8 @@ func incomingWebhook(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
c.Err = model.NewLocAppError("incomingWebhook", "web.incoming_webhook.user.app_error", nil, "err="+result.Err.Message)
|
c.Err = model.NewLocAppError("incomingWebhook", "web.incoming_webhook.user.app_error", nil, "err="+result.Err.Message)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
channelName = model.GetDMNameFromIds(result.Data.(*model.User).Id, hook.UserId)
|
directUserId = result.Data.(*model.User).Id
|
||||||
|
channelName = model.GetDMNameFromIds(directUserId, hook.UserId)
|
||||||
}
|
}
|
||||||
} else if channelName[0] == '#' {
|
} else if channelName[0] == '#' {
|
||||||
channelName = channelName[1:]
|
channelName = channelName[1:]
|
||||||
@@ -433,7 +435,16 @@ func incomingWebhook(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
overrideUsername := parsedRequest.Username
|
overrideUsername := parsedRequest.Username
|
||||||
overrideIconUrl := parsedRequest.IconURL
|
overrideIconUrl := parsedRequest.IconURL
|
||||||
|
|
||||||
if result := <-cchan; result.Err != nil {
|
result := <-cchan
|
||||||
|
if result.Err != nil && result.Err.Id == store.MISSING_CHANNEL_ERROR && directUserId != "" {
|
||||||
|
newChanResult := <-Srv.Store.Channel().CreateDirectChannel(directUserId, hook.UserId)
|
||||||
|
if newChanResult.Err != nil {
|
||||||
|
c.Err = model.NewLocAppError("incomingWebhook", "web.incoming_webhook.channel.app_error", nil, "err="+newChanResult.Err.Message)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
channel = newChanResult.Data.(*model.Channel)
|
||||||
|
}
|
||||||
|
} else if result.Err != nil {
|
||||||
c.Err = model.NewLocAppError("incomingWebhook", "web.incoming_webhook.channel.app_error", nil, "err="+result.Err.Message)
|
c.Err = model.NewLocAppError("incomingWebhook", "web.incoming_webhook.channel.app_error", nil, "err="+result.Err.Message)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -597,8 +597,6 @@ func TestIncomingWebhooks(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
Client.Must(Client.CreateDirectChannel(user2.Id))
|
|
||||||
|
|
||||||
if _, err := Client.DoPost(url, fmt.Sprintf("{\"text\":\"this is a test\", \"channel\":\"@%s\"}", user2.Username), "application/json"); err != nil {
|
if _, err := Client.DoPost(url, fmt.Sprintf("{\"text\":\"this is a test\", \"channel\":\"@%s\"}", user2.Username), "application/json"); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,6 +83,29 @@ func (s SqlChannelStore) Save(channel *model.Channel) StoreChannel {
|
|||||||
return storeChannel
|
return storeChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s SqlChannelStore) CreateDirectChannel(userId string, otherUserId string) StoreChannel {
|
||||||
|
channel := new(model.Channel)
|
||||||
|
|
||||||
|
channel.DisplayName = ""
|
||||||
|
channel.Name = model.GetDMNameFromIds(otherUserId, userId)
|
||||||
|
|
||||||
|
channel.Header = ""
|
||||||
|
channel.Type = model.CHANNEL_DIRECT
|
||||||
|
|
||||||
|
cm1 := &model.ChannelMember{
|
||||||
|
UserId: userId,
|
||||||
|
NotifyProps: model.GetDefaultChannelNotifyProps(),
|
||||||
|
Roles: model.ROLE_CHANNEL_USER.Id,
|
||||||
|
}
|
||||||
|
cm2 := &model.ChannelMember{
|
||||||
|
UserId: otherUserId,
|
||||||
|
NotifyProps: model.GetDefaultChannelNotifyProps(),
|
||||||
|
Roles: model.ROLE_CHANNEL_USER.Id,
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.SaveDirectChannel(channel, cm1, cm2)
|
||||||
|
}
|
||||||
|
|
||||||
func (s SqlChannelStore) SaveDirectChannel(directchannel *model.Channel, member1 *model.ChannelMember, member2 *model.ChannelMember) StoreChannel {
|
func (s SqlChannelStore) SaveDirectChannel(directchannel *model.Channel, member1 *model.ChannelMember, member2 *model.ChannelMember) StoreChannel {
|
||||||
storeChannel := make(StoreChannel, 1)
|
storeChannel := make(StoreChannel, 1)
|
||||||
|
|
||||||
|
|||||||
@@ -94,7 +94,34 @@ func TestChannelStoreSaveDirectChannel(t *testing.T) {
|
|||||||
if err := (<-store.Channel().SaveDirectChannel(&o1, &m1, &m2)).Err; err == nil {
|
if err := (<-store.Channel().SaveDirectChannel(&o1, &m1, &m2)).Err; err == nil {
|
||||||
t.Fatal("Should not be able to save non-direct channel")
|
t.Fatal("Should not be able to save non-direct channel")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChannelStoreCreateDirectChannel(t *testing.T) {
|
||||||
|
Setup()
|
||||||
|
|
||||||
|
u1 := &model.User{}
|
||||||
|
u1.Email = model.NewId()
|
||||||
|
u1.Nickname = model.NewId()
|
||||||
|
Must(store.User().Save(u1))
|
||||||
|
Must(store.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}))
|
||||||
|
|
||||||
|
u2 := &model.User{}
|
||||||
|
u2.Email = model.NewId()
|
||||||
|
u2.Nickname = model.NewId()
|
||||||
|
Must(store.User().Save(u2))
|
||||||
|
Must(store.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}))
|
||||||
|
|
||||||
|
res := <-store.Channel().CreateDirectChannel(u1.Id, u2.Id)
|
||||||
|
if res.Err != nil {
|
||||||
|
t.Fatal("couldn't create direct channel", res.Err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c1 := res.Data.(*model.Channel)
|
||||||
|
|
||||||
|
members := (<-store.Channel().GetMembers(c1.Id)).Data.([]model.ChannelMember)
|
||||||
|
if len(members) != 2 {
|
||||||
|
t.Fatal("should have saved 2 members")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestChannelStoreUpdate(t *testing.T) {
|
func TestChannelStoreUpdate(t *testing.T) {
|
||||||
|
|||||||
@@ -75,6 +75,7 @@ type TeamStore interface {
|
|||||||
|
|
||||||
type ChannelStore interface {
|
type ChannelStore interface {
|
||||||
Save(channel *model.Channel) StoreChannel
|
Save(channel *model.Channel) StoreChannel
|
||||||
|
CreateDirectChannel(userId string, otherUserId string) StoreChannel
|
||||||
SaveDirectChannel(channel *model.Channel, member1 *model.ChannelMember, member2 *model.ChannelMember) StoreChannel
|
SaveDirectChannel(channel *model.Channel, member1 *model.ChannelMember, member2 *model.ChannelMember) StoreChannel
|
||||||
Update(channel *model.Channel) StoreChannel
|
Update(channel *model.Channel) StoreChannel
|
||||||
Get(id string) StoreChannel
|
Get(id string) StoreChannel
|
||||||
|
|||||||
Reference in New Issue
Block a user