2022-06-23 14:05:56 +00:00
|
|
|
import { None, Some } from "@sniptt/monads";
|
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,
|
2020-09-06 16:15:25 +00:00
|
|
|
GetSiteResponse,
|
2022-01-09 17:53:11 +00:00
|
|
|
PersonViewSafe,
|
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 {
|
2022-06-21 21:42:29 +00:00
|
|
|
auth,
|
2020-09-09 02:40:36 +00:00
|
|
|
capitalizeFirstLetter,
|
2021-07-17 20:42:55 +00:00
|
|
|
isBrowser,
|
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";
|
|
|
|
import { PersonListing } from "../person/person-listing";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { SiteForm } from "./site-form";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface AdminSettingsState {
|
|
|
|
siteRes: GetSiteResponse;
|
2022-01-09 17:53:11 +00:00
|
|
|
banned: PersonViewSafe[];
|
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()}`;
|
2022-06-23 14:05:56 +00:00
|
|
|
private isoData = setIsoData(this.context, BannedPersonsResponse);
|
2020-09-06 16:15:25 +00:00
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: 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,
|
2022-02-14 17:27:43 +00:00
|
|
|
leaveAdminTeamLoading: null,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
|
|
|
|
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-06-21 21:42:29 +00:00
|
|
|
this.state.banned = (
|
2022-06-23 14:05:56 +00:00
|
|
|
this.isoData.routeData[0] as BannedPersonsResponse
|
2022-06-21 21:42:29 +00:00
|
|
|
).banned;
|
2020-09-09 02:40:36 +00:00
|
|
|
this.state.loading = false;
|
|
|
|
} else {
|
2022-01-09 17:53:11 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.getBannedPersons({
|
2022-06-21 21:42:29 +00:00
|
|
|
auth: auth().unwrap(),
|
2022-01-09 17:53:11 +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>[] = [];
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
let bannedPersonsForm = new GetBannedPersons({ auth: req.auth.unwrap() });
|
2022-01-09 17:53:11 +00:00
|
|
|
promises.push(req.client.getBannedPersons(bannedPersonsForm));
|
|
|
|
|
|
|
|
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()) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2022-06-21 21:42:29 +00:00
|
|
|
return this.state.siteRes.site_view.match({
|
|
|
|
some: siteView => `${i18n.t("admin_settings")} - ${siteView.site.name}`,
|
|
|
|
none: "",
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
2021-07-17 20:21:31 +00:00
|
|
|
<Spinner large />
|
2020-09-06 16:15:25 +00:00
|
|
|
</h5>
|
|
|
|
) : (
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-12 col-md-6">
|
2022-01-09 17:53:11 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
2022-06-21 21:42:29 +00:00
|
|
|
description={None}
|
|
|
|
image={None}
|
2022-01-09 17:53:11 +00:00
|
|
|
/>
|
2022-06-21 21:42:29 +00:00
|
|
|
{this.state.siteRes.site_view.match({
|
|
|
|
some: siteView => (
|
|
|
|
<SiteForm
|
|
|
|
site={Some(siteView.site)}
|
|
|
|
showLocal={showLocal(this.isoData)}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
none: <></>,
|
|
|
|
})}
|
2022-06-23 14:05:56 +00:00
|
|
|
</div>
|
|
|
|
<div class="col-12 col-md-6">
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.admins()}
|
|
|
|
{this.bannedUsers()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
admins() {
|
|
|
|
return (
|
|
|
|
<>
|
2021-02-22 02:39:04 +00:00
|
|
|
<h5>{capitalizeFirstLetter(i18n.t("admins"))}</h5>
|
2020-09-06 16:15:25 +00:00
|
|
|
<ul class="list-unstyled">
|
|
|
|
{this.state.siteRes.admins.map(admin => (
|
|
|
|
<li class="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)}
|
|
|
|
class="btn btn-danger mb-2"
|
|
|
|
>
|
|
|
|
{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>
|
2020-09-06 16:15:25 +00:00
|
|
|
<ul class="list-unstyled">
|
2022-01-09 17:53:11 +00:00
|
|
|
{this.state.banned.map(banned => (
|
2020-09-06 16:15:25 +00:00
|
|
|
<li class="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) {
|
|
|
|
i.state.leaveAdminTeamLoading = true;
|
2022-06-21 21:42:29 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.leaveAdmin({ auth: auth().unwrap() })
|
|
|
|
);
|
2022-02-14 17:27:43 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
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("/");
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.loading = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
return;
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.EditSite) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<SiteResponse>(msg, SiteResponse);
|
|
|
|
this.state.siteRes.site_view = Some(data.site_view);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
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) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<BannedPersonsResponse>(msg, BannedPersonsResponse);
|
2022-01-09 17:53:11 +00:00
|
|
|
this.state.banned = data.banned;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.loading = false;
|
|
|
|
this.setState(this.state);
|
2022-02-14 17:27:43 +00:00
|
|
|
} else if (op == UserOperation.LeaveAdmin) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<GetSiteResponse>(msg, GetSiteResponse);
|
2022-02-14 17:27:43 +00:00
|
|
|
this.state.siteRes.site_view = data.site_view;
|
|
|
|
this.setState(this.state);
|
|
|
|
this.state.leaveAdminTeamLoading = false;
|
|
|
|
toast(i18n.t("left_admin_team"));
|
|
|
|
this.setState(this.state);
|
|
|
|
this.context.router.history.push("/");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|