gpt4 book ai didi

gun - gundb中的私有(private)写入和公共(public)读取

转载 作者:行者123 更新时间:2023-12-04 12:25:34 24 4
gpt4 key购买 nike

我想创建一个 microblog每个人都可以阅读所有帖子,但只有所有者可以删除或编辑帖子。在 gundb无海人人可以编辑或删除帖子,在sea( gun.user())我必须共享公钥,在海上如何获取所有用户的帖子并在时间轴中显示帖子?

我怎么能用 gundb 创建这个?

最佳答案

我一直在 gun 中寻找有关数据隐私问题的答案,这是我的答案:

  • 变量的导入和定义
  • <script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/gun/sea.js"></script>

    var gun = Gun()
  • 创建微博第一作者
  • gun.user().create('firstMicroblogAuthor', 'somePassword')
    gun.user().auth('firstMicroblogAuthor', 'somePassword')

  • 创建帖子并获取作者
  • var post = {
    title: 'First post',
    text: 'Hello world!'
    }

    var author = gun.get('~@firstMicroblogAuthor') // There should be the same `username` in Step 2
  • 保存帖子
  • gun
    .user()
    .get('posts')
    .set(post) // At this step, we saved the post in a user schedule, which by default is only writable by the user
    .once(function() {
    this.get('author').put(author) // In this step, we link our post with the author (with our user)
    gun.get('posts').set(this) // At this step, we save the post with the author installed in the main graph
    })
  • 检查我们的帖子是否受到其他用户编辑的保护:
  • gun.user().leave()

    gun.user().create('secondMicroblogAuthor', 'somePassword')
    gun.user().auth('secondMicroblogAuthor', 'somePassword')

    gun
    .get('posts') // Read posts from public graph
    .once(function() {
    this.get('text').put('Goodbye world!') // In this case, we will get an error, because this post was protected
    })

    关于gun - gundb中的私有(private)写入和公共(public)读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56044288/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com