UNPKG

@stackend/api

Version:

JS bindings to api.stackend.com

271 lines 11.7 kB
"use strict"; 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 }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getObjectsRequiringModeration = exports.receiveResourceUsage = exports.loadCommunitySettings = exports.editCommunity = exports.fetchCommunities = exports.fetchCommunity = exports.clearCurrentCommunity = exports.removeCommunity = exports.removeCommunities = exports.clearCommunities = exports.setCurrentCommunity = exports.loadCommunity = exports.requestCommunities = exports.receiveCommunities = void 0; var communityReducer_1 = require("./communityReducer"); var api_1 = require("../api"); var index_1 = require("./index"); var shopActions_1 = require("../shop/shopActions"); function receiveCommunities(json) { return { type: communityReducer_1.RECEIVE_COMMUNITIES, json: json, receivedAt: Date.now() }; } exports.receiveCommunities = receiveCommunities; function requestCommunities(status) { return { type: communityReducer_1.REQUEST_COMMUNITIES }; } exports.requestCommunities = requestCommunities; /** * Set the current community * @param community * @param objectsRequiringModeration * @deprecated, use setCurrentCommunity instead */ exports.loadCommunity = setCurrentCommunity; /** * Set the current community * @param community * @param objectsRequiringModeration */ function setCurrentCommunity(community, objectsRequiringModeration) { return { type: communityReducer_1.SET_COMMUNITY_SETTINGS, community: community, objectsRequiringModeration: objectsRequiringModeration }; } exports.setCurrentCommunity = setCurrentCommunity; function clearCommunities() { return { type: communityReducer_1.REMOVE_COMMUNITIES }; } exports.clearCommunities = clearCommunities; /** * Remove all communities from the redux state * @deprecated use clearCommunities instead */ exports.removeCommunities = clearCommunities; /** * Remove the current community from the redux state * @deprecated use clearCurrentCommunity instead */ exports.removeCommunity = clearCurrentCommunity; /** * Remove the current community from the redux state */ function clearCurrentCommunity() { return { type: communityReducer_1.REMOVE_COMMUNITY }; } exports.clearCurrentCommunity = clearCurrentCommunity; /** * Fetch a single community and set it to be the current community * @param id * @param permalink */ function fetchCommunity(_a) { var _this = this; var id = _a.id, permalink = _a.permalink; return function (dispatch /*, getState: any*/) { return __awaiter(_this, void 0, void 0, function () { var json; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, dispatch((0, index_1.getCommunity)({ id: id, permalink: permalink }))]; case 1: json = _a.sent(); dispatch(setCurrentCommunity(json.stackendCommunity, json.objectsRequiringModeration)); return [2 /*return*/, json]; } }); }); }; } exports.fetchCommunity = fetchCommunity; /** * Fetch a list of communities * @param myCommunities * @param creatorUserId * @param status * @param p * @param pageSize */ function fetchCommunities(_a) { var _this = this; var _b = _a.myCommunities, myCommunities = _b === void 0 ? true : _b, creatorUserId = _a.creatorUserId, _c = _a.status, status = _c === void 0 ? '*' : _c, _d = _a.p, p = _d === void 0 ? 1 : _d, _e = _a.pageSize, pageSize = _e === void 0 ? 10 : _e; return function (dispatch /*, getState: any*/) { return __awaiter(_this, void 0, void 0, function () { var json; return __generator(this, function (_a) { switch (_a.label) { case 0: dispatch(requestCommunities(status)); return [4 /*yield*/, dispatch((0, index_1.searchCommunity)({ myCommunities: myCommunities, creatorUserId: creatorUserId, status: status, pageSize: pageSize, p: p }))]; case 1: json = _a.sent(); dispatch(receiveCommunities(json)); return [2 /*return*/, json]; } }); }); }; } exports.fetchCommunities = fetchCommunities; /** * Update the current community * @param community */ function updateCommunity(community) { return { type: communityReducer_1.UPDATE_COMMUNITY, receivedAt: Date.now(), community: community }; } /** * Store a community and update the redux store * @param id * @param name * @param permalink * @param description * @param status * @param locale * @param domains * @param defaultUserId * @param openAIApiKey */ function editCommunity(_a) { var _this = this; var _b = _a.id, id = _b === void 0 ? 0 : _b, _c = _a.name, name = _c === void 0 ? '' : _c, _d = _a.permalink, permalink = _d === void 0 ? '' : _d, _e = _a.description, description = _e === void 0 ? '' : _e, _f = _a.status, status = _f === void 0 ? index_1.CommunityStatus.VISIBLE : _f, _g = _a.locale, locale = _g === void 0 ? 'en_US' : _g, _h = _a.domains, domains = _h === void 0 ? [] : _h, _j = _a.defaultUserId, defaultUserId = _j === void 0 ? 0 : _j, _k = _a.openAIApiKey, openAIApiKey = _k === void 0 ? '' : _k; return function (dispatch /*, getState: any*/) { return __awaiter(_this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, dispatch((0, index_1.storeCommunity)({ id: id, name: name, permalink: permalink, description: description, status: status, locale: locale, domains: domains, defaultUserId: defaultUserId, openAIApiKey: openAIApiKey }))]; case 1: response = _a.sent(); if (!!id && id !== 0) { dispatch(updateCommunity(response.storedCommunity)); } else { //This is a new community dispatch(receiveCommunities({ results: { pageSize: 1, page: 1, entries: [response.storedCommunity] } })); } return [2 /*return*/, response]; } }); }); }; } exports.editCommunity = editCommunity; /** * Load and set the current community * @param id * @param permalink * @param domain */ function loadCommunitySettings(_a) { var id = _a.id, permalink = _a.permalink, domain = _a.domain; return function (dispatch) { return __awaiter(this, void 0, void 0, function () { var r, e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, dispatch((0, index_1.getCommunity)({ id: id, permalink: permalink, domain: domain }))]; case 1: r = _a.sent(); if (r.stackendCommunity === null) { //console.log("couldn't find community: ",permalink) return [2 /*return*/, r]; } dispatch(setCurrentCommunity(r.stackendCommunity, r.objectsRequiringModeration)); dispatch((0, shopActions_1.clearCache)()); dispatch((0, shopActions_1.setCommunityVATS)(r.stackendCommunity)); return [2 /*return*/, r]; case 2: e_1 = _a.sent(); console.error("Stackend: Couldn't loadCommunitySettings: ", e_1); return [2 /*return*/, (0, api_1.newXcapJsonErrorResult)("Couldn't loadCommunitySettings: " + e_1)]; case 3: return [2 /*return*/]; } }); }); }; } exports.loadCommunitySettings = loadCommunitySettings; function receiveResourceUsage(json) { return { type: communityReducer_1.RECEIVE_RESOURCE_USAGE, maximumUseBeforeCharge: json.maximumUseBeforeCharge, resourceUsageLast30Days: json.resourceUsageLast30Days, hasPaymentMethod: json.hasPaymentMethod, isUserExcludedFromBilling: json.isUserExcludedFromBilling }; } exports.receiveResourceUsage = receiveResourceUsage; /** * Get the number of objets requiring moderation for a given community * @param communityState * @param communityId * @return a number, never null */ function getObjectsRequiringModeration(communityState, communityId) { return communityState.objectsRequiringModeration[communityId] || 0; } exports.getObjectsRequiringModeration = getObjectsRequiringModeration; //# sourceMappingURL=communityAction.js.map