diff --git a/src/api/auth.cjs b/src/api/auth.cjs new file mode 100644 index 0000000..43e8603 --- /dev/null +++ b/src/api/auth.cjs @@ -0,0 +1,41 @@ +const express = require('express'); +const axios = require('axios'); +let auth_status = 0; + +async function check_auth_status() { + return await new Promise(resolve => { + const check = setInterval(() => { + console.log("Checking auth status..."); + + if(auth_status == 1) { + console.log("Bot has been authenticated"); + resolve(true); + clearInterval(check); + return true; + } + }, 2000); + }); +}; + +function verify_auth_status() { + auth_status = 1; + return true; +} + +async function get_access_token(host, session_id) { + axios.post(`${host}/api/miauth/${session_id}/check`) + .then((data) => { + const response = data.json(); + + if(response.ok == true) { + console.log(response.token); + return response.token; + } else { + return false; + } + }); +}; + +module.exports.check_auth_status = check_auth_status; +module.exports.verify_auth_status = verify_auth_status; +module.exports.get_access_token = get_access_token; \ No newline at end of file diff --git a/src/api/post.cjs b/src/api/post.cjs new file mode 100644 index 0000000..d81ba2f --- /dev/null +++ b/src/api/post.cjs @@ -0,0 +1,16 @@ +const axios = require('axios'); + +async function create_image(host, access_token, file_path) { + axios.post(`${host}/api/drive/files/create`, { + file: fs.createReadStream(file_path) + }, { + headers: { + 'Content-Type': 'multipart/form-data', + 'Authorization': `Bearer ${access_token}` + } + }).then((data) => { + return data; + }); +}; + +module.exports.create_image = create_image; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 9ef17df..60b2527 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,14 @@ import { v4 as uuidv4 } from 'uuid'; import fs, { createReadStream } from 'node:fs'; import path from 'node:path'; -import fetch from 'node-fetch'; import express from 'express'; import axios from 'axios'; import { fileFromPath } from 'formdata-node/file-from-path'; import * as Misskey from 'misskey-js'; +import * as Post from './api/post.cjs'; +import * as Auth from './api/auth.cjs'; + const __dirname = path.resolve(); const config = JSON.parse(fs.readFileSync(path.resolve("config", "config.json"))); const token_file = JSON.parse(fs.readFileSync(path.resolve("config", "token.json"))); @@ -20,13 +22,8 @@ let app_name = config.app.name; let app_callback = config.app.callback; let app_permission = config.app.permission; -// https://misskey.io/miauth/c1f6d42b-468b-4fd2-8274-e58abdedef6f?name=MyApp&callback=https%3A%2F%2Fmyapp.example.com%2Fcallback&permission=write:notes,write:following,read:drive - let session_id = uuidv4(); let auth_url = `${host}/miauth/${session_id}?name=${app_name}&callback=${app_callback}&permission=${app_permission}`; -let auth_status = 0; - -console.log(auth_url); app.get('/callback', function(req, res) { res.send({ @@ -34,63 +31,16 @@ app.get('/callback', function(req, res) { "success": true, }) - auth_status = 1; + Auth.verify_auth_status(); }); -async function check_auth_status() { - return await new Promise(resolve => { - const check = setInterval(() => { - console.log("Checking auth status..."); - - if(auth_status == 1) { - console.log("Bot has been authenticated"); - resolve(true); - clearInterval(check); - return true; - } - }, 2000); - }); -}; - -async function get_access_token(host, session_id) { - const response = await fetch(`${host}/api/miauth/${session_id}/check`, { - method: 'POST' - }); - - const data = await response.json(); - console.log(data); - - const res_status = data.ok; - - if(data.ok == true) { - return data.token; - } else { - return false; // error occurred - } -}; - -async function create_image(access_token, file_path) { - axios.post(`${host}/api/drive/files/create`, { - file: fs.createReadStream(file_path) - }, { - headers: { - 'Content-Type': 'multipart/form-data', - 'Authorization': `Bearer ${access_token}` - } - }).then((data) => { - return data; - }); - - // return info: - // https://misskey.winscloud.net/api-doc#tag/drive/operation/drive___files___create -}; - (async () => { let access_token = false; if(!token_file.token) { // if existing token not exist - await check_auth_status() - access_token = await get_access_token(host, session_id); + console.log(auth_url); + await Auth.check_auth_status(); + access_token = await Auth.get_access_token(host, session_id); let json_data = { "token": access_token @@ -110,7 +60,7 @@ async function create_image(access_token, file_path) { }); let static_image_path = path.join(__dirname, '/data/current_radar.jpg'); - create_image(access_token, static_image_path); + Post.create_image(host, access_token, static_image_path); })(); app.listen(port);