Skip to content

Commit a8e008d

Browse files
sy-recordsshuashuai
authored andcommitted
feat: Support related and hot questions template
1 parent 8602065 commit a8e008d

File tree

6 files changed

+146
-22
lines changed

6 files changed

+146
-22
lines changed

Diff for: cmd/wire_gen.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: internal/controller/template_controller.go

+51-17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package controller
2222
import (
2323
"encoding/json"
2424
"fmt"
25+
"github.com/apache/incubator-answer/internal/base/middleware"
2526
"github.com/apache/incubator-answer/internal/service/content"
2627
"github.com/apache/incubator-answer/internal/service/event_queue"
2728
"github.com/apache/incubator-answer/plugin"
@@ -58,6 +59,7 @@ type TemplateController struct {
5859
siteInfoService siteinfo_common.SiteInfoCommonService
5960
eventQueueService event_queue.EventQueueService
6061
userService *content.UserService
62+
questionService *content.QuestionService
6163
}
6264

6365
// NewTemplateController new controller
@@ -66,6 +68,7 @@ func NewTemplateController(
6668
siteInfoService siteinfo_common.SiteInfoCommonService,
6769
eventQueueService event_queue.EventQueueService,
6870
userService *content.UserService,
71+
questionService *content.QuestionService,
6972
) *TemplateController {
7073
script, css := GetStyle()
7174
return &TemplateController{
@@ -75,6 +78,7 @@ func NewTemplateController(
7578
siteInfoService: siteInfoService,
7679
eventQueueService: eventQueueService,
7780
userService: userService,
81+
questionService: questionService,
7882
}
7983
}
8084
func GetStyle() (script []string, css string) {
@@ -146,6 +150,14 @@ func (tc *TemplateController) Index(ctx *gin.Context) {
146150
return
147151
}
148152

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+
149161
siteInfo := tc.SiteInfo(ctx)
150162
siteInfo.Canonical = siteInfo.General.SiteUrl
151163

@@ -156,10 +168,11 @@ func (tc *TemplateController) Index(ctx *gin.Context) {
156168
}
157169
siteInfo.Title = ""
158170
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,
163176
})
164177
}
165178

@@ -177,6 +190,15 @@ func (tc *TemplateController) QuestionList(ctx *gin.Context) {
177190
tc.Page404(ctx)
178191
return
179192
}
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+
180202
siteInfo := tc.SiteInfo(ctx)
181203
siteInfo.Canonical = fmt.Sprintf("%s/questions", siteInfo.General.SiteUrl)
182204
if page > 1 {
@@ -190,13 +212,14 @@ func (tc *TemplateController) QuestionList(ctx *gin.Context) {
190212
}
191213
siteInfo.Title = fmt.Sprintf("%s - %s", translator.Tr(handler.GetLang(ctx), constant.QuestionsTitleTrKey), siteInfo.General.Name)
192214
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,
196219
})
197220
}
198221

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) {
200223
questionID := ctx.Param("id")
201224
title := ctx.Param("title")
202225
answerID := uid.DeShortID(title)
@@ -316,7 +339,7 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
316339
}
317340

318341
siteInfo := tc.SiteInfo(ctx)
319-
jump, jumpurl := tc.QuestionInfoeRdirect(ctx, siteInfo, correctTitle)
342+
jump, jumpurl := tc.QuestionInfoRedirect(ctx, siteInfo, correctTitle)
320343
if jump {
321344
ctx.Redirect(http.StatusFound, jumpurl)
322345
return
@@ -337,7 +360,6 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
337360
}
338361

339362
// comments
340-
341363
objectIDs := []string{uid.DeShortID(id)}
342364
for _, answer := range answers {
343365
answerID := uid.DeShortID(answer.ID)
@@ -348,6 +370,17 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
348370
tc.Page404(ctx)
349371
return
350372
}
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+
351384
siteInfo.Canonical = fmt.Sprintf("%s/questions/%s/%s", siteInfo.General.SiteUrl, id, encodeTitle)
352385
if siteInfo.SiteSeo.Permalink == constant.PermalinkQuestionID || siteInfo.SiteSeo.Permalink == constant.PermalinkQuestionIDByShortID {
353386
siteInfo.Canonical = fmt.Sprintf("%s/questions/%s", siteInfo.General.SiteUrl, id)
@@ -389,7 +422,6 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
389422
item.Author.URL = fmt.Sprintf("%s/users/%s", siteInfo.General.SiteUrl, answer.UserInfo.Username)
390423
answerList = append(answerList, item)
391424
}
392-
393425
}
394426
jsonLD.MainEntity.SuggestedAnswer = answerList
395427
jsonLDStr, err := json.Marshal(jsonLD)
@@ -405,12 +437,14 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
405437
siteInfo.Keywords = strings.Replace(strings.Trim(fmt.Sprint(tags), "[]"), " ", ",", -1)
406438
siteInfo.Title = fmt.Sprintf("%s - %s", detail.Title, siteInfo.General.Name)
407439
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,
414448
})
415449
}
416450

