mini_create_article_logic.go
13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package article
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/core"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/message"
"strconv"
"strings"
"text/template"
"unicode/utf8"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool/oss"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniCreateArticleLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniCreateArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniCreateArticleLogic {
return &MiniCreateArticleLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 创建新文章
func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateRequest) (resp *types.MiniArticleCreateResponse, err error) {
var conn = l.svcCtx.DefaultDBConn()
err = l.validateTextLimit(req)
if err != nil {
return nil, err
}
// 检查发布人
author, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.AuthorId)
if err != nil {
return nil, xerr.NewErrMsgErr("创建文章内容失败", err)
}
companyInfo, err := l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, author.CompanyId)
if err != nil {
return nil, xerr.NewErrMsgErr("创建文章内容失败", err)
}
articleAuthor := domain.UserSimple{
Id: author.Id,
Name: author.Name,
Avatar: author.Avatar,
Position: author.Position,
Company: companyInfo.Name,
CompanyId: author.CompanyId,
}
if len(req.Images) > 9 {
return nil, xerr.NewErrMsg("图片数量最多9张")
}
//获取图片的尺寸大小
images := []domain.Image{}
for _, val := range req.Images {
fInfo, _ := oss.GetImageInfo(val)
w, _ := strconv.Atoi(fInfo.ImageWidth.Value)
h, _ := strconv.Atoi(fInfo.ImageHeight.Value)
images = append(images, domain.Image{
Url: val,
Width: w,
Height: h,
})
}
//检查文章可被哪些人查看
whoRead := []int64{}
if len(req.WhoRead) > 0 {
whoRead = lo.Uniq(req.WhoRead)
var u *domain.User
for _, val := range whoRead {
u, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, val)
if err != nil {
return nil, xerr.NewErrMsgErr("文章可查看人设置错误", err)
}
if u.CompanyId != author.CompanyId {
return nil, xerr.NewErrMsg("文章可查看人设置错误")
}
}
}
//检查文章可被哪些人评论
whoReview := []int64{}
if len(req.WhoReview) > 0 {
whoReview = lo.Uniq(req.WhoReview)
}
//有指定可查看人的情况
if len(whoRead) > 0 {
if len(whoReview) > 0 {
// 检查 whoRead 是否 完全包含 whoReview
ok := lo.Every(whoRead, whoReview)
if !ok {
return nil, xerr.NewErrMsg("文章可评论人设置错误")
}
}
if len(whoReview) == 0 {
//有指定可查看人 ,但未指定可评论人
return nil, xerr.NewErrMsg("文章可评论人设置错误")
}
}
//没有指定可查看人的情况
if len(whoRead) == 0 {
if len(whoReview) > 0 {
// 未指定可查看人(全员可看),有指定可评论人,
var u *domain.User
for _, val := range whoReview {
u, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, val)
if err != nil {
return nil, xerr.NewErrMsgErr("文章可评论人设置错误", err)
}
if u.CompanyId != author.CompanyId {
return nil, xerr.NewErrMsg("文章可评论人设置错误")
}
}
}
// if len(whoReview) == 0 {
// 未指定可查看人(全员可看),未指定可评论人 ,忽略判断
// }
}
//切分文章分段
sectionList := NewArticleSectionFromParagraphs(req.Paragraphs, author.CompanyId, 0)
//newStr := ""
//for _, paragraph := range req.Paragraphs {
// strList := []string{paragraph.Text}
// // 无模板样式的
// if paragraph.Type == 0 {
// strList = strings.Split(paragraph.Text, "\n")
// }
// for i2 := range strList {
// newStr = template.HTMLEscapeString(strList[i2])
// newSection := domain.ArticleSection{
// Id: 0,
// CompanyId: author.CompanyId,
// ArticleId: 0,
// Content: newStr,
// SortBy: len(sectionList),
// TotalComment: 0,
//
// Images: paragraph.Images,
// ParagraphType: paragraph.Type,
// ParagraphTemplate: core.NewDomainParagraph(paragraph),
// }
// sectionList = append(sectionList, &newSection)
// }
//}
newArticle := &domain.Article{
Id: 0,
CompanyId: author.CompanyId,
AuthorId: author.Id,
Author: articleAuthor,
Title: req.Title,
Images: images,
WhoRead: whoRead,
WhoReview: whoReview,
Location: domain.Location{
Longitude: req.Location.Longitude,
Latitude: req.Location.Latitude,
Descript: req.Location.Descript,
},
TargetUser: domain.ArticleTargetAll,
CountLove: 0,
CountComment: 0,
CountRead: 0,
Show: domain.ArticleShowEnable,
Tags: []int64{},
MatchUrl: map[string]string{},
Videos: []domain.Video{},
}
if len(whoRead) > 0 {
newArticle.TargetUser = domain.ArticleTargetLimit
}
for k, v := range req.MatchUrl {
newArticle.MatchUrl[k] = v
}
//设置内容概要
newArticle.SetSummary(sectionList)
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
newArticle, err = l.svcCtx.ArticleRepository.Insert(ctx, c, newArticle)
if err != nil {
return xerr.NewErrMsgErr("创建文章失败", err)
}
for i := range sectionList {
sectionList[i].ArticleId = newArticle.Id
_, err = l.svcCtx.ArticleSectionRepository.Insert(ctx, c, sectionList[i])
if err != nil {
return xerr.NewErrMsgErr("创建文章内容失败", err)
}
}
// 生成 备份数据
var backupData domain.ArticleBackup
backupData.MakeBackup(articleAuthor, newArticle, sectionList, "原始版本")
_, err = l.svcCtx.ArticleBackupRepository.Insert(l.ctx, conn, &backupData)
if err != nil {
return xerr.NewErrMsgErr("创建文章失败", err)
}
//发送订阅消息
err = message.NewMiniSubscribeLogic(l.ctx, l.svcCtx).FollowArticle(conn, newArticle)
if err != nil {
return xerr.NewErrMsgErr("创建文章失败", err)
}
// 内容安全检查
if err = core.ContentSecurityCheck(l.ctx, l.svcCtx, c, author, core.NewContentFromArticle(newArticle, sectionList)); err != nil {
return err
}
return nil
}, true)
if err != nil {
return nil, xerr.NewErrMsgErr("创建文章失败", err)
}
resp = &types.MiniArticleCreateResponse{
Id: newArticle.Id,
}
return
}
func (l *MiniCreateArticleLogic) MiniCreateArticleBak(req *types.MiniArticleCreateRequest) (resp *types.MiniArticleCreateResponse, err error) {
var conn = l.svcCtx.DefaultDBConn()
err = l.validateTextLimit(req)
if err != nil {
return nil, err
}
// 检查发布人
author, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.AuthorId)
if err != nil {
return nil, xerr.NewErrMsgErr("创建文章内容失败", err)
}
companyInfo, err := l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, author.CompanyId)
if err != nil {
return nil, xerr.NewErrMsgErr("创建文章内容失败", err)
}
articleAuthor := domain.UserSimple{
Id: author.Id,
Name: author.Name,
Avatar: author.Avatar,
Position: author.Position,
Company: companyInfo.Name,
CompanyId: author.CompanyId,
}
if len(req.Images) > 9 {
return nil, xerr.NewErrMsg("图片数量最多9张")
}
//获取图片的尺寸大小
images := []domain.Image{}
for _, val := range req.Images {
fInfo, _ := oss.GetImageInfo(val)
w, _ := strconv.Atoi(fInfo.ImageWidth.Value)
h, _ := strconv.Atoi(fInfo.ImageHeight.Value)
images = append(images, domain.Image{
Url: val,
Width: w,
Height: h,
})
}
//检查文章可被哪些人查看
whoRead := []int64{}
if len(req.WhoRead) > 0 {
whoRead = lo.Uniq(req.WhoRead)
var u *domain.User
for _, val := range whoRead {
u, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, val)
if err != nil {
return nil, xerr.NewErrMsgErr("文章可查看人设置错误", err)
}
if u.CompanyId != author.CompanyId {
return nil, xerr.NewErrMsg("文章可查看人设置错误")
}
}
}
//检查文章可被哪些人评论
whoReview := []int64{}
if len(req.WhoReview) > 0 {
whoReview = lo.Uniq(req.WhoReview)
}
//有指定可查看人的情况
if len(whoRead) > 0 {
if len(whoReview) > 0 {
// 检查 whoRead 是否 完全包含 whoReview
ok := lo.Every(whoRead, whoReview)
if !ok {
return nil, xerr.NewErrMsg("文章可评论人设置错误")
}
}
if len(whoReview) == 0 {
//有指定可查看人 ,但未指定可评论人
return nil, xerr.NewErrMsg("文章可评论人设置错误")
}
}
//没有指定可查看人的情况
if len(whoRead) == 0 {
if len(whoReview) > 0 {
// 未指定可查看人(全员可看),有指定可评论人,
var u *domain.User
for _, val := range whoReview {
u, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, val)
if err != nil {
return nil, xerr.NewErrMsgErr("文章可评论人设置错误", err)
}
if u.CompanyId != author.CompanyId {
return nil, xerr.NewErrMsg("文章可评论人设置错误")
}
}
}
// if len(whoReview) == 0 {
// 未指定可查看人(全员可看),未指定可评论人 ,忽略判断
// }
}
//切分文章分段
sectionList := []*domain.ArticleSection{}
newStr := ""
for i := range req.Section {
strList := strings.Split(req.Section[i], "\n")
for i2 := range strList {
newStr = template.HTMLEscapeString(strList[i2])
newSection := domain.ArticleSection{
Id: 0,
CompanyId: author.CompanyId,
ArticleId: 0,
Content: newStr,
SortBy: len(sectionList),
TotalComment: 0,
}
sectionList = append(sectionList, &newSection)
}
}
newArticle := &domain.Article{
Id: 0,
CompanyId: author.CompanyId,
AuthorId: author.Id,
Author: articleAuthor,
Title: req.Title,
Images: images,
Attachments: NewDomainAttachments(req.Attachments),
WhoRead: whoRead,
WhoReview: whoReview,
Location: domain.Location{
Longitude: req.Location.Longitude,
Latitude: req.Location.Latitude,
Descript: req.Location.Descript,
},
TargetUser: domain.ArticleTargetAll,
CountLove: 0,
CountComment: 0,
CountRead: 0,
Show: domain.ArticleShowEnable,
Tags: []int64{},
MatchUrl: map[string]string{},
Videos: []domain.Video{},
}
if len(whoRead) > 0 {
newArticle.TargetUser = domain.ArticleTargetLimit
}
for k, v := range req.MatchUrl {
newArticle.MatchUrl[k] = v
}
//设置内容概要
newArticle.SetSummary(sectionList)
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
newArticle, err = l.svcCtx.ArticleRepository.Insert(ctx, c, newArticle)
if err != nil {
return xerr.NewErrMsgErr("创建文章失败", err)
}
for i := range sectionList {
sectionList[i].ArticleId = newArticle.Id
_, err = l.svcCtx.ArticleSectionRepository.Insert(ctx, c, sectionList[i])
if err != nil {
return xerr.NewErrMsgErr("创建文章内容失败", err)
}
}
// 生成 备份数据
var backupData domain.ArticleBackup
backupData.MakeBackup(articleAuthor, newArticle, sectionList, "原始版本")
_, err = l.svcCtx.ArticleBackupRepository.Insert(l.ctx, conn, &backupData)
if err != nil {
return xerr.NewErrMsgErr("创建文章失败", err)
}
//发送订阅消息
err = message.NewMiniSubscribeLogic(l.ctx, l.svcCtx).FollowArticle(conn, newArticle)
if err != nil {
return xerr.NewErrMsgErr("创建文章失败", err)
}
// 内容安全检查
if err = core.ContentSecurityCheck(l.ctx, l.svcCtx, c, author, core.NewContentFromArticle(newArticle, sectionList)); err != nil {
return err
}
return nil
}, true)
if err != nil {
return nil, xerr.NewErrMsgErr("创建文章失败", err)
}
resp = &types.MiniArticleCreateResponse{
Id: newArticle.Id,
}
return
}
// validateTextLimit 验证输入文本长度
func (l *MiniCreateArticleLogic) validateTextLimit(req *types.MiniArticleCreateRequest) error {
titleWordNum := utf8.RuneCountInString(req.Title)
if titleWordNum > 64 {
return xerr.NewErrMsg("标题最多只能输入64字")
}
wordNum := 0
for i := range req.Section {
num := utf8.RuneCountInString(req.Section[i])
wordNum += num
if num > 1000 {
return xerr.NewErrMsg("内容最多只能输入1000字")
}
}
if wordNum > 1000 {
//return xerr.NewErrMsg("内容最多只能输入1000字")
}
return nil
}
func NewDomainAttachments(from []types.FileInfo) []domain.FileInfo {
var to = make([]domain.FileInfo, 0)
lo.ForEach(from, func(item types.FileInfo, index int) {
to = append(to, domain.FileInfo{
Name: item.Name,
Url: item.Url,
})
})
return to
}
func NewTypesAttachments(from []domain.FileInfo) []types.FileInfo {
var to = make([]types.FileInfo, 0)
lo.ForEach(from, func(item domain.FileInfo, index int) {
to = append(to, types.FileInfo{
Name: item.Name,
Url: item.Url,
})
})
return to
}