Merge branch 'main' into fix/1417-communities-search-layout

This commit is contained in:
Dessalines 2023-06-21 08:08:23 -04:00 committed by GitHub
commit 3e0a32a634
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 250 additions and 249 deletions

@ -1 +1 @@
Subproject commit 7fc71d0860bbe5c6d620ec27112350ffe5b9229c Subproject commit a241fe1255a6363c7ae1ec5a09520c066745e6ce

View file

@ -0,0 +1,28 @@
import type { Request, Response } from "express";
import { LemmyHttp } from "lemmy-js-client";
import { getHttpBaseInternal } from "../../shared/env";
import { wrapClient } from "../../shared/services/HttpService";
import generateManifestJson from "../utils/generate-manifest-json";
import { setForwardedHeaders } from "../utils/set-forwarded-headers";
let manifest: Awaited<ReturnType<typeof generateManifestJson>> | undefined =
undefined;
export default async (req: Request, res: Response) => {
if (!manifest) {
const headers = setForwardedHeaders(req.headers);
const client = wrapClient(new LemmyHttp(getHttpBaseInternal(), headers));
const site = await client.getSite({});
if (site.state === "success") {
manifest = await generateManifestJson(site.data);
} else {
res.sendStatus(500);
return;
}
}
res.setHeader("content-type", "application/manifest+json");
res.send(manifest);
};

View file

