gundb 中的私有写入和公共读取

you*_*osk 8 gun gundb

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

我怎样才能用gundb创建这个?

Nar*_*nik 6

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

  1. 变量的导入和定义
<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()
Run Code Online (Sandbox Code Playgroud)
  1. 创建微博第一作者
gun.user().create('firstMicroblogAuthor', 'somePassword')
gun.user().auth('firstMicroblogAuthor', 'somePassword')

Run Code Online (Sandbox Code Playgroud)
  1. 创建帖子并获取作者
var post = {
  title: 'First post',
  text: 'Hello world!'
}

var author = gun.get('~@firstMicroblogAuthor') // There should be the same `username` in Step 2
Run Code Online (Sandbox Code Playgroud)
  1. 保存帖子
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
  })
Run Code Online (Sandbox Code Playgroud)
  1. 检查我们的帖子是否受到其他用户编辑的保护:
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
  })
Run Code Online (Sandbox Code Playgroud)


小智 1

每次创建用户时,公钥都可以与所有其他用户共享。(有一个维护列表的超级用户)然后您的前端网站将迭代所有公钥以获取人们发布的所有帖子并显示它们。这样,人们可以阅读所有帖子,但不能编辑。另一种方法是让超级用户运行一个进程,不断索引帖子并将其“复制”到他自己的图表中,并且该图表可以是所查看的内容。(使其受到更多保护)这是非常高水平的答案,但所有这一切都可以使用gun.user()和枪核心结构实现。