Diff for: ui/template/hot-question.html

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!--
2+
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
20+
-->
21+
{{define "hot-question"}}
22+
<div class="card">
23+
<div class="text-nowrap text-capitalize card-header">{{translator $.language "ui.question.hot_questions"}}</div>
24+
<div class="list-group list-group-flush">
25+
{{ range .hotQuestion }}
26+
{{if $.useTitle }}
27+
<a class="list-group-item list-group-item-action" href="{{$.baseURL}}/questions/{{.ID}}/{{urlTitle .Title}}">
28+
{{else}}
29+
<a class="list-group-item list-group-item-action" href="{{$.baseURL}}/questions/{{.ID}}">
30+
{{end}}
31+
<div class="link-dark">{{ .Title }}</div>
32+
{{if ne 0 .AnswerCount}}
33+
<div class="d-flex align-items-center small mt-1 link-secondary">
34+
<i class="br bi-chat-square-text-fill"></i>
35+
<span class="ms-1">{{translator $.language "ui.question.x_answers" "count" .AnswerCount}}</span>
36+
</div>
37+
{{end}}
38+
</a>
39+
{{ end }}
40+
</div>
41+
</div>
42+
{{end}}

Diff for: ui/template/question-detail.html

+7-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
<div class="mb-5 mb-md-0 col-xxl-7 col-lg-8 col-sm-12">
2525
<div>
2626
<h1 class="h3 mb-3 text-wrap text-break">
27+
{{if $.useTitle }}
28+
<a class="link-dark" href="{{$.baseURL}}/questions/{{.detail.ID}}/{{urlTitle .detail.Title}}">{{.detail.Title}}</a>
29+
{{else}}
2730
<a class="link-dark" href="{{$.baseURL}}/questions/{{.detail.ID}}">{{.detail.Title}}</a>
31+
{{end}}
2832
</h1>
2933
<div
3034
class="d-flex flex-wrap align-items-center small mb-3 text-secondary">
@@ -116,7 +120,7 @@ <h1 class="h3 mb-3 text-wrap text-break">
116120
<h5 class="mb-0">{{.detail.AnswerCount}} Answers</h5>
117121
</div>
118122
{{range .answers}}
119-
<div id="10020000000000930" class="answer-item py-4">
123+
<div class="answer-item py-4">
120124
<article class="fmt">
121125
{{formatLinkNofollow .HTML}}
122126
</article>
@@ -192,8 +196,8 @@ <h5 class="mb-0">{{.detail.AnswerCount}} Answers</h5>
192196
</div>
193197
{{end}}
194198
</div>
195-
<div class="mt-5 mt-lg-0 col-xxl-3 col-lg-4 col-sm-12">
196-
199+
<div class="page-right-side mt-4 mt-xl-0 col">
200+
{{template "related-question" .}}
197201
</div>
198202
</div>
199203
</div>

Diff for: ui/template/question.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ <h5 class="text-wrap text-break">
9494
</div>
9595
</div>
9696
</div>
97-
<div class="mt-5 mt-lg-0 col-xxl-3 col-lg-4 col-sm-12"></div>
97+
<div class="page-right-side mt-4 mt-xl-0 col">
98+
{{template "hot-question" .}}
99+
</div>
98100
</div>
99101
</div>
100102
{{template "footer" .}}

Diff for: ui/template/related-question.html

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!--
2+
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
20+
-->
21+
{{define "related-question"}}
22+
<div class="card">
23+
<div class="card-header">{{translator $.language "ui.related_question.title"}}</div>
24+
<div class="list-group list-group-flush">
25+
{{ range .relatedQuestion }}
26+
{{if $.useTitle }}
27+
<a class="list-group-item list-group-item-action" href="{{$.baseURL}}/questions/{{.ID}}/{{urlTitle .Title}}">
28+
{{else}}
29+
<a class="list-group-item list-group-item-action" href="{{$.baseURL}}/questions/{{.ID}}">
30+
{{end}}
31+
<div class="link-dark">{{ .Title }}</div>
32+
{{if ne 0 .AnswerCount}}
33+
<div class="mt-1 small me-2 link-secondary">
34+
<i class="br bi-chat-square-text-fill me-1"></i>
35+
<span>{{ .AnswerCount }} {{translator $.language "ui.related_question.answers"}}</span>
36+
</div>
37+
{{end}}
38+
</a>
39+
{{ end }}
40+
</div>
41+
</div>
42+
{{end}}

0 commit comments

Comments
 (0)