@ -2,6 +2,7 @@ import express from "express";
import path from "path"; import path from "path";
import process from "process"; import process from "process";
import CatchAllHandler from "./handlers/catch-all-handler"; import CatchAllHandler from "./handlers/catch-all-handler";
import ManifestHandler from "./handlers/manifest-handler";
import RobotsHandler from "./handlers/robots-handler"; import RobotsHandler from "./handlers/robots-handler";
import ServiceWorkerHandler from "./handlers/service-worker-handler"; import ServiceWorkerHandler from "./handlers/service-worker-handler";
import ThemeHandler from "./handlers/theme-handler"; import ThemeHandler from "./handlers/theme-handler";
@ -24,6 +25,7 @@ if (!process.env["LEMMY_UI_DISABLE_CSP"] && !process.env["LEMMY_UI_DEBUG"]) {
server.get("/robots.txt", RobotsHandler); server.get("/robots.txt", RobotsHandler);
server.get("/service-worker.js", ServiceWorkerHandler); server.get("/service-worker.js", ServiceWorkerHandler);
server.get("/manifest", ManifestHandler);
server.get("/css/themes/:name", ThemeHandler); server.get("/css/themes/:name", ThemeHandler);
server.get("/css/themelist", ThemesListHandler); server.get("/css/themelist", ThemesListHandler);
server.get("/*", CatchAllHandler); server.get("/*", CatchAllHandler);

View file

@ -5,32 +5,35 @@ import sharp from "sharp";
import { ILemmyConfig, IsoDataOptionalSite } from "../../shared/interfaces"; import { ILemmyConfig, IsoDataOptionalSite } from "../../shared/interfaces";
import { favIconPngUrl, favIconUrl } from "../../shared/utils"; import { favIconPngUrl, favIconUrl } from "../../shared/utils";
import { fetchIconPng } from "./fetch-icon-png"; import { fetchIconPng } from "./fetch-icon-png";
import { generateManifestBase64 } from "./generate-manifest-base64";
const customHtmlHeader = process.env["LEMMY_UI_CUSTOM_HTML_HEADER"] || ""; const customHtmlHeader = process.env["LEMMY_UI_CUSTOM_HTML_HEADER"] || "";
let appleTouchIcon: string | undefined = undefined;
export async function createSsrHtml( export async function createSsrHtml(
root: string, root: string,
isoData: IsoDataOptionalSite isoData: IsoDataOptionalSite
) { ) {
const site = isoData.site_res; const site = isoData.site_res;
const appleTouchIcon = site?.site_view.site.icon if (!appleTouchIcon) {
? `data:image/png;base64,${sharp( appleTouchIcon = site?.site_view.site.icon
await fetchIconPng(site.site_view.site.icon) ? `data:image/png;base64,${sharp(
) await fetchIconPng(site.site_view.site.icon)
.resize(180, 180) )
.extend({ .resize(180, 180)
bottom: 20, .extend({
top: 20, bottom: 20,
left: 20, top: 20,
right: 20, left: 20,
background: "#222222", right: 20,
}) background: "#222222",
.png() })
.toBuffer() .png()
.then(buf => buf.toString("base64"))}` .toBuffer()
: favIconPngUrl; .then(buf => buf.toString("base64"))}`
: favIconPngUrl;
}
const erudaStr = const erudaStr =
process.env["LEMMY_UI_DEBUG"] === "true" process.env["LEMMY_UI_DEBUG"] === "true"
@ -74,15 +77,7 @@ export async function createSsrHtml(
/> />
<!-- Web app manifest --> <!-- Web app manifest -->
${ <link rel="manifest" href="/manifest" />
site &&
`<link
rel="manifest"
href=${`data:application/manifest+json;base64,${await generateManifestBase64(
site
)}`}
/>`
}
<link rel="apple-touch-icon" href=${appleTouchIcon} /> <link rel="apple-touch-icon" href=${appleTouchIcon} />
<link rel="apple-touch-startup-image" href=${appleTouchIcon} /> <link rel="apple-touch-startup-image" href=${appleTouchIcon} />

View file

@ -5,7 +5,7 @@ import sharp from "sharp";
import { getHttpBaseExternal } from "../../shared/env"; import { getHttpBaseExternal } from "../../shared/env";
import { fetchIconPng } from "./fetch-icon-png"; import { fetchIconPng } from "./fetch-icon-png";
const iconSizes = [72, 96, 144, 192, 512]; const iconSizes = [72, 96, 128, 144, 152, 192, 384, 512];
const defaultLogoPathDirectory = path.join( const defaultLogoPathDirectory = path.join(
process.cwd(), process.cwd(),
@ -14,7 +14,7 @@ const defaultLogoPathDirectory = path.join(
"icons" "icons"
); );
export async function generateManifestBase64({ export default async function ({
my_user, my_user,
site_view: { site_view: {
site, site,
@ -25,7 +25,7 @@ export async function generateManifestBase64({
const icon = site.icon ? await fetchIconPng(site.icon) : null; const icon = site.icon ? await fetchIconPng(site.icon) : null;
const manifest = { return {
name: site.name, name: site.name,
description: site.description ?? "A link aggregator for the fediverse", description: site.description ?? "A link aggregator for the fediverse",
start_url: url, start_url: url,
@ -69,31 +69,24 @@ export async function generateManifestBase64({
short_name: "Communities", short_name: "Communities",
description: "Browse communities", description: "Browse communities",
}, },
] {
.concat( name: "Create Post",
my_user url: "/create_post",
? [ short_name: "Create Post",
{ description: "Create a post.",
name: "Create Post", },
url: "/create_post", ].concat(
short_name: "Create Post", my_user?.local_user_view.person.admin || !community_creation_admin_only
description: "Create a post.", ? [
}, {
] name: "Create Community",
: [] url: "/create_community",
) short_name: "Create Community",
.concat( description: "Create a community",
my_user?.local_user_view.person.admin || !community_creation_admin_only },
? [ ]
{ : []
name: "Create Community", ),
url: "/create_community",
short_name: "Create Community",
description: "Create a community",
},
]
: []
),
related_applications: [ related_applications: [
{ {
platform: "f-droid", platform: "f-droid",
@ -102,6 +95,4 @@ export async function generateManifestBase64({
}, },
], ],
}; };
return Buffer.from(JSON.stringify(manifest)).toString("base64");
} }

View file

@ -570,8 +570,6 @@ export class Home extends Component<any, HomeState> {
data-tippy-content={ data-tippy-content={
subscribedCollapsed ? i18n.t("expand") : i18n.t("collapse") subscribedCollapsed ? i18n.t("expand") : i18n.t("collapse")
} }
data-bs-toggle="collapse"
data-bs-target="#sidebarSubscribedBody"
aria-expanded="true" aria-expanded="true"
aria-controls="sidebarSubscribedBody" aria-controls="sidebarSubscribedBody"
> >
@ -582,24 +580,25 @@ export class Home extends Component<any, HomeState> {
</button> </button>
)} )}
</header> </header>
<div {!subscribedCollapsed && (
id="sidebarSubscribedBody" <div
className="collapse show" id="sidebarSubscribedBody"
aria-labelledby="sidebarSubscribedHeader" aria-labelledby="sidebarSubscribedHeader"
> >
<div className="card-body"> <div className="card-body">
<ul className="list-inline mb-0"> <ul className="list-inline mb-0">
{UserService.Instance.myUserInfo?.follows.map(cfv => ( {UserService.Instance.myUserInfo?.follows.map(cfv => (
<li <li
key={cfv.community.id} key={cfv.community.id}
className="list-inline-item d-inline-block" className="list-inline-item d-inline-block"
> >
<CommunityLink community={cfv.community} /> <CommunityLink community={cfv.community} />
</li> </li>
))} ))}
</ul> </ul>
</div>
</div> </div>
</div> )}
</> </>
); );
} }

View file

@ -42,13 +42,11 @@ export class SiteSidebar extends Component<SiteSidebarProps, SiteSidebarState> {
)} )}
</header> </header>
<div {!this.state.collapsed && (
id="sidebarInfoBody" <div id="sidebarInfoBody" aria-labelledby="sidebarInfoHeader">
className="collapse show" <div className="card-body">{this.siteInfo()}</div>
aria-labelledby="sidebarInfoHeader" </div>
> )}
<div className="card-body">{this.siteInfo()}</div>
</div>
</section> </section>
</div> </div>
); );

