Skip to content

Commit

Permalink
Merge pull request #124 from tgxn/0.19
Browse files Browse the repository at this point in the history
0.19 compatibility
  • Loading branch information
tgxn authored Dec 2, 2023
2 parents a5df801 + 07446b1 commit 72417c4
Show file tree
Hide file tree
Showing 10 changed files with 404 additions and 324 deletions.
502 changes: 251 additions & 251 deletions package-lock.json

Large diffs are not rendered by default.

29 changes: 15 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lemmy-modder",
"version": "1.3.10",
"version": "1.9.1",
"description": "Lemmy Moderation App",
"author": "tgxn",
"license": "MIT",
Expand All @@ -14,27 +14,28 @@
"test": "mocha --require @babel/register --require @babel/polyfill ./tests/api.mjs"
},
"dependencies": {
"@babel/core": "^7.23.3",
"@babel/preset-env": "^7.23.3",
"@babel/core": "^7.23.5",
"@babel/preset-env": "^7.23.5",
"@babel/preset-react": "^7.23.3",
"@braintree/sanitize-url": "^6.0.2",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@mui/base": "^5.0.0-beta.23",
"@mui/icons-material": "^5.14.16",
"@mui/joy": "^5.0.0-beta.14",
"@mui/lab": "^5.0.0-alpha.152",
"@mui/base": "^5.0.0-beta.25",
"@mui/icons-material": "^5.14.19",
"@mui/joy": "^5.0.0-beta.16",
"@mui/lab": "^5.0.0-alpha.154",
"@reduxjs/toolkit": "^1.9.7",
"@tanstack/react-query": "^5.8.1",
"@tanstack/react-query-devtools": "^5.8.1",
"axios": "^1.6.1",
"@tanstack/react-query": "^5.12.1",
"@tanstack/react-query-devtools": "^5.12.1",
"axios": "^1.6.2",
"babel-loader": "^9.1.3",
"copy-webpack-plugin": "^11.0.0",
"css-loader": "^6.8.1",
"css-minimizer-webpack-plugin": "^5.0.1",
"file-loader": "^6.2.0",
"html-webpack-plugin": "^5.5.3",
"lemmy-js-client": "^0.18.1",
"lemmy-js-client18": "npm:lemmy-js-client@^0.18.1",
"lemmy-js-client": "npm:lemmy-js-client@^0.19.0-rc.19",
"moment": "^2.29.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand All @@ -43,13 +44,13 @@
"react-moment": "^1.1.3",
"react-number-format": "^5.3.1",
"react-redux": "^8.1.1",
"react-router-dom": "^6.18.0",
"react-router-dom": "^6.20.1",
"redux": "^4.2.1",
"remove-markdown": "^0.5.0",
"redux-persist": "^6.0.0",
"sass": "^1.69.5",
"sass-loader": "^13.3.2",
"sonner": "^1.2.0",
"sonner": "^1.2.4",
"style-loader": "^3.3.3",
"url-loader": "^4.1.1",
"webpack": "^5.89.0",
Expand All @@ -59,6 +60,6 @@
"devDependencies": {
"@babel/polyfill": "^7.12.1",
"@babel/register": "^7.22.5",
"prettier": "^3.0.3"
"prettier": "^3.1.0"
}
}
2 changes: 1 addition & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ const queryClient = new QueryClient();
export default function App() {
return (
<QueryClientProvider client={queryClient}>
<ReactQueryDevtools initialIsOpen={false} buttonPosition="bottom-left" />
<ReactQueryDevtools initialIsOpen={false} />

<Provider store={store}>
<PageRouter />
Expand Down
19 changes: 13 additions & 6 deletions src/components/Header/AccountMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { useDispatch, useSelector } from "react-redux";

import { useQueryClient } from "@tanstack/react-query";

import { LemmyHttp } from "lemmy-js-client";
import LemmyHttpMixed from "../../lib/LemmyHttpMixed.js";
// import { LemmyHttp } from "lemmy-js-client";
import { Toaster, toast } from "sonner";

import Menu from "@mui/joy/Menu";
Expand Down Expand Up @@ -63,19 +64,25 @@ function UserListItem({ user }) {
dispatch(setAccountIsLoading(true));

try {
const lemmyClient = new LemmyHttp(`https://${user.base}`);
const lemmyClient = new LemmyHttpMixed(`https://${user.base}`);
await lemmyClient.setupAuth(user.jwt);
const getSite = await lemmyClient.call("getSite");

const getSite = await lemmyClient.getSite({
auth: user.jwt,
});
// const lemmyClient = new LemmyHttp(`https://${user.base}`, {
// headers: {
// Authorization: `Bearer ${user.jwt}`,
// },
// });

// const getSite = await lemmyClient.getSite();

// there must be a user returned in this api call
if (!getSite.my_user) {
throw new Error("jwt does not provide auth, re-authenticate");
}

// TODO we need to update the user's details in the saved accounts array too, if this is a saved session
dispatch(setCurrentUser({base: user.base, jwt: user.jwt, site: getSite}));
dispatch(setCurrentUser({ base: user.base, jwt: user.jwt, site: getSite }));
} catch (e) {
toast(typeof e == "string" ? e : e.message);
} finally {
Expand Down
42 changes: 27 additions & 15 deletions src/hooks/useLemmyHttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { useDispatch, useSelector } from "react-redux";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { getSiteData } from "../hooks/getSiteData";

import { LemmyHttp } from "lemmy-js-client";
import LemmyHttpMixed from "../lib/LemmyHttpMixed";
// import { LemmyHttp } from "lemmy-js-client";

import {
selectCurrentUser,
Expand All @@ -30,12 +31,9 @@ export function useLemmyHttp(callLemmyMethod, formData = {}) {
const { isSuccess, isLoading, isError, error, data, isFetching, refetch } = useQuery({
queryKey: ["lemmyHttp", localPerson.id, callLemmyMethod, formDataArray],
queryFn: async () => {
const lemmyClient = new LemmyHttp(`https://${currentUser.base}`);

const siteData = await lemmyClient[callLemmyMethod]({
auth: currentUser.jwt,
...formData,
});
const lemmyClient = new LemmyHttpMixed(`https://${currentUser.base}`);
await lemmyClient.setupAuth(currentUser.jwt);
const siteData = await lemmyClient.call(callLemmyMethod, formData);

return siteData;
},
Expand All @@ -62,10 +60,18 @@ export function useLemmyHttpAction(callLemmyMethod) {

const mutation = useMutation({
mutationFn: async (formData) => {
const lemmyClient = new LemmyHttp(`https://${currentUser.base}`);

const resultData = await lemmyClient[callLemmyMethod]({
auth: currentUser.jwt,
const lemmyClientAuthed = new LemmyHttpMixed(`https://${currentUser.base}`);
await lemmyClientAuthed.setupAuth(currentUser.jwt);
// const getSite = await lemmyClientAuthed.call("getSite");

// const lemmyClient = new LemmyHttp(`https://${currentUser.base}`, {
// headers: {
// Authorization: `Bearer ${currentUser.jwt}`,
// },
// });

const resultData = await lemmyClientAuthed.call(callLemmyMethod, {
// auth: currentUser.jwt,
...formData,
});

Expand Down Expand Up @@ -97,11 +103,17 @@ export function refreshAllData() {
mutationFn: async () => {
dispatch(setAccountIsLoading(true));

const lemmyClient = new LemmyHttp(`https://${currentUser.base}`);
const lemmyClientAuthed = new LemmyHttpMixed(`https://${currentUser.base}`);
await lemmyClientAuthed.setupAuth(currentUser.jwt);
const getSite = await lemmyClientAuthed.call("getSite");

const getSite = await lemmyClient.getSite({
auth: currentUser.jwt,
});
// const lemmyClient = new LemmyHttp(`https://${currentUser.base}`, {
// headers: {
// Authorization: `Bearer ${currentUser.jwt}`,
// },
// });

// const getSite = await lemmyClient.getSite();

dispatch(updateCurrentUserData(getSite));

Expand Down
17 changes: 13 additions & 4 deletions src/hooks/useLemmyInfinite.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { getSiteData } from "../hooks/getSiteData";

import { useSelector } from "react-redux";

import { LemmyHttp } from "lemmy-js-client";
import LemmyHttpMixed from "../lib/LemmyHttpMixed";
// import { LemmyHttp } from "lemmy-js-client";
import { selectCurrentUser } from "../redux/reducer/accountReducer";

export default function useLemmyInfinite({
Expand Down Expand Up @@ -50,10 +51,18 @@ export default function useLemmyInfinite({
queryFn: async ({ pageParam = 1, ...rest }, optional) => {
console.log("LemmyHttp inner infinite", callLemmyMethod, pageParam, rest, optional);

const lemmyClient = new LemmyHttp(`https://${currentUser.base}`);
const lemmyClientAuthed = new LemmyHttpMixed(`https://${currentUser.base}`);
await lemmyClientAuthed.setupAuth(currentUser.jwt);
// const getSite = await lemmyClientAuthed.call("getSite");

const apiResultData = await lemmyClient[callLemmyMethod]({
auth: currentUser.jwt,
// const lemmyClient = new LemmyHttp(`https://${currentUser.base}`, {
// headers: {
// Authorization: `Bearer ${currentUser.jwt}`,
// },
// });

const apiResultData = await lemmyClientAuthed.call(callLemmyMethod, {
// auth: currentUser.jwt,
page: pageParam,
limit: perPage,
...formData,
Expand Down
6 changes: 1 addition & 5 deletions src/hooks/useLemmyModLog.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { useEffect, useMemo } from "react";

import { useQuery, useMutation, useInfiniteQuery } from "@tanstack/react-query";
import { useMemo } from "react";

import { getSiteData } from "../hooks/getSiteData";

import { useSelector } from "react-redux";

import { LemmyHttp } from "lemmy-js-client";

import useLemmyInfinite from "./useLemmyInfinite";
import { getModLogTypeNames } from "../utils";
import { selectModLogCommunityId, selectModLogType } from "../redux/reducer/configReducer";
Expand Down
2 changes: 0 additions & 2 deletions src/hooks/useLemmyReports.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { getSiteData } from "../hooks/getSiteData";

import { useSelector } from "react-redux";

import { LemmyHttp } from "lemmy-js-client";

import useLemmyInfinite from "./useLemmyInfinite";
import {
selectFilterCommunity,
Expand Down
61 changes: 61 additions & 0 deletions src/lib/LemmyHttpMixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Re-Export LemmyHttp with Version Helpers

import { LemmyHttp } from "lemmy-js-client18";
import { LemmyHttp as LemmyHttp19 } from "lemmy-js-client";

export default class LemmyHttpMixed {
constructor(baseUrl, options = {}) {
this.baseUrl = baseUrl;

this.lemmyClient = new LemmyHttp(baseUrl, options);
this.lemmyClient19 = new LemmyHttp19(baseUrl, options);
}

async getSiteVersion() {
// load cache
const cachedVersion = localStorage.getItem(this.baseUrl + ".version");
const cachedExpires = localStorage.getItem(this.baseUrl + ".expires");

// check if cache is valid
if (cachedVersion && cachedExpires && cachedExpires > Date.now()) {
return cachedVersion;
}

// call getSite and store version
const siteData = await this.lemmyClient.getSite();

// cache in cookies for 2 hours
localStorage.setItem(this.baseUrl + ".version", siteData.version);
localStorage.setItem(this.baseUrl + ".expires", Date.now() + 1000 * 60 * 60 * 2);

return siteData.version;
}

async setupAuth(userJwt) {
this.userJwt = userJwt;

this.lemmyClient19.setHeaders({
Authorization: `Bearer ${userJwt}`,
});
}

async call(method, formData) {
// get and cache version
if (!this.version) {
this.version = await this.getSiteVersion();
}
console.log("this.version", this.version);

// check version
if (this.version.indexOf("0.18") === -1) {
// call super
return await this.lemmyClient19[method](formData);
} else {
// call super
return await this.lemmyClient[method]({
auth: this.userJwt,
...formData,
});
}
}
}
48 changes: 22 additions & 26 deletions src/pages/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ import ListItemContent from "@mui/joy/ListItemContent";
import IconButton from "@mui/joy/IconButton";
import Delete from "@mui/icons-material/Delete";

import LemmyHttpMixed from "../lib/LemmyHttpMixed";
import { LemmyHttp } from "lemmy-js-client";

import { useLemmyUserDataAction } from "../hooks/useLemmyHttp";

import {
addUser,
setAccountIsLoading,
Expand Down Expand Up @@ -74,29 +73,20 @@ export default function LoginForm() {

const auth = await lemmyClient.login(loginPayload);

/**
* 0.19.x :/ hmm
// this shows the `jwt` is present 👍
// as long aws there is a JWT in the response from login, logged in!
if (auth.jwt) {
console.log("auth", auth);
const lemmyClientAuthed = new LemmyHttpMixed(`https://${instanceBase}`);
await lemmyClientAuthed.setupAuth(auth.jwt);
const getSite = await lemmyClientAuthed.call("getSite");

// cookie is called `auth` now.
lemmyClient.setHeaders({
"Cookie": `auth=${auth.jwt}`
});
// this no longer has any params !
const getSite = await lemmyClient.getSite();
console.log("getSite", getSite);
// const lemmyClientAuthed = new LemmyHttp(`https://${instanceBase}`, {
// headers: {
// Authorization: `Bearer ${auth.jwt}`,
// },
// });

*
*/

// as long aws there is a JWT in the response from login, logged in!
if (auth.jwt) {
const getSite = await lemmyClient.getSite({
auth: auth.jwt,
});
// const getSite = await lemmyClientAuthed.getSite();

// save if they chose to
if (saveSession) {
Expand Down Expand Up @@ -316,11 +306,17 @@ export default function LoginForm() {
dispatch(setAccountIsLoading(true));

try {
const lemmyClient = new LemmyHttp(`https://${user.base}`);
const lemmyClient = new LemmyHttpMixed(`https://${user.base}`);
await lemmyClient.setupAuth(user.jwt);
const getSite = await lemmyClient.call("getSite");

// const lemmyClientAuthed = new LemmyHttp(`https://${user.base}`, {
// headers: {
// Authorization: `Bearer ${user.jwt}`,
// },
// });

const getSite = await lemmyClient.getSite({
auth: user.jwt,
});
// const getSite = await lemmyClientAuthed.getSite();

if (!getSite || !getSite.my_user) {
// set instance base to the current instance for easy login
Expand Down

0 comments on commit 72417c4

Please sign in to comment.