Deprecate API v2 (was Revert "Revert "Deprecate API v2"") (#7802)

* Revert "Revert "Deprecate API v2""

* fix path in shops controller
This commit is contained in:
Matteo Pagliazzi
2016-08-01 22:36:10 +02:00
committed by GitHub
parent 2a3e3fb9d3
commit 60f34dafb0
194 changed files with 231 additions and 11488 deletions
+4 -105
View File
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';
import Bluebird from 'bluebird';
import validator from 'validator';
import baseModel from '../libs/api-v3/baseModel';
import baseModel from '../libs/baseModel';
import _ from 'lodash';
import * as Tasks from './task';
import { model as User } from './user';
@@ -9,10 +9,10 @@ import {
model as Group,
TAVERN_ID,
} from './group';
import { removeFromArray } from '../libs/api-v3/collectionManipulators';
import { removeFromArray } from '../libs/collectionManipulators';
import shared from '../../../common';
import { sendTxn as txnEmail } from '../libs/api-v3/email';
import sendPushNotification from '../libs/api-v3/pushNotifications';
import { sendTxn as txnEmail } from '../libs/email';
import sendPushNotification from '../libs/pushNotifications';
import cwait from 'cwait';
const Schema = mongoose.Schema;
@@ -332,105 +332,4 @@ schema.methods.closeChal = async function closeChal (broken = {}) {
Bluebird.all(backgroundTasks);
};
// Methods to adapt the new schema to API v2 responses (mostly tasks inside the challenge model)
// These will be removed once API v2 is discontinued
// Get all the tasks belonging to a challenge,
schema.methods.getTasks = function getChallengeTasks () {
let args = Array.from(arguments);
let cb;
let type;
if (args.length === 1) {
cb = args[0];
} else if (args.length > 1) {
type = args[0];
cb = args[1];
} else {
cb = function noop () {};
}
let query = {
userId: {
$exists: false,
},
'challenge.id': this._id,
};
if (type) query.type = type;
return Tasks.Task.find(query, cb); // so we can use it as a promise
};
// Given challenge and an array of tasks and one of members return an API compatible challenge + tasks obj + members
schema.methods.addToChallenge = function addToChallenge (tasks, members) {
let obj = this.toJSON();
obj.members = members;
let tasksOrder = obj.tasksOrder; // Saving a reference because we won't return it
obj.habits = [];
obj.dailys = [];
obj.todos = [];
obj.rewards = [];
obj.tasksOrder = undefined;
let unordered = [];
tasks.forEach((task) => {
// We want to push the task at the same position where it's stored in tasksOrder
let pos = tasksOrder[`${task.type}s`].indexOf(task._id);
if (pos === -1) { // Should never happen, it means the lists got out of sync
unordered.push(task.toJSONV2());
} else {
obj[`${task.type}s`][pos] = task.toJSONV2();
}
});
// Reconcile unordered items
unordered.forEach((task) => {
obj[`${task.type}s`].push(task);
});
// Remove null values that can be created when inserting tasks at an index > length
['habits', 'dailys', 'rewards', 'todos'].forEach((type) => {
obj[type] = _.compact(obj[type]);
});
return obj;
};
// Return the data maintaining backward compatibility
schema.methods.getTransformedData = function getTransformedData (options) {
let self = this;
let cb = options.cb;
let populateMembers = options.populateMembers;
let queryMembers = {
challenges: self._id,
};
let selectDataMembers = '_id';
if (populateMembers) {
selectDataMembers += ` ${populateMembers}`;
}
let membersQuery = User.find(queryMembers).select(selectDataMembers);
if (options.limitPopulation) membersQuery.limit(15);
Bluebird.all([
membersQuery.exec(),
self.getTasks(),
])
.then((results) => {
cb(null, self.addToChallenge(results[1], results[0]));
})
.catch(cb);
};
// END of API v2 methods
export let model = mongoose.model('Challenge', schema);
+2 -2
View File
@@ -4,11 +4,11 @@ import mongoose from 'mongoose';
import _ from 'lodash';
import shared from '../../../common';
import couponCode from 'coupon-code';
import baseModel from '../libs/api-v3/baseModel';
import baseModel from '../libs/baseModel';
import {
BadRequest,
NotAuthorized,
} from '../libs/api-v3/errors';
} from '../libs/errors';
export let schema = new mongoose.Schema({
_id: {type: String, default: couponCode.generate},
+1 -1
View File
@@ -1,6 +1,6 @@
import mongoose from 'mongoose';
import validator from 'validator';
import baseModel from '../libs/api-v3/baseModel';
import baseModel from '../libs/baseModel';
// A collection used to store mailing list unsubscription for non registered email addresses
export let schema = new mongoose.Schema({
+6 -63
View File
@@ -7,17 +7,17 @@ import shared from '../../../common';
import _ from 'lodash';
import { model as Challenge} from './challenge';
import validator from 'validator';
import { removeFromArray } from '../libs/api-v3/collectionManipulators';
import { removeFromArray } from '../libs/collectionManipulators';
import {
InternalServerError,
BadRequest,
} from '../libs/api-v3/errors';
import baseModel from '../libs/api-v3/baseModel';
import { sendTxn as sendTxnEmail } from '../libs/api-v3/email';
} from '../libs/errors';
import baseModel from '../libs/baseModel';
import { sendTxn as sendTxnEmail } from '../libs/email';
import Bluebird from 'bluebird';
import nconf from 'nconf';
import sendPushNotification from '../libs/api-v3/pushNotifications';
import pusher from '../libs/api-v3/pusher';
import sendPushNotification from '../libs/pushNotifications';
import pusher from '../libs/pusher';
const questScrolls = shared.content.quests;
const Schema = mongoose.Schema;
@@ -749,63 +749,6 @@ schema.methods.leave = async function leaveGroup (user, keep = 'keep-all') {
return await Bluebird.all(promises);
};
// API v2 compatibility methods
schema.methods.getTransformedData = function getTransformedData (options) {
let cb = options.cb;
let populateMembers = options.populateMembers;
let populateInvites = options.populateInvites;
let populateChallenges = options.populateChallenges;
let obj = this.toJSON();
let queryMembers = {};
let queryInvites = {};
if (this.type === 'guild') {
queryInvites['invitations.guilds.id'] = this._id;
} else {
queryInvites['invitations.party.id'] = this._id;
}
if (this.type === 'guild') {
queryMembers.guilds = this._id;
} else {
queryMembers['party._id'] = this._id;
}
let selectDataMembers = '_id';
let selectDataInvites = '_id';
let selectDataChallenges = '_id';
if (populateMembers) {
selectDataMembers += ` ${populateMembers}`;
}
if (populateInvites) {
selectDataInvites += ` ${populateInvites}`;
}
if (populateChallenges) {
selectDataChallenges += ` ${populateChallenges}`;
}
let membersQuery = User.find(queryMembers).select(selectDataMembers);
if (options.limitPopulation) membersQuery.limit(15);
Bluebird.all([
membersQuery.exec(),
User.find(queryInvites).select(populateInvites).exec(),
Challenge.find({group: obj._id}).select(populateMembers).exec(),
])
.then((results) => {
obj.members = results[0];
obj.invites = results[1];
obj.challenges = results[2];
cb(null, obj);
})
.catch(cb);
};
// END API v2 compatibility methods
export let model = mongoose.model('Group', schema);
// initialize tavern if !exists (fresh installs)
+1 -1
View File
@@ -1,5 +1,5 @@
import mongoose from 'mongoose';
import baseModel from '../libs/api-v3/baseModel';
import baseModel from '../libs/baseModel';
import validator from 'validator';
const Schema = mongoose.Schema;
+1 -1
View File
@@ -1,5 +1,5 @@
import mongoose from 'mongoose';
import baseModel from '../libs/api-v3/baseModel';
import baseModel from '../libs/baseModel';
const Schema = mongoose.Schema;
+1 -1
View File
@@ -1,5 +1,5 @@
import mongoose from 'mongoose';
import baseModel from '../libs/api-v3/baseModel';
import baseModel from '../libs/baseModel';
import { v4 as uuid } from 'uuid';
import validator from 'validator';
+3 -44
View File
@@ -2,10 +2,10 @@ import mongoose from 'mongoose';
import shared from '../../../common';
import validator from 'validator';
import moment from 'moment';
import baseModel from '../libs/api-v3/baseModel';
import { InternalServerError } from '../libs/api-v3/errors';
import baseModel from '../libs/baseModel';
import { InternalServerError } from '../libs/errors';
import _ from 'lodash';
import { preenHistory } from '../libs/api-v3/preening';
import { preenHistory } from '../libs/preening';
const Schema = mongoose.Schema;
@@ -22,7 +22,6 @@ export let tasksTypes = ['habit', 'daily', 'todo', 'reward'];
// Important
// When something changes here remember to update the client side model at common/script/libs/taskDefaults
export let TaskSchema = new Schema({
_legacyId: String, // TODO Remove when v2 is deprecated
type: {type: String, enum: tasksTypes, required: true, default: tasksTypes[0]},
text: {type: String, required: true},
notes: {type: String, default: ''},
@@ -160,46 +159,6 @@ TaskSchema.methods.scoreChallengeTask = async function scoreChallengeTask (delta
await chalTask.save();
};
// Methods to adapt the new schema to API v2 responses (mostly tasks inside the user model)
// These will be removed once API v2 is discontinued
// toJSON for API v2
TaskSchema.methods.toJSONV2 = function toJSONV2 () {
let toJSON = this.toJSON();
if (toJSON._legacyId) {
toJSON.id = toJSON._legacyId;
} else {
toJSON.id = toJSON._id;
}
if (!toJSON.challenge) toJSON.challenge = {};
let v3Tags = this.tags;
toJSON.tags = {};
v3Tags.forEach(tag => {
toJSON.tags[tag] = true;
});
toJSON.dateCreated = this.createdAt;
return toJSON;
};
TaskSchema.statics.fromJSONV2 = function fromJSONV2 (taskObj) {
if (taskObj.id) taskObj._id = taskObj.id;
let v2Tags = taskObj.tags || {};
taskObj.tags = [];
taskObj.tags = _.map(v2Tags, (tag, key) => key);
return taskObj;
};
// END of API v2 methods
export let Task = mongoose.model('Task', TaskSchema);
Task.schema.path('alias').validate(function valiateAliasNotTaken (alias, respond) {
+1 -1
View File
@@ -3,7 +3,7 @@ import _ from 'lodash';
import moment from 'moment';
import * as Tasks from '../task';
import Bluebird from 'bluebird';
import baseModel from '../../libs/api-v3/baseModel';
import baseModel from '../../libs/baseModel';
import schema from './schema';
+1 -87
View File
@@ -1,6 +1,4 @@
import shared from '../../../../common';
import _ from 'lodash';
import * as Tasks from '../task';
import Bluebird from 'bluebird';
import {
chatDefaults,
@@ -42,88 +40,4 @@ schema.methods.addNotification = function addUserNotification (type, data = {})
type,
data,
});
};
// Methods to adapt the new schema to API v2 responses (mostly tasks inside the user model)
// These will be removed once API v2 is discontinued
// Get all the tasks belonging to a user,
schema.methods.getTasks = function getUserTasks () {
let args = Array.from(arguments);
let cb;
let type;
if (args.length === 1) {
cb = args[0];
} else {
type = args[0];
cb = args[1];
}
let query = {
userId: this._id,
};
if (type) query.type = type;
Tasks.Task.find(query, cb);
};
// Given user and an array of tasks, return an API compatible user + tasks obj
schema.methods.addTasksToUser = function addTasksToUser (tasks) {
let obj = this.toJSON();
obj.id = obj._id;
obj.filters = {};
obj.tags = obj.tags.map(tag => {
return {
id: tag.id,
name: tag.name,
challenge: tag.challenge,
};
});
let tasksOrder = obj.tasksOrder; // Saving a reference because we won't return it
obj.habits = [];
obj.dailys = [];
obj.todos = [];
obj.rewards = [];
obj.tasksOrder = undefined;
let unordered = [];
tasks.forEach((task) => {
// We want to push the task at the same position where it's stored in tasksOrder
let pos = tasksOrder[`${task.type}s`].indexOf(task._id);
if (pos === -1) { // Should never happen, it means the lists got out of sync
unordered.push(task.toJSONV2());
} else {
obj[`${task.type}s`][pos] = task.toJSONV2();
}
});
// Reconcile unordered items
unordered.forEach((task) => {
obj[`${task.type}s`].push(task);
});
// Remove null values that can be created when inserting tasks at an index > length
['habits', 'dailys', 'rewards', 'todos'].forEach((type) => {
obj[type] = _.compact(obj[type]);
});
return obj;
};
// Return the data maintaining backward compatibility
schema.methods.getTransformedData = function getTransformedData (cb) {
let self = this;
this.getTasks((err, tasks) => {
if (err) return cb(err);
cb(null, self.addTasksToUser(tasks));
});
};
// END of API v2 methods
};
+1 -1
View File
@@ -1,5 +1,5 @@
import mongoose from 'mongoose';
import baseModel from '../libs/api-v3/baseModel';
import baseModel from '../libs/baseModel';
import { v4 as uuid } from 'uuid';
import validator from 'validator';