@@ -22,6 +22,7 @@ package controller
22
22
import (
23
23
"encoding/json"
24
24
"fmt"
25
+ "github.com/apache/incubator-answer/internal/base/middleware"
25
26
"github.com/apache/incubator-answer/internal/service/content"
26
27
"github.com/apache/incubator-answer/internal/service/event_queue"
27
28
"github.com/apache/incubator-answer/plugin"
@@ -58,6 +59,7 @@ type TemplateController struct {
58
59
siteInfoService siteinfo_common.SiteInfoCommonService
59
60
eventQueueService event_queue.EventQueueService
60
61
userService * content.UserService
62
+ questionService * content.QuestionService
61
63
}
62
64
63
65
// NewTemplateController new controller
@@ -66,6 +68,7 @@ func NewTemplateController(
66
68
siteInfoService siteinfo_common.SiteInfoCommonService ,
67
69
eventQueueService event_queue.EventQueueService ,
68
70
userService * content.UserService ,
71
+ questionService * content.QuestionService ,
69
72
) * TemplateController {
70
73
script , css := GetStyle ()
71
74
return & TemplateController {
@@ -75,6 +78,7 @@ func NewTemplateController(
75
78
siteInfoService : siteInfoService ,
76
79
eventQueueService : eventQueueService ,
77
80
userService : userService ,
81
+ questionService : questionService ,
78
82
}
79
83
}
80
84
func GetStyle () (script []string , css string ) {
@@ -146,6 +150,14 @@ func (tc *TemplateController) Index(ctx *gin.Context) {
146
150
return
147
151
}
148
152
153
+ hotQuestionReq := & schema.QuestionPageReq {
154
+ Page : 1 ,
155
+ PageSize : 6 ,
156
+ OrderCond : "hot" ,
157
+ InDays : 7 ,
158
+ }
159
+ hotQuestion , _ , _ := tc .templateRenderController .Index (ctx , hotQuestionReq )
160
+
149
161
siteInfo := tc .SiteInfo (ctx )
150
162
siteInfo .Canonical = siteInfo .General .SiteUrl
151
163
@@ -156,10 +168,11 @@ func (tc *TemplateController) Index(ctx *gin.Context) {
156
168
}
157
169
siteInfo .Title = ""
158
170
tc .html (ctx , http .StatusOK , "question.html" , siteInfo , gin.H {
159
- "data" : data ,
160
- "useTitle" : UrlUseTitle ,
161
- "page" : templaterender .Paginator (page , req .PageSize , count ),
162
- "path" : "questions" ,
171
+ "data" : data ,
172
+ "useTitle" : UrlUseTitle ,
173
+ "page" : templaterender .Paginator (page , req .PageSize , count ),
174
+ "path" : "questions" ,
175
+ "hotQuestion" : hotQuestion ,
163
176
})
164
177
}
165
178
@@ -177,6 +190,15 @@ func (tc *TemplateController) QuestionList(ctx *gin.Context) {
177
190
tc .Page404 (ctx )
178
191
return
179
192
}
193
+
194
+ hotQuestionReq := & schema.QuestionPageReq {
195
+ Page : 1 ,
196
+ PageSize : 6 ,
197
+ OrderCond : "hot" ,
198
+ InDays : 7 ,
199
+ }
200
+ hotQuestion , _ , _ := tc .templateRenderController .Index (ctx , hotQuestionReq )
201
+
180
202
siteInfo := tc .SiteInfo (ctx )
181
203
siteInfo .Canonical = fmt .Sprintf ("%s/questions" , siteInfo .General .SiteUrl )
182
204
if page > 1 {
@@ -190,13 +212,14 @@ func (tc *TemplateController) QuestionList(ctx *gin.Context) {
190
212
}
191
213
siteInfo .Title = fmt .Sprintf ("%s - %s" , translator .Tr (handler .GetLang (ctx ), constant .QuestionsTitleTrKey ), siteInfo .General .Name )
192
214
tc .html (ctx , http .StatusOK , "question.html" , siteInfo , gin.H {
193
- "data" : data ,
194
- "useTitle" : UrlUseTitle ,
195
- "page" : templaterender .Paginator (page , req .PageSize , count ),
215
+ "data" : data ,
216
+ "useTitle" : UrlUseTitle ,
217
+ "page" : templaterender .Paginator (page , req .PageSize , count ),
218
+ "hotQuestion" : hotQuestion ,
196
219
})
197
220
}
198
221
199
- func (tc * TemplateController ) QuestionInfoeRdirect (ctx * gin.Context , siteInfo * schema.TemplateSiteInfoResp , correctTitle bool ) (jump bool , url string ) {
222
+ func (tc * TemplateController ) QuestionInfoRedirect (ctx * gin.Context , siteInfo * schema.TemplateSiteInfoResp , correctTitle bool ) (jump bool , url string ) {
200
223
questionID := ctx .Param ("id" )
201
224
title := ctx .Param ("title" )
202
225
answerID := uid .DeShortID (title )
@@ -316,7 +339,7 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
316
339
}
317
340
318
341
siteInfo := tc .SiteInfo (ctx )
319
- jump , jumpurl := tc .QuestionInfoeRdirect (ctx , siteInfo , correctTitle )
342
+ jump , jumpurl := tc .QuestionInfoRedirect (ctx , siteInfo , correctTitle )
320
343
if jump {
321
344
ctx .Redirect (http .StatusFound , jumpurl )
322
345
return
@@ -337,7 +360,6 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
337
360
}
338
361
339
362
// comments
340
-
341
363
objectIDs := []string {uid .DeShortID (id )}
342
364
for _ , answer := range answers {
343
365
answerID := uid .DeShortID (answer .ID )
@@ -348,6 +370,17 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
348
370
tc .Page404 (ctx )
349
371
return
350
372
}
373
+
374
+ UrlUseTitle := false
375
+ if siteInfo .SiteSeo .Permalink == constant .PermalinkQuestionIDAndTitle ||
376
+ siteInfo .SiteSeo .Permalink == constant .PermalinkQuestionIDAndTitleByShortID {
377
+ UrlUseTitle = true
378
+ }
379
+
380
+ //related question
381
+ userID := middleware .GetLoginUserIDFromContext (ctx )
382
+ relatedQuestion , _ , _ := tc .questionService .SimilarQuestion (ctx , id , userID )
383
+
351
384
siteInfo .Canonical = fmt .Sprintf ("%s/questions/%s/%s" , siteInfo .General .SiteUrl , id , encodeTitle )
352
385
if siteInfo .SiteSeo .Permalink == constant .PermalinkQuestionID || siteInfo .SiteSeo .Permalink == constant .PermalinkQuestionIDByShortID {
353
386
siteInfo .Canonical = fmt .Sprintf ("%s/questions/%s" , siteInfo .General .SiteUrl , id )
@@ -389,7 +422,6 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
389
422
item .Author .URL = fmt .Sprintf ("%s/users/%s" , siteInfo .General .SiteUrl , answer .UserInfo .Username )
390
423
answerList = append (answerList , item )
391
424
}
392
-
393
425
}
394
426
jsonLD .MainEntity .SuggestedAnswer = answerList
395
427
jsonLDStr , err := json .Marshal (jsonLD )
@@ -405,12 +437,14 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
405
437
siteInfo .Keywords = strings .Replace (strings .Trim (fmt .Sprint (tags ), "[]" ), " " , "," , - 1 )
406
438
siteInfo .Title = fmt .Sprintf ("%s - %s" , detail .Title , siteInfo .General .Name )
407
439
tc .html (ctx , http .StatusOK , "question-detail.html" , siteInfo , gin.H {
408
- "id" : id ,
409
- "answerid" : answerid ,
410
- "detail" : detail ,
411
- "answers" : answers ,
412
- "comments" : comments ,
413
- "noindex" : detail .Show == entity .QuestionHide ,
440
+ "id" : id ,
441
+ "answerid" : answerid ,
442
+ "detail" : detail ,
443
+ "answers" : answers ,
444
+ "comments" : comments ,
445
+ "noindex" : detail .Show == entity .QuestionHide ,
446
+ "useTitle" : UrlUseTitle ,
447
+ "relatedQuestion" : relatedQuestion ,
414
448
})
415
449
}
416
450
0 commit comments