Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add reactions to question & answer #943

Merged
merged 13 commits into from
May 27, 2024
13 changes: 8 additions & 5 deletions cmd/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 18 additions & 7 deletions i18n/en_US.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# The following fields are used for back-end

backend:

base:
success:
other: Success.
Expand Down Expand Up @@ -202,6 +201,9 @@ backend:
other: The old password verification failed
new_password_same_as_previous_setting:
other: The new password is the same as the previous one.
meta:
object_not_found:
other: Meta object not found
question:
already_deleted:
other: This post has been deleted.
Expand Down Expand Up @@ -314,7 +316,8 @@ backend:
name:
other: rude or abusive
desc:
other: A reasonable person would find this content inappropriate for respectful
other:
A reasonable person would find this content inappropriate for respectful
discourse.
a_duplicate:
name:
Expand All @@ -327,7 +330,8 @@ backend:
name:
other: not an answer
desc:
other: This was posted as an answer, but it does not attempt to answer the
other:
This was posted as an answer, but it does not attempt to answer the
question. It should possibly be an edit, a comment, another question,
or deleted altogether.
no_longer_needed:
Expand Down Expand Up @@ -388,7 +392,8 @@ backend:
name:
other: needs details or clarity
desc:
other: This question currently includes multiple questions in one. It should
other:
This question currently includes multiple questions in one. It should
focus on one problem only.
other:
name:
Expand Down Expand Up @@ -505,6 +510,9 @@ backend:
other: Flagged post
suggested_post_edit:
other: Suggested edits
reaction:
tooltip:
other: "{{ .Names }} and {{ .Count }} more..."

# The following fields are used for interface presentation(Front-end)
ui:
Expand Down Expand Up @@ -765,6 +773,10 @@ ui:
day: day
hours: hours
days: days
reaction:
heart: heart
smile: smile
frown: frown
comment:
btn_add_comment: Add comment
reply_to: Reply to
Expand Down Expand Up @@ -1441,7 +1453,6 @@ ui:
qrcode_login_tip: Please use {{ agentName }} to scan the QR code and log in.
login_failed_email_tip: Login failed, please allow this app to access your email information before try again.


admin:
admin_header:
title: Admin
Expand Down Expand Up @@ -1822,7 +1833,7 @@ ui:
text: Choose the reputation required for the privileges
msg:
should_be_number: the input should be number
number_larger_1 : number should be equal or larger than 1
number_larger_1: number should be equal or larger than 1

form:
optional: (optional)
Expand Down Expand Up @@ -1906,7 +1917,7 @@ ui:
post_deleted: This post has been deleted.
post_pin: This post has been pinned.
post_unpin: This post has been unpinned.
post_hide_list: This post has been hidden from list.
post_hide_list: This post has been hidden from list.
post_show_list: This post has been shown to list.
post_reopen: This post has been reopened.
post_list: This post has been listed.
Expand Down
14 changes: 12 additions & 2 deletions i18n/zh_CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ backend:
other: 旧密码验证失败。
new_password_same_as_previous_setting:
other: 新密码和旧密码相同。
meta:
object_not_found:
other: Meta 对象未找到
question:
already_deleted:
other: 该帖子已被删除。
Expand Down Expand Up @@ -499,6 +502,9 @@ backend:
other: 举报的帖子
suggested_post_edit:
other: 建议的编辑
reaction:
tooltip:
other: "{{ .Names }} 以及另外 {{ .Count }} 个..."
# The following fields are used for interface presentation(Front-end)
ui:
how_to_format:
Expand Down Expand Up @@ -747,6 +753,10 @@ ui:
day: 天
hours: 小时
days: 日
reaction:
heart: 爱心
smile: 笑脸
frown: 难过
comment:
btn_add_comment: 添加评论
reply_to: 回复
Expand Down Expand Up @@ -1792,7 +1802,7 @@ ui:
proposed: 提案
question_edit: 问题编辑
answer_edit: 回答编辑
tag_edit: '标签管理: 编辑标签'
tag_edit: "标签管理: 编辑标签"
edit_summary: 编辑备注
edit_question: 编辑问题
edit_answer: 编辑回答
Expand Down Expand Up @@ -1821,7 +1831,7 @@ ui:
upvote: 点赞
accept: 采纳
cancelled: 已取消
commented: '评论:'
commented: "评论:"
rollback: 回滚
edited: 最后编辑于
answered: 回答于
Expand Down
24 changes: 24 additions & 0 deletions internal/base/constant/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 constant

const (
ReactionTooltipLabel = "reaction.tooltip"
)
1 change: 1 addition & 0 deletions internal/base/reason/reason.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const (
AddBulkUsersFormatError = "error.user.add_bulk_users_format_error"
AddBulkUsersAmountError = "error.user.add_bulk_users_amount_error"
InvalidURLError = "error.common.invalid_url"
MetaObjectNotFound = "error.meta.object_not_found"
)

