2024-01-02 16:31:11 +00:00
|
|
|
package Post
|
2023-12-20 15:18:07 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-17 15:54:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2024-01-16 11:53:55 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2024-01-17 15:54:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/gas"
|
2023-12-20 15:18:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
2024-01-16 19:19:30 +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
|
|
|
text string
|
2024-01-16 11:53:55 +00:00
|
|
|
login string
|
2023-12-20 15:18:07 +00:00
|
|
|
likes 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-16 14:30:21 +00:00
|
|
|
lastIndex = "user_post_index"
|
|
|
|
id = "current_post_id"
|
|
|
|
comment_prefix = "comment_"
|
2024-01-17 18:52:35 +00:00
|
|
|
post_hash = "post_hash"
|
2024-01-17 15:54:48 +00:00
|
|
|
gas_decimals = 1_0000_0000
|
2024-01-02 16:31:11 +00:00
|
|
|
)
|
2023-12-20 15:18:07 +00:00
|
|
|
|
2024-01-17 18:52:35 +00:00
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
|
|
|
if isUpdate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
args := data.(struct {
|
|
|
|
postHash interop.Hash160
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(args.postHash) != interop.Hash160Len {
|
|
|
|
panic("invalid hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
storage.Put(ctx, post_hash, args.postHash)
|
|
|
|
}
|
|
|
|
|
2024-01-16 11:53:55 +00:00
|
|
|
func NewPost(login string, text string, postName string) {
|
2023-12-20 15:18:07 +00:00
|
|
|
ctx := storage.GetContext()
|
2024-01-16 11:53:55 +00:00
|
|
|
updatePostId()
|
|
|
|
id := storage.Get(ctx, id).(int)
|
|
|
|
post_id := "post_" + std.Itoa10(id)
|
2023-12-20 15:18:07 +00:00
|
|
|
|
|
|
|
post := Post{
|
|
|
|
text: text,
|
2024-01-16 11:53:55 +00:00
|
|
|
login: login,
|
2023-12-22 13:09:35 +00:00
|
|
|
likes: 0,
|
2024-01-02 16:31:11 +00:00
|
|
|
postName: postName,
|
2024-01-16 11:53:55 +00:00
|
|
|
id: post_id,
|
2023-12-20 15:18:07 +00:00
|
|
|
}
|
2024-01-14 18:28:44 +00:00
|
|
|
|
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-16 11:53:55 +00:00
|
|
|
updatePostIndex(login)
|
2024-01-16 19:19:30 +00:00
|
|
|
lastPostIndex := storage.Get(ctx, login+lastIndex).(int)
|
|
|
|
storage.Put(ctx, login+"_p_"+std.Itoa10(lastPostIndex), post.id)
|
2024-01-17 09:26:20 +00:00
|
|
|
runtime.Log("Post created. Post id: " + post.id + "; User post id" + login + "_p_" + std.Itoa10(lastPostIndex))
|
2024-01-15 12:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetAllPosts() []Post {
|
2024-01-16 11:53:55 +00:00
|
|
|
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)
|
2024-01-16 19:19:30 +00:00
|
|
|
runtime.Log(string(std.Serialize(post)))
|
2024-01-16 11:53:55 +00:00
|
|
|
posts = append(posts, post)
|
|
|
|
}
|
2024-01-16 19:19:30 +00:00
|
|
|
|
2024-01-15 12:26:08 +00:00
|
|
|
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-16 11:53:55 +00:00
|
|
|
func GetAllPostsByUser(login string) []Post {
|
2024-01-15 12:26:08 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2024-01-16 19:19:30 +00:00
|
|
|
var postsByUser []Post = make([]Post, 0)
|
|
|
|
i := 1
|
2024-01-16 11:53:55 +00:00
|
|
|
n := getPostIndex(login)
|
2024-01-16 19:19:30 +00:00
|
|
|
for i < n+1 {
|
2024-01-17 09:31:41 +00:00
|
|
|
post_key := storage.Get(ctx, login+"_p_"+std.Itoa10(i))
|
|
|
|
if post_key == nil {
|
|
|
|
panic("post key not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
post_key = post_key.(string)
|
|
|
|
|
2024-01-16 19:19:30 +00:00
|
|
|
post := storage.Get(ctx, post_key)
|
2024-01-17 09:31:41 +00:00
|
|
|
if post == nil {
|
|
|
|
panic("post not found")
|
|
|
|
}
|
|
|
|
|
2024-01-15 12:26:08 +00:00
|
|
|
i++
|
|
|
|
postsByUser = append(postsByUser, std.Deserialize(post.([]byte)).(Post))
|
|
|
|
}
|
|
|
|
return postsByUser
|
|
|
|
}
|
|
|
|
|
2024-01-17 15:54:48 +00:00
|
|
|
func Rate(postId string, walletHashFrom interop.Hash160) {
|
2024-01-02 16:31:11 +00:00
|
|
|
ctx := storage.GetContext()
|
2023-12-20 15:18:07 +00:00
|
|
|
post := GetPost(postId)
|
2024-01-17 15:54:48 +00:00
|
|
|
success := gas.Transfer(walletHashFrom, storage.Get(ctx, post.login+"_hash").(interop.Hash160), gas_decimals, nil)
|
|
|
|
if !success {
|
|
|
|
panic("gas transfer failed")
|
2023-12-20 15:18:07 +00:00
|
|
|
} else {
|
2024-01-17 15:54:48 +00:00
|
|
|
post.likes += 1
|
|
|
|
storage.Put(ctx, post.id, std.Serialize(post))
|
2023-12-20 15:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-02 16:31:11 +00:00
|
|
|
|
|
|
|
func getPostIndex(userId string) int {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
index := storage.Get(ctx, userId+lastIndex)
|
2024-01-16 12:11:13 +00:00
|
|
|
if index == nil {
|
|
|
|
index = 0
|
|
|
|
}
|
2024-01-02 16:31:11 +00:00
|
|
|
return index.(int)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updatePostIndex(userId string) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
index := getPostIndex(userId)
|
|
|
|
storage.Put(ctx, userId+lastIndex, index+1)
|
|
|
|
}
|
2024-01-16 11:53:55 +00:00
|
|
|
|
|
|
|
func getPostId() int {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
current_id := storage.Get(ctx, id)
|
2024-01-16 12:11:13 +00:00
|
|
|
if current_id == nil {
|
|
|
|
current_id = 0
|
|
|
|
}
|
2024-01-16 11:53:55 +00:00
|
|
|
return current_id.(int)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updatePostId() {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
cur_id := getPostId()
|
|
|
|
storage.Put(ctx, id, cur_id+1)
|
|
|
|
}
|