Thank you Andy,

You solution works well as you showed.

I tried to be clever by creating the "userX-rooms" channels on the fly 
inside the sync function, but it does not work that way. If you create the 
channels in the config it works as expected. 

There is a lot to understand when coming from a regular SQL background, but 
I really feel it's worth the time.

Thanks!
Jonas



On Friday, July 17, 2015 at 7:00:36 PM UTC+2, ajres wrote:
>
> I'm using this modified version of your sg.json config, this adds a 
> 'user<N>-rooms' channel to each users admin_channels property, the chatroom 
> doc is added to each chatroom members -rooms channel.
>
> { 
>
>   "log": ["foo"], 
>
>   "databases": { 
>
>     "couchbase-chat": { 
>
>       "server": "http://localhost:8091/";, 
>
>       "bucket":"bucket-1", 
>
>       "users": { 
>
>         "user1": { 
>
>           "password": "user1", 
>
>           "admin_channels": ["users","user1-rooms"] 
>
>         }, 
>
>         "user2": { 
>
>           "password": "user2", 
>
>           "admin_channels": ["users","user2-rooms"] 
>
>         }, 
>
>         "user3": { 
>
>           "password": "user3", 
>
>           "admin_channels": ["users", "user3-rooms"] 
>
>         }, 
>
>         "user4": { 
>
>           "password": "user4", 
>
>           "admin_channels": ["users","user4-rooms"] 
>
>         }, 
>
>         "admin": { 
>
>           "password": "admin", 
>
>           "admin_roles": ["admin"] 
>
>         } 
>
>       }, 
>
>       "roles": { 
>
>         "admin": { 
>
>           "admin_channels": ["*"] 
>
>         } 
>
>       }, 
>
>       "sync": 
>
>       ` 
>
> function(doc, oldDoc) { 
>
>    
>
>   if(doc._deleted) { 
>
>     if(oldDoc.type == "chatroom") { 
>
>       requireUser(oldDoc.user); 
>
>     } else if (oldDoc.type == "message") { 
>
>       requireUser(oldDoc.user); 
>
>       var roomId = oldDoc.room; 
>
>       var messageChannel = "room-" + roomId; 
>
>       channel(messageChannel); 
>
>     } 
>
>     return; 
>
>   } 
>
>    
>
>   if(doc.type == "chatroom") { 
>
>     var roomId = doc._id; 
>
>     var roomMembers = doc.members; 
>
>
>  
>
>     for (var i = 0; i < roomMembers.length; i++) { 
>
>       var member = roomMembers[i]; 
>
>       channel(member + "-rooms"); 
>
>     } 
>
>
>  
>
>     var roomChannel = "room-" + roomId; 
>
>     channel(roomChannel); 
>
>     access(roomMembers, roomChannel); 
>
>   } else if(doc.type == "message") { 
>
>     // A user can only update its own messages 
>
>     if(oldDoc) { 
>
>       requireUser(oldDoc.user); 
>
>     } 
>
>   
>
>     var roomId = doc.room; 
>
>     var messageChannel = "room-" + roomId; 
>
>     channel(messageChannel); 
>
>   } 
>
> } 
>
>       ` 
>
>     } 
>
>   } 
>
> }
>
>
> if I start a continuous changes feed:
>
> curl -X GET 
> http://user1:user1@localhost:4984/couchbase-chat/_changes?feed=continuous
>
>
> Then add a chatroom:
>
>
> curl -X PUT http://user1:user1@localhost:4984/couchbase-chat/testchannel1 
> -d '{"members": ["user2","user1","user4"],"name": "still","type": 
> "chatroom","user": "user1"}' -H "Content-Type: application/json"
>
>
> response:
>
>
> {"id":"testchannel1","ok":true,"rev":"1-9bda027ae976eaec039a180fd16f3a8d"}
>
>
> Then delete the chat room:
>
>
> curl -X PUT http://user1:user1@localhost:4984/couchbase-chat/testchannel1 
> -d '{"_rev":"1-9bda027ae976eaec039a180fd16f3a8d","_deleted":true}' -H 
> "Content-Type: application/json"
>
>
> response:
>
>
> {"id":"testchannel1","ok":true,"rev":"2-e5851b5cb894cb7ff2badf951e21b3f5"}
>
>
> I see both revisions now on the _changes feed:
>
>
>
> {"seq":2,"id":"_user/user1","changes":[]}
>
>
>
> {"seq":7,"id":"testchannel1","changes":[{"rev":"1-9bda027ae976eaec039a180fd16f3a8d"}]}
>
>
>
> {"seq":8,"id":"testchannel1","deleted":true,"removed":["user1-rooms"],"changes":[{"rev":"2-e5851b5cb894cb7ff2badf951e21b3f5"}]}
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mobile-couchbase/c850c3b7-5710-4d81-bb1f-ff03a617dafe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to