|
|
|
@ -3,11 +3,11 @@ import { v1 as uuidV1 } from 'uuid'
|
|
|
|
|
import { io } from 'socket.io-client' |
|
|
|
|
import { createHash, hmacHex } from './crypto.js' |
|
|
|
|
import { isObject } from './util.js' |
|
|
|
|
import { aoEnv } from './settings.js' |
|
|
|
|
//import { aoEnv } from './settings.js'
|
|
|
|
|
|
|
|
|
|
// The AO API server endpoint this ao-cli client will attempt to connect to
|
|
|
|
|
export const AO_DEFAULT_HOSTNAME = 'localhost:8003' |
|
|
|
|
const HOSTNAME = aoEnv('AO_CLI_TARGET_HOSTNAME') || AO_DEFAULT_HOSTNAME |
|
|
|
|
const HOSTNAME = /*aoEnv('AO_CLI_TARGET_HOSTNAME') ||*/ AO_DEFAULT_HOSTNAME |
|
|
|
|
const [HOST, PORT] = HOSTNAME.split(':') |
|
|
|
|
|
|
|
|
|
// The AO API server websocket endpoint this ao-cli client will attempt to connect to
|
|
|
|
@ -20,9 +20,9 @@ export const socket = io(AO_SOCKET_URL, {
|
|
|
|
|
export let socketStatus |
|
|
|
|
|
|
|
|
|
// Load the current session cookies from the AO .env file
|
|
|
|
|
let currentMemberId = aoEnv('AO_CLI_SESSION_MEMBERID') |
|
|
|
|
let currentSessionId = aoEnv('AO_CLI_SESSION_ID') |
|
|
|
|
let currentSessionToken = aoEnv('AO_CLI_SESSION_TOKEN') |
|
|
|
|
let currentMemberId = '13844ae0-2270-11ea-b45c-83ea6e9b1aa1' //aoEnv('AO_CLI_SESSION_MEMBERID')
|
|
|
|
|
let currentSessionId = 'c5c9ccd0-f28d-11ec-9614-15596c298860' //aoEnv('AO_CLI_SESSION_ID')
|
|
|
|
|
let currentSessionToken = '51ad3b959b89d859786e232c72cda792ce5e99ff9dbb639d24f8ae45a70ff753' //aoEnv('AO_CLI_SESSION_TOKEN')
|
|
|
|
|
|
|
|
|
|
// Performs a GET request to the specified endpoint, sending the given payload
|
|
|
|
|
export async function getRequest(endpoint, payload = null, alternateHost = null, verbose = true) { |
|
|
|
@ -49,6 +49,7 @@ export async function postRequest(endpoint, payload = null, verbose = true) {
|
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
if(payload) { |
|
|
|
|
console.log("about to post to", HOSTNAME, currentSessionToken) |
|
|
|
|
return await request |
|
|
|
|
.post(HOSTNAME + endpoint) |
|
|
|
|
.send(payload) |
|
|
|
@ -70,6 +71,25 @@ export async function postEvent(event, verbose) {
|
|
|
|
|
return await postRequest('/events', event, verbose) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Attempts login with a new guest session. If successful, returns the generated session and token (login cookies).
|
|
|
|
|
// Guest sessions be be convertable to permanent sessions by setting a password on them
|
|
|
|
|
export async function createGuestSession() { |
|
|
|
|
const session = uuidV1() |
|
|
|
|
//const user = 'anon'
|
|
|
|
|
let sessionKey = createHash(session) |
|
|
|
|
const token = hmacHex(session, sessionKey) |
|
|
|
|
const result = await request |
|
|
|
|
.post(HOSTNAME + '/session') |
|
|
|
|
.set('authorization', token) |
|
|
|
|
.set('session', session) |
|
|
|
|
//.set('name', user)
|
|
|
|
|
.on('error', () => false) |
|
|
|
|
currentMemberId = result.body.memberId |
|
|
|
|
currentSessionToken = token |
|
|
|
|
currentSessionId = session |
|
|
|
|
return { session, token, memberId: currentMemberId } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Attempts login with the given username and password combo. If successful, returns the generated session and token (login cookies).
|
|
|
|
|
export async function createSession(user, pass) { |
|
|
|
|
const session = uuidV1() |
|
|
|
@ -281,18 +301,22 @@ export async function cacheMeme(taskId) {
|
|
|
|
|
// Cards feature
|
|
|
|
|
// Returns the card and other cards as specified by the alsoGetRelevant arg
|
|
|
|
|
// If multiple cards are returned, they will be returned in their global deck order (global creation order on server)
|
|
|
|
|
export async function getCard(taskId, alsoGetRelevant = 'subcards') { |
|
|
|
|
export async function getCard(taskId, alsoGetRelevant = 'subcards', verbose = true) { |
|
|
|
|
console.log('about to getCard, taskId: ', taskId) |
|
|
|
|
taskId = taskId.trim().toLowerCase() |
|
|
|
|
let payload = { taskId: taskId } |
|
|
|
|
const result = await postRequest('/fetchTaskByID', payload, false) // todo: change to flat text, not JSON (?)
|
|
|
|
|
let payload = { taskIds: [ taskId ] } |
|
|
|
|
console.log('payload', payload) |
|
|
|
|
const result = await postRequest('/fetchTasks', payload, verbose) // todo: change to flat text, not JSON (?)
|
|
|
|
|
console.log('result', result) |
|
|
|
|
if(!result || !result.body) { |
|
|
|
|
//console.log('Error fetching task.')
|
|
|
|
|
console.log('Error fetching task.') |
|
|
|
|
return null |
|
|
|
|
} |
|
|
|
|
if(alsoGetRelevant) { |
|
|
|
|
let relevantCards = await getAllRelevantCards(result.body, alsoGetRelevant) |
|
|
|
|
return [result.body, ...relevantCards] |
|
|
|
|
} |
|
|
|
|
console.log('result', result.body) |
|
|
|
|
return [result.body] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|