Chaining access rights on sync function

Hi.

Considering the following scenario:
A bucket containing documents of type “person” (the actual users) and type “note”.
1 - Lets consider that every “note” document belongs to a given “person” (think of a property owner: person_id).
2 - A “person1” can add many other “personX”, (in this case “personX” will have a property owner: person1_id);
3 - personX can add more children personXX (in this case “personXX” will hava a property owner: personx_id);

This relations would be like a tree:

├── Person 0
│   ├── Person 1
│   │   ├── 01_note_a
│   │   ├── 01_note_b
│   │   ├── 01_note_c
│   ├── Person 2
│   │   ├── 02_note_a
│   │   ├── 02_note_b
│   │   ├── 02_note_b
│   │   ├── Person 4
│   │   │   ├── 04_note_a
│   │   │   ├── 04_note_b
│   │   │   ├── 04_note_c
│   │   │   ├── Person 5
│   │   │   │   ├── 05_note_a
│   │   │   │   ├── 05_note_b
│   │   │   │   ├── 05_note_c

How should I design a sync function in order to achive this desired behavior, so that on syncing:
Person 0 receives all documents
Person 1 receives itself + its notes
Person 2 receives itself + its notes + Person 4 + Person 4’s notes + Person 5 + Person 5’s notes

I tried so far to do something like this in the sync function:

if(doc.type=="note") {
  channels(doc.owner+"-notes");
}
if(doc.type=="person") {
  channels([doc.id, doc.owner]);
  access(doc.owner, doc.id);
  access(doc.owner, doc.owner);
  access(doc.id, doc.id);
  access(doc.id, doc.owner);
}

However using this approach, Person 2 would get documents only until Person 5. (Person 5’s notes are not synced)

Should I include roles of some sort in this case?

Thanks a lot.

Well,
I’ve tried roles, seems that it will work in the following way.
Create a role for each “person”. (I’ve named my role the same as doc.id)
In the sync function:

1 - Add the created channels to doc.id role.
2 - Assign this role to doc.owner

Am I missing something?