MM-47171 Export direct channel favorites (#21570)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
279754c6b0
Коммит
b73bf144aa
@@ -626,7 +626,12 @@ func (a *App) exportAllDirectChannels(writer io.Writer) *model.AppError {
|
||||
continue
|
||||
}
|
||||
|
||||
channelLine := ImportLineFromDirectChannel(channel)
|
||||
favoritedBy, err := a.buildFavoritedByList(channel.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
channelLine := ImportLineFromDirectChannel(channel, favoritedBy)
|
||||
if err := a.exportWriteLine(writer, channelLine); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -636,6 +641,29 @@ func (a *App) exportAllDirectChannels(writer io.Writer) *model.AppError {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) buildFavoritedByList(channelID string) ([]string, *model.AppError) {
|
||||
prefs, err := a.Srv().Store().Preference().GetCategoryAndName(model.PreferenceCategoryFavoriteChannel, channelID)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("buildFavoritedByList", "app.preference.get_category.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
}
|
||||
|
||||
userIDs := make([]string, 0, len(prefs))
|
||||
for _, pref := range prefs {
|
||||
if pref.Value != "true" {
|
||||
continue
|
||||
}
|
||||
|
||||
user, err := a.Srv().Store().User().Get(context.Background(), pref.UserId)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("buildFavoritedByList", "app.user.get.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
}
|
||||
|
||||
userIDs = append(userIDs, user.Username)
|
||||
}
|
||||
|
||||
return userIDs, nil
|
||||
}
|
||||
|
||||
func (a *App) exportAllDirectPosts(ctx request.CTX, writer io.Writer, withAttachments bool) ([]imports.AttachmentImportData, *model.AppError) {
|
||||
var attachments []imports.AttachmentImportData
|
||||
afterId := strings.Repeat("0", 26)
|
||||
|
||||
@@ -39,18 +39,25 @@ func ImportLineFromChannel(channel *model.ChannelForExport) *imports.LineImportD
|
||||
}
|
||||
}
|
||||
|
||||
func ImportLineFromDirectChannel(channel *model.DirectChannelForExport) *imports.LineImportData {
|
||||
func ImportLineFromDirectChannel(channel *model.DirectChannelForExport, favoritedBy []string) *imports.LineImportData {
|
||||
channelMembers := *channel.Members
|
||||
if len(channelMembers) == 1 {
|
||||
channelMembers = []string{channelMembers[0], channelMembers[0]}
|
||||
}
|
||||
return &imports.LineImportData{
|
||||
|
||||
line := &imports.LineImportData{
|
||||
Type: "direct_channel",
|
||||
DirectChannel: &imports.DirectChannelImportData{
|
||||
Header: &channel.Header,
|
||||
Members: &channelMembers,
|
||||
},
|
||||
}
|
||||
|
||||
if len(favoritedBy) != 0 {
|
||||
line.DirectChannel.FavoritedBy = &favoritedBy
|
||||
}
|
||||
|
||||
return line
|
||||
}
|
||||
|
||||
func ImportLineFromUser(user *model.User, exportedPrefs map[string]*string) *imports.LineImportData {
|
||||
|
||||
@@ -223,7 +223,16 @@ func TestExportDMChannel(t *testing.T) {
|
||||
defer th1.TearDown()
|
||||
|
||||
// DM Channel
|
||||
th1.CreateDmChannel(th1.BasicUser2)
|
||||
ch := th1.CreateDmChannel(th1.BasicUser2)
|
||||
|
||||
th1.App.Srv().Store().Preference().Save(model.Preferences{
|
||||
{
|
||||
UserId: th1.BasicUser2.Id,
|
||||
Category: model.PreferenceCategoryFavoriteChannel,
|
||||
Name: ch.Id,
|
||||
Value: "true",
|
||||
},
|
||||
})
|
||||
|
||||
var b bytes.Buffer
|
||||
err := th1.App.BulkExport(th1.Context, &b, "somePath", model.BulkExportOpts{})
|
||||
@@ -250,6 +259,12 @@ func TestExportDMChannel(t *testing.T) {
|
||||
require.NoError(t, nErr)
|
||||
require.Equal(t, 1, len(channels))
|
||||
assert.ElementsMatch(t, []string{th1.BasicUser.Username, th1.BasicUser2.Username}, *channels[0].Members)
|
||||
|
||||
// Ensure the favorited channel was retained
|
||||
fav, nErr := th2.App.Srv().Store().Preference().Get(th2.BasicUser2.Id, model.PreferenceCategoryFavoriteChannel, channels[0].Id)
|
||||
require.NoError(t, nErr)
|
||||
require.NotNil(t, fav)
|
||||
require.Equal(t, "true", fav.Value)
|
||||
})
|
||||
|
||||
t.Run("Invalid DM channel export", func(t *testing.T) {
|
||||
|
||||
Ссылка в новой задаче
Block a user