mirror of
1
Fork 0
gotosocial/web/source/settings-panel/lib/api/index.js

185 lines
4.7 KiB
JavaScript
Raw Normal View History

2022-09-11 18:41:29 +02:00
/*
GoToSocial
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
const Promise = require("bluebird");
2022-09-12 19:34:04 +02:00
const { isPlainObject } = require("is-plain-object");
2022-09-13 21:32:27 +02:00
const d = require("dotty");
2022-09-11 18:41:29 +02:00
2022-09-15 20:02:55 +02:00
const { APIError, AuthenticationError } = require("../errors");
2022-09-12 20:50:51 +02:00
const { setInstanceInfo, setNamedInstanceInfo } = require("../../redux/reducers/instances").actions;
2022-09-11 19:05:06 +02:00
const oauth = require("../../redux/reducers/oauth").actions;
2022-09-13 21:32:27 +02:00
function apiCall(method, route, payload, type = "json") {
2022-09-11 19:05:06 +02:00
return function (dispatch, getState) {
const state = getState();
let base = state.oauth.instance;
let auth = state.oauth.token;
console.log(method, base, route, "auth:", auth != undefined);
2022-09-13 21:32:27 +02:00
2022-09-11 19:05:06 +02:00
return Promise.try(() => {
let url = new URL(base);
2022-09-16 13:27:07 +02:00
let [path, query] = route.split("?");
url.pathname = path;
2022-09-16 18:50:25 +02:00
if (query != undefined) {
url.search = query;
}
2022-09-11 19:05:06 +02:00
let body;
2022-09-13 21:32:27 +02:00
2022-09-11 19:05:06 +02:00
let headers = {
"Accept": "application/json",
};
2022-09-11 19:42:13 +02:00
if (payload != undefined) {
if (type == "json") {
headers["Content-Type"] = "application/json";
body = JSON.stringify(payload);
} else if (type == "form") {
const formData = new FormData();
Object.entries(payload).forEach(([key, val]) => {
2022-09-12 19:34:04 +02:00
if (isPlainObject(val)) {
Object.entries(val).forEach(([key2, val2]) => {
2022-09-13 21:32:27 +02:00
if (val2 != undefined) {
formData.set(`${key}[${key2}]`, val2);
}
2022-09-12 19:34:04 +02:00
});
} else {
2022-09-13 21:32:27 +02:00
if (val != undefined) {
formData.set(key, val);
}
2022-09-12 19:34:04 +02:00
}
2022-09-11 19:42:13 +02:00
});
body = formData;
}
}
2022-09-13 21:32:27 +02:00
2022-09-11 19:05:06 +02:00
if (auth != undefined) {
headers["Authorization"] = auth;
}
2022-09-13 21:32:27 +02:00
2022-09-11 19:05:06 +02:00
return fetch(url.toString(), {
method,
headers,
body
});
}).then((res) => {
// try parse json even with error
let json = res.json().catch((e) => {
throw new APIError(`JSON parsing error: ${e.message}`);
});
2022-09-13 21:32:27 +02:00
2022-09-11 19:05:06 +02:00
return Promise.all([res, json]);
}).then(([res, json]) => {
if (!res.ok) {
2022-09-15 20:02:55 +02:00
if (auth != undefined && (res.status == 401 || res.status == 403)) {
2022-09-11 19:05:06 +02:00
// stored access token is invalid
2022-09-15 20:02:55 +02:00
throw new AuthenticationError("401: Authentication error", {json, status: res.status});
} else {
throw new APIError(json.error, { json });
2022-09-11 19:05:06 +02:00
}
} else {
return json;
}
2022-09-11 18:41:29 +02:00
});
2022-09-11 19:05:06 +02:00
};
2022-09-11 18:41:29 +02:00
}
2022-09-13 21:32:27 +02:00
function getChanges(state, keys) {
const { formKeys = [], fileKeys = [], renamedKeys = {} } = keys;
const update = {};
formKeys.forEach((key) => {
let value = d.get(state, key);
if (value == undefined) {
return;
}
if (renamedKeys[key]) {
key = renamedKeys[key];
}
d.put(update, key, value);
});
fileKeys.forEach((key) => {
let file = d.get(state, `${key}File`);
if (file != undefined) {
if (renamedKeys[key]) {
key = renamedKeys[key];
}
d.put(update, key, file);
}
});
return update;
}
2022-09-11 18:41:29 +02:00
function getCurrentUrl() {
return `${window.location.origin}${window.location.pathname}`;
}
2022-09-12 20:50:51 +02:00
function fetchInstanceWithoutStore(domain) {
2022-09-13 21:32:27 +02:00
return function (dispatch, getState) {
2022-09-11 18:41:29 +02:00
return Promise.try(() => {
let lookup = getState().instances.info[domain];
if (lookup != undefined) {
return lookup;
}
// apiCall expects to pull the domain from state,
// but we don't want to store it there yet
// so we mock the API here with our function argument
let fakeState = {
2022-09-13 21:32:27 +02:00
oauth: { instance: domain }
2022-09-11 18:41:29 +02:00
};
2022-09-11 19:05:06 +02:00
return apiCall("GET", "/api/v1/instance")(dispatch, () => fakeState);
2022-09-11 18:41:29 +02:00
}).then((json) => {
if (json && json.uri) { // TODO: validate instance json more?
2022-09-12 20:50:51 +02:00
dispatch(setNamedInstanceInfo([domain, json]));
return json;
}
});
};
}
function fetchInstance() {
2022-09-13 21:32:27 +02:00
return function (dispatch, _getState) {
2022-09-12 20:50:51 +02:00
return Promise.try(() => {
return dispatch(apiCall("GET", "/api/v1/instance"));
}).then((json) => {
if (json && json.uri) {
dispatch(setInstanceInfo(json));
2022-09-11 18:41:29 +02:00
return json;
}
});
};
}
2022-09-13 21:32:27 +02:00
let submoduleArgs = { apiCall, getCurrentUrl, getChanges };
2022-09-11 18:41:29 +02:00
module.exports = {
instance: {
2022-09-12 20:50:51 +02:00
fetchWithoutStore: fetchInstanceWithoutStore,
2022-09-11 18:41:29 +02:00
fetch: fetchInstance
},
2022-09-13 21:32:27 +02:00
oauth: require("./oauth")(submoduleArgs),
user: require("./user")(submoduleArgs),
2022-09-13 22:05:14 +02:00
admin: require("./admin")(submoduleArgs),
2022-09-13 21:32:27 +02:00
apiCall,
getChanges
2022-09-11 18:41:29 +02:00
};