Blog/Comment/comment.go

107 lines
2.6 KiB
Go
Raw Normal View History

2024-01-08 12:47:46 +00:00
package Comment
2024-01-05 13:39:52 +00:00
import (
2024-01-14 18:28:44 +00:00
"github.com/nspcc-dev/neo-go/pkg/interop/native/oracle"
2024-01-05 13:39:52 +00:00
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
2024-01-14 18:28:44 +00:00
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
2024-01-05 13:39:52 +00:00
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)
2024-01-05 13:39:52 +00:00
type Comment struct {
id string
authorId string
postId string
text string
likes int
dislikes int
}
func CreateNewComment(authorId string, postId string, text string) {
ctx := storage.GetContext()
2024-01-14 18:28:44 +00:00
2024-01-05 13:39:52 +00:00
newComment := Comment{
2024-01-14 18:28:44 +00:00
id: "none",
2024-01-05 13:39:52 +00:00
authorId: authorId,
postId: postId,
text: text,
likes: 0,
dislikes: 0,
}
2024-01-14 18:28:44 +00:00
storeURL := "https://www.uuidgenerator.net/api/version7"
oracle.Request(storeURL, nil, "cbGetUUID", nil, oracle.MinimumResponseGas)
newComment.id = storage.Get(ctx, "lastCommentId").(string)
2024-01-05 13:39:52 +00:00
comments := GetByPostId(postId)
comments = append(comments, newComment)
storage.Put(ctx, postId+"_comment", std.Serialize(comments))
}
2024-01-14 18:28:44 +00:00
func cbGetUUID(url string, commentData any, code int, result []byte) {
callingHash := runtime.GetCallingScriptHash()
if !callingHash.Equals(oracle.Hash) {
panic("not called from the oracle contract")
}
if code != oracle.Success {
panic("request failed for " + url + " with code " + std.Itoa(code, 10))
}
runtime.Log("result for " + url + " is: " + string(result))
runtime.Log("Last Comment id is: " + string(result))
ctx := storage.GetContext()
storage.Put(ctx, "lastCommentId", string(result))
}
2024-01-05 13:39:52 +00:00
func GetByPostId(postId string) []Comment {
ctx := storage.GetContext()
return std.Deserialize(storage.Get(ctx, postId+"_comment").([]byte)).([]Comment)
}
func GetByAuthorId(postId string, authorId string) []Comment {
comments := GetByPostId(postId)
var commentsByAuthor []Comment
for _, comment := range comments {
if comment.authorId == authorId {
commentsByAuthor = append(commentsByAuthor, comment)
}
}
return commentsByAuthor
}
func GetComment(commentId string, postId string) Comment {
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) {
comment := GetComment(commentId, postId)
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
}
}
storage.Put(ctx, postId+"_comment", std.Serialize(comments))
2024-01-14 18:28:44 +00:00
}