2021-07-17 20:42:55 +00:00
|
|
|
import autosize from "autosize";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2022-01-09 17:53:11 +00:00
|
|
|
BannedPersonsResponse,
|
|
|
|
GetBannedPersons,
|
2023-05-11 18:32:32 +00:00
|
|
|
GetFederatedInstancesResponse,
|
2020-09-06 16:15:25 +00:00
|
|
|
GetSiteResponse,
|
2023-05-11 18:32:32 +00:00
|
|
|
PersonView,
|
2021-07-17 20:42:55 +00:00
|
|
|
SiteResponse,
|
|
|
|
UserOperation,
|
2022-06-21 21:42:29 +00:00
|
|
|
wsJsonToRes,
|
|
|
|
wsUserOp,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { InitialFetchRequest } from "../../interfaces";
|
|
|
|
import { WebSocketService } from "../../services";
|
2020-09-09 02:40:36 +00:00
|
|
|
import {
|
|
|
|
capitalizeFirstLetter,
|
2021-07-17 20:42:55 +00:00
|
|
|
isBrowser,
|
2023-01-04 16:56:24 +00:00
|
|
|
myAuth,
|
2020-09-09 02:40:36 +00:00
|
|
|
randomStr,
|
|
|
|
setIsoData,
|
2022-05-23 19:19:14 +00:00
|
|
|
showLocal,
|
2021-07-17 20:42:55 +00:00
|
|
|
toast,
|
|
|
|
wsClient,
|
2020-09-09 02:40:36 +00:00
|
|
|
wsSubscribe,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
|
|
|
import { HtmlTags } from "../common/html-tags";
|
|
|
|
import { Spinner } from "../common/icon";
|
2023-05-20 19:39:12 +00:00
|
|
|
import Tabs from "../common/tabs";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { PersonListing } from "../person/person-listing";
|
2022-11-27 18:02:32 +00:00
|
|
|
import { EmojiForm } from "./emojis-form";
|
2023-05-21 13:07:22 +00:00
|
|
|
import RateLimitForm from "./rate-limit-form";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { SiteForm } from "./site-form";
|
2023-03-30 20:36:57 +00:00
|
|
|
import { TaglineForm } from "./tagline-form";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface AdminSettingsState {
|
|
|
|
siteRes: GetSiteResponse;
|
2023-05-11 18:32:32 +00:00
|
|
|
instancesRes?: GetFederatedInstancesResponse;
|
|
|
|
banned: PersonView[];
|
2022-06-21 21:42:29 +00:00
|
|
|
loading: boolean;
|
2022-02-14 17:27:43 +00:00
|
|
|
leaveAdminTeamLoading: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class AdminSettings extends Component<any, AdminSettingsState> {
|
|
|
|
private siteConfigTextAreaId = `site-config-${randomStr()}`;
|
2023-01-04 16:56:24 +00:00
|
|
|
private isoData = setIsoData(this.context);
|
|
|
|
private subscription?: Subscription;
|
|
|
|
state: AdminSettingsState = {
|
2020-12-24 01:58:27 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2022-01-09 17:53:11 +00:00
|
|
|
banned: [],
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: true,
|
2023-01-04 16:56:24 +00:00
|
|
|
leaveAdminTeamLoading: false,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
2020-09-09 02:40:36 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
|
|
|
|
|
|
|
// Only fetch the data if coming from another route
|
|
|
|
if (this.isoData.path == this.context.router.route.match.url) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
banned: (this.isoData.routeData[0] as BannedPersonsResponse).banned,
|
2023-05-11 18:32:32 +00:00
|
|
|
instancesRes: this.isoData
|
|
|
|
.routeData[1] as GetFederatedInstancesResponse,
|
2022-09-22 15:03:35 +00:00
|
|
|
loading: false,
|
|
|
|
};
|
2020-09-09 02:40:36 +00:00
|
|
|
} else {
|
2023-01-04 16:56:24 +00:00
|
|
|
let cAuth = myAuth();
|
|
|
|
if (cAuth) {
|
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.getBannedPersons({
|
|
|
|
auth: cAuth,
|
|
|
|
})
|
|
|
|
);
|
2023-05-11 18:32:32 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.getFederatedInstances({ auth: cAuth })
|
|
|
|
);
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2020-09-09 02:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-11-12 21:56:46 +00:00
|
|
|
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
|
2022-01-09 17:53:11 +00:00
|
|
|
let promises: Promise<any>[] = [];
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
let auth = req.auth;
|
|
|
|
if (auth) {
|
|
|
|
let bannedPersonsForm: GetBannedPersons = { auth };
|
|
|
|
promises.push(req.client.getBannedPersons(bannedPersonsForm));
|
2023-05-11 18:32:32 +00:00
|
|
|
promises.push(req.client.getFederatedInstances({ auth }));
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2022-01-09 17:53:11 +00:00
|
|
|
|
|
|
|
return promises;
|
2020-09-09 02:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
if (isBrowser()) {
|
|
|
|
var textarea: any = document.getElementById(this.siteConfigTextAreaId);
|
|
|
|
autosize(textarea);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-09-09 02:40:36 +00:00
|
|
|
if (isBrowser()) {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.subscription?.unsubscribe();
|
2020-09-09 02:40:36 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2022-11-09 19:53:07 +00:00
|
|
|
return `${i18n.t("admin_settings")} - ${
|
|
|
|
this.state.siteRes.site_view.site.name
|
|
|
|
}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2022-10-03 18:16:36 +00:00
|
|
|
<div className="container-lg">
|
2023-05-20 19:39:12 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
2021-07-17 20:21:31 +00:00
|
|
|
<Spinner large />
|
2020-09-06 16:15:25 +00:00
|
|
|
</h5>
|
|
|
|
) : (
|
2023-05-20 19:39:12 +00:00
|
|
|
<Tabs
|
|
|
|
tabs={[
|
|
|
|
{
|
|
|
|
key: "site",
|
|
|
|
label: i18n.t("site"),
|
|
|
|
getNode: () => (
|
|
|
|
<div className="row">
|
|
|
|
<div className="col-12 col-md-6">
|
|
|
|
<SiteForm
|
|
|
|
siteRes={this.state.siteRes}
|
|
|
|
instancesRes={this.state.instancesRes}
|
|
|
|
showLocal={showLocal(this.isoData)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="col-12 col-md-6">
|
|
|
|
{this.admins()}
|
|
|
|
{this.bannedUsers()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
},
|
2023-05-21 13:07:22 +00:00
|
|
|
{
|
|
|
|
key: "rate_limiting",
|
|
|
|
label: "Rate Limiting",
|
|
|
|
getNode: () => (
|
|
|
|
<RateLimitForm
|
|
|
|
localSiteRateLimit={
|
|
|
|
this.state.siteRes.site_view.local_site_rate_limit
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
},
|
2023-05-20 19:39:12 +00:00
|
|
|
{
|
|
|
|
key: "taglines",
|
|
|
|
label: i18n.t("taglines"),
|
|
|
|
getNode: () => (
|
|
|
|
<div className="row">
|
|
|
|
<TaglineForm siteRes={this.state.siteRes} />
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "emojis",
|
|
|
|
label: i18n.t("emojis"),
|
|
|
|
getNode: () => (
|
|
|
|
<div className="row">
|
|
|
|
<EmojiForm />
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
admins() {
|
|
|
|
return (
|
|
|
|
<>
|
2021-02-22 02:39:04 +00:00
|
|
|
<h5>{capitalizeFirstLetter(i18n.t("admins"))}</h5>
|
2022-09-22 15:03:35 +00:00
|
|
|
<ul className="list-unstyled">
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.state.siteRes.admins.map(admin => (
|
2022-09-22 15:03:35 +00:00
|
|
|
<li key={admin.person.id} className="list-inline-item">
|
2021-03-15 18:09:31 +00:00
|
|
|
<PersonListing person={admin.person} />
|
2020-09-06 16:15:25 +00:00
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
2022-02-14 17:27:43 +00:00
|
|
|
{this.leaveAdmin()}
|
2020-09-06 16:15:25 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-14 17:27:43 +00:00
|
|
|
leaveAdmin() {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
onClick={linkEvent(this, this.handleLeaveAdminTeam)}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-danger mb-2"
|
2022-02-14 17:27:43 +00:00
|
|
|
>
|
|
|
|
{this.state.leaveAdminTeamLoading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
|
|
|
i18n.t("leave_admin_team")
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
bannedUsers() {
|
|
|
|
return (
|
|
|
|
<>
|
2021-02-22 02:39:04 +00:00
|
|
|
<h5>{i18n.t("banned_users")}</h5>
|
2022-09-22 15:03:35 +00:00
|
|
|
<ul className="list-unstyled">
|
2022-01-09 17:53:11 +00:00
|
|
|
{this.state.banned.map(banned => (
|
2022-09-22 15:03:35 +00:00
|
|
|
<li key={banned.person.id} className="list-inline-item">
|
2021-03-15 18:09:31 +00:00
|
|
|
<PersonListing person={banned.person} />
|
2020-09-06 16:15:25 +00:00
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-14 17:27:43 +00:00
|
|
|
handleLeaveAdminTeam(i: AdminSettings) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let auth = myAuth();
|
|
|
|
if (auth) {
|
|
|
|
i.setState({ leaveAdminTeamLoading: true });
|
|
|
|
WebSocketService.Instance.send(wsClient.leaveAdmin({ auth }));
|
|
|
|
}
|
2022-02-14 17:27:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
let op = wsUserOp(msg);
|
2021-04-07 15:54:38 +00:00
|
|
|
console.log(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t(msg.error), "danger");
|
|
|
|
this.context.router.history.push("/");
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ loading: false });
|
2020-09-06 16:15:25 +00:00
|
|
|
return;
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.EditSite) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let data = wsJsonToRes<SiteResponse>(msg);
|
2022-11-09 19:53:07 +00:00
|
|
|
this.setState(s => ((s.siteRes.site_view = data.site_view), s));
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t("site_saved"));
|
2022-01-09 17:53:11 +00:00
|
|
|
} else if (op == UserOperation.GetBannedPersons) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let data = wsJsonToRes<BannedPersonsResponse>(msg);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ banned: data.banned, loading: false });
|
2022-02-14 17:27:43 +00:00
|
|
|
} else if (op == UserOperation.LeaveAdmin) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let data = wsJsonToRes<GetSiteResponse>(msg);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState(s => ((s.siteRes.site_view = data.site_view), s));
|
|
|
|
this.setState({ leaveAdminTeamLoading: false });
|
2022-02-14 17:27:43 +00:00
|
|
|
toast(i18n.t("left_admin_team"));
|
|
|
|
this.context.router.history.push("/");
|
2023-05-11 18:32:32 +00:00
|
|
|
} else if (op == UserOperation.GetFederatedInstances) {
|
|
|
|
let data = wsJsonToRes<GetFederatedInstancesResponse>(msg);
|
|
|
|
this.setState({ instancesRes: data });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|