2024-01-08 12:47:46 +00:00
|
|
|
package Comment
|
2024-01-04 23:14:02 +00:00
|
|
|
|
2024-01-05 13:39:52 +00:00
|
|
|
import (
|
2024-01-16 13:00:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2024-01-05 13:39:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
)
|
2024-01-04 23:14:02 +00:00
|
|
|
|
2024-01-05 13:39:52 +00:00
|
|
|
type Comment struct {
|
2024-01-16 13:00:16 +00:00
|
|
|
id string
|
|
|
|
userLogin string
|
|
|
|
postId string
|
|
|
|
text string
|
|
|
|
likes int
|
|
|
|
dislikes int
|
2024-01-05 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 13:00:16 +00:00
|
|
|
const (
|
|
|
|
comment_prefix = "comment_"
|
|
|
|
post_prefix = "post_"
|
|
|
|
user_comment_count_prefix = "comment_count_"
|
|
|
|
)
|
2024-01-04 23:14:02 +00:00
|
|
|
|
2024-01-16 13:00:16 +00:00
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
storage.Put(ctx, "index_comment", 0)
|
|
|
|
}
|
2024-01-14 18:28:44 +00:00
|
|
|
|
2024-01-16 13:00:16 +00:00
|
|
|
func CreateNewComment(userLogin string, postId string, text string) {
|
|
|
|
ctx := storage.GetContext()
|
2024-01-14 18:28:44 +00:00
|
|
|
|
2024-01-16 13:00:16 +00:00
|
|
|
id := storage.Get(ctx, "index_comment").(int)
|
|
|
|
id++
|
|
|
|
storage.Put(ctx, "index_comment", id)
|
2024-01-04 23:14:02 +00:00
|
|
|
|
2024-01-16 13:00:16 +00:00
|
|
|
if storage.Get(ctx, user_comment_count_prefix+userLogin) == nil {
|
|
|
|
storage.Put(ctx, user_comment_count_prefix+userLogin, 0)
|
2024-01-14 18:28:44 +00:00
|
|
|
}
|
2024-01-16 13:00:16 +00:00
|
|
|
|
|
|
|
commentId := comment_prefix + std.Itoa10(id)
|
|
|
|
newComment := Comment{
|
|
|
|
id: commentId,
|
|
|
|
userLogin: userLogin,
|
|
|
|
postId: postId,
|
|
|
|
text: text,
|
|
|
|
likes: 0,
|
|
|
|
dislikes: 0,
|
2024-01-14 18:28:44 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 13:00:16 +00:00
|
|
|
comments := GetByPostId(postId)
|
|
|
|
comments = append(comments, newComment)
|
|
|
|
storage.Put(ctx, comment_prefix+postId, std.Serialize(comments)) // добавление комментов в общий список комментов поста
|
|
|
|
storage.Put(ctx, commentId, std.Serialize(newComment)) // добавление коммента отдельно по ключу
|
2024-01-14 18:28:44 +00:00
|
|
|
|
2024-01-16 13:00:16 +00:00
|
|
|
commentCount := storage.Get(ctx, user_comment_count_prefix+userLogin).(int)
|
|
|
|
commentCount++
|
|
|
|
storage.Put(ctx, user_comment_count_prefix+userLogin, commentCount) // обновление счетчика коммента юзера
|
|
|
|
storage.Put(ctx, userLogin+"_c_"+std.Itoa10(commentCount), commentId) // хранение айдишника коммента по юзеру
|
2024-01-14 18:28:44 +00:00
|
|
|
}
|
|
|
|
|
2024-01-05 13:39:52 +00:00
|
|
|
func GetByPostId(postId string) []Comment {
|
|
|
|
ctx := storage.GetContext()
|
2024-01-16 13:00:16 +00:00
|
|
|
return std.Deserialize(storage.Get(ctx, comment_prefix+postId).([]byte)).([]Comment)
|
2024-01-05 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 13:00:16 +00:00
|
|
|
func GetByLoginInPost(postId string, login string) []Comment {
|
2024-01-05 13:39:52 +00:00
|
|
|
comments := GetByPostId(postId)
|
|
|
|
var commentsByAuthor []Comment
|
|
|
|
for _, comment := range comments {
|
2024-01-16 13:00:16 +00:00
|
|
|
if comment.userLogin == login {
|
2024-01-05 13:39:52 +00:00
|
|
|
commentsByAuthor = append(commentsByAuthor, comment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return commentsByAuthor
|
|
|
|
}
|
|
|
|
|
2024-01-16 13:00:16 +00:00
|
|
|
func GetByLogin(login string) []Comment {
|
|
|
|
var comments []Comment
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
it := storage.Find(ctx, login+"_c_", storage.ValuesOnly|storage.DeserializeValues)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
commentId := iterator.Value(it).(string)
|
|
|
|
comments = append(comments, GetComment(commentId))
|
|
|
|
}
|
|
|
|
|
|
|
|
return comments
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetComment(commentId string) Comment {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
return storage.Get(ctx, commentId).(Comment)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCommentInPost(commentId string, postId string) Comment {
|
2024-01-05 13:39:52 +00:00
|
|
|
comments := GetByPostId(postId)
|
|
|
|
for _, comment := range comments {
|
|
|
|
if comment.id == commentId {
|
|
|
|
return comment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 15:07:07 +00:00
|
|
|
panic("Коммента с таким айдишником нету")
|
2024-01-05 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RateComment(isLike bool, postId string, commentId string) {
|
2024-01-16 13:00:16 +00:00
|
|
|
comment := GetComment(commentId)
|
2024-01-05 13:39:52 +00:00
|
|
|
if isLike {
|
|
|
|
comment.likes++
|
|
|
|
} else {
|
|
|
|
comment.dislikes++
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateComment(comment, postId)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateComment(comment Comment, postId string) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
comments := GetByPostId(postId)
|
|
|
|
for i := 0; i < len(comments); i++ {
|
|
|
|
if comments[i].id == comment.id {
|
|
|
|
comments[i] = comment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 13:00:16 +00:00
|
|
|
storage.Put(ctx, comment_prefix+postId, std.Serialize(comments))
|
|
|
|
storage.Put(ctx, comment.id, std.Serialize(comment))
|
2024-01-14 18:28:44 +00:00
|
|
|
}
|