chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package templaterender
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/apache/answer/internal/schema"
|
||||
)
|
||||
|
||||
func (t *TemplateRenderController) AnswerList(ctx context.Context, req *schema.AnswerListReq) ([]*schema.AnswerInfo, int64, error) {
|
||||
return t.answerService.SearchList(ctx, req)
|
||||
}
|
||||
|
||||
func (t *TemplateRenderController) AnswerDetail(ctx context.Context, id string) (*schema.AnswerInfo, error) {
|
||||
return t.answerService.GetDetail(ctx, id)
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package templaterender
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/apache/answer/internal/base/pager"
|
||||
"github.com/apache/answer/internal/schema"
|
||||
)
|
||||
|
||||
func (t *TemplateRenderController) CommentList(
|
||||
ctx context.Context,
|
||||
objectIDs []string,
|
||||
) (
|
||||
comments map[string][]*schema.GetCommentResp,
|
||||
err error,
|
||||
) {
|
||||
comments = make(map[string][]*schema.GetCommentResp, len(objectIDs))
|
||||
|
||||
for _, objectID := range objectIDs {
|
||||
var (
|
||||
req = &schema.GetCommentWithPageReq{
|
||||
Page: 1,
|
||||
PageSize: 3,
|
||||
ObjectID: objectID,
|
||||
QueryCond: "vote",
|
||||
UserID: "",
|
||||
}
|
||||
pageModel *pager.PageModel
|
||||
)
|
||||
pageModel, err = t.commentService.GetCommentWithPage(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
li := pageModel.List
|
||||
comments[objectID] = li.([]*schema.GetCommentResp)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package templaterender
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/apache/answer/internal/service/content"
|
||||
questioncommon "github.com/apache/answer/internal/service/question_common"
|
||||
|
||||
"github.com/apache/answer/internal/service/comment"
|
||||
"github.com/apache/answer/internal/service/siteinfo_common"
|
||||
"github.com/google/wire"
|
||||
|
||||
"github.com/apache/answer/internal/schema"
|
||||
"github.com/apache/answer/internal/service/tag"
|
||||
)
|
||||
|
||||
// ProviderSetTemplateRenderController is template render controller providers.
|
||||
var ProviderSetTemplateRenderController = wire.NewSet(
|
||||
NewTemplateRenderController,
|
||||
)
|
||||
|
||||
type TemplateRenderController struct {
|
||||
questionService *content.QuestionService
|
||||
userService *content.UserService
|
||||
tagService *tag.TagService
|
||||
answerService *content.AnswerService
|
||||
commentService *comment.CommentService
|
||||
siteInfoService siteinfo_common.SiteInfoCommonService
|
||||
questionRepo questioncommon.QuestionRepo
|
||||
}
|
||||
|
||||
func NewTemplateRenderController(
|
||||
questionService *content.QuestionService,
|
||||
userService *content.UserService,
|
||||
tagService *tag.TagService,
|
||||
answerService *content.AnswerService,
|
||||
commentService *comment.CommentService,
|
||||
siteInfoService siteinfo_common.SiteInfoCommonService,
|
||||
questionRepo questioncommon.QuestionRepo,
|
||||
) *TemplateRenderController {
|
||||
return &TemplateRenderController{
|
||||
questionService: questionService,
|
||||
userService: userService,
|
||||
tagService: tagService,
|
||||
answerService: answerService,
|
||||
commentService: commentService,
|
||||
questionRepo: questionRepo,
|
||||
siteInfoService: siteInfoService,
|
||||
}
|
||||
}
|
||||
|
||||
// Paginator page
|
||||
// page : now page
|
||||
// pageSize : Number per page
|
||||
// nums : Total
|
||||
// Returns the contents of the page in the format of 1, 2, 3, 4, and 5. If the contents are less than 5 pages, the page number is returned
|
||||
func Paginator(page, pageSize int, nums int64) *schema.Paginator {
|
||||
if pageSize == 0 {
|
||||
pageSize = 10
|
||||
}
|
||||
|
||||
var prevpage int // Previous page address
|
||||
var nextpage int // Address on the last page
|
||||
// Generate the total number of pages based on the total number of nums and the number of prepage pages
|
||||
totalpages := int(math.Ceil(float64(nums) / float64(pageSize))) // Total number of Pages
|
||||
if page > totalpages {
|
||||
page = totalpages
|
||||
}
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
var pages []int
|
||||
switch {
|
||||
case page >= totalpages-5 && totalpages > 5: // The last 5 pages
|
||||
start := totalpages - 5 + 1
|
||||
prevpage = page - 1
|
||||
nextpage = int(math.Min(float64(totalpages), float64(page+1)))
|
||||
pages = make([]int, 5)
|
||||
for i := range pages {
|
||||
pages[i] = start + i
|
||||
}
|
||||
case page >= 3 && totalpages > 5:
|
||||
start := page - 3 + 1
|
||||
pages = make([]int, 5)
|
||||
for i := range pages {
|
||||
pages[i] = start + i
|
||||
}
|
||||
prevpage = page - 1
|
||||
nextpage = page + 1
|
||||
default:
|
||||
pages = make([]int, int(math.Min(5, float64(totalpages))))
|
||||
for i := range pages {
|
||||
pages[i] = i + 1
|
||||
}
|
||||
prevpage = int(math.Max(float64(1), float64(page-1)))
|
||||
nextpage = page + 1
|
||||
}
|
||||
paginator := &schema.Paginator{}
|
||||
paginator.Pages = pages
|
||||
paginator.Totalpages = totalpages
|
||||
paginator.Prevpage = prevpage
|
||||
paginator.Nextpage = nextpage
|
||||
paginator.Currpage = page
|
||||
return paginator
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package templaterender
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"math"
|
||||
"net/http"
|
||||
|
||||
"github.com/apache/answer/internal/base/constant"
|
||||
"github.com/apache/answer/internal/schema"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/segmentfault/pacman/log"
|
||||
)
|
||||
|
||||
func (t *TemplateRenderController) Index(ctx *gin.Context, req *schema.QuestionPageReq) ([]*schema.QuestionPageResp, int64, error) {
|
||||
return t.questionService.GetQuestionPage(ctx, req)
|
||||
}
|
||||
|
||||
func (t *TemplateRenderController) QuestionDetail(ctx *gin.Context, id string) (resp *schema.QuestionInfoResp, err error) {
|
||||
return t.questionService.GetQuestion(ctx, id, "", schema.QuestionPermission{})
|
||||
}
|
||||
|
||||
func (t *TemplateRenderController) Sitemap(ctx *gin.Context) {
|
||||
general, err := t.siteInfoService.GetSiteGeneral(ctx)
|
||||
if err != nil {
|
||||
log.Error("get site general failed:", err)
|
||||
return
|
||||
}
|
||||
siteInfo, err := t.siteInfoService.GetSiteSeo(ctx)
|
||||
if err != nil {
|
||||
log.Error("get site GetSiteSeo failed:", err)
|
||||
return
|
||||
}
|
||||
|
||||
questions, err := t.questionRepo.SitemapQuestions(ctx, 1, constant.SitemapMaxSize)
|
||||
if err != nil {
|
||||
log.Errorf("get sitemap questions failed: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Header("Content-Type", "application/xml")
|
||||
if len(questions) < constant.SitemapMaxSize {
|
||||
ctx.HTML(
|
||||
http.StatusOK, "sitemap.xml", gin.H{
|
||||
"xmlHeader": template.HTML(`<?xml version="1.0" encoding="UTF-8"?>`),
|
||||
"list": questions,
|
||||
"general": general,
|
||||
"hastitle": siteInfo.Permalink == constant.PermalinkQuestionIDAndTitle ||
|
||||
siteInfo.Permalink == constant.PermalinkQuestionIDAndTitleByShortID,
|
||||
},
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
questionNum, err := t.questionRepo.GetQuestionCount(ctx)
|
||||
if err != nil {
|
||||
log.Error("GetQuestionCount error", err)
|
||||
return
|
||||
}
|
||||
var pageList []int
|
||||
totalPages := int(math.Ceil(float64(questionNum) / float64(constant.SitemapMaxSize)))
|
||||
for i := 1; i <= totalPages; i++ {
|
||||
pageList = append(pageList, i)
|
||||
}
|
||||
ctx.HTML(
|
||||
http.StatusOK, "sitemap-list.xml", gin.H{
|
||||
"xmlHeader": template.HTML(`<?xml version="1.0" encoding="UTF-8"?>`),
|
||||
"page": pageList,
|
||||
"general": general,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (t *TemplateRenderController) OpenSearch(ctx *gin.Context) {
|
||||
general, err := t.siteInfoService.GetSiteGeneral(ctx)
|
||||
if err != nil {
|
||||
log.Error("get site general failed:", err)
|
||||
return
|
||||
}
|
||||
|
||||
favicon := general.SiteUrl + "/favicon.ico"
|
||||
branding, err := t.siteInfoService.GetSiteBranding(ctx)
|
||||
if err == nil && len(branding.Favicon) > 0 {
|
||||
favicon = branding.Favicon
|
||||
}
|
||||
|
||||
ctx.Header("Content-Type", "application/xml")
|
||||
ctx.HTML(
|
||||
http.StatusOK, "opensearch.xml", gin.H{
|
||||
"general": general,
|
||||
"favicon": favicon,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (t *TemplateRenderController) SitemapPage(ctx *gin.Context, page int) error {
|
||||
general, err := t.siteInfoService.GetSiteGeneral(ctx)
|
||||
if err != nil {
|
||||
log.Error("get site general failed:", err)
|
||||
return err
|
||||
}
|
||||
siteInfo, err := t.siteInfoService.GetSiteSeo(ctx)
|
||||
if err != nil {
|
||||
log.Error("get site GetSiteSeo failed:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
questions, err := t.questionRepo.SitemapQuestions(ctx, page, constant.SitemapMaxSize)
|
||||
if err != nil {
|
||||
log.Errorf("get sitemap questions failed: %s", err)
|
||||
return err
|
||||
}
|
||||
ctx.Header("Content-Type", "application/xml")
|
||||
ctx.HTML(
|
||||
http.StatusOK, "sitemap.xml", gin.H{
|
||||
"xmlHeader": template.HTML(`<?xml version="1.0" encoding="UTF-8"?>`),
|
||||
"list": questions,
|
||||
"general": general,
|
||||
"hastitle": siteInfo.Permalink == constant.PermalinkQuestionIDAndTitle ||
|
||||
siteInfo.Permalink == constant.PermalinkQuestionIDAndTitleByShortID,
|
||||
},
|
||||
)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package templaterender
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/apache/answer/internal/base/pager"
|
||||
"github.com/apache/answer/internal/schema"
|
||||
"github.com/jinzhu/copier"
|
||||
)
|
||||
|
||||
func (q *TemplateRenderController) TagList(ctx context.Context, req *schema.GetTagWithPageReq) (resp *pager.PageModel, err error) {
|
||||
resp, err = q.tagService.GetTagWithPage(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (q *TemplateRenderController) TagInfo(ctx context.Context, req *schema.GetTamplateTagInfoReq) (resp *schema.GetTagResp, questionList []*schema.QuestionPageResp, questionCount int64, err error) {
|
||||
dto := &schema.GetTagInfoReq{}
|
||||
_ = copier.Copy(dto, req)
|
||||
resp, err = q.tagService.GetTagInfo(ctx, dto)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
searchQuestion := &schema.QuestionPageReq{}
|
||||
searchQuestion.Page = req.Page
|
||||
searchQuestion.PageSize = req.PageSize
|
||||
searchQuestion.OrderCond = "newest"
|
||||
searchQuestion.Tag = req.Name
|
||||
searchQuestion.LoginUserID = req.UserID
|
||||
questionList, questionCount, err = q.questionService.GetQuestionPage(ctx, searchQuestion)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return resp, questionList, questionCount, err
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package templaterender
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/apache/answer/internal/schema"
|
||||
)
|
||||
|
||||
func (q *TemplateRenderController) UserInfo(ctx context.Context, req *schema.GetOtherUserInfoByUsernameReq) (resp *schema.GetOtherUserInfoByUsernameResp, err error) {
|
||||
return q.userService.GetOtherUserInfoByUsername(ctx, req)
|
||||
}
|
||||
Reference in New Issue
Block a user