// user external login reasons
Expand Down
1 change: 1 addition & 0 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ var ProviderSetController = wire.NewSet(
NewUserPluginController,
NewReviewController,
NewCaptchaController,
NewMetaController,
)
82 changes: 82 additions & 0 deletions internal/controller/meta_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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 controller

import (
"github.com/apache/incubator-answer/internal/base/handler"
"github.com/apache/incubator-answer/internal/base/middleware"
"github.com/apache/incubator-answer/internal/schema"
"github.com/apache/incubator-answer/internal/service/meta"
"github.com/gin-gonic/gin"
)

type MetaController struct {
metaService *meta.MetaService
}

func NewMetaController(
metaService *meta.MetaService,
) *MetaController {
return &MetaController{
metaService: metaService,
}
}

// AddOrUpdateReaction add or update reaction
// @Summary add or update reaction
// @Description update reaction. if not exist, add one
// @Tags Meta
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param data body schema.UpdateReactionReq true "reaction"
// @Success 200 {object} handler.RespBody
// @Router /answer/api/v1/meta/reaction [put]
func (mc *MetaController) AddOrUpdateReaction(ctx *gin.Context) {
req := &schema.UpdateReactionReq{}
if handler.BindAndCheck(ctx, req) {
return
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)

resp, err := mc.metaService.AddOrUpdateReaction(ctx, req)
handler.HandleResponse(ctx, err, resp)
}

// GetReaction get reaction
// @Summary get reaction
// @Description get reaction for an object
// @Tags Meta
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param object_id query string true "object_id"
// @Success 200 {object} handler.RespBody{data=schema.ReactionResp}
// @Router /answer/api/v1/meta/reaction [get]
func (mc *MetaController) GetReaction(ctx *gin.Context) {
req := &schema.GetReactionReq{}
if handler.BindAndCheck(ctx, req) {
return
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)

resp, err := mc.metaService.GetReactionByObjectId(ctx, req)
handler.HandleResponse(ctx, err, resp)
}
1 change: 1 addition & 0 deletions internal/entity/meta_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
QuestionCloseReasonKey = "question.close.reason"
AnswerEditSummaryKey = "answer.edit.summary"
TagEditSummaryKey = "tag.edit.summary"
ObjectReactSummaryKey = "object.react.summary"
)

// Meta meta
Expand Down
34 changes: 32 additions & 2 deletions internal/repo/meta/meta_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (
"github.com/apache/incubator-answer/internal/base/data"
"github.com/apache/incubator-answer/internal/base/reason"
"github.com/apache/incubator-answer/internal/entity"
"github.com/apache/incubator-answer/internal/service/meta"
"github.com/apache/incubator-answer/internal/service/meta_common"
"github.com/segmentfault/pacman/errors"
"xorm.io/builder"
"xorm.io/xorm"
)

// metaRepo meta repository
Expand All @@ -36,7 +37,7 @@ type metaRepo struct {
}

// NewMetaRepo new repository
func NewMetaRepo(data *data.Data) meta.MetaRepo {
func NewMetaRepo(data *data.Data) metacommon.MetaRepo {
return &metaRepo{
data: data,
}
Expand Down Expand Up @@ -69,6 +70,35 @@ func (mr *metaRepo) UpdateMeta(ctx context.Context, meta *entity.Meta) (err erro
return
}

// AddOrUpdateMetaByObjectIdAndKey if exist record with same objectID and key, update it. Or create a new one
func (mr *metaRepo) AddOrUpdateMetaByObjectIdAndKey(ctx context.Context, objectId, key string, f func(*entity.Meta, bool) (*entity.Meta, error)) error {
_, err := mr.data.DB.Transaction(func(session *xorm.Session) (interface{}, error) {
session = session.Context(ctx)

// 1. acquire meta entity with target object id and key
metaEntity := &entity.Meta{}
exist, err := session.Where(builder.Eq{"object_id": objectId}.And(builder.Eq{"`key`": key})).ForUpdate().Get(metaEntity)
if err != nil {
return nil, err
}

meta, err := f(metaEntity, exist)
if err != nil {
return nil, err
}

// return entity.Meta
if exist {
_, err = session.ID(metaEntity.ID).Update(meta)
} else {
_, err = session.Insert(meta)
}

return nil, err
})
return err
}

// GetMetaByObjectIdAndKey get meta one
func (mr *metaRepo) GetMetaByObjectIdAndKey(ctx context.Context, objectID, key string) (
meta *entity.Meta, exist bool, err error) {
Expand Down