154 lines
3.3 KiB
Go
Executable file
154 lines
3.3 KiB
Go
Executable file
package Post
|
|
|
|
import (
|
|
<<<<<<< HEAD
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/oracle"
|
|
=======
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
|
>>>>>>> post-brench
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
)
|
|
|
|
type Post struct {
|
|
postName string
|
|
header string
|
|
text string
|
|
login string
|
|
likes int
|
|
dislikes int
|
|
id string
|
|
}
|
|
|
|
const (
|
|
lastIndex = "user_post_index"
|
|
id = "current_post_id"
|
|
)
|
|
|
|
func NewPost(login string, text string, postName string) {
|
|
ctx := storage.GetContext()
|
|
updatePostId()
|
|
id := storage.Get(ctx, id).(int)
|
|
post_id := "post_" + std.Itoa10(id)
|
|
|
|
post := Post{
|
|
text: text,
|
|
login: login,
|
|
dislikes: 0,
|
|
likes: 0,
|
|
postName: postName,
|
|
id: post_id,
|
|
}
|
|
|
|
storage.Put(ctx, post.id, std.Serialize(post))
|
|
|
|
updatePostIndex(login)
|
|
lastPostIndex := storage.Get(ctx, login+lastIndex).(string)
|
|
|
|
<<<<<<< HEAD
|
|
storage.Put(ctx, authorId+lastPostIndex, 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.Itoa10(code))
|
|
}
|
|
runtime.Log("result for " + url + " is: " + string(result))
|
|
|
|
ctx := storage.GetContext()
|
|
storage.Put(ctx, "lastPostId", string(result))
|
|
}
|
|
|
|
func appendPostToAllPosts(post Post) {
|
|
ctx := storage.GetContext()
|
|
all_posts := GetAllPosts()
|
|
posts := append(all_posts, post)
|
|
storage.Put(ctx, all_posts_key, posts)
|
|
=======
|
|
storage.Put(ctx, login+"_p_"+lastPostIndex, post.id)
|
|
>>>>>>> post-brench
|
|
}
|
|
|
|
func GetAllPosts() []Post {
|
|
posts := make([]Post, 0)
|
|
ctx := storage.GetReadOnlyContext()
|
|
it := storage.Find(ctx, "post", storage.ValuesOnly|storage.DeserializeValues)
|
|
for iterator.Next(it) {
|
|
post := iterator.Value(it).(Post)
|
|
posts = append(posts, post)
|
|
}
|
|
return posts
|
|
}
|
|
|
|
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 GetAllPostsByUser(login string) []Post {
|
|
ctx := storage.GetReadOnlyContext()
|
|
var postsByUser []Post
|
|
i := 0
|
|
n := getPostIndex(login)
|
|
for i < n {
|
|
<<<<<<< HEAD
|
|
post := storage.Get(ctx, userId+std.Itoa10(i))
|
|
=======
|
|
post := storage.Get(ctx, login+"_p_"+std.Itoa10(i))
|
|
>>>>>>> post-brench
|
|
i++
|
|
postsByUser = append(postsByUser, std.Deserialize(post.([]byte)).(Post))
|
|
}
|
|
return postsByUser
|
|
}
|
|
|
|
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)
|
|
if index == nil {
|
|
index = 0
|
|
}
|
|
return index.(int)
|
|
}
|
|
|
|
func updatePostIndex(userId string) {
|
|
ctx := storage.GetContext()
|
|
index := getPostIndex(userId)
|
|
storage.Put(ctx, userId+lastIndex, index+1)
|
|
}
|
|
|
|
func getPostId() int {
|
|
ctx := storage.GetContext()
|
|
current_id := storage.Get(ctx, id)
|
|
if current_id == nil {
|
|
current_id = 0
|
|
}
|
|
return current_id.(int)
|
|
}
|
|
|
|
func updatePostId() {
|
|
ctx := storage.GetContext()
|
|
cur_id := getPostId()
|
|
storage.Put(ctx, id, cur_id+1)
|
|
}
|