38 lines
936 B
JavaScript
38 lines
936 B
JavaScript
/**
|
|
* TODO: this code should maybe be the constructor of a session model
|
|
*/
|
|
import pickRandomFile from './pickRandomFile.js';
|
|
import {randomBytes} from './utils.js';
|
|
import {client} from '../idb.js';
|
|
import {MAX_SESSION_TIME, TAGS} from '../R.js';
|
|
import {Session} from '../models/Session.js';
|
|
|
|
/**
|
|
* @param {string} websiteKey
|
|
* @param {Object} cookies
|
|
* @return {Promise<Session>}
|
|
*/
|
|
export default async function initializeSession(websiteKey, cookies) {
|
|
const image = await pickRandomFile();
|
|
|
|
const imageTagId = 1; // TODO: Change later to be dynamic based on `image`
|
|
const sessionId = cookies[websiteKey] ? cookies[websiteKey] : randomBytes(8);
|
|
|
|
client.setex(sessionId, MAX_SESSION_TIME, JSON.stringify({
|
|
sessionId,
|
|
websiteKey,
|
|
image,
|
|
obj: imageTagId,
|
|
score: 0.5,
|
|
}));
|
|
|
|
|
|
const session = new Session({
|
|
sessionId,
|
|
websiteKey,
|
|
imageTagId: TAGS[imageTagId],
|
|
});
|
|
|
|
return session;
|
|
};
|