From 485195689fb27ef318397c4dc1ecd327475a4a30 Mon Sep 17 00:00:00 2001 From: Retro <70205403+retrouser955@users.noreply.github.com> Date: Fri, 27 Sep 2024 19:02:11 +0630 Subject: [PATCH] chore(workflows): set up lint --- .github/workflows/lint.yml | 27 +++++++++++++++++ src/hooks/user/helpers/Base/BaseUser.ts | 36 +++++++++++------------ src/hooks/user/helpers/User/ClientUser.ts | 6 +++- 3 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/lint.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..6b7069d --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,27 @@ +name: Pull Request Test + +on: + pull_request: + branches: [master] + +jobs: + lint: + name: Run ESLint + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install NodeJS + uses: actions/setup-node@v3 + with: + node-version: 20 + + - name: Install PNPM + run: npm install -g pnpm + + - name: Install dependencies + run: pnpm install + + - name: Run lints + run: pnpm lint \ No newline at end of file diff --git a/src/hooks/user/helpers/Base/BaseUser.ts b/src/hooks/user/helpers/Base/BaseUser.ts index 3fbede2..8680e79 100644 --- a/src/hooks/user/helpers/Base/BaseUser.ts +++ b/src/hooks/user/helpers/Base/BaseUser.ts @@ -2,12 +2,14 @@ import { IGunInstance } from 'gun'; import type { GunUserInstance } from '../../useMainUser'; import { Util } from '@/lib/utils/Utils/Util'; -export interface UserInfo { +type UserFriends = T extends boolean ? string[] : undefined; + +export interface UserInfo { username: string; avatar: string; displayName: string; bio: string; - friends: string[]; + friends: UserFriends; } export const UserKeys = { @@ -35,7 +37,8 @@ export class BaseUser { _db: IGunInstance; _user: GunUserInstance; - info?: UserInfo; + info?: UserInfo; + _friends: string[] = [] constructor(db: IGunInstance, user: GunUserInstance, preventFetch = false) { this._db = db; @@ -43,15 +46,23 @@ export class BaseUser { if (!preventFetch) { (async () => { - this.info = await this.refetch(); + this.info = await this.refetch(false, this._friends); })(); } } + /** + * THIS METHOD IS ONLY FOR THE LISTENER AND DOES NOT ACTUALLY UPDATE THE DATABASE. DONT USE IT UNLESS YOURE RETRO + */ + pushFriends(...friend: string[]) { + this._friends.push(...friend) + this.info?.friends.push(...friend) + } + async fetch(): Promise { if (this.info) return this.info; else { - const info = await this.refetch(); + const info = await this.refetch(false, this._friends); this._setUserInfo(info); return info; } @@ -62,7 +73,7 @@ export class BaseUser { } // WARN: ONLY CALL WHEN USER CHANGES THEIR DATA - async refetch(updateCache = false): Promise { + async refetch(updateCache = false, friends: string[] = []): Promise { const bioPro = this.createPromiseGunGetUser( UserKeys.Bio, ) as Promise; @@ -76,24 +87,13 @@ export class BaseUser { const displaynamePro = this.createPromiseGunGetUser( UserKeys.DisplayName, ) as Promise; - const friendsPro = this.createPromiseGunGetUser( - UserKeys.Friends, - {} as Record, - ) as Promise>; - console.log("Polling"); - const [bio, avatar, displayName, rawFriends] = await Promise.all([ + const [bio, avatar, displayName] = await Promise.all([ bioPro, avatarPro, displaynamePro, - friendsPro, ]); - console.log("Got data"); - const friends = JSON.parse(JSON.stringify(rawFriends)) as { [key: string]: string }; - console.log("Re", friends) - delete friends._ - const userData: UserInfo = { bio, avatar, diff --git a/src/hooks/user/helpers/User/ClientUser.ts b/src/hooks/user/helpers/User/ClientUser.ts index f062996..74989fe 100644 --- a/src/hooks/user/helpers/User/ClientUser.ts +++ b/src/hooks/user/helpers/User/ClientUser.ts @@ -44,7 +44,7 @@ export class ClientUser extends BaseUser { const peer = await cache.fetch(pub); - this._user.get("friends").get(peer.info.username).put("%removed%"); + this._user.get("friends").get(peer.info.username).put(null); } onFriendsUpdate(onUpdate: OnFriendsUpdateHandler, forceMultiple = false) { @@ -62,9 +62,13 @@ export class ClientUser extends BaseUser { const map = await Promise.all( Object.values(data).map((v) => { if (v.startsWith('~')) v = v.replace('~', ''); + + this.pushFriends(v) + return cache.fetch(v); }), ); + onUpdate(map); });