slate/common/actions.js

539 lines
14 KiB
JavaScript
Raw Normal View History

2020-06-19 06:57:57 +03:00
import "isomorphic-fetch";
2021-07-07 23:50:57 +03:00
import microlink from "@microlink/mql";
2020-02-19 09:30:47 +03:00
import * as Logging from "~/common/logging";
2021-07-07 23:50:57 +03:00
import * as Events from "~/common/custom-events";
import * as Websockets from "~/common/browser-websockets";
2020-06-19 06:57:57 +03:00
import * as Strings from "~/common/strings";
2021-07-07 23:50:57 +03:00
import * as Credentials from "~/common/credentials";
2020-02-19 09:30:47 +03:00
2021-01-14 09:30:26 +03:00
//NOTE(martina): call Websockets.checkWebsocket() before any api call that uses websockets to return updates
// to make sure that websockets are properly connected (and to reconnect them if they are not)
// otherwise updates may not occur properly
2021-07-07 23:50:57 +03:00
//NOTE(martina): if the server is the slate backend, you should set credentials: "include". If it is cross origin (aka a call to shovel or lens), you will not be able to use credentials
// Instead, if a cross origin server requires authorization, pass an API key with Authorization: getCookie(Credentials.session.key)
const getCookie = (name) => {
var match = document.cookie.match(new RegExp("(^| )" + name + "=([^;]+)"));
if (match) return match[2];
};
2020-02-19 09:30:47 +03:00
const REQUEST_HEADERS = {
2020-06-19 06:57:57 +03:00
Accept: "application/json",
"Content-Type": "application/json",
2020-02-19 09:30:47 +03:00
};
2021-07-07 23:50:57 +03:00
//NOTE(martina): used for calls to the server
2020-08-07 05:17:49 +03:00
const DEFAULT_OPTIONS = {
method: "POST",
headers: REQUEST_HEADERS,
credentials: "include",
};
2021-07-07 23:50:57 +03:00
//NOTE(martina): used for calls to other servers (where sending credentials isn't allowed b/c it's cross origin) which also don't require API keys
2020-10-31 02:12:20 +03:00
const CORS_OPTIONS = {
method: "POST",
headers: REQUEST_HEADERS,
credentials: "omit",
};
2020-08-07 05:17:49 +03:00
const returnJSON = async (route, options) => {
2021-07-07 23:50:57 +03:00
try {
const response = await fetch(route, options);
const json = await response.json();
2020-08-07 05:17:49 +03:00
2021-07-07 23:50:57 +03:00
return json;
} catch (e) {
Logging.error(e);
2021-07-07 23:50:57 +03:00
}
2020-08-07 05:17:49 +03:00
};
2021-03-05 10:32:08 +03:00
export const createZipToken = async ({ files, resourceURI }) => {
return await returnJSON(`${resourceURI}/api/download/create-zip-token`, {
2021-03-05 11:56:42 +03:00
...CORS_OPTIONS,
2021-03-02 16:33:10 +03:00
body: JSON.stringify({ files }),
});
2021-03-05 10:32:08 +03:00
};
2021-03-02 16:33:10 +03:00
2021-03-05 10:32:08 +03:00
export const downloadZip = ({ token, name, resourceURI }) =>
`${resourceURI}/api/download/download-by-token?downloadId=${token}&name=${name}`;
2021-03-02 16:33:10 +03:00
2020-08-19 21:17:12 +03:00
export const health = async (data = {}) => {
await Websockets.checkWebsocket();
return await returnJSON(`/api/_`, {
...DEFAULT_OPTIONS,
2020-08-19 21:17:12 +03:00
body: JSON.stringify({ data: { buckets: data.buckets } }),
});
};
export const sendFilecoin = async (data) => {
if (Strings.isEmpty(data.source)) {
return null;
}
if (Strings.isEmpty(data.target)) {
return null;
2020-02-19 09:30:47 +03:00
}
if (!data.amount) {
return null;
2020-02-19 09:30:47 +03:00
}
await Websockets.checkWebsocket();
2020-08-07 05:17:49 +03:00
return await returnJSON(`/api/addresses/send`, {
...DEFAULT_OPTIONS,
2020-08-27 07:24:49 +03:00
body: JSON.stringify({ data }),
});
};
2021-07-07 23:50:57 +03:00
// export const mql = async (url) => {
// try {
// const res = await microlink(url, { screenshot: true });
// return res;
// } catch (e) {
// console.log(e);
// if (e.description) {
// Events.dispatchMessage({ message: e.description });
// }
// }
// };
export const checkUsername = async (data) => {
return await returnJSON(`/api/users/check-username`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
2021-06-09 01:53:30 +03:00
export const checkEmail = async (data) => {
return await returnJSON(`/api/users/check-email`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
//NOTE(toast): this functionality comes with the upgraded sg plan
// export const validateEmail = async (data) => {
// return await returnJSON("/api/emails/validate", {
// ...DEFAULT_OPTIONS,
// body: JSON.stringify({ data }),
// });
// };
export const sendEmail = async (data) => {
return await returnJSON("/api/emails/send-email", {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
export const sendTemplateEmail = async (data) => {
return await returnJSON("/api/emails/send-template", {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
2020-09-23 14:17:56 +03:00
export const archive = async (data) => {
await Websockets.checkWebsocket();
return await returnJSON(`/api/data/archive`, {
...DEFAULT_OPTIONS,
2020-09-23 14:17:56 +03:00
body: JSON.stringify({ data }),
});
};
export const removeFromBucket = async (data) => {
await Websockets.checkWebsocket();
2020-09-23 14:17:56 +03:00
return await returnJSON(`/api/data/bucket-remove`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
2020-09-03 09:00:02 +03:00
export const getSlateById = async (data) => {
await Websockets.checkWebsocket();
2020-09-03 09:00:02 +03:00
return await returnJSON(`/api/slates/get`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
2020-08-07 05:17:49 +03:00
});
2020-02-19 09:30:47 +03:00
};
2020-11-17 10:12:35 +03:00
export const getSlatesByIds = async (data) => {
2021-01-24 09:20:57 +03:00
return await returnJSON(`/api/slates/get`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
export const getSocial = async (data) => {
2021-01-24 09:20:57 +03:00
return await returnJSON(`/api/users/get-social`, {
2020-11-17 10:12:35 +03:00
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
2020-12-14 19:51:14 +03:00
export const deleteTrustRelationship = async (data) => {
await Websockets.checkWebsocket();
2020-12-14 19:51:14 +03:00
return await returnJSON(`/api/users/trust-delete`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
export const updateTrustRelationship = async (data) => {
await Websockets.checkWebsocket();
2020-12-14 19:51:14 +03:00
return await returnJSON(`/api/users/trust-update`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
export const createTrustRelationship = async (data) => {
await Websockets.checkWebsocket();
2020-12-14 19:51:14 +03:00
return await returnJSON(`/api/users/trust`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
export const createSubscription = async (data) => {
await Websockets.checkWebsocket();
return await returnJSON(`/api/subscribe`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
export const search = async (data) => {
await Websockets.checkWebsocket();
2020-11-12 00:08:59 +03:00
if (Strings.isEmpty(data.query)) {
return { decorator: "NO_SERVER_TRIP", data: { results: [] } };
}
2020-11-16 11:18:02 +03:00
2020-11-12 00:08:59 +03:00
if (Strings.isEmpty(data.resourceURI)) {
return { decorator: "NO_RESOURCE_URI", data: { results: [] } };
}
2020-11-11 05:38:22 +03:00
return await returnJSON(`${data.resourceURI}/search`, {
2020-10-31 02:12:20 +03:00
...CORS_OPTIONS,
2020-11-10 00:20:38 +03:00
body: JSON.stringify({ data }),
});
};
export const createFile = async (data) => {
await Websockets.checkWebsocket();
return await returnJSON(`/api/data/create`, {
2020-10-05 00:30:28 +03:00
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
2020-10-05 00:30:28 +03:00
});
};
2021-07-07 23:50:57 +03:00
export const createLink = async (data) => {
await Websockets.checkWebsocket();
return await returnJSON(`/api/data/create-link`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
2020-09-23 23:52:00 +03:00
export const addFileToSlate = async (data) => {
await Websockets.checkWebsocket();
return await returnJSON(`/api/slates/add-file`, {
2020-09-23 23:52:00 +03:00
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
2020-09-23 23:52:00 +03:00
});
};
2021-06-09 01:53:30 +03:00
export const requestTwitterToken = async () => {
return await returnJSON(`/api/twitter/request-token`, {
...DEFAULT_OPTIONS,
});
};
export const authenticateViaTwitter = async (data) => {
return await returnJSON(`/api/twitter/authenticate`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
export const createUserViaTwitter = async (data) => {
return await returnJSON(`/api/twitter/signup`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
export const createUserViaTwitterWithVerification = async (data) => {
return await returnJSON(`/api/twitter/signup-with-verification`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
export const updateViewer = async (data) => {
await Websockets.checkWebsocket();
2020-08-07 05:17:49 +03:00
return await returnJSON(`/api/users/update`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
2020-08-07 05:17:49 +03:00
});
};
export const signIn = async (data) => {
2020-08-07 05:17:49 +03:00
return await returnJSON(`/api/sign-in`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
2020-08-07 05:17:49 +03:00
});
};
export const hydrateAuthenticatedUser = async () => {
await Websockets.checkWebsocket();
2020-08-07 05:17:49 +03:00
return await returnJSON(`/api/hydrate`, {
...DEFAULT_OPTIONS,
});
};
export const deleteViewer = async () => {
2020-08-07 05:17:49 +03:00
return await returnJSON(`/api/users/delete`, {
...DEFAULT_OPTIONS,
});
};
export const createUser = async (data) => {
2020-08-07 05:17:49 +03:00
return await returnJSON(`/api/users/create`, {
...DEFAULT_OPTIONS,
2020-10-26 00:11:27 +03:00
body: JSON.stringify({ data }),
});
};
2020-12-09 07:22:17 +03:00
export const updateStatus = async (data) => {
await Websockets.checkWebsocket();
2020-12-09 07:22:17 +03:00
return await returnJSON(`/api/users/status-update`, {
2020-10-26 00:11:27 +03:00
...DEFAULT_OPTIONS,
2020-11-10 00:20:38 +03:00
body: JSON.stringify({ data }),
});
};
export const updateSearch = async (data) => {
return await returnJSON(`/api/search/update`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
2020-08-07 05:17:49 +03:00
});
};
// export const checkCIDStatus = async (data) => {
// return await returnJSON(`/api/data/cid-status`, {
// ...DEFAULT_OPTIONS,
// body: JSON.stringify({ data }),
// });
// };
export const createSlate = async (data) => {
await Websockets.checkWebsocket();
2020-08-07 05:17:49 +03:00
return await returnJSON(`/api/slates/create`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
2020-08-07 05:17:49 +03:00
});
};
export const updateSlate = async (data) => {
await Websockets.checkWebsocket();
2020-08-07 05:17:49 +03:00
return await returnJSON(`/api/slates/update`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
2020-08-07 05:17:49 +03:00
});
};
export const updateSlateLayout = async (data) => {
await Websockets.checkWebsocket();
return await returnJSON(`/api/slates/update-layout`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
2020-08-07 05:17:49 +03:00
export const deleteSlate = async (data) => {
await Websockets.checkWebsocket();
2020-08-07 05:17:49 +03:00
return await returnJSON(`/api/slates/delete`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
export const removeFileFromSlate = async (data) => {
await Websockets.checkWebsocket();
return await returnJSON(`/api/slates/remove-file`, {
2020-08-07 05:17:49 +03:00
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
2020-08-07 05:17:49 +03:00
export const generateAPIKey = async () => {
await Websockets.checkWebsocket();
2020-08-07 05:17:49 +03:00
return await returnJSON(`/api/keys/generate`, {
...DEFAULT_OPTIONS,
});
};
export const deleteAPIKey = async (data) => {
await Websockets.checkWebsocket();
2020-08-07 05:17:49 +03:00
return await returnJSON(`/api/keys/delete`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
2020-08-07 05:17:49 +03:00
});
};
export const saveCopy = async (data) => {
await Websockets.checkWebsocket();
return await returnJSON(`/api/data/save-copy`, {
2020-10-05 00:30:28 +03:00
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
export const updateFile = async (data) => {
await Websockets.checkWebsocket();
2020-11-16 03:29:13 +03:00
return await returnJSON(`/api/data/update`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
2021-01-09 07:13:00 +03:00
});
};
export const deleteFiles = async (data) => {
await Websockets.checkWebsocket();
return await returnJSON(`/api/data/delete`, {
2020-09-18 06:40:10 +03:00
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
2020-09-03 06:26:56 +03:00
export const getSerializedSlate = async (data) => {
return await returnJSON(`/api/slates/get-serialized`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
export const getSerializedProfile = async (data) => {
return await returnJSON(`/api/users/get-serialized`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
2020-09-17 02:26:15 +03:00
export const createSupportMessage = async (data) => {
return await returnJSON(`/api/support-message`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
2020-12-04 02:34:19 +03:00
export const createDownloadActivity = async (data) => {
return await returnJSON(`/api/activity/create-download-activity`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
2021-01-14 09:30:26 +03:00
export const getActivity = async (data) => {
return await returnJSON(`/api/activity/get-activity`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
export const getExplore = async (data) => {
return await returnJSON(`/api/activity/get-explore`, {
2020-12-04 02:34:19 +03:00
...DEFAULT_OPTIONS,
2021-01-14 09:30:26 +03:00
body: JSON.stringify({ data }),
2020-12-04 02:34:19 +03:00
});
};
2020-12-25 02:44:47 +03:00
export const getZipFilePaths = async (data) => {
2021-03-04 09:31:11 +03:00
return await returnJSON(`/api/zip/get-paths`, {
2020-12-25 02:44:47 +03:00
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};
2021-01-21 04:07:40 +03:00
export const cleanDatabase = async () => {
2021-06-09 01:53:30 +03:00
return await returnJSON(`/api/clean-up/users`, {
2021-01-21 04:07:40 +03:00
...DEFAULT_OPTIONS,
});
};
2021-04-20 23:35:30 +03:00
2021-06-09 01:53:30 +03:00
export const createTwitterEmailVerification = async (data) => {
return await returnJSON(`/api/verifications/twitter/create`, {
...DEFAULT_OPTIONS,
2021-06-09 01:53:30 +03:00
body: JSON.stringify({ data }),
});
};
export const verifyTwitterEmail = async (data) => {
return await returnJSON(`/api/verifications/twitter/verify`, {
...DEFAULT_OPTIONS,
2021-06-09 01:53:30 +03:00
body: JSON.stringify({ data }),
});
};
export const createPasswordResetVerification = async (data) => {
return await returnJSON(`/api/verifications/password-reset/create`, {
...DEFAULT_OPTIONS,
2021-06-09 01:53:30 +03:00
body: JSON.stringify({ data }),
});
};
export const verifyPasswordResetEmail = async (data) => {
return await returnJSON(`/api/verifications/password-reset/verify`, {
...DEFAULT_OPTIONS,
2021-06-09 01:53:30 +03:00
body: JSON.stringify({ data }),
});
};
export const resetPassword = async (data) => {
return await returnJSON(`/api/users/reset-password`, {
...DEFAULT_OPTIONS,
2021-06-09 01:53:30 +03:00
body: JSON.stringify({ data }),
});
};
export const createLegacyVerification = async (data) => {
return await returnJSON(`/api/verifications/legacy/create`, {
...DEFAULT_OPTIONS,
2021-06-09 01:53:30 +03:00
body: JSON.stringify({ data }),
});
};
export const migrateUser = async (data) => {
return await returnJSON(`/api/users/migrate`, {
...DEFAULT_OPTIONS,
2021-06-09 01:53:30 +03:00
body: JSON.stringify({ data }),
});
};
export const createVerification = async (data) => {
return await returnJSON(`/api/verifications/create`, {
...DEFAULT_OPTIONS,
2021-06-09 01:53:30 +03:00
body: JSON.stringify({ data }),
});
};
export const verifyEmail = async (data) => {
return await returnJSON(`/api/verifications/verify`, {
...DEFAULT_OPTIONS,
2021-06-09 01:53:30 +03:00
body: JSON.stringify({ data }),
});
};
export const resendVerification = async (data) => {
return await returnJSON(`/api/verifications/resend`, {
...DEFAULT_OPTIONS,
2021-06-09 01:53:30 +03:00
body: JSON.stringify({ data }),
});
};
export const getUserVersion = async (data) => {
return await returnJSON(`/api/users/get-version`, {
...DEFAULT_OPTIONS,
2021-04-20 23:35:30 +03:00
body: JSON.stringify({ data }),
});
};