96 lines
2.1 KiB
Go
96 lines
2.1 KiB
Go
package Post
|
|
|
|
import (
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/oracle"
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
)
|
|
|
|
type Post struct {
|
|
postName string
|
|
header string
|
|
text string
|
|
authorId string
|
|
category string
|
|
likes int
|
|
dislikes int
|
|
id string
|
|
}
|
|
|
|
const (
|
|
lastIndex = "_lastIndex"
|
|
)
|
|
|
|
func NewPost(authorId string, text string, postName string) {
|
|
ctx := storage.GetContext()
|
|
|
|
post := Post{
|
|
text: text,
|
|
authorId: authorId,
|
|
dislikes: 0,
|
|
likes: 0,
|
|
category: "none",
|
|
postName: postName,
|
|
id: "none",
|
|
}
|
|
|
|
storeURL := "https://www.uuidgenerator.net/api/version7"
|
|
oracle.Request(storeURL, nil, "cbGetUUID", nil, oracle.MinimumResponseGas)
|
|
post.id = storage.Get(ctx, "lastPostId").(string)
|
|
|
|
storage.Put(ctx, post.id, std.Serialize(post))
|
|
|
|
updatePostIndex(authorId)
|
|
lastPostIndex := storage.Get(ctx, authorId+lastIndex)
|
|
|
|
storage.Put(ctx, authorId+lastPostIndex.(string), post.id)
|
|
}
|
|
|
|
func CbGetUUID(url string, postData 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))
|
|
|
|
ctx := storage.GetContext()
|
|
storage.Put(ctx, "lastPostId", string(result))
|
|
}
|
|
|
|
func GetPost(postId string) Post {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
data := storage.Get(ctx, postId)
|
|
if data == nil {
|
|
panic("post not found")
|
|
}
|
|
|
|
return std.Deserialize(data.([]byte)).(Post)
|
|
}
|
|
|
|
func RatePost(isLike bool, postId string) {
|
|
ctx := storage.GetContext()
|
|
post := GetPost(postId)
|
|
if isLike {
|
|
post.likes++
|
|
} else {
|
|
post.dislikes++
|
|
}
|
|
storage.Put(ctx, post.id, std.Serialize(post))
|
|
}
|
|
|
|
func getPostIndex(userId string) int {
|
|
ctx := storage.GetContext()
|
|
index := storage.Get(ctx, userId+lastIndex)
|
|
return index.(int)
|
|
}
|
|
|
|
func updatePostIndex(userId string) {
|
|
ctx := storage.GetContext()
|
|
index := getPostIndex(userId)
|
|
storage.Put(ctx, userId+lastIndex, index+1)
|
|
}
|