Skip to content

Commit

Permalink
fix login, add support for .18
Browse files Browse the repository at this point in the history
  • Loading branch information
gkasdorf committed Oct 13, 2023
1 parent cacd661 commit 4b3801c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
65 changes: 61 additions & 4 deletions src/api/common/ApiInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { truncateText } from '@helpers/text';
import { buildCommentChains } from '@helpers/comments';
import { addCommentsToPost } from '@src/state/post/actions';
import { addComments } from '@src/state/comment/actions';
import { useSiteStore } from '@src/state/site/siteStore';

export enum EInitializeResult {
SUCCESS,
Expand All @@ -54,6 +55,8 @@ class ApiInstance {

authToken: string | null = null;

isUpdate: boolean = false;

async initialize(
options: ApiOptions,
signup = false,
Expand All @@ -75,7 +78,25 @@ class ApiInstance {
headers,
});

// We need to check the lemmy version real fast so we will just get and store the site here
// TODO This should be done elsewhere later as to not block loading

const siteRes = await this.instance.getSite();

console.log(siteRes.version);

if (siteRes.version.includes('.19')) {
this.isUpdate = true;
}

console.log(this.isUpdate);

// Save the site info
useSiteStore.getState().setSite(siteRes);

if (options.authToken != null) {
console.log(options.authToken);

this.initialized = true;
this.authToken = options.authToken;

Expand Down Expand Up @@ -132,10 +153,10 @@ class ApiInstance {
return EInitializeResult.PASSWORD;
}

options.authToken = res.jwt;
this.authToken = res.jwt;

void this.initialize(options);
await this.initialize({
...options,
authToken: res.jwt,
});

return EInitializeResult.SUCCESS;
} catch (e: any) {
Expand Down Expand Up @@ -233,6 +254,8 @@ class ApiInstance {
await this.instance?.likePost({
post_id: postId,
score: vote,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
usePostStore.setState((state) => {
Expand Down Expand Up @@ -299,6 +322,8 @@ class ApiInstance {
sort: 'New',
limit: 50,
page: 1,
// @ts-expect-error TODO Remove this later
auth: this.authToken,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand All @@ -310,6 +335,8 @@ class ApiInstance {
try {
return await this.instance?.getCommunity({
name,
// @ts-expect-error TODO Remove this later
auth: this.authToken,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand All @@ -325,6 +352,8 @@ class ApiInstance {
return await this.instance?.getReplies({
page,
limit,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand Down Expand Up @@ -369,6 +398,8 @@ class ApiInstance {
page: options.page,
community_id: options.communityId,
community_name: options.communityName,
// @ts-expect-error TODO Remove this later
auth: this.authToken,
});

const links: string[] = [];
Expand Down Expand Up @@ -425,6 +456,8 @@ class ApiInstance {
try {
return await this.instance?.getPost({
id: postId,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand Down Expand Up @@ -475,6 +508,8 @@ class ApiInstance {
return await this.instance?.listCommunities({
page,
limit,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand All @@ -487,6 +522,8 @@ class ApiInstance {
await this.instance?.markPostAsRead({
post_id: postId,
read: true,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand All @@ -501,6 +538,8 @@ class ApiInstance {
await this.instance?.followCommunity({
community_id: communityId,
follow: subscribe,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand All @@ -512,6 +551,8 @@ class ApiInstance {
await this.instance?.createCommentReport({
comment_id: commentId,
reason,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand All @@ -523,6 +564,8 @@ class ApiInstance {
await this.instance?.createPostReport({
post_id: postId,
reason,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand All @@ -534,6 +577,8 @@ class ApiInstance {
await this.instance?.blockPerson({
person_id: userId,
block,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand All @@ -545,6 +590,8 @@ class ApiInstance {
await this.instance?.blockCommunity({
community_id: communityId,
block,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand All @@ -556,6 +603,8 @@ class ApiInstance {
await this.instance?.editComment({
comment_id: commentId,
content,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand All @@ -570,6 +619,8 @@ class ApiInstance {
return await this.instance?.createComment({
post_id: postId,
content,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand All @@ -588,6 +639,8 @@ class ApiInstance {
language_id: options.languageId ?? 0, // TODO Fix this
community_id: options.communityId,
nsfw: options.nsfw,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand All @@ -600,6 +653,8 @@ class ApiInstance {
await this.instance?.deleteComment({
comment_id: commentId,
deleted: true,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand All @@ -611,6 +666,8 @@ class ApiInstance {
await this.instance?.deletePost({
post_id: postId,
deleted: true,
// @ts-expect-error TODO remove this later
auth: this.authToken!,
});
} catch (e: any) {
ApiInstance.handleError(e.toString());
Expand Down
2 changes: 0 additions & 2 deletions src/helpers/image/cacheImages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export const cacheImages = async (links: string[]): Promise<void> => {

for (const link of links) {
if (getLinkType(link) === 'image') {
console.log('Caching ', link);

imageLinks.push(link);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const useLogin = (): UseLogin => {
error: false,
});

const doLogin = async(options: DoLoginOptions): Promise<void> => {
const doLogin = async (options: DoLoginOptions): Promise<void> => {
setStatus({
loading: true,
error: false,
Expand Down
20 changes: 20 additions & 0 deletions src/state/site/siteStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { GetSiteResponse } from 'lemmy-js-client';
import { immer } from 'zustand/middleware/immer';
import { create } from 'zustand';

interface SiteStore {
site?: GetSiteResponse;
setSite: (site: GetSiteResponse) => void;
}

export const useSiteStore = create(
immer<SiteStore>((set) => ({
site: undefined,

setSite: (site: GetSiteResponse) => {
set((state) => {
state.site = site;
});
},
})),
);

0 comments on commit 4b3801c

Please sign in to comment.