"use strict";var E=Object.defineProperty;var f=(d,e)=>E(d,"name",{value:e,configurable:true});const S=use("App/Models/User");const g=use("App/Models/Service");const h=use("App/Models/Workspace");const{validateAll:y}=use("Validator");const M=require("node:crypto");const{v4:I}=require("uuid");const{DEFAULT_APP_SETTINGS:b}=require("../../../../config");const{convertToJSON:u}=require("../../../../jsUtils");const{API_VERSION:J}=require("../../../../environment-remote");const{default:C}=require("../../../../helpers/userAgent-helpers");const p=f((d,e,t,r)=>new Promise((i,o)=>{try{fetch(`${d}/${J}/${e}`,{method:t,headers:{Authorization:`Bearer ${r}`,"User-Agent":C()}}).then(s=>s.json()).then(s=>i(s))}catch(s){console.log(s);o()}}),"apiRequest");const O="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJGZXJkaSBJbnRlcm5hbCBTZXJ2ZXIiLCJpYXQiOjE1NzEwNDAyMTUsImV4cCI6MjUzMzk1NDE3ODQ0LCJhdWQiOiJnZXRmZXJkaS5jb20iLCJzdWIiOiJmZXJkaUBsb2NhbGhvc3QiLCJ1c2VySWQiOiIxIn0.9_TWFGp6HROv8Yg82Rt6i1-95jqWym40a-HmgrdMC6M";const A={accountType:"individual",beta:false,email:"",emailValidated:true,features:{},firstname:"Ferdium",id:"82c1cf9d-ab58-4da2-b55e-aaa41d2142d8",isSubscriptionOwner:true,lastname:"Application",locale:b.fallbackLocale};class T{async signup({request:e,response:t}){const r=await y(e.all(),{firstname:"required",email:"required|email",password:"required"});if(r.fails()){return t.status(401).send({message:"Invalid POST arguments",messages:r.messages(),status:401})}return t.send({message:"Successfully created account",token:O})}async login({request:e,response:t}){if(!e.header("Authorization")){return t.status(401).send({message:"Please provide authorization",status:401})}return t.send({message:"Successfully logged in",token:O})}async me({response:e}){const t=await S.find(1);const r=u(t.settings);return e.send({...A,...r})}async updateMe({request:e,response:t}){const r=await S.find(1);const i=u(r.settings||{});const o={...i,...e.all()};r.settings=JSON.stringify(o);await r.save();return t.send({data:{...A,...o},status:["data-updated"]})}async import({request:e,response:t}){const r=await y(e.all(),{email:"required|email",password:"required",server:"required"});if(r.fails()){let a="There was an error while trying to import your account:\n";for(const n of r.messages()){if(n.validation==="required"){a+=`- Please make sure to supply your ${n.field}
`}else if(n.validation==="unique"){a+="- There is already a user with this email.\n"}else{a+=`${n.message}
`}}return t.status(401).send(a)}const{email:i,password:o,server:s}=e.all();const c=M.createHash("sha256").update(o).digest("base64");let l;try{const a=Buffer.from(`${i}:${c}`).toString("base64");const n=await fetch(`${s}/${J}/auth/login`,{method:"POST",headers:{Authorization:`Basic ${a}`,"User-Agent":C()}});const m=await n.json();if(!m.message||m.message!=="Successfully logged in"){const k="Could not login into Franz with your supplied credentials. Please check and try again";return t.status(401).send(k)}l=m.token}catch(a){return t.status(401).send({message:"Cannot login to Franz",error:a})}let w=false;try{w=await p(s,"me","GET",l)}catch(a){const n=`Could not get your user info from Franz. Please check your credentials or try again later.
Error: ${a}`;return t.status(401).send(n)}if(!w){const a="Could not get your user info from Franz. Please check your credentials or try again later";return t.status(401).send(a)}const v={};try{const a=await p(s,"me/services","GET",l);for(const n of a){await this._createAndCacheService(n,v)}}catch(a){const n=`Could not import your services into our system.
Error: ${a}`;return t.status(401).send(n)}try{const a=await p(s,"workspace","GET",l);for(const n of a){await this._createWorkspace(n,v)}}catch(a){const n=`Could not import your workspaces into our system.
Error: ${a}`;return t.status(401).send(n)}return t.send("Your account has been imported. You can now use your Franz account in Ferdium.")}async export({response:e}){const t=await g.all();const r=t.toJSON();const i=await h.all();const o=i.toJSON();const s={username:"Ferdium",mail:"internal@ferdium.org",services:r,workspaces:o};return e.header("Content-Type","application/force-download").header("Content-disposition","attachment; filename=export.ferdium-data").send(s)}async importFerdium({request:e,response:t}){const r=await y(e.all(),{file:"required"});if(r.fails()){return t.send(r.messages())}let i;try{i=JSON.parse(e.input("file"))}catch(s){console.log(s);return t.send("Could not import: Invalid file, could not read file")}if(!i||!i.services||!i.workspaces){return t.send("Could not import: Invalid file (2)")}const o={};try{for(const s of i.services){await this._createAndCacheService(s,o)}}catch(s){const c=`Could not import your services into our system.
Error: ${s}`;return t.send(c)}try{for(const s of i.workspaces){await this._createWorkspace(s,o)}}catch(s){const c=`Could not import your workspaces into our system.
Error: ${s}`;return t.status(401).send(c)}return t.send("Your account has been imported.")}async _createWorkspace(e,t){let r;do{r=I()}while((await h.query().where("workspaceId",r).fetch()).rows.length>0);if(e.services){e.services=u(e.services)}const i=e.services&&typeof e.services==="object"?e.services.map(o=>t[o]):[];if(e.data){e.data=u(e.data)}await h.create({workspaceId:r,name:e.name,order:e.order,services:JSON.stringify(i),data:JSON.stringify(e.data||{})})}async _createAndCacheService(e,t){let r;do{r=I()}while((await g.query().where("serviceId",r).fetch()).rows.length>0);t[e.serviceId]=r;if(e.settings){e.settings=u(e.settings)}await g.create({serviceId:r,name:e.name,recipeId:e.recipeId,settings:JSON.stringify(e.settings||{})})}}f(T,"UserController");module.exports=T;
