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 {
|
|
|
|
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";
|
|
|
|
import { PersonListing } from "../person/person-listing";
|
2022-11-27 18:02:32 +00:00
|
|
|
import { EmojiForm } from "./emojis-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;
|
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;
|
2022-11-27 18:02:32 +00:00
|
|
|
currentTab: string;
|
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,
|
2022-11-27 18:02:32 +00:00
|
|
|
currentTab: "site",
|
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,
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
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));
|
|
|
|
}
|
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">
|
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>
|
|
|
|
) : (
|
2022-11-27 18:02:32 +00:00
|
|
|
<div>
|
|
|
|
<HtmlTags
|
2023-03-27 16:49:46 +00:00
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
/>
|
2022-11-27 18:02:32 +00:00
|
|
|
<ul className="nav nav-tabs mb-2">
|
|
|
|
<li className="nav-item">
|
|
|
|
<button
|
2023-03-27 16:49:46 +00:00
|
|
|
className={`nav-link btn ${
|
|
|
|
this.state.currentTab == "site" && "active"
|
|
|
|
}`}
|
|
|
|
onClick={linkEvent(
|
|
|
|
{ ctx: this, tab: "site" },
|
|
|
|
this.handleSwitchTab
|
|
|
|
)}
|
|
|
|
>
|
2022-11-27 18:02:32 +00:00
|
|
|
{i18n.t("site")}
|
|
|
|
</button>
|
|
|
|
</li>
|
2023-03-30 20:36:57 +00:00
|
|
|
<li className="nav-item">
|
|
|
|
<button
|
|
|
|
className={`nav-link btn ${
|
|
|
|
this.state.currentTab == "taglines" && "active"
|
|
|
|
}`}
|
|
|
|
onClick={linkEvent(
|
|
|
|
{ ctx: this, tab: "taglines" },
|
|
|
|
this.handleSwitchTab
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{i18n.t("taglines")}
|
|
|
|
</button>
|
|
|
|
</li>
|
2022-11-27 18:02:32 +00:00
|
|
|
<li className="nav-item">
|
|
|
|
<button
|
2023-03-27 16:49:46 +00:00
|
|
|
className={`nav-link btn ${
|
|
|
|
this.state.currentTab == "emojis" && "active"
|
|
|
|
}`}
|
|
|
|
onClick={linkEvent(
|
|
|
|
{ ctx: this, tab: "emojis" },
|
|
|
|
this.handleSwitchTab
|
|
|
|
)}
|
|
|
|
>
|
2022-11-27 18:02:32 +00:00
|
|
|
{i18n.t("emojis")}
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
</ul>
|
2023-03-27 16:49:46 +00:00
|
|
|
{this.state.currentTab == "site" && (
|
|
|
|
<div className="row">
|
|
|
|
<div className="col-12 col-md-6">
|
|
|
|
<SiteForm
|
|
|
|
siteRes={this.state.siteRes}
|
|
|
|
showLocal={showLocal(this.isoData)}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-11-27 18:02:32 +00:00
|
|
|
<div className="col-12 col-md-6">
|
|
|
|
{this.admins()}
|
|
|
|
{this.bannedUsers()}
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-03-27 16:49:46 +00:00
|
|
|
)}
|
2023-03-30 20:36:57 +00:00
|
|
|
{this.state.currentTab == "taglines" && (
|
|
|
|
<div className="row">
|
|
|
|
<TaglineForm siteRes={this.state.siteRes}></TaglineForm>
|
|
|
|
</div>
|
|
|
|
)}
|
2023-03-27 16:49:46 +00:00
|
|
|
{this.state.currentTab == "emojis" && (
|
2022-11-27 18:02:32 +00:00
|
|
|
<div className="row">
|
|
|
|
<EmojiForm></EmojiForm>
|
|
|
|
</div>
|
2023-03-27 16:49:46 +00:00
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</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>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-27 16:49:46 +00:00
|
|
|
handleSwitchTab(i: { ctx: AdminSettings; tab: string }) {
|
2022-11-27 18:02:32 +00:00
|
|
|
i.ctx.setState({ currentTab: i.tab });
|
|
|
|
}
|
|
|
|
|
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("/");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|