private write and public read in gundb
Asked Answered
P

3

8

I want to create a microblog where everyone can read all posts, but just owner can delete or edit the posts. In gundb without sea everyone can edit or delete posts, in sea( gun.user()) I have to share public key, in sea how I get all user's post and show posts in timeline?

How could I create this with gundb?

Pamphylia answered 8/5, 2019 at 15:34 Comment(0)
G
1

Each time a user is created, the public key could be shared with all other users. (Have a super user who maintains a list) Then your front-end website would iterate over all public keys to get all the posts people have made and show them. That way, people can read all the posts, but not edit. Another way to do this, is have a super user run a process that constantly indexes and 'copies' posts into his own graph and that graph can be what is viewed. (making it even more protected) This is very high level answer but all this is possible using gun.user() and the gun core structure.

Gault answered 8/5, 2019 at 21:45 Comment(2)
Thanks @Gault ! Yes, @Pamphylia gun.user() ( gun.eco/docs/Todo-Dapp ) already is public-read user-only-write.Exoergic
Thanks. How can I show the posts in order of time in this way?Pamphylia
Y
6

I've been looking for answers to questions regarding data privacy in gun, and here's my answer:

  1. Import and definition of variables
<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()
  1. Create first author of microblog
gun.user().create('firstMicroblogAuthor', 'somePassword')
gun.user().auth('firstMicroblogAuthor', 'somePassword')

  1. Create post and get author
var post = {
  title: 'First post',
  text: 'Hello world!'
}

var author = gun.get('~@firstMicroblogAuthor') // There should be the same `username` in Step 2
  1. Save post
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
  })
  1. Check that our posts are protected from editing by other users:
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
  })
Yetac answered 17/9, 2019 at 9:48 Comment(0)
G
1

Each time a user is created, the public key could be shared with all other users. (Have a super user who maintains a list) Then your front-end website would iterate over all public keys to get all the posts people have made and show them. That way, people can read all the posts, but not edit. Another way to do this, is have a super user run a process that constantly indexes and 'copies' posts into his own graph and that graph can be what is viewed. (making it even more protected) This is very high level answer but all this is possible using gun.user() and the gun core structure.

Gault answered 8/5, 2019 at 21:45 Comment(2)
Thanks @Gault ! Yes, @Pamphylia gun.user() ( gun.eco/docs/Todo-Dapp ) already is public-read user-only-write.Exoergic
Thanks. How can I show the posts in order of time in this way?Pamphylia
T
1

Saying the todo-dapp is public read user only write is misleading, it does not actually provide you with the ability to see other users documents.

In fact, its been a long-term peeve of me that there is actually no documentation or examples on how to do this, and when you ask the devs you just face evasion after evasion.

A database is only useful if theres a way to separate users concerns from each other

Territory answered 12/7, 2019 at 11:1 Comment(1)
Sad to hear you felt avoided, the community tries to be very responsive (nobody is paid) but sometimes things slip through the cracks. The answer to your Q is you share the public key (which you can get from your user after logged in gun.user().is.pub) with others (via URLs, whatever), and then can read their data gun.user(pubKey).on(cb). Hope this helps!!!!! :)Exoergic

© 2022 - 2024 — McMap. All rights reserved.