@smontero/ppp-client-api
Version:
Project People & Profile client api
928 lines (809 loc) • 31.9 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _ = _interopRequireDefault(require(".."));
var _awsAmplify = require("aws-amplify");
var _lite = _interopRequireDefault(require("mime/lite"));
var _BaseApi2 = _interopRequireDefault(require("./BaseApi"));
var _RequestApi = _interopRequireDefault(require("./RequestApi"));
var _util = require("../util");
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
/**
* @desc Enables the interaction with the profile and comms services. When creating the object
* the activeUser has to be passed into the constructor
* @see BaseApi
*/
var ProfileApi =
/*#__PURE__*/
function (_BaseApi) {
(0, _inherits2["default"])(ProfileApi, _BaseApi);
function ProfileApi() {
(0, _classCallCheck2["default"])(this, ProfileApi);
return (0, _possibleConstructorReturn2["default"])(this, (0, _getPrototypeOf2["default"])(ProfileApi).apply(this, arguments));
}
(0, _createClass2["default"])(ProfileApi, [{
key: "init",
value: function init() {
var _this = this;
_["default"].events.on('activeUserChanged', function (activeUser) {
return _this.setActiveUser(activeUser);
});
}
}, {
key: "_request",
value: function () {
var _request2 = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee(_ref) {
var endpoint, payload;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
endpoint = _ref.endpoint, payload = _ref.payload;
return _context.abrupt("return", _RequestApi["default"].post({
endpoint: endpoint,
body: _objectSpread({}, payload, {
originAppId: _["default"].originAppId
}),
apiName: 'profileApi'
}));
case 2:
case "end":
return _context.stop();
}
}
}, _callee);
}));
function _request(_x) {
return _request2.apply(this, arguments);
}
return _request;
}()
/**
* @desc Register a new user or edit users data. When registering a new user at least
* one of the emailAddress or smsNumber must be sent. When the emailAddress or smsNumber is
* modified, a verification code will be sent to the changed medium, and the medium must be
* verified
*
* @param {Object} data
* @param {String} data.emailAddress, @see ppp-common/RootFields
* @param {String} data.smsNumber @see ppp-common/RootFields it must be full number including country code example
* +525583686747,
* @param {String} data.commPref @see ppp-common/RootFields @see ppp-common/CommMethods possible values SMS or EMAIL, preferred comunication medium
* @param {String} data.publicData.name @see ppp-common/PublicFields
* @param {String} data.publicData.timeZone, @see ppp-common/PublicFields
* @param {String} data.publicData.avatarImage file name key @see ppp-common/PublicFields
* @param {String} data.publicData.identity aws user identityId @see ppp-common/PublicFields
* @param {String} data.publicData.tags aws user identityId @see ppp-common/PublicFields
* @param {String} data.publicData.bio aws user identityId @see ppp-common/PublicFields
* @param {Object} data.publicData More fields can be added to the common public data
* @param {Object} data.privateData Common fields that only the owner should see
* @param {Object} data.appData.publicData App specific public fields
* @param {Object} data.appData.privateData App specific fields that only the owner should see
*
*
* @return {Object} object indicating the succesful call, it contains a profile property
* with the new profile data this object doesn't have to be analyzed, it can be assumed
* that if the method does not throw an error the call was succesful
* @throws error on an unsuccesful call to the service
*/
}, {
key: "register",
value: function () {
var _register = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee2(data) {
var _ref2, profile;
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return this._request({
endpoint: 'register',
payload: data
});
case 2:
_ref2 = _context2.sent;
profile = _ref2.profile;
return _context2.abrupt("return", profile);
case 5:
case "end":
return _context2.stop();
}
}
}, _callee2, this);
}));
function register(_x2) {
return _register.apply(this, arguments);
}
return register;
}()
/**
* @desc Uploads an image associated to the current users account
*
* @param {Object} file the image to upload
*
* @return {string} the name of the image in the store
* @throws error on an unsuccesful call to the service
*/
}, {
key: "uploadImage",
value: function () {
var _uploadImage = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee3(file) {
var toMB, MAX_SIZE, types, accountName, filename, stored;
return _regenerator["default"].wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
toMB = function toMB(bytes) {
return bytes / 1000000;
};
MAX_SIZE = Number(_["default"].getConfig('maxImageSize'));
types = _["default"].getConfig('imageTypes');
if (!(types.indexOf(file.type) < 0)) {
_context3.next = 5;
break;
}
throw new Error("File type is: ".concat(file.type, ". Allowed types are: ").concat(types));
case 5:
if (!(file.size > MAX_SIZE)) {
_context3.next = 7;
break;
}
throw new Error("File size is: ".concat(toMB(file.size), " MB. Max allowed size is: ").concat(toMB(MAX_SIZE), " MB"));
case 7:
_context3.next = 9;
return this.getAccountName();
case 9:
accountName = _context3.sent;
filename = "".concat(accountName, "-").concat(Date.now(), ".").concat(_lite["default"].getExtension(file.type));
_context3.next = 13;
return _awsAmplify.Storage.put(filename, file, {
level: 'protected',
contentType: file.type
});
case 13:
stored = _context3.sent;
return _context3.abrupt("return", stored.key);
case 15:
case "end":
return _context3.stop();
}
}
}, _callee3, this);
}));
function uploadImage(_x3) {
return _uploadImage.apply(this, arguments);
}
return uploadImage;
}()
/**
* @desc Returns the url to the image file
*
* @param {string} key the name of the file in the store, returned by the upload image method
* @param {string} identity id of the user account that owns the image
*
* @return {string} the name of the image in the store
* @throws error on an unsuccesful call to the service
*/
}, {
key: "getImageUrl",
value: function () {
var _getImageUrl = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee4(key, identity) {
return _regenerator["default"].wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
return _context4.abrupt("return", _awsAmplify.Storage.get(key, {
level: 'protected',
identityId: identity
}));
case 1:
case "end":
return _context4.stop();
}
}
}, _callee4);
}));
function getImageUrl(_x4, _x5) {
return _getImageUrl.apply(this, arguments);
}
return getImageUrl;
}()
/**
* @desc Retrieves registered user data
* @param {String} [fetchType] @see ppp-common/ProfileFetchTypes indicates the wanted data base only,app only or both
* @return {object} with the user data or null if user is not registered
*/
}, {
key: "getProfile",
value: function () {
var _getProfile = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee5() {
var fetchType,
_ref3,
profile,
_args5 = arguments;
return _regenerator["default"].wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
fetchType = _args5.length > 0 && _args5[0] !== undefined ? _args5[0] : null;
_context5.next = 3;
return this._request({
endpoint: 'get-profile',
payload: {
fetchType: fetchType
}
});
case 3:
_ref3 = _context5.sent;
profile = _ref3.profile;
return _context5.abrupt("return", profile);
case 6:
case "end":
return _context5.stop();
}
}
}, _callee5, this);
}));
function getProfile() {
return _getProfile.apply(this, arguments);
}
return getProfile;
}()
/**
* @desc Retrieves public profiles for one or more eosAccounts
*
* @param {Array} of strings indicating the eosAccounts for which data wants to be retrieved
* @param {String} [fetchType] @see ppp-common/ProfileFetchTypes indicates the wanted data base only,app only or both
* @return {object} mapping eosAccount to their profile data of the form {eosAccount: profile}
*/
}, {
key: "getProfiles",
value: function () {
var _getProfiles = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee6(eosAccounts) {
var fetchType,
_ref4,
profiles,
_args6 = arguments;
return _regenerator["default"].wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
fetchType = _args6.length > 1 && _args6[1] !== undefined ? _args6[1] : null;
eosAccounts = _util.Util.removeDuplicates(eosAccounts);
_context6.next = 4;
return this._request({
endpoint: 'get-profiles',
payload: {
eosAccounts: eosAccounts,
fetchType: fetchType
}
});
case 4:
_ref4 = _context6.sent;
profiles = _ref4.profiles;
return _context6.abrupt("return", profiles);
case 7:
case "end":
return _context6.stop();
}
}
}, _callee6, this);
}));
function getProfiles(_x6) {
return _getProfiles.apply(this, arguments);
}
return getProfiles;
}()
}, {
key: "hydrateWithUser",
value: function () {
var _hydrateWithUser = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee8(objs) {
var _this2 = this;
var accountProp,
hydratedProp,
_args8 = arguments;
return _regenerator["default"].wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
accountProp = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : 'eosAccount';
hydratedProp = _args8.length > 2 && _args8[2] !== undefined ? _args8[2] : null;
return _context8.abrupt("return", _util.Util.hydrate(objs, accountProp, hydratedProp || "".concat(accountProp, "Info"),
/*#__PURE__*/
function () {
var _ref5 = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee7(eosAccounts) {
return _regenerator["default"].wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
return _context7.abrupt("return", _this2.getAppData(eosAccounts));
case 1:
case "end":
return _context7.stop();
}
}
}, _callee7);
}));
return function (_x8) {
return _ref5.apply(this, arguments);
};
}()));
case 3:
case "end":
return _context8.stop();
}
}
}, _callee8);
}));
function hydrateWithUser(_x7) {
return _hydrateWithUser.apply(this, arguments);
}
return hydrateWithUser;
}()
/**
* @desc Send a message to a user
*
* @param {String} eosAccount of the user who the message is going to be sent to
* @param {String} message to send
* @return object indicating the succesful call, this object doesn't have to be
* analyzed, it can be assumed that if the method does not throw an error
* the call was succesful
* @throws error on an unsuccesful call to the service
*/
}, {
key: "sendMessage",
value: function () {
var _sendMessage = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee9(eosAccount, message) {
return _regenerator["default"].wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
return _context9.abrupt("return", this._request({
endpoint: 'send-msg',
payload: {
eosAccount: eosAccount,
message: message
}
}));
case 1:
case "end":
return _context9.stop();
}
}
}, _callee9, this);
}));
function sendMessage(_x9, _x10) {
return _sendMessage.apply(this, arguments);
}
return sendMessage;
}()
/**
* @desc Verify an sms number
*
* @param {String} smsOtp verification code
* @return object indicating the succesful call, this object doesn't have to be
* analyzed, it can be assumed that if the method does not throw an error
* the call was succesful
* @throws error on an unsuccesful call to the service
*/
}, {
key: "verifySms",
value: function () {
var _verifySms = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee10(smsOtp) {
return _regenerator["default"].wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
return _context10.abrupt("return", this._request({
endpoint: 'verify-sms',
payload: {
smsOtp: smsOtp
}
}));
case 1:
case "end":
return _context10.stop();
}
}
}, _callee10, this);
}));
function verifySms(_x11) {
return _verifySms.apply(this, arguments);
}
return verifySms;
}()
/**
* @desc Verify an email
*
* @param {String} emailOtp verification code
* @return object indicating the succesful call, this object doesn't have to be
* analyzed, it can be assumed that if the method does not throw an error
* the call was succesful
* @throws error on an unsuccesful call to the service
*/
}, {
key: "verifyEmail",
value: function () {
var _verifyEmail = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee11(emailOtp) {
return _regenerator["default"].wrap(function _callee11$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
return _context11.abrupt("return", this._request({
endpoint: 'verify-email',
payload: {
emailOtp: emailOtp
}
}));
case 1:
case "end":
return _context11.stop();
}
}
}, _callee11, this);
}));
function verifyEmail(_x12) {
return _verifyEmail.apply(this, arguments);
}
return verifyEmail;
}()
/**
* @desc Retrieves the chats a specific user is involved in, returns from the most recent
* chat to the least recent, it can also search for a chat with specific counter party
*
* @param {String} [search] start part of the counter party eos account
* @param {Number} [limit] the number of items to retrieve
* @param {Object} [lastEvaluatedKey] used when paginating, this is the value that was returned
* on the response on the previous call
* @return {Object} with the following properties: items, count, lastEvaluatedKey.
* lastEvaluatedKey is returned
* only if there are more results to be retrieved
* @throws error on an unsuccesful call to the service
*/
}, {
key: "getChats",
value: function () {
var _getChats = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee12(search, limit, lastEvaluatedKey) {
var _ref6, chats;
return _regenerator["default"].wrap(function _callee12$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
_context12.next = 2;
return this._request({
endpoint: 'get-chats',
payload: {
search: search,
limit: limit,
lastEvaluatedKey: lastEvaluatedKey
}
});
case 2:
_ref6 = _context12.sent;
chats = _ref6.chats;
chats.items = chats.items.map(function (_item) {
var item = _item;
item.sentAt = new Date(item.sentAt);
return item;
});
return _context12.abrupt("return", chats);
case 6:
case "end":
return _context12.stop();
}
}
}, _callee12, this);
}));
function getChats(_x13, _x14, _x15) {
return _getChats.apply(this, arguments);
}
return getChats;
}()
/**
* @desc Retrieves the messages between the current user and the user specified in the eosAccount parameter,
* returns from the most recent message to the least recent
*
* @param {String} eosAccount the eosAccount of the messaging counter party
* @param {Number} [limit] the number of items to retrieve
* @param {Object} [lastEvaluatedKey] used when paginating, this is the value that was returned
* on the response on the previous call
* @return {Object} with the following properties: items, count, lastEvaluatedKey.
* lastEvaluatedKey is returned
* only if there are more results to be retrieved
* @throws error on an unsuccesful call to the service
*/
}, {
key: "getMessages",
value: function () {
var _getMessages = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee13(eosAccount, limit, lastEvaluatedKey) {
var _ref7, messages;
return _regenerator["default"].wrap(function _callee13$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
_context13.next = 2;
return this._request({
endpoint: 'get-messages',
payload: {
eosAccount2: eosAccount,
limit: limit,
lastEvaluatedKey: lastEvaluatedKey
}
});
case 2:
_ref7 = _context13.sent;
messages = _ref7.messages;
messages.items = messages.items.map(function (_item) {
var item = _item;
item.sentAt = new Date(item.sentAt);
return item;
});
return _context13.abrupt("return", messages);
case 6:
case "end":
return _context13.stop();
}
}
}, _callee13, this);
}));
function getMessages(_x16, _x17, _x18) {
return _getMessages.apply(this, arguments);
}
return getMessages;
}()
/**
* @desc Enables the search of profiles using their eosAccount
* @param {String} search start part of the eos account
* @param {Number} [limit] the number of items to retrieve
* @param {Object} [lastEvaluatedKey] used when paginating, this is the value that was returned
* on the response on the previous call
* @return {Object} with the following properties: items, count, lastEvaluatedKey.
* lastEvaluatedKey is returned
* only if there are more results to be retrieved
* @throws error on an unsuccesful call to the service
*/
}, {
key: "searchProfiles",
value: function () {
var _searchProfiles = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee14(search, limit, lastEvaluatedKey) {
var _ref8, profiles;
return _regenerator["default"].wrap(function _callee14$(_context14) {
while (1) {
switch (_context14.prev = _context14.next) {
case 0:
_context14.next = 2;
return this._request({
endpoint: 'search-profiles',
payload: {
search: search,
limit: limit,
lastEvaluatedKey: lastEvaluatedKey
}
});
case 2:
_ref8 = _context14.sent;
profiles = _ref8.profiles;
return _context14.abrupt("return", profiles);
case 5:
case "end":
return _context14.stop();
}
}
}, _callee14, this);
}));
function searchProfiles(_x19, _x20, _x21) {
return _searchProfiles.apply(this, arguments);
}
return searchProfiles;
}()
/**
* @desc Registers an app with the profile service, enabling it to interact with it
* @param {object} data
* @param {String} data.type app type @see ppp-common/AppTypes
* @param {String} data.isPrivate indicates whether the app can keep the client secret private or not,
* apps with backend server are private
* @param {String} [data.appId] used for updates, this is the id associated with the app returned
* by this service
* @param {String} [data.baseUrl] the url where the app is hosted, and from which the app-metadata.json
* and chain-manifests.json files can be downloaded, required for web apps
* @param {String} [data.name] app name required for non web apps
* @param {String} [data.shortname] app shortname required for non web apps
* @param {String} [data.icon] app icon url required for non web apps
* @param {String} [data.oauthRedirectUrls] an array of urls, that app intends to use as oauth redirect urls
* @return {Object} object indicating the succesful call, it contains an app property
* with the new app data this object doesn't have to be analyzed, it can be assumed
* that if the method does not throw an error the call was succesful
* @throws error on an unsuccesful call to the service
*/
}, {
key: "registerApp",
value: function () {
var _registerApp = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee15(data) {
var _ref9, app;
return _regenerator["default"].wrap(function _callee15$(_context15) {
while (1) {
switch (_context15.prev = _context15.next) {
case 0:
_context15.next = 2;
return this._request({
endpoint: 'register-app',
payload: _objectSpread({}, data)
});
case 2:
_ref9 = _context15.sent;
app = _ref9.app;
return _context15.abrupt("return", app);
case 5:
case "end":
return _context15.stop();
}
}
}, _callee15, this);
}));
function registerApp(_x22) {
return _registerApp.apply(this, arguments);
}
return registerApp;
}()
/**
* @desc Deletes an app
*
* @param {String} appId to delete
* @return {object} with success message
* @throws error on an unsuccesful call to the service
*/
}, {
key: "deleteApp",
value: function () {
var _deleteApp = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee16(appId) {
return _regenerator["default"].wrap(function _callee16$(_context16) {
while (1) {
switch (_context16.prev = _context16.next) {
case 0:
_context16.next = 2;
return this._request({
endpoint: 'delete-app',
payload: {
appId: appId
}
});
case 2:
return _context16.abrupt("return", _context16.sent);
case 3:
case "end":
return _context16.stop();
}
}
}, _callee16, this);
}));
function deleteApp(_x23) {
return _deleteApp.apply(this, arguments);
}
return deleteApp;
}()
/**
* @desc Retrieves public apps by one or more appIds
*
* @param {Array} of strings indicating the appIds for which data wants to be retrieved
* @return {object} mapping appId to their app data of the form {appId: app}
*/
}, {
key: "getApps",
value: function () {
var _getApps = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee17(appIds) {
var _ref10, apps;
return _regenerator["default"].wrap(function _callee17$(_context17) {
while (1) {
switch (_context17.prev = _context17.next) {
case 0:
appIds = _util.Util.removeDuplicates(appIds);
_context17.next = 3;
return this._request({
endpoint: 'get-apps',
payload: {
appIds: appIds
}
});
case 3:
_ref10 = _context17.sent;
apps = _ref10.apps;
return _context17.abrupt("return", apps);
case 6:
case "end":
return _context17.stop();
}
}
}, _callee17, this);
}));
function getApps(_x24) {
return _getApps.apply(this, arguments);
}
return getApps;
}()
/**
* @desc Retrieves the apps associated to the logged in account
*
* @return {Array} of apps
*/
}, {
key: "getMyApps",
value: function () {
var _getMyApps = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee18() {
var _ref11, apps;
return _regenerator["default"].wrap(function _callee18$(_context18) {
while (1) {
switch (_context18.prev = _context18.next) {
case 0:
_context18.next = 2;
return this._request({
endpoint: 'get-my-apps'
});
case 2:
_ref11 = _context18.sent;
apps = _ref11.apps;
return _context18.abrupt("return", apps);
case 5:
case "end":
return _context18.stop();
}
}
}, _callee18, this);
}));
function getMyApps() {
return _getMyApps.apply(this, arguments);
}
return getMyApps;
}()
}]);
return ProfileApi;
}(_BaseApi2["default"]);
var _default = ProfileApi;
exports["default"] = _default;