add createCharacter, transferCharacter endpoints
This commit is contained in:
@@ -104,19 +104,58 @@ Router.map(function () {
|
||||
where: "server"
|
||||
}).post(
|
||||
function () {
|
||||
ifPostOK(this, "createCharacter", () => {
|
||||
this.response.setHeader("Content-Type", "application/json");
|
||||
const header = this.request.headers;
|
||||
const key = header && header['authorization'];
|
||||
ifKeyValid(key, this.response, "createCharacter", () => {
|
||||
const character = this.request.body;
|
||||
let error;
|
||||
|
||||
character.owner = userIdFromKey(key);
|
||||
let id = Characters.insert(character, (err) => {
|
||||
if (err)
|
||||
error = err.message;
|
||||
});
|
||||
|
||||
if (error) {
|
||||
this.response.writeHead(400, "Failed to insert character");
|
||||
this.response.end(JSON.stringify({err: error}));
|
||||
} else {
|
||||
this.response.end(JSON.stringify({id: id}));
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
this.route("transferCharacterOwnership", { // POST /api/character/:_id/owner
|
||||
this.route("transferCharacterOwnership", { // PUT /api/character/:_id/owner
|
||||
path: "/api/character/:_id/owner",
|
||||
where: "server"
|
||||
}).post(
|
||||
}).put(
|
||||
function () {
|
||||
ifPostOK(this, "transferCharacterOwnership", () => {
|
||||
this.response.setHeader("Content-Type", "application/json");
|
||||
const header = this.request.headers;
|
||||
const key = header && header['authorization'];
|
||||
const charId = this.params._id;
|
||||
ifKeyValid(key, this.response, "transferCharacterOwnership", () => {
|
||||
if (isOwner(charId, userIdFromKey(key))) {
|
||||
const newOwner = this.request.body['id'];
|
||||
let error;
|
||||
Characters.update({_id: charId}, {"$set": {owner: newOwner}}, null,
|
||||
(err) => {
|
||||
if (err)
|
||||
error = err.message;
|
||||
});
|
||||
|
||||
if (error) {
|
||||
this.response.writeHead(400, "Failed to update character");
|
||||
this.response.end(JSON.stringify({err: error}));
|
||||
} else {
|
||||
this.response.end(JSON.stringify({success: true}));
|
||||
}
|
||||
} else {
|
||||
this.response.writeHead(403, "You do not have permission to transfer this character");
|
||||
this.response.end();
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
isOwner = function(charId, userId) {
|
||||
userId = userId || Meteor.userId();
|
||||
var char = Characters.findOne(charId, {fields: {owner: 1}});
|
||||
if (!char) return true;
|
||||
return (userId === char.owner);
|
||||
};
|
||||
|
||||
canEditCharacter = function(charId, userId){
|
||||
userId = userId || Meteor.userId();
|
||||
var char = Characters.findOne(charId, {fields: {owner: 1, writers: 1}});
|
||||
|
||||
Reference in New Issue
Block a user