@stackend/api
Version:
JS bindings to api.stackend.com
361 lines • 15.8 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.receiveCommentLikes = exports.getMinutesToEdit = exports.getCommentsFromStore = exports.removeCommentFromStore = exports.fetchComment = exports.fetchComments = exports.updateComment = exports.invalidateComments = exports.requestComments = exports.receiveComments = exports.fetchMultipleComments = exports.requestGroupComments = exports.receiveGroupComments = exports.getCommentsStateKey = exports._getCommentsStateKey = void 0;
var commentReducer_1 = require("./commentReducer");
var index_1 = require("./index");
var SortOrder_1 = __importDefault(require("../api/SortOrder"));
var voteActions_1 = require("../vote/voteActions");
var likeActions_1 = require("../like/likeActions");
/**
* The default page size: 3
*/
var DEFAULT_PAGE_SIZE = 3;
/**
* Get the key used to look up comments
* @param action
* @returns {string}
*/
function _getCommentsStateKey(action) {
return getCommentsStateKey(action.module, action.referenceGroupId, action.commentSortCriteria, action.order);
}
exports._getCommentsStateKey = _getCommentsStateKey;
/**
* Get the key used to look up comments
* @param module
* @param referenceGroupId
* @param commentSortCriteria
* @param order
* @returns {string}
*/
function getCommentsStateKey(module, referenceGroupId, commentSortCriteria, order) {
return module + ':' + referenceGroupId + ':' + commentSortCriteria + ':' + order;
}
exports.getCommentsStateKey = getCommentsStateKey;
/**
* Add comments to the redux store (typically run by the fetch-methods)
* @param module
* @param referenceGroupId
* @param json
* @param commentSortCriteria
* @param order
*/
function receiveGroupComments(module, referenceGroupId, json, commentSortCriteria, order) {
return {
type: commentReducer_1.RECEIVE_GROUP_COMMENTS,
module: module,
referenceGroupId: referenceGroupId,
commentSortCriteria: commentSortCriteria,
order: order,
json: json,
receivedAt: Date.now()
};
}
exports.receiveGroupComments = receiveGroupComments;
/**
* Request comments from the server
* @param module
* @param referenceGroupId
* @param commentSortCriteria
* @param order
*/
function requestGroupComments(module, referenceGroupId, commentSortCriteria, order) {
return {
type: commentReducer_1.REQUEST_GROUP_COMMENTS,
module: module,
referenceGroupId: referenceGroupId,
commentSortCriteria: commentSortCriteria,
order: order
};
}
exports.requestGroupComments = requestGroupComments;
/**
* Requests and receive comments and store them in redux-state
* @param module
* @param referenceIds
* @param referenceGroupId
* @param p
* @param pageSize
* @param commentSortCriteria
* @param order
*/
function fetchMultipleComments(_a) {
var _this = this;
var module = _a.module, referenceIds = _a.referenceIds, referenceGroupId = _a.referenceGroupId, _b = _a.p, p = _b === void 0 ? 1 : _b, _c = _a.pageSize, pageSize = _c === void 0 ? DEFAULT_PAGE_SIZE : _c, _d = _a.commentSortCriteria, commentSortCriteria = _d === void 0 ? index_1.CommentSortCriteria.CREATED_WITH_REPLIES : _d, _e = _a.order, order = _e === void 0 ? SortOrder_1.default.DESCENDING : _e;
return function (dispatch) { return __awaiter(_this, void 0, void 0, function () {
var json;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
dispatch(requestGroupComments(module, referenceGroupId, commentSortCriteria, order));
return [4 /*yield*/, dispatch((0, index_1.getMultipleComments)({ module: module, referenceIds: referenceIds, pageSize: pageSize, p: p, sortCriteria: commentSortCriteria, order: order }))];
case 1:
json = _a.sent();
dispatch(receiveGroupComments(module, referenceGroupId, json, commentSortCriteria, order));
dispatch(receiveCommentLikes(json));
return [2 /*return*/, json];
}
});
}); };
}
exports.fetchMultipleComments = fetchMultipleComments;
/**
* Add comments into the redux store (typically run by the fetch-methods)
* @param module
* @param referenceId
* @param referenceGroupId
* @param json
* @param commentSortCriteria
* @param order
*/
function receiveComments(module, referenceId, referenceGroupId, json, commentSortCriteria, order) {
return {
type: commentReducer_1.RECEIVE_COMMENTS,
module: module,
referenceId: referenceId,
referenceGroupId: referenceGroupId,
commentSortCriteria: commentSortCriteria,
order: order,
receivedAt: Date.now(),
json: json
};
}
exports.receiveComments = receiveComments;
/**
* Request comments from the server
*/
function requestComments(module, referenceId, referenceGroupId, commentSortCriteria, order) {
return {
type: commentReducer_1.REQUEST_COMMENTS,
module: module,
referenceId: referenceId,
referenceGroupId: referenceGroupId,
commentSortCriteria: commentSortCriteria,
order: order
};
}
exports.requestComments = requestComments;
/**
* Remove comments from the redux store (but not from the backend storage)
*/
function invalidateComments(_a) {
var module = _a.module, referenceGroupId = _a.referenceGroupId, commentSortCriteria = _a.commentSortCriteria, order = _a.order;
return {
type: commentReducer_1.INVALIDATE_GROUP_COMMENTS,
module: module,
referenceGroupId: referenceGroupId,
commentSortCriteria: commentSortCriteria,
order: order
};
}
exports.invalidateComments = invalidateComments;
/**
* When loading comments receive is run when the server has responded
* @param id
* @param module
* @param referenceId
* @param referenceGroupId
* @param json
* @param commentSortCriteria
* @param order
*/
function updateComment(id, module, referenceId, referenceGroupId, json, commentSortCriteria, order) {
return {
type: commentReducer_1.UPDATE_COMMENT,
id: id,
referenceId: referenceId,
referenceGroupId: referenceGroupId,
commentSortCriteria: commentSortCriteria,
order: order,
module: module,
receivedAt: Date.now(),
json: json
};
}
exports.updateComment = updateComment;
/**
* Requests and receive comments and store them in redux-state
*/
function fetchComments(_a) {
var _this = this;
var module = _a.module, referenceId = _a.referenceId, _b = _a.referenceGroupId, referenceGroupId = _b === void 0 ? 0 : _b, _c = _a.commentSortCriteria, commentSortCriteria = _c === void 0 ? index_1.CommentSortCriteria.CREATED_WITH_REPLIES : _c, _d = _a.order, order = _d === void 0 ? SortOrder_1.default.DESCENDING : _d, _e = _a.p, p = _e === void 0 ? 1 : _e, _f = _a.pageSize, pageSize = _f === void 0 ? DEFAULT_PAGE_SIZE : _f, _g = _a.useVotes, useVotes = _g === void 0 ? false : _g;
return function (dispatch) { return __awaiter(_this, void 0, void 0, function () {
var r, comments, likesByCurrentUser, error, voteSummary, votes, hasVoted, myReview, json;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
dispatch(requestComments(module, referenceId, referenceGroupId, commentSortCriteria, order));
return [4 /*yield*/, dispatch((0, index_1.getComments)({ module: module, referenceId: referenceId, pageSize: pageSize, p: p, useVotes: useVotes, sortCriteria: commentSortCriteria, order: order }))];
case 1:
r = _a.sent();
comments = r.comments, likesByCurrentUser = r.likesByCurrentUser, error = r.error, voteSummary = r.voteSummary, votes = r.votes, hasVoted = r.hasVoted, myReview = r.myReview;
if (voteSummary) {
dispatch((0, voteActions_1.receiveVotes)(module ? module : 'comments', voteSummary, votes, hasVoted, myReview));
}
json = { comments: comments, likesByCurrentUser: likesByCurrentUser, error: error };
dispatch(receiveCommentLikes(json));
dispatch(receiveComments(module, referenceId, referenceGroupId, json, commentSortCriteria, order));
return [2 /*return*/, r];
}
});
}); };
}
exports.fetchComments = fetchComments;
/**
* Fetch a single comment
* @param module
* @param id
* @param referenceId
* @param referenceGroupId
* @param commentSortCriteria
* @param order
* @param useVotes
*/
function fetchComment(_a) {
var _this = this;
var module = _a.module, id = _a.id, referenceId = _a.referenceId, _b = _a.referenceGroupId, referenceGroupId = _b === void 0 ? 0 : _b, _c = _a.commentSortCriteria, commentSortCriteria = _c === void 0 ? index_1.CommentSortCriteria.CREATED_WITH_REPLIES : _c, _d = _a.order, order = _d === void 0 ? SortOrder_1.default.DESCENDING : _d, _e = _a.useVotes, useVotes = _e === void 0 ? false : _e;
return function (dispatch) { return __awaiter(_this, void 0, void 0, function () {
var r;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, dispatch((0, index_1.getComment)({ id: id, module: module, useVotes: useVotes }))];
case 1:
r = _a.sent();
dispatch(receiveComments(module, referenceId, referenceGroupId, {
comments: {
entries: r.comment ? [r.comment] : []
},
likesByCurrentUser: {},
error: r.error
}, commentSortCriteria, order));
// FIXME: receiveCommentLikes
return [2 /*return*/, r];
}
});
}); };
}
exports.fetchComment = fetchComment;
/**
* Remove a comment from the store.
* @param module
* @param id
* @param referenceId
* @param referenceGroupId
* @param commentSortCriteria
* @param order
*/
function removeCommentFromStore(_a) {
var module = _a.module, id = _a.id, referenceId = _a.referenceId, _b = _a.referenceGroupId, referenceGroupId = _b === void 0 ? 0 : _b, commentSortCriteria = _a.commentSortCriteria, order = _a.order;
return function (dispatch) {
dispatch({
type: commentReducer_1.COMMENT_REMOVED,
id: id,
module: module,
referenceId: referenceId,
referenceGroupId: referenceGroupId,
commentSortCriteria: commentSortCriteria,
order: order
});
};
}
exports.removeCommentFromStore = removeCommentFromStore;
/**
* Get comments from the redux store
* @param commentsState
* @param commentType
* @param referenceGroupId
* @param referenceId
* @param commentSortCriteria
* @param order
* @returns a CommentCollectionState or null, if not loaded
*/
function getCommentsFromStore(commentsState, commentType, referenceGroupId, referenceId, commentSortCriteria, order) {
var _a, _b;
var key = getCommentsStateKey(commentType, referenceGroupId, commentSortCriteria, order);
var x = (_b = (_a = commentsState[key]) === null || _a === void 0 ? void 0 : _a.json) === null || _b === void 0 ? void 0 : _b.comments;
if (!x) {
return null;
}
var y = x[referenceId];
return y ? y : null;
}
exports.getCommentsFromStore = getCommentsFromStore;
/**
* Get the number of minutes to edit a comment, or -1 for eternal.
* @param commentsState
* @param commentType
* @param referenceGroupId
* @param referenceId
* @param commentSortCriteria
* @param order
*/
function getMinutesToEdit(commentsState, commentType, referenceGroupId, referenceId, commentSortCriteria, order) {
var _a;
var key = getCommentsStateKey(commentType, referenceGroupId, commentSortCriteria, order);
var json = (_a = commentsState[key]) === null || _a === void 0 ? void 0 : _a.json;
if (json) {
var minutesToEdit = json === null || json === void 0 ? void 0 : json.minutesToEdit; // FIXME: not included in interface
if (typeof minutesToEdit === 'number') {
return minutesToEdit;
}
}
return -1;
}
exports.getMinutesToEdit = getMinutesToEdit;
/**
* Update the comments likes in the redux store
* @param json
*/
function receiveCommentLikes(json) {
var _a;
var likeDataMap = {};
if ((_a = json === null || json === void 0 ? void 0 : json.comments) === null || _a === void 0 ? void 0 : _a.entries) {
json.comments.entries.forEach(function (c) {
likeDataMap[c.obfuscatedReference] = {
likes: c.numberOfLikes,
likedByCurrentUser: json.likesByCurrentUser[c.id] || false
};
});
}
return function (dispatch) { return dispatch((0, likeActions_1.receiveLikes)(likeDataMap)); };
}
exports.receiveCommentLikes = receiveCommentLikes;
//# sourceMappingURL=commentAction.js.map