2020-11-19 21:14:09 +03:00
import * as Websockets from "~/common/browser-websockets" ;
import * as Credentials from "~/common/credentials" ;
import * as Actions from "~/common/actions" ;
2020-11-22 00:25:40 +03:00
import * as Window from "~/common/window" ;
2020-11-24 09:31:52 +03:00
import * as Validations from "~/common/validations" ;
import * as Strings from "~/common/strings" ;
2020-11-20 07:03:30 +03:00
import * as FileUtilities from "~/common/file-utilities" ;
2020-11-28 07:39:01 +03:00
import * as Events from "~/common/custom-events" ;
2020-11-19 21:14:09 +03:00
import Cookies from "universal-cookie" ;
2020-12-25 02:44:47 +03:00
import JSZip from "jszip" ;
2021-01-21 00:50:29 +03:00
2020-12-25 02:44:47 +03:00
import { saveAs } from "file-saver" ;
2020-11-19 21:14:09 +03:00
2021-01-21 00:50:29 +03:00
//NOTE(martina): this file is for utility *API-calling* functions
//For non API related utility functions, see common/utilities.js
//And for uploading related utility functions, see common/file-utilities.js
2020-11-19 21:14:09 +03:00
const cookies = new Cookies ( ) ;
2020-11-22 00:25:40 +03:00
export const authenticate = async ( state ) => {
2020-11-20 07:03:30 +03:00
// NOTE(jim): Kills existing session cookie if there is one.
const jwt = cookies . get ( Credentials . session . key ) ;
if ( jwt ) {
2021-05-06 03:08:14 +03:00
cookies . remove ( Credentials . session . key , {
path : "/" ,
2021-11-11 05:05:22 +03:00
maxAge : 3600 * 24 * 30 ,
2021-05-06 03:08:14 +03:00
sameSite : "strict" ,
} ) ;
2020-11-20 07:03:30 +03:00
}
let response = await Actions . signIn ( state ) ;
2021-09-13 22:33:06 +03:00
if ( Events . hasError ( response ) ) {
2021-05-27 21:59:57 +03:00
return response ;
2020-11-20 07:03:30 +03:00
}
if ( response . token ) {
// NOTE(jim):
// + One week.
// + Only requests to the same site.
// + Not using sessionStorage so the cookie doesn't leave when the browser dies.
2021-05-06 03:08:14 +03:00
cookies . set ( Credentials . session . key , response . token , {
2020-11-20 07:03:30 +03:00
path : "/" ,
2021-11-11 05:05:22 +03:00
maxAge : 3600 * 24 * 30 ,
2020-11-20 07:03:30 +03:00
sameSite : "strict" ,
} ) ;
}
return response ;
} ;
2021-06-09 01:53:30 +03:00
export const authenticateViaTwitter = ( response ) => {
// NOTE(jim): Kills existing session cookie if there is one.
const jwt = cookies . get ( Credentials . session . key ) ;
if ( jwt ) {
cookies . remove ( Credentials . session . key , {
path : "/" ,
2021-11-11 05:05:22 +03:00
maxAge : 3600 * 24 * 30 ,
2021-06-09 01:53:30 +03:00
sameSite : "strict" ,
} ) ;
}
if ( Events . hasError ( response ) ) {
return false ;
}
if ( response . token ) {
// NOTE(jim):
// + One week.
// + Only requests to the same site.
// + Not using sessionStorage so the cookie doesn't leave when the browser dies.
cookies . set ( Credentials . session . key , response . token , {
path : "/" ,
2021-11-11 05:05:22 +03:00
maxAge : 3600 * 24 * 30 ,
2021-06-09 01:53:30 +03:00
sameSite : "strict" ,
} ) ;
}
return response ;
} ;
// NOTE(jim): Signs a user out and redirects to the sign in screen.
2021-04-07 01:08:42 +03:00
export const signOut = async ( { viewer } ) => {
2020-11-19 21:14:09 +03:00
let wsclient = Websockets . getClient ( ) ;
if ( wsclient ) {
2021-04-07 01:08:42 +03:00
wsclient . send ( JSON . stringify ( { type : "UNSUBSCRIBE_VIEWER" , data : viewer } ) ) ;
2020-11-19 21:14:09 +03:00
await Websockets . deleteClient ( ) ;
wsclient = null ;
}
const jwt = cookies . get ( Credentials . session . key ) ;
if ( jwt ) {
2021-05-06 03:08:14 +03:00
cookies . remove ( Credentials . session . key , {
path : "/" ,
2021-11-11 05:05:22 +03:00
maxAge : 3600 * 24 * 30 ,
2021-05-06 03:08:14 +03:00
sameSite : "strict" ,
} ) ;
2020-11-19 21:14:09 +03:00
}
2021-05-06 03:08:14 +03:00
window . location . replace ( "/_/auth" ) ;
2020-11-19 21:14:09 +03:00
return ;
} ;
// NOTE(jim): Permanently deletes you, forever.
2021-04-20 01:28:57 +03:00
export const deleteMe = async ( { viewer } ) => {
2020-11-19 21:14:09 +03:00
let response = await Actions . deleteViewer ( ) ;
2020-11-28 07:39:01 +03:00
if ( Events . hasError ( response ) ) {
2020-11-19 21:14:09 +03:00
return response ;
}
2021-04-20 01:28:57 +03:00
await signOut ( { viewer } ) ;
2020-11-19 21:14:09 +03:00
2021-05-06 03:08:14 +03:00
// let wsclient = Websockets.getClient();
// if (wsclient) {
// await Websockets.deleteClient();
// wsclient = null;
// }
2020-11-19 21:14:09 +03:00
return response ;
} ;
2020-11-20 07:03:30 +03:00
export const hydrate = async ( ) => {
const response = await Actions . hydrateAuthenticatedUser ( ) ;
2020-11-28 07:39:01 +03:00
if ( Events . hasError ( response ) ) {
2020-11-20 07:03:30 +03:00
return null ;
}
2020-11-22 05:24:26 +03:00
2020-11-20 07:03:30 +03:00
return JSON . parse ( JSON . stringify ( response . data ) ) ;
} ;
2021-08-04 20:59:25 +03:00
export const uploadImage = async ( file ) => {
2020-11-22 00:25:40 +03:00
if ( ! file ) {
2020-11-28 07:39:01 +03:00
Events . dispatchMessage ( { message : "Something went wrong with the upload. Please try again" } ) ;
2020-11-22 00:25:40 +03:00
return ;
}
if ( ! Validations . isPreviewableImage ( file . type ) ) {
2020-11-28 07:39:01 +03:00
Events . dispatchMessage ( { message : "Upload failed. Only images and gifs are allowed" } ) ;
2020-11-22 00:25:40 +03:00
return ;
}
2021-08-04 20:59:25 +03:00
const response = await FileUtilities . upload ( { file } ) ;
2020-11-22 00:25:40 +03:00
2020-11-28 07:39:01 +03:00
if ( Events . hasError ( response ) ) {
2020-11-22 00:25:40 +03:00
return false ;
}
2021-03-07 23:53:54 +03:00
return response ;
2020-11-22 00:25:40 +03:00
} ;
2021-03-07 23:53:54 +03:00
export const deleteFiles = async ( fileIds = [ ] , noAlert ) => {
2020-11-25 12:17:37 +03:00
let ids = Array . isArray ( fileIds ) ? fileIds : [ fileIds ] ;
2021-03-07 23:53:54 +03:00
const response = await Actions . deleteFiles ( { ids } ) ;
2020-11-28 07:39:01 +03:00
2020-11-30 02:47:08 +03:00
if ( ! noAlert ) {
if ( Events . hasError ( response ) ) {
return false ;
}
2020-11-28 07:39:01 +03:00
2021-10-13 23:39:01 +03:00
Events . dispatchMessage ( { message : "Successfully deleted!" , status : "INFO" } ) ;
2020-11-30 02:47:08 +03:00
return response ;
}
2020-11-22 00:25:40 +03:00
} ;
export const removeFromSlate = async ( { slate , ids } ) => {
const response = await Actions . removeFileFromSlate ( {
slateId : slate . id ,
ids ,
} ) ;
2020-11-28 07:39:01 +03:00
if ( Events . hasError ( response ) ) {
2020-11-22 00:25:40 +03:00
return false ;
}
return response ;
} ;
2021-06-01 02:41:00 +03:00
// export const addToSlate = async ({ slate, files }) => {
// const addResponse = await Actions.addFileToSlate({ slate, files });
// if (Events.hasError(addResponse)) {
// return false;
// }
// const { added, skipped } = addResponse;
// let message = Strings.formatAsUploadMessage(added, skipped, true);
// Events.dispatchMessage({ message, status: !added ? null : "INFO" });
// return true;
// };
//NOTE(martina): save copy includes add to slate now. If it's already in the user's files but not in that slate, it'll skip the adding to files and just add to slate
2021-10-26 19:23:03 +03:00
export const saveCopy = async ( { files , slate , showAlerts = true } ) => {
2021-06-01 02:41:00 +03:00
let response = await Actions . saveCopy ( { files , slate } ) ;
2020-11-28 07:39:01 +03:00
if ( Events . hasError ( response ) ) {
2020-11-22 00:25:40 +03:00
return false ;
}
2021-06-01 02:41:00 +03:00
let message = Strings . formatAsUploadMessage ( response . data . added , response . data . skipped , slate ) ;
2021-10-26 19:23:03 +03:00
if ( showAlerts ) Events . dispatchMessage ( { message , status : ! response . data . added ? null : "INFO" } ) ;
2020-11-22 00:25:40 +03:00
return response ;
} ;
2021-10-26 19:23:03 +03:00
export const download = async ( file , rootRef ) => {
2021-05-19 05:58:14 +03:00
Actions . createDownloadActivity ( { file } ) ;
2021-07-07 23:50:57 +03:00
if ( file . isLink ) return ;
2021-08-31 01:03:45 +03:00
if ( Validations . isUnityType ( file . type ) ) {
2021-05-19 05:58:14 +03:00
return await downloadZip ( file ) ;
}
2021-03-07 23:53:54 +03:00
let uri = Strings . getURLfromCID ( file . cid ) ;
2021-10-26 19:23:03 +03:00
await Window . saveAs ( uri , file . filename , rootRef ) ;
2021-05-19 05:58:14 +03:00
return { data : true } ;
2020-11-22 00:25:40 +03:00
} ;
2020-12-25 02:44:47 +03:00
export const downloadZip = async ( file ) => {
2021-02-02 17:18:50 +03:00
try {
const { data } = await Actions . getZipFilePaths ( file ) ;
const filesPaths = data . filesPaths . map ( ( item ) => item . replace ( ` / ${ file . id } / ` , "" ) ) ;
2021-03-07 23:53:54 +03:00
const baseUrl = Strings . getURLfromCID ( file . cid ) ;
const zipFileName = file . filename ;
2020-12-25 02:44:47 +03:00
2021-02-02 17:18:50 +03:00
let zip = new JSZip ( ) ;
2020-12-25 02:44:47 +03:00
2021-03-03 21:36:37 +03:00
await Promise . all (
filesPaths . map ( async ( filePath ) => {
let url = ` ${ baseUrl } / ${ filePath } ` ;
const blob = await Window . getBlobFromUrl ( url ) ;
zip . file ( filePath , blob ) ;
} )
) ;
2020-12-25 02:44:47 +03:00
2021-02-02 17:18:50 +03:00
zip . generateAsync ( { type : "blob" } ) . then ( ( blob ) => {
saveAs ( blob , zipFileName ) ;
} ) ;
return {
decorator : "UNITY_ZIP_DOWNLOAD_SUCCESS" ,
error : false ,
} ;
} catch ( e ) {
return {
decorator : "UNITY_ZIP_DOWNLOAD_FAILED" ,
error : true ,
} ;
}
2020-12-25 02:44:47 +03:00
} ;
2021-03-05 19:37:46 +03:00
const _nativeDownload = ( { url , onError } ) => {
2021-03-05 10:32:08 +03:00
const iframe = document . createElement ( "iframe" ) ;
iframe . style . display = "none" ;
2021-03-05 19:37:46 +03:00
iframe . src = url ;
const ERROR _MESSAGE = "SLATE_DOWNLOAD_ERROR" ;
2021-03-06 01:01:29 +03:00
const handleIframeErrors = ( e ) => {
2021-03-05 19:37:46 +03:00
if ( e . data === ERROR _MESSAGE && onError ) {
onError ( e . data ) ;
}
} ;
2021-03-06 01:01:29 +03:00
window . addEventListener ( "message" , handleIframeErrors ) ;
2021-08-04 20:59:25 +03:00
iframe . onload = ( ) => window . removeEventListener ( "message" , handleIframeErrors ) ;
2021-03-05 10:32:08 +03:00
document . body . appendChild ( iframe ) ;
2021-03-03 11:48:59 +03:00
} ;
2021-08-04 20:59:25 +03:00
export const compressAndDownloadFiles = async ( { files , name = "slate.zip" } ) => {
2021-03-05 19:37:46 +03:00
const errorMessage = "Something went wrong with the download. Please try again" ;
2021-03-03 13:01:34 +03:00
try {
2021-07-07 23:50:57 +03:00
if ( ! files || files . length == 0 ) {
2021-04-24 05:24:03 +03:00
Events . dispatchMessage ( { message : "No files in collection to download" } ) ;
return ;
}
2021-03-03 13:01:34 +03:00
Events . dispatchMessage ( { message : "We're preparing your files to download" , status : "INFO" } ) ;
let downloadFiles = [ ] ;
for ( const file of files ) {
2021-07-07 23:50:57 +03:00
if ( file . isLink ) continue ;
2021-08-31 01:03:45 +03:00
if ( Validations . isUnityType ( file . type ) ) {
2021-03-03 13:01:34 +03:00
const { data } = await Actions . getZipFilePaths ( file ) ;
const unityFiles = data . filesPaths . map ( ( item ) => ( {
2021-03-07 23:53:54 +03:00
url : item . replace ( ` / ${ file . id } / ` , ` ${ Strings . getURLfromCID ( file . cid ) } / ` ) ,
name : item . replace ( ` / ${ file . id } / ` , ` / ${ file . filename } / ` ) ,
2021-03-03 13:01:34 +03:00
} ) ) ;
downloadFiles = downloadFiles . concat ( unityFiles ) ;
continue ;
}
downloadFiles . push ( {
2021-03-07 23:53:54 +03:00
name : file . filename ,
url : Strings . getURLfromCID ( file . cid ) ,
2021-03-03 13:01:34 +03:00
} ) ;
2021-03-03 11:48:59 +03:00
}
2021-07-07 23:50:57 +03:00
if ( downloadFiles . length == 0 ) {
Events . dispatchMessage ( {
message : "Links cannot be downloaded" ,
} ) ;
return ;
}
2021-03-03 11:48:59 +03:00
2021-07-07 23:50:57 +03:00
Actions . createDownloadActivity ( { files } ) ;
2021-08-04 20:59:25 +03:00
const res = await Actions . createZipToken ( downloadFiles ) ;
const downloadLink = Actions . downloadZip ( { token : res . data . token , name } ) ;
2021-03-05 19:37:46 +03:00
await _nativeDownload ( {
2021-03-03 13:01:34 +03:00
url : downloadLink ,
2021-08-04 20:59:25 +03:00
onError : ( ) =>
2021-03-05 19:37:46 +03:00
Events . dispatchMessage ( {
message : errorMessage ,
} ) ,
2021-03-03 11:48:59 +03:00
} ) ;
2021-03-03 13:01:34 +03:00
} catch ( e ) {
2021-03-05 10:32:08 +03:00
console . error ( e ) ;
2021-03-05 19:37:46 +03:00
Events . dispatchMessage ( { message : errorMessage } ) ;
2021-03-03 11:48:59 +03:00
}
2021-03-02 16:33:10 +03:00
} ;
2020-11-20 07:03:30 +03:00
// export const createSlate = async (data) => {
// let response = await Actions.createSlate({
// name: data.name,
2021-03-07 23:53:54 +03:00
// isPublic: data.isPublic,
2020-11-20 07:03:30 +03:00
// body: data.body,
// });
// return response;
// }
// export const createWalletAddress = async (data) => {
// let response = await Actions.updateViewer({
// type: "CREATE_FILECOIN_ADDRESS",
// address: {
// name: data.name,
// type: data.wallet_type,
// makeDefault: data.makeDefault,
// },
// });
// return response;
// }
// export const sendWalletAddressFilecoin = (data) => {
// let response = await Actions.sendFilecoin({
// source: data.source,
// target: data.target,
// amount: data.amount,
// });
// return response;
// }