Skip to content

Instantly share code, notes, and snippets.

@KnowledgeGarden
Created June 22, 2014 20:57
Show Gist options
  • Save KnowledgeGarden/3d20acf86c8827dab15b to your computer and use it in GitHub Desktop.
Save KnowledgeGarden/3d20acf86c8827dab15b to your computer and use it in GitHub Desktop.
self.create = function (blog, user, callback) {
console.log('ARTICLES_CREATE '+JSON.stringify(blog));
var article = new Topic();
article.locator = uuid.newUUID();
article.instanceOf = self.types.BLOG_TYPE;
article.creatorId = user._id;
article.largeIcon = icons.PUBLICATION;
article.smallIcon = icons.PUBLICATION_SM;
//we have to take this apart, build the object from scratch
// because of tags, which are relations.
var label = blog.title;
article.label = label;
var details = blog.body;
article.details = details;
// fetch the user for later linking
console.log('ARTICLES_CREATE-00 '+JSON.stringify(user));
Topic.findNodeByLocator(user.username, function(err,result) {
var thisUser = result;
console.log('ARTICLES_CREATE-0 '+thisUser);
var tags = blog.tags;
console.log('ARTICLES_CREATE-1 '+article);
console.log("ARTICLES_CREATE_1a "+self.TagModel);
var tagList = tags.split(',');
//process the tags -- wants the user topic so it can link user to tags
self.TagModel.processTags(tagList, thisUser, article.locator, label, function(err,result) {
console.log('NEW_POST '+JSON.stringify(result));
//result is a structure:
//{ locators: [],labels: [] }
//which allows us to store the locators for each tag and the labels.
//IN FACT: the TopicSchema is not setup to handle this structure,
// but interestingly, could accept it anyway.
//PERHAPS that's a better way to pass relations: a locator and some kind of label
// with which to make an HREF at the browser
var lox = result.locators;
var labs = result.labels;
//see to it that we have some tags
if (lox) {
var len = lox.length;
for (var i=0;i<len;i++) {
article.addRelation(self.types.SIMPLE_RELATION_TYPE, self.types.TAG_DOCUMENT_RELATION_TYPE, 'Tag-Document Relation', lox[i],labs[i]);
}
}
console.log("ARTICLES_CREATE_2 "+JSON.stringify(article));
article.addRelation(self.types.SIMPLE_RELATION_TYPE, self.types.DOCUMENT_CREATOR_RELATION_TYPE, 'Document-Creator Relation', user.handle,user.handle);
article.save(function(err) {
console.log('ARTICLES_CREATE-3 '+err);
if (!err) {
console.log('ARTICLES_CREATE-3a '+thisUser);
thisUser.addRelation(self.types.SIMPLE_RELATION_TYPE, self.types.DOCUMENT_CREATOR_RELATION_TYPE, 'Document-Creator Relation', article.locator,"Blog Post");
thisUser.save(function(err) {
console.log('ARTICLES_CREATE-4 '+err);
if (!err) {
callback(err,article._id);
} else {
callback(err,null);
}
});
} else {
callback(err,null);
}
});
});
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment