UNPKG

@stackend/api

Version:

JS bindings to api.stackend.com

978 lines 34.5 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 }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.removeUser = exports.getStackendUrl = exports.getReferenceUrl = exports.inviteUserToCommunity = exports.setCommunityAccess = exports.setAdminStatus = exports.listAdminUsers = exports.getComponentClassFromModuleType = exports.getComponentLabel = exports.detectModules = exports.removeModule = exports.storeModuleRules = exports.storeModule = exports.newModule = exports.getSingletonModule = exports.getModule = exports.getModules = exports.hasStackendCreateAccess = exports.hasCommunityAdminAccess = exports.hasCommunityAdminOrModeratorAccess = exports.hasStackendAdminAccess = exports.isCommunityAdmin = exports.isCommunityModerator = exports._getCurrentCommunity = exports._getCurrentCommunityPermalinkFromUrl = exports.isCommunityUrlBlocked = exports.getCurrentCommunity = exports.getCurrentStackendUser = exports.searchCommunity = exports.removeCommunity = exports.setCommunityStatus = exports.storeCommunityPrivateSettings = exports.getCommunityPrivateSettings = exports.storeCommunityTheme = exports.setCommunitySetting = exports.setCommunitySettings = exports.storeCommunity = exports.newCommunity = exports.validateCommunityPermalink = exports.getCommunity = exports.STACKEND_COM_COMMUNITY_PERMALINK = exports.COMPONENT_CLASS = exports.COMMUNITY_MANAGER_CONTEXT = exports.getThemeLabel = exports.OrderBy = exports.Theme = exports.CommmunitySettings = exports.STACKEND_DOT_COM_COMMUNITY = exports.STACKEND_COMMUNITY = exports.CommunityStatus = void 0; var api_1 = require("../api"); var Order_1 = __importDefault(require("../api/Order")); var moduleAction_1 = require("./moduleAction"); var user_1 = require("../user"); var privileges_1 = require("../user/privileges"); /** * Stackend API constants and methods. * * @since 20 apr 2017 */ /** * CommunityStatus * @type {{VISIBLE: string, HIDDEN: string, REMOVED: string}} */ var CommunityStatus; (function (CommunityStatus) { CommunityStatus["VISIBLE"] = "VISIBLE"; CommunityStatus["HIDDEN"] = "HIDDEN"; CommunityStatus["REMOVED"] = "REMOVED"; })(CommunityStatus = exports.CommunityStatus || (exports.CommunityStatus = {})); /** * Xcap community name for stackend. * @type {string} */ exports.STACKEND_COMMUNITY = 'stackend'; /** * Commmunity permalink used for stackend.com news and examples * @type {string} */ exports.STACKEND_DOT_COM_COMMUNITY = 'stackend-com'; /** * Community setting keys */ exports.CommmunitySettings = { LOGIN_ENABLED: 'loginEnabled', REGISTER_ENABLED: 'registerEnabled', TERMS_AND_CONDITIONS_LINK: 'termsAndConditionsLink', FACEBOOK_LOGIN: 'facebookLogin', GOOGLE_LOGIN: 'googleLogin', OAUTH2_LOGIN: 'oauth2Login' }; /** * A community theme */ exports.Theme = { STACKEND: 'stackend' }; /** * Community search sort ordering */ var OrderBy; (function (OrderBy) { OrderBy["NAME"] = "NAME"; OrderBy["CREATED_DATE"] = "CREATED_DATE"; })(OrderBy = exports.OrderBy || (exports.OrderBy = {})); /** * Given a theme, get a human readable label * @param theme * @returns {string} */ function getThemeLabel(theme) { if (!theme || theme.length === 0) { return ''; } var r = theme.replace(/_/g, ' '); return r.charAt(0).toUpperCase() + r.substring(1).toLowerCase(); } exports.getThemeLabel = getThemeLabel; /** * CommunityManager context * @type {string} */ exports.COMMUNITY_MANAGER_CONTEXT = 'community'; /** * CommunityManager component class * @type {string} */ exports.COMPONENT_CLASS = 'se.josh.xcap.community.CommunityManager'; /** * Community permalink reserverd for news and documentation on stackend.com * @type {string} */ exports.STACKEND_COM_COMMUNITY_PERMALINK = 'stackend-com'; /** * Get a community. * If no parameter is present, the domain is taken from the referer header. * * @param id {String} * @param permalink {String} * @param domain {String} * */ function getCommunity(_a) { var id = _a.id, permalink = _a.permalink, domain = _a.domain; return (0, api_1.getJson)({ url: '/stackend/community/get', parameters: { id: id, permalink: permalink, domain: domain }, community: exports.STACKEND_COMMUNITY }); } exports.getCommunity = getCommunity; /** * Validate a community permalink and get suggestions based on the permalink/name. * * @param permalink {String} * @param name {String} generate permalink suggestions based on this name (Optional) * */ function validateCommunityPermalink(_a) { var permalink = _a.permalink, name = _a.name; return (0, api_1.getJson)({ url: '/stackend/community/validate-permalink', parameters: { permalink: permalink, name: name }, community: exports.STACKEND_COMMUNITY }); } exports.validateCommunityPermalink = validateCommunityPermalink; /** * Create, but do not store a new community object that can be used to store * @param name * @param permalink */ function newCommunity(name, permalink) { return { id: 0, permalink: permalink, name: name, description: '', status: CommunityStatus.VISIBLE, locale: 'en_US', domains: [], logotypeId: 0, admins: [], moderators: [], theme: exports.Theme.STACKEND, openAIApiKey: '' }; } exports.newCommunity = newCommunity; /** * Edit/create a community. * * @param id {String} (optional, only when editing) * @param permalink {String} Required * @param name {String} Name * @param description {String} * @param status {CommunityStatus} * @param locale {String} Locale (default: en_US) * @param domains {string[]} List of valid domains * @param settings {String} Implementation specific settings data (typically JS) for front end. * @param logotypeId {number} Media id of logotype image * @param admins {number[]} List of admin user ids * @param moderators {number[]} List of moderator user ids * @param theme {String} Name of theme to use * @param style {String} Implementation specific style data (typically CSS) for front end. * @param defaultUserId * @param openAIApiKey */ function storeCommunity(_a) { var id = _a.id, permalink = _a.permalink, name = _a.name, description = _a.description, _b = _a.status, status = _b === void 0 ? CommunityStatus.VISIBLE : _b, _c = _a.locale, locale = _c === void 0 ? 'en_US' : _c, _d = _a.domains, domains = _d === void 0 ? [] : _d, settings = _a.settings, logotypeId = _a.logotypeId, _e = _a.admins, admins = _e === void 0 ? [] : _e, _f = _a.moderators, moderators = _f === void 0 ? [] : _f, theme = _a.theme, _g = _a.style, style = _g === void 0 ? undefined : _g, defaultUserId = _a.defaultUserId, openAIApiKey = _a.openAIApiKey; return (0, api_1.post)({ url: '/stackend/community/store', parameters: { id: id, permalink: permalink, name: name, description: description, status: status, locale: locale, domains: domains, logotypeId: logotypeId, admins: admins, moderators: moderators, theme: theme, style: style ? JSON.stringify(style) : '{}', settings: settings ? JSON.stringify(settings) : '{}', defaultUserId: defaultUserId, openAIApiKey: openAIApiKey }, community: exports.STACKEND_COMMUNITY }); } exports.storeCommunity = storeCommunity; /** * Set all community settings without affecting anything else. * * @param id {number} * @param settings {any} Settings */ function setCommunitySettings(_a) { var id = _a.id, settings = _a.settings; return (0, api_1.post)({ url: '/stackend/community/set-settings', parameters: { id: id, settings: JSON.stringify(settings) }, community: exports.STACKEND_COMMUNITY }); } exports.setCommunitySettings = setCommunitySettings; /** * Set a single community setting without affecting anything else. * * @param id {number} * @param name {string} * @param value {any} */ function setCommunitySetting(_a) { var id = _a.id, name = _a.name, value = _a.value; return (0, api_1.post)({ url: '/stackend/community/set-setting', parameters: { id: id, name: name, value: JSON.stringify(value) }, community: exports.STACKEND_COMMUNITY }); } exports.setCommunitySetting = setCommunitySetting; /** * Set a single community setting without affecting anything else. * * @param id {number} * @param theme StackendTheme */ function storeCommunityTheme(_a) { var id = _a.id, theme = _a.theme; return (0, api_1.post)({ url: '/stackend/community/store-theme', parameters: { id: id, theme: typeof theme === 'string' ? theme : JSON.stringify(theme) }, community: exports.STACKEND_COMMUNITY }); } exports.storeCommunityTheme = storeCommunityTheme; /** * Get the community's private settings that are not exposed to the frontend * @param key Get a specific setting * @param prefix Get settings with a specific prefix ("" for all) * @param community */ function getCommunityPrivateSettings(_a) { var key = _a.key, prefix = _a.prefix, community = _a.community; return (0, api_1.getJson)({ url: '/stackend/community/private/get-settings', parameters: { key: key, prefix: prefix }, community: community }); } exports.getCommunityPrivateSettings = getCommunityPrivateSettings; /** * Store the communitys private settings that are not exposed to the frontend. * Store a single key/value or a set of values. * @param key * @param value * @param values * @param community * @returns {Thunk<XcapJsonResult>} */ function storeCommunityPrivateSettings(_a) { var key = _a.key, value = _a.value, values = _a.values, community = _a.community; var x = { key: key, values: values ? JSON.stringify(values) : null }; return (0, api_1.post)({ url: '/stackend/community/private/store-settings', parameters: x, community: community }); } exports.storeCommunityPrivateSettings = storeCommunityPrivateSettings; /** * Set visible / hidden status of a community */ function setCommunityStatus(_a) { var id = _a.id, status = _a.status; return (0, api_1.post)({ url: '/stackend/community/set-status', parameters: { id: id, status: status }, community: exports.STACKEND_COMMUNITY }); } exports.setCommunityStatus = setCommunityStatus; /** * Remove a community. If new or empty, the data will also be removed. * You can force data to be removed by setting removeData. That requires back office access however. * * @param id {String} * @param removeData {boolean} Remove the data, even if the community is not empty. Requires back office access. */ function removeCommunity(_a) { var id = _a.id, removeData = _a.removeData; return (0, api_1.post)({ url: '/stackend/community/remove', parameters: { id: id, removeData: removeData }, community: exports.STACKEND_COMMUNITY }); } exports.removeCommunity = removeCommunity; /** * Search for a communities. * @param myCommunities {boolean} Search the current users communities only * @param creatorUserId {number} find communities created by this user only * @param status * @param q Search string * @param p * @param pageSize * @param orderBy * @param order */ function searchCommunity(_a) { var _b = _a.myCommunities, myCommunities = _b === void 0 ? true : _b, creatorUserId = _a.creatorUserId, _c = _a.status, status = _c === void 0 ? CommunityStatus.VISIBLE : _c, q = _a.q, _d = _a.p, p = _d === void 0 ? 1 : _d, pageSize = _a.pageSize, _e = _a.orderBy, orderBy = _e === void 0 ? OrderBy.NAME : _e, _f = _a.order, order = _f === void 0 ? Order_1.default.ASCENDING : _f; return (0, api_1.getJson)({ url: '/stackend/community/search', parameters: { myCommunities: myCommunities, creatorUserId: creatorUserId, status: status, q: q, p: p, pageSize: pageSize, orderBy: orderBy, order: order }, community: exports.STACKEND_COMMUNITY }); } exports.searchCommunity = searchCommunity; /** * Get the current user (with privileges from stackend rather than the current community) */ function getCurrentStackendUser() { return (0, api_1.getJson)({ url: '/user/get', community: exports.STACKEND_COMMUNITY }); } exports.getCurrentStackendUser = getCurrentStackendUser; /** * In a list of communities, find the one that matches the permalink * @return {Community} may return null, */ function getCurrentCommunity(communities) { return function (dispatch) { if (typeof communities === 'undefined' || communities === null || communities.length === 0) { return null; } var currentCommunityPermalink = dispatch((0, api_1.getCurrentCommunityPermalink)()); if (currentCommunityPermalink === null) { // FIXME: Fall back to url like _getCurrentCommunity return null; } for (var i = 0; i < communities.length; i++) { var community = communities[i]; if (community.permalink === currentCommunityPermalink) { return community; } } return null; }; } exports.getCurrentCommunity = getCurrentCommunity; /** * Check if the potential community url permalink is blocked. * @param communityUrl * @returns {boolean} */ function isCommunityUrlBlocked(communityUrl) { var blockedUrls = { create: true, 'my-settings': true, contact: true, stacks: true, user: true, register: true, billing: true, oauth2: true, google: true, facebook: true, shopify: true, 'shopify-app': true, healthcheck: true }; // Remove inital / var u = communityUrl; if (u.startsWith('/')) { u = u.substring(1); } // Remove extra path var i = u.indexOf('/'); if (i !== -1) { u = u.substring(0, i); } var t = blockedUrls[u]; return typeof t === 'undefined' ? false : t; } exports.isCommunityUrlBlocked = isCommunityUrlBlocked; /** * From the request url try to get the communityPermalink * @return {Community} may return null, */ function _getCurrentCommunityPermalinkFromUrl(request) { var p = request.location.pathname; // Needs to work with: // - /XXX // - /stacks/XXX // - /contextPath/XXX // - /contextPath/stacks/XXX var re = new RegExp('^' + request.contextPath + '(:?/stacks)?/([^/]+).*'); var r = re.exec(p); if (!r) { return null; } var currentCommunityPermalink = r[2]; if (isCommunityUrlBlocked(currentCommunityPermalink)) { return null; } // FIXME: Fall back to domain return currentCommunityPermalink; } exports._getCurrentCommunityPermalinkFromUrl = _getCurrentCommunityPermalinkFromUrl; /** * In a list of communities, find the one that matches the permalink * @return {Community} may return null, */ function _getCurrentCommunity(communities, request) { if (typeof communities === 'undefined' || communities === null || communities.length === 0) { return null; } var currentCommunityPermalink = _getCurrentCommunityPermalinkFromUrl(request); if (currentCommunityPermalink === null) { return null; } for (var i = 0; i < communities.length; i++) { var community = communities[i]; if (community.permalink === currentCommunityPermalink) { return community; } } return null; } exports._getCurrentCommunity = _getCurrentCommunity; /** * Check if the user is a community moderator, but not admin * @param community * @param userId * @returns {boolean} */ function isCommunityModerator(community, userId) { if (!(community && community.id)) { return false; } if (!userId) { return false; } if (typeof community.moderatorUserIds !== 'undefined' && community.moderatorUserIds.includes(userId)) { return true; } return false; } exports.isCommunityModerator = isCommunityModerator; /** * Check if a user is admin * @param community * @param userId * @returns {boolean} */ function isCommunityAdmin(community, userId) { if (typeof community === 'undefined' || community === null || !userId) { return false; } if (userId === community.creatorUserId) { return true; } return typeof community.adminUserIds !== 'undefined' && community.adminUserIds.includes(userId); } exports.isCommunityAdmin = isCommunityAdmin; /** * Check if the user has stackend admin access (any community/stack). * @param currentUser * @returns {boolean} */ function hasStackendAdminAccess(currentUser) { return (0, user_1.hasElevatedPrivilege)(currentUser, exports.COMMUNITY_MANAGER_CONTEXT, exports.COMPONENT_CLASS, privileges_1.PrivilegeTypeId.ADMIN); } exports.hasStackendAdminAccess = hasStackendAdminAccess; /** * Check if there is any way a user may see the current community * @param community * @param currentUser */ function hasCommunityAdminOrModeratorAccess(community, currentUser) { if (!(community && community.id)) { return false; } if (!(currentUser && currentUser.user)) { return false; } var userId = currentUser.user.id; if (userId === community.creatorUserId) { return true; } if (typeof community.adminUserIds !== 'undefined' && community.adminUserIds.includes(userId)) { return true; } if (typeof community.moderatorUserIds !== 'undefined' && community.moderatorUserIds.includes(userId)) { return true; } return hasStackendAdminAccess(currentUser); } exports.hasCommunityAdminOrModeratorAccess = hasCommunityAdminOrModeratorAccess; /** * Check if the current user has community admin access * @param community * @param currentUser * @returns {boolean} */ function hasCommunityAdminAccess(community, currentUser) { if (!(currentUser && currentUser.user)) { return false; } if (hasStackendAdminAccess(currentUser)) { return true; } return isCommunityAdmin(community, currentUser.user.id); } exports.hasCommunityAdminAccess = hasCommunityAdminAccess; /** * Check if the user has stackend access and may create new stacks. * @param currentUser * @returns {boolean} */ function hasStackendCreateAccess(currentUser) { return (0, user_1.hasElevatedPrivilege)(currentUser, exports.COMMUNITY_MANAGER_CONTEXT, exports.COMPONENT_CLASS, privileges_1.PrivilegeTypeId.TRUSTED); } exports.hasStackendCreateAccess = hasStackendCreateAccess; /** * Get the modules of a community. * * @param communityId * @returns {Promise} */ function getModules(_a) { var communityId = _a.communityId; return (0, api_1.getJson)({ url: '/stackend/module/list', parameters: { communityId: communityId, pageSize: 1000 }, community: exports.STACKEND_COMMUNITY }); } exports.getModules = getModules; /** * Get a module of a community. * * @param communityId * @param moduleId * @returns {Thunk<GetModuleResult>} */ function getModule(_a) { var communityId = _a.communityId, moduleId = _a.moduleId; return (0, api_1.getJson)({ url: '/stackend/module/get', parameters: { communityId: communityId, moduleId: moduleId }, community: exports.STACKEND_COMMUNITY }); } exports.getModule = getModule; /** * Get a singleton module given it's component class * @param communityId * @param componentClass * @param componentContext * @returns {Thunk<GetModuleResult>} */ function getSingletonModule(_a) { var communityId = _a.communityId, componentClass = _a.componentClass, componentContext = _a.componentContext; return (0, api_1.getJson)({ url: '/stackend/module/get-singleton', parameters: { communityId: communityId, singletonComponentClass: componentClass, singletonComponentContext: componentContext }, community: exports.STACKEND_COMMUNITY }); } exports.getSingletonModule = getSingletonModule; /** * Construct the object that can be passed to storeModule() * @param communityId * @param componentClass * @param componentContext * @param name */ function newModule(_a) { var communityId = _a.communityId, componentClass = _a.componentClass, componentContext = _a.componentContext, name = _a.name; if (!communityId) { throw Error('communityId required'); } if (!componentClass) { throw Error('componentClass required'); } if (!componentContext) { throw Error('componentContext required'); } return { id: 0, communityId: communityId, name: name, enabled: true, componentClass: componentClass, componentContext: componentContext, ruleTypeId: 0, settings: {}, style: {}, extraData: {} }; } exports.newModule = newModule; /** * Store a module of a community. * * When creating a new module, this will also set up the module data, for example the blog. * * @param id * @param communityId * @param name * @param enabled * @param componentClass * @param componentContext * @param ruleTypeId * @param settings Implementation specific (js) string (Max 64KB) * @param style Implementation specific (css) string (Max 64KB) * @param extraData Component specific extra JSON data. * * Supported optional extra data: * <dl> * <dt>permalink</dt> * <dt>description</dt> * <dt>forumAnonymity</dt><dd>ForumAnonymityLevel</dd> * <dt>groupVisibile</dt><dd>true/false</dd> * <dt>groupContentVisibile</dt><dd>true/false</dd> * <dt>body<dt> * <dt>teaser</dt> * <dl> * @returns {Promise} */ function storeModule(_a) { var _this = this; var id = _a.id, communityId = _a.communityId, name = _a.name, enabled = _a.enabled, componentClass = _a.componentClass, componentContext = _a.componentContext, ruleTypeId = _a.ruleTypeId, settings = _a.settings, style = _a.style, extraData = _a.extraData; return function (dispatch) { return __awaiter(_this, void 0, void 0, function () { var module; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, dispatch((0, api_1.post)({ url: '/stackend/module/store', parameters: { id: id, communityId: communityId, name: name, enabled: enabled, componentClass: componentClass, componentContext: componentContext, ruleTypeId: ruleTypeId, settings: settings ? JSON.stringify(settings) : '{}', style: style ? JSON.stringify(style) : '{}', extraData: extraData ? JSON.stringify(extraData) : '{}' }, community: exports.STACKEND_COMMUNITY }))]; case 1: module = _a.sent(); dispatch((0, moduleAction_1.fetchModules)({ communityId: communityId })); // FIXME: Update state without re-fetch return [2 /*return*/, module]; } }); }); }; } exports.storeModule = storeModule; /** * Store rules for a module * @param moduleId * @param communityId * @param rule * @param commentRule * @param trustedUsers */ function storeModuleRules(_a) { var communityId = _a.communityId, moduleId = _a.moduleId, rule = _a.rule, commentRule = _a.commentRule, trustedUsers = _a.trustedUsers; var r = { createPrivilege: rule.createPrivilege, moderationStatus: rule.moderationStatus, contentFiltering: rule.contentFiltering, postModerationTtlMinutes: rule.postModerationTtlMinutes }; var cr = null; if (commentRule) { cr = { createPrivilege: commentRule.createPrivilege, moderationStatus: commentRule.moderationStatus, contentFiltering: commentRule.contentFiltering, postModerationTtlMinutes: commentRule.postModerationTtlMinutes }; } return (0, api_1.post)({ url: '/stackend/module/rules/store', parameters: { communityId: communityId, moduleId: moduleId, trustedUsers: trustedUsers, rule: JSON.stringify(r), commentRule: JSON.stringify(cr) }, community: exports.STACKEND_COMMUNITY }); } exports.storeModuleRules = storeModuleRules; /** * Remove a module. * * @param id * @param communityId */ function removeModule(_a) { var id = _a.id, communityId = _a.communityId; return (0, api_1.post)({ url: '/stackend/module/remove', parameters: { id: id, communityId: communityId }, community: exports.STACKEND_COMMUNITY }); } exports.removeModule = removeModule; /** * Detect modules by inspecting existing data. Developer tool. * @param communityId */ function detectModules(_a) { var communityId = _a.communityId; return (0, api_1.post)({ url: '/stackend/modules/update', parameters: { communityId: communityId }, community: exports.STACKEND_COMMUNITY }); } exports.detectModules = detectModules; /** * Translates component class names to human readable names. */ var COMPONENT_CLASS_TO_MODULE_NAME = { 'se.josh.xcap.comment.impl.CommentManagerImpl': 'Comments', 'se.josh.xcap.comment.CommentManager': 'Comments', 'net.josh.community.blog.BlogManager': 'Blog', 'net.josh.community.forum.impl.ForumManagerImpl': 'Forum', 'net.josh.community.forum.ForumManager': 'Forum', 'se.josh.xcap.cms.CmsManager': 'CMS', 'se.josh.xcap.cms.impl.CmsManagerImpl': 'CMS', 'se.josh.xcap.like.impl.LikeManagerImpl': 'Like', 'net.josh.community.group.GroupManager': 'Group', 'net.josh.community.category.CategoryManager': 'Page', 'com.stackend.live.LiveEventManager': 'Live Event' }; var MODULE_TYPE_TO_COMPONENT_CLASS = { comment: 'se.josh.xcap.comment.impl.CommentManagerImpl', blog: 'net.josh.community.blog.BlogManager', forum: 'net.josh.community.forum.impl.ForumManagerImpl', cms: 'se.josh.xcap.cms.impl.CmsManagerImpl', group: 'net.josh.community.group.GroupManager', page: 'net.josh.community.category.CategoryManager', live: 'com.stackend.live.LiveEventManager' }; /** * Get a human readable component name * @param componentClass class */ function getComponentLabel(componentClass) { var t = COMPONENT_CLASS_TO_MODULE_NAME[componentClass]; if (typeof t === 'undefined') { return 'Unknown'; } return t; } exports.getComponentLabel = getComponentLabel; /** * Get a component class * @param moduleType */ function getComponentClassFromModuleType(moduleType) { var t = MODULE_TYPE_TO_COMPONENT_CLASS[moduleType]; if (typeof t === 'undefined') { return 'Unknown'; } return t; } exports.getComponentClassFromModuleType = getComponentClassFromModuleType; /** * List users with stackend admin status. * Requires stackend admin status. * @param privilege {number} PrivilegeType */ function listAdminUsers(_a) { var privilege = _a.privilege; return (0, api_1.getJson)({ url: '/stackend/user/list-admins', parameters: { privilege: privilege }, community: exports.STACKEND_COMMUNITY }); } exports.listAdminUsers = listAdminUsers; /** * Grant/revoke stackend admin status for a user. * Requires stackend admin status. * Any privilegeType lower than PrivilegeType.VERIFIED will remove the grants. * @param userId {number} User id * @param privilege {number} PrivilegeType */ function setAdminStatus(_a) { var userId = _a.userId, privilege = _a.privilege; return (0, api_1.post)({ url: '/stackend/user/set-admin-status', parameters: { userId: userId, privilege: privilege }, community: exports.STACKEND_COMMUNITY }); } exports.setAdminStatus = setAdminStatus; /** * Make a user moderator or admin status from a community. Or remove that status * @param communityId * @param userId * @param communityPrivilegeType Privilege: ADMIN for admins, TRUSTED for moderators. All other privs will revoke the access. */ function setCommunityAccess(_a) { var communityId = _a.communityId, userId = _a.userId, privilegeType = _a.privilegeType; return (0, api_1.post)({ url: '/stackend/user/set-community-access', parameters: arguments, community: exports.STACKEND_COMMUNITY }); } exports.setCommunityAccess = setCommunityAccess; /** * Invite a user as administrator or moderator of this community. * The user may or may not already be registered. If not, an email is sent inviting the user to manage the community. * * @param email Users email * @param communityId Id of community * @param communityPrivilegeType Privilege. Supports PrivilegeType.ADMIN (admin) and PrivilegeType.TRUSTED (moderator) * @param message Optional welcome message */ function inviteUserToCommunity(_a) { var email = _a.email, communityId = _a.communityId, communityPrivilegeType = _a.communityPrivilegeType, message = _a.message; return (0, api_1.post)({ url: '/stackend/user/invite', parameters: arguments, community: exports.STACKEND_COMMUNITY }); } exports.inviteUserToCommunity = inviteUserToCommunity; /** * Get the domain (excluding www) and path of an url. * "http://www.josh.se/test" would return "josh.se/test" * "/test" would return "/test" * @param url * @returns {*|string} */ function getReferenceUrl(url) { var _a, _b; if (window !== undefined) { // Shopify specific for reference url normalization // @ts-ignore if (((_b = (_a = window === null || window === void 0 ? void 0 : window.Shopify) === null || _a === void 0 ? void 0 : _a.routes) === null || _b === void 0 ? void 0 : _b.root) !== undefined) { // @ts-ignore url = url.replace(window.Shopify.routes.root.slice(0, -1), ''); } } var r = /(?:https|http)?(?::\/\/)?(?:www\.)?([^?#]*)/.exec(url); return r ? r[1] : url; } exports.getReferenceUrl = getReferenceUrl; /** * Get the url to a stackend community and module * @param request * @param community * @param module * @param path * @returns {string} */ function getStackendUrl(_a) { var request = _a.request, community = _a.community, module = _a.module, path = _a.path; var s = request.contextPath; if (community) { s += '/stacks/' + community.permalink; if (module) { s += '/module/' + module.id; } } if (path) { if (!s.endsWith('/') && !path.startsWith('/')) { s += '/'; } s += path; } return s; } exports.getStackendUrl = getStackendUrl; /** * Remove a user. Requires stackend admin status. Fails if the user has communities. * @param userId * @returns {Thunk<XcapJsonResult>} */ function removeUser(_a) { var userId = _a.userId; return (0, api_1.post)({ url: '/stackend/user/remove', parameters: arguments }); } exports.removeUser = removeUser; //# sourceMappingURL=index.js.map