|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package migrations |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "encoding/json" |
| 25 | + |
| 26 | + "github.com/apache/answer/internal/base/constant" |
| 27 | + "github.com/apache/answer/internal/base/reason" |
| 28 | + "github.com/apache/answer/internal/entity" |
| 29 | + "github.com/apache/answer/internal/schema" |
| 30 | + "github.com/segmentfault/pacman/errors" |
| 31 | + "xorm.io/builder" |
| 32 | + "xorm.io/xorm" |
| 33 | +) |
| 34 | + |
| 35 | +func updateAdminMenuSettings(ctx context.Context, x *xorm.Engine) (err error) { |
| 36 | + err = splitWriteMenu(ctx, x) |
| 37 | + if err != nil { |
| 38 | + return |
| 39 | + } |
| 40 | + return |
| 41 | +} |
| 42 | + |
| 43 | +// splitWriteMenu splits the site write settings into advanced, questions, and tags settings |
| 44 | +func splitWriteMenu(ctx context.Context, x *xorm.Engine) error { |
| 45 | + var ( |
| 46 | + siteInfo = &entity.SiteInfo{} |
| 47 | + siteInfoAdvanced = &entity.SiteInfo{} |
| 48 | + siteInfoQuestions = &entity.SiteInfo{} |
| 49 | + siteInfoTags = &entity.SiteInfo{} |
| 50 | + ) |
| 51 | + exist, err := x.Context(ctx).Where(builder.Eq{"type": constant.SiteTypeWrite}).Get(siteInfo) |
| 52 | + if err != nil { |
| 53 | + err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() |
| 54 | + return err |
| 55 | + } |
| 56 | + if !exist { |
| 57 | + return nil |
| 58 | + } |
| 59 | + siteWrite := &schema.SiteWriteResp{} |
| 60 | + if err := json.Unmarshal([]byte(siteInfo.Content), siteWrite); err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + // site advanced settings |
| 64 | + siteAdvanced := &schema.SiteAdvancedResp{ |
| 65 | + MaxImageSize: siteWrite.MaxImageSize, |
| 66 | + MaxAttachmentSize: siteWrite.MaxAttachmentSize, |
| 67 | + MaxImageMegapixel: siteWrite.MaxImageMegapixel, |
| 68 | + AuthorizedImageExtensions: siteWrite.AuthorizedImageExtensions, |
| 69 | + AuthorizedAttachmentExtensions: siteWrite.AuthorizedAttachmentExtensions, |
| 70 | + } |
| 71 | + // site questions settings |
| 72 | + siteQuestions := &schema.SiteQuestionsResp{ |
| 73 | + MinimumContent: siteWrite.MinimumContent, |
| 74 | + RestrictAnswer: siteWrite.RestrictAnswer, |
| 75 | + } |
| 76 | + // site tags settings |
| 77 | + siteTags := &schema.SiteTagsResp{ |
| 78 | + ReservedTags: siteWrite.ReservedTags, |
| 79 | + RecommendTags: siteWrite.RecommendTags, |
| 80 | + MinimumTags: siteWrite.MinimumTags, |
| 81 | + RequiredTag: siteWrite.RequiredTag, |
| 82 | + } |
| 83 | + |
| 84 | + // save site settings |
| 85 | + // save advanced settings |
| 86 | + existsAdvanced, err := x.Context(ctx).Where(builder.Eq{"type": constant.SiteTypeWrite}).Get(siteInfoAdvanced) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + advancedContent, err := json.Marshal(siteAdvanced) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + if existsAdvanced { |
| 95 | + _, err = x.Context(ctx).ID(siteInfoAdvanced.ID).Update(&entity.SiteInfo{ |
| 96 | + Type: constant.SiteTypeAdvanced, |
| 97 | + Content: string(advancedContent), |
| 98 | + Status: 1, |
| 99 | + }) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + } else { |
| 104 | + _, err = x.Context(ctx).Insert(&entity.SiteInfo{ |
| 105 | + Type: constant.SiteTypeAdvanced, |
| 106 | + Content: string(advancedContent), |
| 107 | + Status: 1, |
| 108 | + }) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // save questions settings |
| 115 | + existsQuestions, err := x.Context(ctx).Where(builder.Eq{"type": constant.SiteTypeQuestions}).Get(siteInfoQuestions) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + questionsContent, err := json.Marshal(siteQuestions) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + if existsQuestions { |
| 124 | + _, err = x.Context(ctx).ID(siteInfoQuestions.ID).Update(&entity.SiteInfo{ |
| 125 | + Type: constant.SiteTypeQuestions, |
| 126 | + Content: string(questionsContent), |
| 127 | + Status: 1, |
| 128 | + }) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + } else { |
| 133 | + _, err = x.Context(ctx).Insert(&entity.SiteInfo{ |
| 134 | + Type: constant.SiteTypeQuestions, |
| 135 | + Content: string(questionsContent), |
| 136 | + Status: 1, |
| 137 | + }) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // save tags settings |
| 144 | + existsTags, err := x.Context(ctx).Where(builder.Eq{"type": constant.SiteTypeTags}).Get(siteInfoTags) |
| 145 | + if err != nil { |
| 146 | + return err |
| 147 | + } |
| 148 | + tagsContent, err := json.Marshal(siteTags) |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + if existsTags { |
| 153 | + _, err = x.Context(ctx).ID(siteInfoTags.ID).Update(&entity.SiteInfo{ |
| 154 | + Type: constant.SiteTypeTags, |
| 155 | + Content: string(tagsContent), |
| 156 | + Status: 1, |
| 157 | + }) |
| 158 | + if err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + } else { |
| 162 | + _, err = x.Context(ctx).Insert(&entity.SiteInfo{ |
| 163 | + Type: constant.SiteTypeTags, |
| 164 | + Content: string(tagsContent), |
| 165 | + Status: 1, |
| 166 | + }) |
| 167 | + if err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return nil |
| 173 | +} |
0 commit comments