2022-09-13 21:33:13 +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-16 13:27:07 +02:00
|
|
|
const isValidDomain = require("is-valid-domain");
|
2022-09-13 21:33:13 +02:00
|
|
|
|
2022-09-13 22:05:14 +02:00
|
|
|
const instance = require("../../redux/reducers/instances").actions;
|
2022-09-15 21:16:18 +02:00
|
|
|
const admin = require("../../redux/reducers/admin").actions;
|
2022-09-13 21:33:13 +02:00
|
|
|
|
|
|
|
module.exports = function ({ apiCall, getChanges }) {
|
2022-09-16 13:27:07 +02:00
|
|
|
const adminAPI = {
|
2022-09-13 22:05:14 +02:00
|
|
|
updateInstance: function updateInstance() {
|
|
|
|
return function (dispatch, getState) {
|
2022-09-13 21:33:13 +02:00
|
|
|
return Promise.try(() => {
|
2022-09-13 22:05:14 +02:00
|
|
|
const state = getState().instances.adminSettings;
|
2022-09-13 21:33:13 +02:00
|
|
|
|
2022-09-13 22:05:14 +02:00
|
|
|
const update = getChanges(state, {
|
2022-09-16 21:04:50 +02:00
|
|
|
formKeys: ["title", "short_description", "description", "contact_account.username", "email", "terms"],
|
|
|
|
renamedKeys: {"contact_account.username": "contact_username"},
|
2022-09-13 22:05:14 +02:00
|
|
|
// fileKeys: ["avatar", "header"]
|
|
|
|
});
|
2022-09-13 21:33:13 +02:00
|
|
|
|
2022-09-13 22:05:14 +02:00
|
|
|
return dispatch(apiCall("PATCH", "/api/v1/instance", update, "form"));
|
|
|
|
}).then((data) => {
|
|
|
|
return dispatch(instance.setInstanceInfo(data));
|
|
|
|
});
|
2022-09-13 21:33:13 +02:00
|
|
|
};
|
2022-09-15 20:02:55 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
fetchDomainBlocks: function fetchDomainBlocks() {
|
|
|
|
return function (dispatch, _getState) {
|
|
|
|
return Promise.try(() => {
|
|
|
|
return dispatch(apiCall("GET", "/api/v1/admin/domain_blocks"));
|
2022-09-15 21:16:18 +02:00
|
|
|
}).then((data) => {
|
|
|
|
return dispatch(admin.setBlockedInstances(data));
|
2022-09-15 20:02:55 +02:00
|
|
|
});
|
|
|
|
};
|
2022-09-16 13:27:07 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
updateDomainBlock: function updateDomainBlock(domain) {
|
|
|
|
return function (dispatch, getState) {
|
|
|
|
return Promise.try(() => {
|
2022-09-18 22:24:41 +02:00
|
|
|
const state = getState().admin.newInstanceBlocks[domain];
|
2022-09-16 13:27:07 +02:00
|
|
|
const update = getChanges(state, {
|
|
|
|
formKeys: ["domain", "obfuscate", "public_comment", "private_comment"],
|
|
|
|
});
|
|
|
|
|
|
|
|
return dispatch(apiCall("POST", "/api/v1/admin/domain_blocks", update, "form"));
|
|
|
|
}).then((block) => {
|
2022-09-18 22:24:41 +02:00
|
|
|
return Promise.all([
|
|
|
|
dispatch(admin.newDomainBlock([domain, block])),
|
|
|
|
dispatch(admin.setDomainBlock([domain, block]))
|
|
|
|
]);
|
2022-09-16 13:27:07 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2022-09-18 22:24:41 +02:00
|
|
|
getEditableDomainBlock: function getEditableDomainBlock(domain) {
|
|
|
|
return function (dispatch, getState) {
|
|
|
|
let data = getState().admin.blockedInstances[domain];
|
|
|
|
return dispatch(admin.newDomainBlock([domain, data]));
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2022-09-16 13:27:07 +02:00
|
|
|
bulkDomainBlock: function bulkDomainBlock() {
|
|
|
|
return function (dispatch, getState) {
|
|
|
|
let invalidDomains = [];
|
|
|
|
let success = 0;
|
|
|
|
|
|
|
|
return Promise.try(() => {
|
|
|
|
const state = getState().admin.bulkBlock;
|
|
|
|
let list = state.list;
|
|
|
|
let domains;
|
|
|
|
|
|
|
|
let fields = getChanges(state, {
|
|
|
|
formKeys: ["obfuscate", "public_comment", "private_comment"]
|
|
|
|
});
|
|
|
|
|
|
|
|
let defaultDate = new Date().toUTCString();
|
|
|
|
|
|
|
|
if (list[0] == "[") {
|
|
|
|
domains = JSON.parse(state.list);
|
|
|
|
} else {
|
|
|
|
domains = list.split("\n").map((line_) => {
|
|
|
|
let line = line_.trim();
|
|
|
|
if (line.length == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isValidDomain(line, {wildcard: true, allowUnicode: true})) {
|
|
|
|
invalidDomains.push(line);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
domain: line,
|
|
|
|
created_at: defaultDate,
|
|
|
|
...fields
|
|
|
|
};
|
|
|
|
}).filter((a) => a != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (domains.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(domains);
|
|
|
|
|
|
|
|
const update = {
|
|
|
|
domains: new Blob([JSON.stringify(domains)], {type: "application/json"})
|
|
|
|
};
|
|
|
|
|
|
|
|
return dispatch(apiCall("POST", "/api/v1/admin/domain_blocks?import=true", update, "form"));
|
|
|
|
}).then((blocks) => {
|
|
|
|
if (blocks != undefined) {
|
|
|
|
return Promise.each(blocks, (block) => {
|
|
|
|
success += 1;
|
|
|
|
return dispatch(admin.setDomainBlock([block.domain, block]));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).then(() => {
|
|
|
|
return {
|
|
|
|
success,
|
|
|
|
invalidDomains
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
removeDomainBlock: function removeDomainBlock(domain) {
|
|
|
|
return function (dispatch, getState) {
|
|
|
|
return Promise.try(() => {
|
|
|
|
const id = getState().admin.blockedInstances[domain].id;
|
|
|
|
return dispatch(apiCall("DELETE", `/api/v1/admin/domain_blocks/${id}`));
|
2022-09-18 22:24:41 +02:00
|
|
|
}).then((removed) => {
|
|
|
|
return dispatch(admin.removeDomainBlock(removed.domain));
|
2022-09-16 13:27:07 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
},
|
2022-09-16 15:49:27 +02:00
|
|
|
|
|
|
|
mediaCleanup: function mediaCleanup(days) {
|
|
|
|
return function (dispatch, _getState) {
|
|
|
|
return Promise.try(() => {
|
|
|
|
return dispatch(apiCall("POST", `/api/v1/admin/media_cleanup?remote_cache_days=${days}`));
|
|
|
|
});
|
|
|
|
};
|
2022-09-16 16:13:32 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
fetchCustomEmoji: function fetchCustomEmoji() {
|
|
|
|
return function (dispatch, _getState) {
|
|
|
|
return Promise.try(() => {
|
|
|
|
return dispatch(apiCall("GET", "/api/v1/custom_emojis"));
|
|
|
|
}).then((emoji) => {
|
|
|
|
return dispatch(admin.setEmoji(emoji));
|
|
|
|
});
|
|
|
|
};
|
2022-09-16 18:50:25 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
newEmoji: function newEmoji() {
|
|
|
|
return function (dispatch, getState) {
|
|
|
|
return Promise.try(() => {
|
|
|
|
const state = getState().admin.newEmoji;
|
|
|
|
|
|
|
|
const update = getChanges(state, {
|
|
|
|
formKeys: ["shortcode"],
|
|
|
|
fileKeys: ["image"]
|
|
|
|
});
|
|
|
|
|
|
|
|
return dispatch(apiCall("POST", "/api/v1/admin/custom_emojis", update, "form"));
|
|
|
|
}).then((emoji) => {
|
|
|
|
return dispatch(admin.addEmoji(emoji));
|
|
|
|
});
|
|
|
|
};
|
2022-09-16 15:49:27 +02:00
|
|
|
}
|
2022-09-13 21:33:13 +02:00
|
|
|
};
|
2022-09-16 13:27:07 +02:00
|
|
|
return adminAPI;
|
2022-09-13 21:33:13 +02:00
|
|
|
};
|