2024-01-02 16:31:11 +00:00
|
|
|
package Post
|
2023-12-20 15:18:07 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-14 18:28:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/oracle"
|
2023-12-20 15:18:07 +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"
|
2023-12-20 15:18:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Post struct {
|
2024-01-02 16:31:11 +00:00
|
|
|
postName string
|
2023-12-20 15:18:07 +00:00
|
|
|
header string
|
|
|
|
text string
|
|
|
|
authorId string
|
|
|
|
category string
|
|
|
|
likes int
|
|
|
|
dislikes int
|
2024-01-02 16:31:11 +00:00
|
|
|
id string
|
2023-12-20 15:18:07 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 16:31:11 +00:00
|
|
|
const (
|
2024-01-15 12:26:08 +00:00
|
|
|
lastIndex = "_lastIndex"
|
|
|
|
all_posts_key = "all_posts"
|
2024-01-02 16:31:11 +00:00
|
|
|
)
|
2023-12-20 15:18:07 +00:00
|
|
|
|
2024-01-02 16:31:11 +00:00
|
|
|
func NewPost(authorId string, text string, postName string) {
|
2023-12-20 15:18:07 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
post := Post{
|
|
|
|
text: text,
|
|
|
|
authorId: authorId,
|
|
|
|
dislikes: 0,
|
2023-12-22 13:09:35 +00:00
|
|
|
likes: 0,
|
2023-12-20 15:18:07 +00:00
|
|
|
category: "none",
|
2024-01-02 16:31:11 +00:00
|
|
|
postName: postName,
|
2024-01-14 18:28:44 +00:00
|
|
|
id: "none",
|
2023-12-20 15:18:07 +00:00
|
|
|
}
|
2024-01-14 18:28:44 +00:00
|
|
|
|
|
|
|
storeURL := "https://www.uuidgenerator.net/api/version7"
|
|
|
|
oracle.Request(storeURL, nil, "cbGetUUID", nil, oracle.MinimumResponseGas)
|
|
|
|
post.id = storage.Get(ctx, "lastPostId").(string)
|
|
|
|
|
2023-12-20 15:18:07 +00:00
|
|
|
storage.Put(ctx, post.id, std.Serialize(post))
|
2024-01-14 18:28:44 +00:00
|
|
|
|
2024-01-02 16:31:11 +00:00
|
|
|
updatePostIndex(authorId)
|
2024-01-15 12:26:08 +00:00
|
|
|
lastPostIndex := storage.Get(ctx, authorId+lastIndex).(string)
|
2024-01-14 18:28:44 +00:00
|
|
|
|
2024-01-15 12:26:08 +00:00
|
|
|
storage.Put(ctx, authorId+lastPostIndex, post.id)
|
2023-12-20 15:18:07 +00:00
|
|
|
}
|
2024-01-14 18:28:44 +00:00
|
|
|
|
|
|
|
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 {
|
2024-01-15 20:09:10 +00:00
|
|
|
panic("request failed for " + url + " with code " + std.Itoa10(code))
|
2024-01-14 18:28:44 +00:00
|
|
|
}
|
|
|
|
runtime.Log("result for " + url + " is: " + string(result))
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
storage.Put(ctx, "lastPostId", string(result))
|
|
|
|
}
|
|
|
|
|
2024-01-15 12:26:08 +00:00
|
|
|
func appendPostToAllPosts(post Post) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
all_posts := GetAllPosts()
|
|
|
|
posts := append(all_posts, post)
|
|
|
|
storage.Put(ctx, all_posts_key, posts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAllPosts() []Post {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
posts := std.Deserialize(storage.Get(ctx, all_posts_key).([]byte)).([]Post)
|
|
|
|
return posts
|
|
|
|
}
|
|
|
|
|
2023-12-20 15:18:07 +00:00
|
|
|
func GetPost(postId string) Post {
|
|
|
|
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
data := storage.Get(ctx, postId)
|
|
|
|
if data == nil {
|
2024-01-14 18:28:44 +00:00
|
|
|
panic("post not found")
|
2023-12-20 15:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std.Deserialize(data.([]byte)).(Post)
|
|
|
|
}
|
|
|
|
|
2024-01-15 12:26:08 +00:00
|
|
|
func GetAllPostsByUser(userId string) []Post {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
var postsByUser []Post
|
|
|
|
i := 0
|
|
|
|
n := getPostIndex(userId)
|
|
|
|
for i < n {
|
2024-01-15 20:09:10 +00:00
|
|
|
post := storage.Get(ctx, userId+std.Itoa10(i))
|
2024-01-15 12:26:08 +00:00
|
|
|
i++
|
|
|
|
postsByUser = append(postsByUser, std.Deserialize(post.([]byte)).(Post))
|
|
|
|
}
|
|
|
|
return postsByUser
|
|
|
|
}
|
|
|
|
|
2023-12-20 15:18:07 +00:00
|
|
|
func RatePost(isLike bool, postId string) {
|
2024-01-02 16:31:11 +00:00
|
|
|
ctx := storage.GetContext()
|
2023-12-20 15:18:07 +00:00
|
|
|
post := GetPost(postId)
|
|
|
|
if isLike {
|
|
|
|
post.likes++
|
|
|
|
} else {
|
|
|
|
post.dislikes++
|
|
|
|
}
|
|
|
|
storage.Put(ctx, post.id, std.Serialize(post))
|
|
|
|
}
|
2024-01-02 16:31:11 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|