Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Updated doc ownership schema
Browse files Browse the repository at this point in the history
Only wikis, not pages, have owners/members.
The member editing UI is still on pages for now, but controls access to the whole wiki.
Pull replication now syncs all channels (["*"]).
  • Loading branch information
snej committed Jan 24, 2013
1 parent 0dbcd17 commit 4640163
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 64 deletions.
2 changes: 2 additions & 0 deletions TouchWiki.xcodeproj/project.pbxproj
Expand Up @@ -489,6 +489,7 @@
GCC_SYMBOLS_PRIVATE_EXTERN = NO; GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_TREAT_WARNINGS_AS_ERRORS = YES; GCC_TREAT_WARNINGS_AS_ERRORS = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 6.0; IPHONEOS_DEPLOYMENT_TARGET = 6.0;
Expand All @@ -514,6 +515,7 @@
GCC_C_LANGUAGE_STANDARD = gnu99; GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_TREAT_WARNINGS_AS_ERRORS = YES; GCC_TREAT_WARNINGS_AS_ERRORS = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 6.0; IPHONEOS_DEPLOYMENT_TARGET = 6.0;
Expand Down
9 changes: 4 additions & 5 deletions TouchWiki/PageController.m
Expand Up @@ -109,8 +109,7 @@ - (void) syncManager: (SyncManager*)manager addedReplication: (TDReplication*)re
if (replication.pull) { if (replication.pull) {
// Pull replication: Define the set of channels to sync // Pull replication: Define the set of channels to sync
replication.filter = @"sync_gateway/bychannel"; replication.filter = @"sync_gateway/bychannel";
NSArray* channels = _wikiStore.channelsToSync; replication.query_params = @{@"channels": @[@"*"]};
replication.query_params = @{@"channels": [channels componentsJoinedByString: @","]};
} else { } else {
// Push replication: Set filter to block pushing draft documents // Push replication: Set filter to block pushing draft documents
replication.filter = @"notDraft"; // defined in WikiStore.m replication.filter = @"notDraft"; // defined in WikiStore.m
Expand Down Expand Up @@ -193,8 +192,8 @@ - (void) loadContent {


//FIX: Use a real template engine! //FIX: Use a real template engine!
replace(html, @"{{ACCESSCLASS}}", klass); replace(html, @"{{ACCESSCLASS}}", klass);
replace(html, @"{{OWNER}}", _page.owner_id); replace(html, @"{{OWNER}}", _page.wiki.owner_id);
replace(html, @"{{MEMBERS}}", [_page.members componentsJoinedByString: @", "]); replace(html, @"{{MEMBERS}}", [_page.wiki.members componentsJoinedByString: @", "]);


if (_page.draft) { if (_page.draft) {
[html appendString: @"<div id='banner'>PREVIEW</div>\n"]; [html appendString: @"<div id='banner'>PREVIEW</div>\n"];
Expand Down Expand Up @@ -380,7 +379,7 @@ - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)butto


- (void) updateMembers: (NSArray*)members { - (void) updateMembers: (NSArray*)members {
members = [[NSOrderedSet orderedSetWithArray: members] array]; // Remove duplicates members = [[NSOrderedSet orderedSetWithArray: members] array]; // Remove duplicates
_page.members = members; _page.wiki.members = members;
[_page.wiki addMembers: members]; [_page.wiki addMembers: members];
} }


Expand Down
5 changes: 5 additions & 0 deletions TouchWiki/Wiki.h
Expand Up @@ -28,6 +28,11 @@


- (NSString*) docIDForPageWithTitle: (NSString*)title; - (NSString*) docIDForPageWithTitle: (NSString*)title;


@property (readonly) NSString* owner_id;
@property (copy) NSArray* members;

- (void) addMembers: (NSArray*)newMembers;

@end @end




Expand Down
29 changes: 27 additions & 2 deletions TouchWiki/Wiki.m
Expand Up @@ -18,13 +18,14 @@ @implementation Wiki
NSSet* _allPageTitles; NSSet* _allPageTitles;
} }


@dynamic title, markdown, created_at, updated_at; @dynamic title, markdown, created_at, updated_at, owner_id, members;




- (id) initNewWithTitle: (NSString*)title inWikiStore: (WikiStore*)wikiStore { - (id) initNewWithTitle: (NSString*)title inWikiStore: (WikiStore*)wikiStore {
self = [super initWithNewDocumentInDatabase: wikiStore.database]; self = [super initWithNewDocumentInDatabase: wikiStore.database];
if (self) { if (self) {
[self setType: @"wiki" owner: wikiStore.username]; [self setupType: @"wiki"];
[self setValue: wikiStore.username ofProperty: @"owner_id"];
self.title = title; self.title = title;
} }
return self; return self;
Expand Down Expand Up @@ -90,4 +91,28 @@ - (void) observeValueForKeyPath:(NSString *)keyPath
} }




- (bool) editable {
NSString* username = self.wikiStore.username;
return [self.owner_id isEqualToString: username] || [self.members containsObject: username];
}


- (bool) owned {
NSString* username = self.wikiStore.username;
return [self.owner_id isEqualToString: username];
}


- (void) addMembers: (NSArray*)newMembers {
NSArray* oldMembers = self.members;
if (!oldMembers) {
self.members = newMembers;
return;
}
NSMutableOrderedSet* members = [NSMutableOrderedSet orderedSetWithArray: self.members];
[members addObjectsFromArray: newMembers];
self.members = members.array;
}


@end @end
7 changes: 1 addition & 6 deletions TouchWiki/WikiItem.h
Expand Up @@ -20,17 +20,12 @@
@property (strong) NSDate* created_at; @property (strong) NSDate* created_at;
@property (strong) NSDate* updated_at; @property (strong) NSDate* updated_at;


@property (readonly) NSString* owner_id;
@property (copy) NSArray* members;

- (void) addMembers: (NSArray*)newMembers;

@property (readonly) bool editable; @property (readonly) bool editable;
@property (readonly) bool owned; @property (readonly) bool owned;


@end @end




@interface WikiItem (Private) @interface WikiItem (Private)
- (void) setType: (NSString*)type owner: (NSString*)owner; - (void) setupType: (NSString*)type;
@end @end
29 changes: 4 additions & 25 deletions TouchWiki/WikiItem.m
Expand Up @@ -10,15 +10,10 @@
#import "WikiStore.h" #import "WikiStore.h"




@interface WikiItem ()
@property (readwrite) NSString* owner_id;
@end


@implementation WikiItem @implementation WikiItem




@dynamic title, markdown, created_at, updated_at, owner_id, members; @dynamic title, markdown, created_at, updated_at;




- (id) initWithDocument: (TDDocument*)document { - (id) initWithDocument: (TDDocument*)document {
Expand All @@ -30,10 +25,8 @@ - (id) initWithDocument: (TDDocument*)document {
} }




- (void) setType: (NSString*)type owner: (NSString*)owner { - (void) setupType: (NSString*)type {
NSParameterAssert(owner.length > 0);
[self setValue: type ofProperty: @"type"]; [self setValue: type ofProperty: @"type"];
self.owner_id = owner;
self.created_at = self.updated_at = [NSDate date]; self.created_at = self.updated_at = [NSDate date];
} }


Expand All @@ -56,26 +49,12 @@ - (WikiStore*) wikiStore {




- (bool) editable { - (bool) editable {
NSString* username = self.wikiStore.username; return false; // abstract
return [self.owner_id isEqualToString: username] || [self.members containsObject: username];
} }




- (bool) owned { - (bool) owned {
NSString* username = self.wikiStore.username; return false; // abstract
return [self.owner_id isEqualToString: username];
}


- (void) addMembers: (NSArray*)newMembers {
NSArray* oldMembers = self.members;
if (!oldMembers) {
self.members = newMembers;
return;
}
NSMutableOrderedSet* members = [NSMutableOrderedSet orderedSetWithArray: self.members];
[members addObjectsFromArray: newMembers];
self.members = members.array;
} }




Expand Down
2 changes: 1 addition & 1 deletion TouchWiki/WikiPage.h
Expand Up @@ -17,10 +17,10 @@


@property (readonly) Wiki* wiki; @property (readonly) Wiki* wiki;


// A document with this flag set will not be pushed to the server. */
@property bool draft; @property bool draft;


// Ephemeral properties: // Ephemeral properties:
//@property bool editing;
@property NSRange selectedRange; @property NSRange selectedRange;


+ (bool) parseDocID: (NSString*)docID + (bool) parseDocID: (NSString*)docID
Expand Down
17 changes: 11 additions & 6 deletions TouchWiki/WikiPage.m
Expand Up @@ -11,11 +11,6 @@
#import "WikiStore.h" #import "WikiStore.h"




@interface WikiPage ()
@property (readwrite) NSString* owner_id;
@end


@implementation WikiPage @implementation WikiPage




Expand All @@ -26,7 +21,7 @@ - (id) initNewWithTitle: (NSString*)title inWiki: (Wiki*)wiki {
TDDocument* doc = [wiki.database documentWithID: [wiki docIDForPageWithTitle: title]]; TDDocument* doc = [wiki.database documentWithID: [wiki docIDForPageWithTitle: title]];
self = [super initWithDocument: doc]; self = [super initWithDocument: doc];
if (self) { if (self) {
[self setType: @"page" owner: wiki.wikiStore.username]; [self setupType: @"page"];
[self setValue: wiki.wikiID ofProperty: @"wiki_id"]; [self setValue: wiki.wikiID ofProperty: @"wiki_id"];
} }
return self; return self;
Expand Down Expand Up @@ -72,4 +67,14 @@ - (NSTimeInterval) autosaveDelay {
} }




- (bool) editable {
return self.wiki.editable;
}


- (bool) owned {
return self.wiki.owned;
}


@end @end
2 changes: 0 additions & 2 deletions TouchWiki/WikiStore.h
Expand Up @@ -32,6 +32,4 @@


@property (strong) NSString* username; @property (strong) NSString* username;


@property (strong) NSArray* channelsToSync;

@end @end
17 changes: 0 additions & 17 deletions TouchWiki/WikiStore.m
Expand Up @@ -95,23 +95,6 @@ - (void) setUsername:(NSString *)username {
} }




- (NSArray*) channelsToSync {
//FIX: This would be better stored in a _local document in the database
NSArray* channels = [[NSUserDefaults standardUserDefaults] objectForKey: @"ChannelsToSync"];
if (!channels)
channels = @[];
NSString* username = self.username;
if (username && ![channels containsObject: username])
channels = [channels arrayByAddingObject: username];
return channels;
}


- (void) setChannelsToSync:(NSArray *)channelsToSync {
[[NSUserDefaults standardUserDefaults] setObject: channelsToSync forKey: @"ChannelsToSync"];
}


- (TDQuery*) queryPagesOfWiki: (Wiki*)wiki { - (TDQuery*) queryPagesOfWiki: (Wiki*)wiki {
TDQuery* query = [[_database viewNamed: @"pagesByTitle"] query]; TDQuery* query = [[_database viewNamed: @"pagesByTitle"] query];
query.startKey = @[wiki.wikiID]; query.startKey = @[wiki.wikiID];
Expand Down

0 comments on commit 4640163

Please sign in to comment.