View file

@ -114,7 +114,7 @@ export class CreatePost extends Component<
if (res.state === "success") { if (res.state === "success") {
this.setState({ this.setState({
selectedCommunityChoice: { selectedCommunityChoice: {
label: res.data.community_view.community.name, label: res.data.community_view.community.title,
value: res.data.community_view.community.id.toString(), value: res.data.community_view.community.id.toString(),
}, },
loading: false, loading: false,

View file

@ -79,6 +79,143 @@ interface PostFormState {
submitted: boolean; submitted: boolean;
} }
function handlePostSubmit(i: PostForm, event: any) {
event.preventDefault();
// Coerce empty url string to undefined
if ((i.state.form.url ?? "") === "") {
i.setState(s => ((s.form.url = undefined), s));
}
i.setState({ loading: true, submitted: true });
const auth = myAuthRequired();
const pForm = i.state.form;
const pv = i.props.post_view;
if (pv) {
i.props.onEdit?.({
name: pForm.name,
url: pForm.url,
body: pForm.body,
nsfw: pForm.nsfw,
post_id: pv.post.id,
language_id: pForm.language_id,
auth,
});
} else if (pForm.name && pForm.community_id) {
i.props.onCreate?.({
name: pForm.name,
community_id: pForm.community_id,
url: pForm.url,
body: pForm.body,
nsfw: pForm.nsfw,
language_id: pForm.language_id,
honeypot: pForm.honeypot,
auth,
});
}
}
function copySuggestedTitle(d: { i: PostForm; suggestedTitle?: string }) {
const sTitle = d.suggestedTitle;
if (sTitle) {
d.i.setState(
s => ((s.form.name = sTitle?.substring(0, MAX_POST_TITLE_LENGTH)), s)
);
d.i.setState({ suggestedPostsRes: { state: "empty" } });
setTimeout(() => {
const textarea: any = document.getElementById("post-title");
autosize.update(textarea);
}, 10);
}
}
function handlePostUrlChange(i: PostForm, event: any) {
const url = event.target.value;
i.setState(prev => ({
...prev,
form: {
...prev.form,
url,
},
imageDeleteUrl: "",
}));
i.fetchPageTitle();
}
function handlePostNsfwChange(i: PostForm, event: any) {
i.setState(s => ((s.form.nsfw = event.target.checked), s));
}
function handleHoneyPotChange(i: PostForm, event: any) {
i.setState(s => ((s.form.honeypot = event.target.value), s));
}
function handleCancel(i: PostForm) {
i.props.onCancel?.();
}
function handleImageUploadPaste(i: PostForm, event: any) {
const image = event.clipboardData.files[0];
if (image) {
handleImageUpload(i, image);
}
}
function handleImageUpload(i: PostForm, event: any) {
let file: any;
if (event.target) {
event.preventDefault();
file = event.target.files[0];
} else {
file = event;
}
i.setState({ imageLoading: true });
HttpService.client.uploadImage({ image: file }).then(res => {
console.log("pictrs upload:");
console.log(res);
if (res.state === "success") {
if (res.data.msg === "ok") {
i.state.form.url = res.data.url;
i.setState({
imageLoading: false,
imageDeleteUrl: res.data.delete_url as string,
});
} else {
toast(JSON.stringify(res), "danger");
}
} else if (res.state === "failed") {
console.error(res.msg);
toast(res.msg, "danger");
i.setState({ imageLoading: false });
}
});
}
function handlePostNameChange(i: PostForm, event: any) {
i.setState(s => ((s.form.name = event.target.value), s));
i.fetchSimilarPosts();
}
function handleImageDelete(i: PostForm) {
const { imageDeleteUrl } = i.state;
fetch(imageDeleteUrl);
i.setState(prev => ({
...prev,
imageDeleteUrl: "",
imageLoading: false,
form: {
...prev.form,
url: "",
},
}));
}
export class PostForm extends Component<PostFormProps, PostFormState> { export class PostForm extends Component<PostFormProps, PostFormState> {
state: PostFormState = { state: PostFormState = {
suggestedPostsRes: { state: "empty" }, suggestedPostsRes: { state: "empty" },
@ -123,16 +260,16 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
...this.state.form, ...this.state.form,
community_id: getIdFromString(selectedCommunityChoice.value), community_id: getIdFromString(selectedCommunityChoice.value),
}, },
communitySearchOptions: [selectedCommunityChoice] communitySearchOptions: [selectedCommunityChoice].concat(
.concat( (
this.props.initialCommunities?.map( this.props.initialCommunities?.map(
({ community: { id, title } }) => ({ ({ community: { id, title } }) => ({
label: title, label: title,
value: id.toString(), value: id.toString(),
}) })
) ?? [] ) ?? []
) ).filter(option => option.value !== selectedCommunityChoice.value)
.filter(option => option.value !== selectedCommunityChoice.value), ),
}; };
} else { } else {
this.state = { this.state = {
@ -188,15 +325,8 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
const url = this.state.form.url; const url = this.state.form.url;
// TODO
// const promptCheck =
// !!this.state.form.name || !!this.state.form.url || !!this.state.form.body;
// <Prompt when={promptCheck} message={i18n.t("block_leaving")} />
return ( return (
<form <form className="post-form" onSubmit={linkEvent(this, handlePostSubmit)}>
className="post-form"
onSubmit={linkEvent(this, this.handlePostSubmit)}
>
<NavigationPrompt <NavigationPrompt
when={ when={
!!( !!(
@ -215,9 +345,9 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
type="url" type="url"
id="post-url" id="post-url"
className="form-control" className="form-control"
value={this.state.form.url} value={url}
onInput={linkEvent(this, this.handlePostUrlChange)} onInput={linkEvent(this, handlePostUrlChange)}
onPaste={linkEvent(this, this.handleImageUploadPaste)} onPaste={linkEvent(this, handleImageUploadPaste)}
/> />
{this.renderSuggestedTitleCopy()} {this.renderSuggestedTitleCopy()}
<form> <form>
@ -237,7 +367,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
name="file" name="file"
className="d-none" className="d-none"
disabled={!UserService.Instance.myUserInfo} disabled={!UserService.Instance.myUserInfo}
onChange={linkEvent(this, this.handleImageUpload)} onChange={linkEvent(this, handleImageUpload)}
/> />
</form> </form>
{url && validURL(url) && ( {url && validURL(url) && (
@ -276,7 +406,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
{this.state.imageDeleteUrl && ( {this.state.imageDeleteUrl && (
<button <button
className="btn btn-danger btn-sm mt-2" className="btn btn-danger btn-sm mt-2"
onClick={linkEvent(this, this.handleImageDelete)} onClick={linkEvent(this, handleImageDelete)}
aria-label={i18n.t("delete")} aria-label={i18n.t("delete")}
data-tippy-content={i18n.t("delete")} data-tippy-content={i18n.t("delete")}
> >
@ -327,7 +457,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
<textarea <textarea
value={this.state.form.name} value={this.state.form.name}
id="post-title" id="post-title"
onInput={linkEvent(this, this.handlePostNameChange)} onInput={linkEvent(this, handlePostNameChange)}
className={`form-control ${ className={`form-control ${
!validTitle(this.state.form.name) && "is-invalid" !validTitle(this.state.form.name) && "is-invalid"
}`} }`}
@ -392,7 +522,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
id="post-nsfw" id="post-nsfw"
type="checkbox" type="checkbox"
checked={this.state.form.nsfw} checked={this.state.form.nsfw}
onChange={linkEvent(this, this.handlePostNsfwChange)} onChange={linkEvent(this, handlePostNsfwChange)}
/> />
</div> </div>
</div> </div>
@ -413,7 +543,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
className="form-control honeypot" className="form-control honeypot"
id="register-honey" id="register-honey"
value={this.state.form.honeypot} value={this.state.form.honeypot}
onInput={linkEvent(this, this.handleHoneyPotChange)} onInput={linkEvent(this, handleHoneyPotChange)}
/> />
<div className="mb-3 row"> <div className="mb-3 row">
<div className="col-sm-10"> <div className="col-sm-10">
@ -434,7 +564,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
<button <button
type="button" type="button"
className="btn btn-secondary" className="btn btn-secondary"
onClick={linkEvent(this, this.handleCancel)} onClick={linkEvent(this, handleCancel)}
> >
{i18n.t("cancel")} {i18n.t("cancel")}
</button> </button>
@ -459,7 +589,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
role="button" role="button"
onClick={linkEvent( onClick={linkEvent(
{ i: this, suggestedTitle }, { i: this, suggestedTitle },
this.copySuggestedTitle copySuggestedTitle
)} )}
> >
{i18n.t("copy_suggested_title", { title: "" })} {suggestedTitle} {i18n.t("copy_suggested_title", { title: "" })} {suggestedTitle}
@ -517,69 +647,6 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
} }
} }
handlePostSubmit(i: PostForm, event: any) {
event.preventDefault();
// Coerce empty url string to undefined
if ((i.state.form.url ?? "") === "") {
i.setState(s => ((s.form.url = undefined), s));
}
i.setState({ loading: true, submitted: true });
const auth = myAuthRequired();
const pForm = i.state.form;
const pv = i.props.post_view;
if (pv) {
i.props.onEdit?.({
name: pForm.name,
url: pForm.url,
body: pForm.body,
nsfw: pForm.nsfw,
post_id: pv.post.id,
language_id: pForm.language_id,
auth,
});
} else if (pForm.name && pForm.community_id) {
i.props.onCreate?.({
name: pForm.name,
community_id: pForm.community_id,
url: pForm.url,
body: pForm.body,
nsfw: pForm.nsfw,
language_id: pForm.language_id,
honeypot: pForm.honeypot,
auth,
});
}
}
copySuggestedTitle(d: { i: PostForm; suggestedTitle?: string }) {
const sTitle = d.suggestedTitle;
if (sTitle) {
d.i.setState(
s => ((s.form.name = sTitle?.substring(0, MAX_POST_TITLE_LENGTH)), s)
);
d.i.setState({ suggestedPostsRes: { state: "empty" } });
setTimeout(() => {
const textarea: any = document.getElementById("post-title");
autosize.update(textarea);
}, 10);
}
}
handlePostUrlChange(i: PostForm, event: any) {
const url = event.target.value;
i.setState({
form: {
url,
},
imageDeleteUrl: "",
});
i.fetchPageTitle();
}
async fetchPageTitle() { async fetchPageTitle() {
const url = this.state.form.url; const url = this.state.form.url;
if (url && validURL(url)) { if (url && validURL(url)) {
@ -590,11 +657,6 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
} }
} }
handlePostNameChange(i: PostForm, event: any) {
i.setState(s => ((s.form.name = event.target.value), s));
i.fetchSimilarPosts();
}
async fetchSimilarPosts() { async fetchSimilarPosts() {
const q = this.state.form.name; const q = this.state.form.name;
if (q && q !== "") { if (q && q !== "") {
@ -618,84 +680,10 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
this.setState(s => ((s.form.body = val), s)); this.setState(s => ((s.form.body = val), s));
} }
handlePostCommunityChange(i: PostForm, event: any) {
i.setState(s => ((s.form.community_id = Number(event.target.value)), s));
}
handlePostNsfwChange(i: PostForm, event: any) {
i.setState(s => ((s.form.nsfw = event.target.checked), s));
}
handleLanguageChange(val: number[]) { handleLanguageChange(val: number[]) {
this.setState(s => ((s.form.language_id = val.at(0)), s)); this.setState(s => ((s.form.language_id = val.at(0)), s));
} }
handleHoneyPotChange(i: PostForm, event: any) {
i.setState(s => ((s.form.honeypot = event.target.value), s));
}
handleCancel(i: PostForm) {
i.props.onCancel?.();
}
handlePreviewToggle(i: PostForm, event: any) {
event.preventDefault();
i.setState({ previewMode: !i.state.previewMode });
}
handleImageUploadPaste(i: PostForm, event: any) {
const image = event.clipboardData.files[0];
if (image) {
i.handleImageUpload(i, image);
}
}
handleImageUpload(i: PostForm, event: any) {
let file: any;
if (event.target) {
event.preventDefault();
file = event.target.files[0];
} else {
file = event;
}
i.setState({ imageLoading: true });
HttpService.client.uploadImage({ image: file }).then(res => {
console.log("pictrs upload:");
console.log(res);
if (res.state === "success") {
if (res.data.msg === "ok") {
i.state.form.url = res.data.url;
i.setState({
imageLoading: false,
imageDeleteUrl: res.data.delete_url as string,
});
} else {
toast(JSON.stringify(res), "danger");
}
} else if (res.state === "failed") {
console.error(res.msg);
toast(res.msg, "danger");
i.setState({ imageLoading: false });
}
});
}
handleImageDelete(i: PostForm) {
const { imageDeleteUrl } = i.state;
fetch(imageDeleteUrl);
i.setState({
imageDeleteUrl: "",
imageLoading: false,
form: {
url: "",
},
});
}
handleCommunitySearch = debounce(async (text: string) => { handleCommunitySearch = debounce(async (text: string) => {
const { selectedCommunityChoice } = this.props; const { selectedCommunityChoice } = this.props;
this.setState({ communitySearchLoading: true }); this.setState({ communitySearchLoading: true });

View file

@ -21,7 +21,7 @@
"noFallthroughCasesInSwitch": true, "noFallthroughCasesInSwitch": true,
"paths": { "paths": {
"@/*": ["/*"], "@/*": ["/*"],
"@utils/*": ["shared/utils/*"], "@utils/*": ["shared/utils/*"]
} }
}, },
"include": [ "include": [