From 6990d052d5e95295e729aae28a0d30bfdcb98573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Thu, 11 Jan 2018 16:57:47 +0100 Subject: [XYZ-6] Add sampledata platform command (#8027) * Add fake dependency * [XYZ-6] Add sampledata platform command * Creating EMOJI_NAME_MAX_LENGTH as a constant and using it where needed --- app/import.go | 236 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 223 insertions(+), 13 deletions(-) (limited to 'app/import.go') diff --git a/app/import.go b/app/import.go index 850e9c43d..6291794b0 100644 --- a/app/import.go +++ b/app/import.go @@ -9,6 +9,7 @@ import ( "encoding/json" "io" "net/http" + "os" "regexp" "strings" "sync" @@ -53,17 +54,18 @@ type ChannelImportData struct { } type UserImportData struct { - Username *string `json:"username"` - Email *string `json:"email"` - AuthService *string `json:"auth_service"` - AuthData *string `json:"auth_data"` - Password *string `json:"password"` - Nickname *string `json:"nickname"` - FirstName *string `json:"first_name"` - LastName *string `json:"last_name"` - Position *string `json:"position"` - Roles *string `json:"roles"` - Locale *string `json:"locale"` + ProfileImage *string `json:"profile_image"` + Username *string `json:"username"` + Email *string `json:"email"` + AuthService *string `json:"auth_service"` + AuthData *string `json:"auth_data"` + Password *string `json:"password"` + Nickname *string `json:"nickname"` + FirstName *string `json:"first_name"` + LastName *string `json:"last_name"` + Position *string `json:"position"` + Roles *string `json:"roles"` + Locale *string `json:"locale"` Teams *[]UserTeamImportData `json:"teams"` @@ -111,6 +113,22 @@ type UserChannelNotifyPropsImportData struct { MarkUnread *string `json:"mark_unread"` } +type ReactionImportData struct { + User *string `json:"user"` + CreateAt *int64 `json:"create_at"` + EmojiName *string `json:"emoji_name"` +} + +type ReplyImportData struct { + User *string `json:"user"` + + Message *string `json:"message"` + CreateAt *int64 `json:"create_at"` + + FlaggedBy *[]string `json:"flagged_by"` + Reactions *[]ReactionImportData `json:"reactions"` +} + type PostImportData struct { Team *string `json:"team"` Channel *string `json:"channel"` @@ -119,7 +137,9 @@ type PostImportData struct { Message *string `json:"message"` CreateAt *int64 `json:"create_at"` - FlaggedBy *[]string `json:"flagged_by"` + FlaggedBy *[]string `json:"flagged_by"` + Reactions *[]ReactionImportData `json:"reactions"` + Replies *[]ReplyImportData `json:"replies"` } type DirectChannelImportData struct { @@ -136,7 +156,9 @@ type DirectPostImportData struct { Message *string `json:"message"` CreateAt *int64 `json:"create_at"` - FlaggedBy *[]string `json:"flagged_by"` + FlaggedBy *[]string `json:"flagged_by"` + Reactions *[]ReactionImportData `json:"reactions"` + Replies *[]ReplyImportData `json:"replies"` } type LineImportWorkerData struct { @@ -690,6 +712,16 @@ func (a *App) ImportUser(data *UserImportData, dryRun bool) *model.AppError { savedUser = user } + if data.ProfileImage != nil { + file, err := os.Open(*data.ProfileImage) + if err != nil { + l4g.Error(utils.T("api.import.import_user.profile_image.error"), err) + } + if err := a.SetProfileImageFromFile(savedUser.Id, file); err != nil { + l4g.Error(utils.T("api.import.import_user.profile_image.error"), err) + } + } + // Preferences. var preferences model.Preferences @@ -869,6 +901,11 @@ func (a *App) ImportUserChannels(user *model.User, team *model.Team, teamMember } func validateUserImportData(data *UserImportData) *model.AppError { + if data.ProfileImage != nil { + if _, err := os.Stat(*data.ProfileImage); os.IsNotExist(err) { + return model.NewAppError("BulkImport", "app.import.validate_user_import_data.profile_image.error", nil, "", http.StatusBadRequest) + } + } if data.Username == nil { return model.NewAppError("BulkImport", "app.import.validate_user_import_data.username_missing.error", nil, "", http.StatusBadRequest) @@ -1019,6 +1056,79 @@ func validateUserChannelsImportData(data *[]UserChannelImportData) *model.AppErr return nil } +func (a *App) ImportReaction(data *ReactionImportData, post *model.Post, dryRun bool) *model.AppError { + if err := validateReactionImportData(data, post.CreateAt); err != nil { + return err + } + + var user *model.User + if result := <-a.Srv.Store.User().GetByUsername(*data.User); result.Err != nil { + return model.NewAppError("BulkImport", "app.import.import_post.user_not_found.error", map[string]interface{}{"Username": data.User}, "", http.StatusBadRequest) + } else { + user = result.Data.(*model.User) + } + reaction := &model.Reaction{ + UserId: user.Id, + PostId: post.Id, + EmojiName: *data.EmojiName, + CreateAt: *data.CreateAt, + } + if result := <-a.Srv.Store.Reaction().Save(reaction); result.Err != nil { + return result.Err + } + return nil +} + +func (a *App) ImportReply(data *ReplyImportData, post *model.Post, dryRun bool) *model.AppError { + if err := validateReplyImportData(data, post.CreateAt); err != nil { + return err + } + + var user *model.User + if result := <-a.Srv.Store.User().GetByUsername(*data.User); result.Err != nil { + return model.NewAppError("BulkImport", "app.import.import_post.user_not_found.error", map[string]interface{}{"Username": data.User}, "", http.StatusBadRequest) + } else { + user = result.Data.(*model.User) + } + + // Check if this post already exists. + var replies []*model.Post + if result := <-a.Srv.Store.Post().GetPostsCreatedAt(post.ChannelId, *data.CreateAt); result.Err != nil { + return result.Err + } else { + replies = result.Data.([]*model.Post) + } + + var reply *model.Post + for _, r := range replies { + if r.Message == *data.Message { + reply = r + break + } + } + + if reply == nil { + reply = &model.Post{} + } + reply.UserId = user.Id + reply.ChannelId = post.ChannelId + reply.ParentId = post.Id + reply.RootId = post.Id + reply.Message = *data.Message + reply.CreateAt = *data.CreateAt + + if reply.Id == "" { + if result := <-a.Srv.Store.Post().Save(reply); result.Err != nil { + return result.Err + } + } else { + if result := <-a.Srv.Store.Post().Overwrite(reply); result.Err != nil { + return result.Err + } + } + return nil +} + func (a *App) ImportPost(data *PostImportData, dryRun bool) *model.AppError { if err := validatePostImportData(data); err != nil { return err @@ -1114,6 +1224,66 @@ func (a *App) ImportPost(data *PostImportData, dryRun bool) *model.AppError { } } + if data.Reactions != nil { + for _, reaction := range *data.Reactions { + if err := a.ImportReaction(&reaction, post, dryRun); err != nil { + return err + } + } + } + + if data.Replies != nil { + for _, reply := range *data.Replies { + if err := a.ImportReply(&reply, post, dryRun); err != nil { + return err + } + } + } + + return nil +} + +func validateReactionImportData(data *ReactionImportData, parentCreateAt int64) *model.AppError { + if data.User == nil { + return model.NewAppError("BulkImport", "app.import.validate_reaction_import_data.user_missing.error", nil, "", http.StatusBadRequest) + } + + if data.EmojiName == nil { + return model.NewAppError("BulkImport", "app.import.validate_reaction_import_data.emoji_name_missing.error", nil, "", http.StatusBadRequest) + } else if utf8.RuneCountInString(*data.EmojiName) > model.EMOJI_NAME_MAX_LENGTH { + return model.NewAppError("BulkImport", "app.import.validate_reaction_import_data.emoji_name_length.error", nil, "", http.StatusBadRequest) + } + + if data.CreateAt == nil { + return model.NewAppError("BulkImport", "app.import.validate_reaction_import_data.create_at_missing.error", nil, "", http.StatusBadRequest) + } else if *data.CreateAt == 0 { + return model.NewAppError("BulkImport", "app.import.validate_reaction_import_data.create_at_zero.error", nil, "", http.StatusBadRequest) + } else if *data.CreateAt < parentCreateAt { + return model.NewAppError("BulkImport", "app.import.validate_reaction_import_data.create_at_before_parent.error", nil, "", http.StatusBadRequest) + } + + return nil +} + +func validateReplyImportData(data *ReplyImportData, parentCreateAt int64) *model.AppError { + if data.User == nil { + return model.NewAppError("BulkImport", "app.import.validate_reply_import_data.user_missing.error", nil, "", http.StatusBadRequest) + } + + if data.Message == nil { + return model.NewAppError("BulkImport", "app.import.validate_reply_import_data.message_missing.error", nil, "", http.StatusBadRequest) + } else if utf8.RuneCountInString(*data.Message) > model.POST_MESSAGE_MAX_RUNES { + return model.NewAppError("BulkImport", "app.import.validate_reply_import_data.message_length.error", nil, "", http.StatusBadRequest) + } + + if data.CreateAt == nil { + return model.NewAppError("BulkImport", "app.import.validate_reply_import_data.create_at_missing.error", nil, "", http.StatusBadRequest) + } else if *data.CreateAt == 0 { + return model.NewAppError("BulkImport", "app.import.validate_reply_import_data.create_at_zero.error", nil, "", http.StatusBadRequest) + } else if *data.CreateAt < parentCreateAt { + return model.NewAppError("BulkImport", "app.import.validate_reply_import_data.create_at_before_parent.error", nil, "", http.StatusBadRequest) + } + return nil } @@ -1142,6 +1312,18 @@ func validatePostImportData(data *PostImportData) *model.AppError { return model.NewAppError("BulkImport", "app.import.validate_post_import_data.create_at_zero.error", nil, "", http.StatusBadRequest) } + if data.Reactions != nil { + for _, reaction := range *data.Reactions { + validateReactionImportData(&reaction, *data.CreateAt) + } + } + + if data.Replies != nil { + for _, reply := range *data.Replies { + validateReplyImportData(&reply, *data.CreateAt) + } + } + return nil } @@ -1365,6 +1547,22 @@ func (a *App) ImportDirectPost(data *DirectPostImportData, dryRun bool) *model.A } } + if data.Reactions != nil { + for _, reaction := range *data.Reactions { + if err := a.ImportReaction(&reaction, post, dryRun); err != nil { + return err + } + } + } + + if data.Replies != nil { + for _, reply := range *data.Replies { + if err := a.ImportReply(&reply, post, dryRun); err != nil { + return err + } + } + } + return nil } @@ -1412,6 +1610,18 @@ func validateDirectPostImportData(data *DirectPostImportData) *model.AppError { } } + if data.Reactions != nil { + for _, reaction := range *data.Reactions { + validateReactionImportData(&reaction, *data.CreateAt) + } + } + + if data.Replies != nil { + for _, reply := range *data.Replies { + validateReplyImportData(&reply, *data.CreateAt) + } + } + return nil } -- cgit v1.2.3-1-g7c22