summaryrefslogtreecommitdiff
path: root/web/source/settings/lib/api/index.js
blob: 8af7d5b43395153de98d878e77beefb3aa7923e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
	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");
const { isPlainObject } = require("is-plain-object");
const d = require("dotty");

const { APIError, AuthenticationError } = require("../errors");
const { setInstanceInfo, setNamedInstanceInfo } = require("../../redux/reducers/instances").actions;

function apiCall(method, route, payload, type = "json") {
	return function (dispatch, getState) {
		const state = getState();
		let base = state.oauth.instance;
		let auth = state.oauth.token;

		return Promise.try(() => {
			let url = new URL(base);
			let [path, query] = route.split("?");
			url.pathname = path;
			if (query != undefined) {
				url.search = query;
			}
			let body;

			let headers = {
				"Accept": "application/json",
			};

			if (payload != undefined) {
				if (type == "json") {
					headers["Content-Type"] = "application/json";
					body = JSON.stringify(payload);
				} else if (type == "form") {
					body = convertToForm(payload);
				}
			}

			if (auth != undefined) {
				headers["Authorization"] = auth;
			}

			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}`);
			});

			return Promise.all([res, json]);
		}).then(([res, json]) => {
			if (!res.ok) {
				if (auth != undefined && (res.status == 401 || res.status == 403)) {
					// stored access token is invalid
					throw new AuthenticationError("401: Authentication error", {json, status: res.status});
				} else {
					throw new APIError(json.error, { json });
				}
			} else {
				return json;
			}
		});
	};
}

/*
	Takes an object with (nested) keys, and transforms it into
	a FormData object to be sent over the API
*/
function convertToForm(payload) {
	const formData = new FormData();
	Object.entries(payload).forEach(([key, val]) => {
		if (isPlainObject(val)) {
			Object.entries(val).forEach(([key2, val2]) => {
				if (val2 != undefined) {
					formData.set(`${key}[${key2}]`, val2);
				}
			});
		} else {
			if (val != undefined) {
				formData.set(key, val);
			}
		}
	});
	return formData;
}

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;
}

function getCurrentUrl() {
	let [pre, _past] = window.location.pathname.split("/settings");
	return `${window.location.origin}${pre}/settings`;
}

function fetchInstanceWithoutStore(domain) {
	return function (dispatch, getState) {
		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 = {
				oauth: { instance: domain }
			};

			return apiCall("GET", "/api/v1/instance")(dispatch, () => fakeState);
		}).then((json) => {
			if (json && json.uri) { // TODO: validate instance json more?
				dispatch(setNamedInstanceInfo([domain, json]));
				return json;
			}
		});
	};
}

function fetchInstance() {
	return function (dispatch, _getState) {
		return Promise.try(() => {
			return dispatch(apiCall("GET", "/api/v1/instance"));
		}).then((json) => {
			if (json && json.uri) {
				dispatch(setInstanceInfo(json));
				return json;
			}
		});
	};
}

let submoduleArgs = { apiCall, getCurrentUrl, getChanges };

module.exports = {
	instance: {
		fetchWithoutStore: fetchInstanceWithoutStore,
		fetch: fetchInstance
	},
	oauth: require("./oauth")(submoduleArgs),
	user: require("./user")(submoduleArgs),
	admin: require("./admin")(submoduleArgs),
	apiCall,
	convertToForm,
	getChanges
};