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

perf(notify): shorten the length of notify key #466

Merged
merged 1 commit into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/core/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Gen(genType string, specificPath string, overwrite bool) {
logrus.Info("File Generated: " + absPath)
}

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*")
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*1234567890")

func RandStringRunes(n int) string {
b := make([]rune, n)
Expand Down
10 changes: 7 additions & 3 deletions internal/entity/notify.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package entity

import (
"fmt"
"math/rand"
"sync"
"time"

"github.com/ArtalkJS/Artalk/internal/utils"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -36,5 +35,10 @@ func (n *Notify) SetComment(comment Comment) {

// 操作时的验证密钥(判断是否本人操作)
func (n *Notify) GenerateKey() {
n.Key = utils.GetMD5Hash(fmt.Sprintf("%v %v %v", n.UserID, n.CommentID, time.Now().Unix()))
letterRunes := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
b := make([]rune, 5)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
n.Key = string(b)
}
4 changes: 2 additions & 2 deletions internal/query/query_find.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ func FindNotify(userID uint, commentID uint) entity.Notify {
return notify
}

func FindNotifyByKey(key string) entity.Notify {
func FindNotifyForComment(commentID uint, key string) entity.Notify {
var notify entity.Notify
DB().Where(entity.Notify{Key: key}).First(&notify)
DB().Where(entity.Notify{CommentID: commentID, Key: key}).First(&notify)
return notify
}

Expand Down
6 changes: 4 additions & 2 deletions server/handler/mark_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

type ParamsMarkRead struct {
CommentID uint `form:"comment_id"`
NotifyKey string `form:"notify_key"`

Name string `form:"name"`
Expand All @@ -22,7 +23,8 @@ type ParamsMarkRead struct {
// @Summary Notify Mark Read
// @Description Mark specific notification as read for user
// @Tags Notify
// @Param notify_key formData string true "the notify key you want to mark as read"
// @Param comment_id formData int true "the comment id of the notify you want to mark as read"
// @Param notify_key formData string true "the key of the notify"
// @Param name formData string false "the username"
// @Param email formData string false "the user email"
// @Param all_read formData bool false "the option if mark all user's notify as read"
Expand Down Expand Up @@ -55,7 +57,7 @@ func MarkRead(router fiber.Router) {
}

// find notify
notify := query.FindNotifyByKey(p.NotifyKey)
notify := query.FindNotifyForComment(p.CommentID, p.NotifyKey)
if notify.IsEmpty() {
return common.RespError(c, i18n.T("{{name}} not found", Map{"name": i18n.T("Notify")}))
}
Expand Down
4 changes: 3 additions & 1 deletion ui/packages/artalk/src/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ export default class UserApi extends ApiBase {
}

/** 已读标记 */
public markRead(notifyKey: string, readAll = false) {
public markRead(commentID: number, notifyKey: string, readAll = false) {
const params: any = {
comment_id: commentID,
notify_key: notifyKey,
}

if (readAll) {
delete params.comment_id
delete params.notify_key
params.read_all = true
params.name = User.data.nick
Expand Down
2 changes: 1 addition & 1 deletion ui/packages/artalk/src/list/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default class List extends ListLite {
// 已阅 API
const notifyKey = Utils.getQueryParam('atk_notify_key')
if (notifyKey) {
this.ctx.getApi().user.markRead(notifyKey)
this.ctx.getApi().user.markRead(commentId, notifyKey)
.then(() => {
this.unread = this.unread.filter(o => o.comment_id !== commentId)
this.updateUnread(this.unread)
Expand Down