GH-4187 Create direct channel during incoming webhook if not exists (#4206)

Этот коммит содержится в:
Alexander Smaga
2016-10-17 15:12:56 +03:00
коммит произвёл Christopher Speller
родитель b1e2b23b88
Коммит e7b25f4cd8
6 изменённых файлов: 67 добавлений и 24 удалений

Просмотреть файл

@@ -137,36 +137,19 @@ func createDirectChannel(c *Context, w http.ResponseWriter, r *http.Request) {
func CreateDirectChannel(userId string, otherUserId string) (*model.Channel, *model.AppError) {
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 {
return nil, model.NewLocAppError("CreateDirectChannel", "api.channel.create_direct_channel.invalid_user.app_error", nil, otherUserId)
}
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,
}
if result := <-Srv.Store.Channel().SaveDirectChannel(channel, cm1, cm2); result.Err != nil {
if result := <-Srv.Store.Channel().CreateDirectChannel(userId, otherUserId); result.Err != nil {
if result.Err.Id == store.CHANNEL_EXISTS_ERROR {
return result.Data.(*model.Channel), nil
} else {
return nil, result.Err
}
} else {
channel := result.Data.(*model.Channel)
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_DIRECT_ADDED, "", channel.Id, "", nil)
message.Add("teammate_id", otherUserId)
go Publish(message)

Просмотреть файл

@@ -412,6 +412,7 @@ func incomingWebhook(c *Context, w http.ResponseWriter, r *http.Request) {
var channel *model.Channel
var cchan store.StoreChannel
var directUserId string
if len(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)
return
} 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] == '#' {
channelName = channelName[1:]
@@ -433,7 +435,16 @@ func incomingWebhook(c *Context, w http.ResponseWriter, r *http.Request) {
overrideUsername := parsedRequest.Username
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)
return
} else {

Просмотреть файл

@@ -597,8 +597,6 @@ func TestIncomingWebhooks(t *testing.T) {
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 {
t.Fatal(err)
}