Skip to content

Commit

Permalink
chore(workflows): set up lint
Browse files Browse the repository at this point in the history
  • Loading branch information
retrouser955 committed Sep 27, 2024
1 parent 3083e48 commit 4851956
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -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
36 changes: 18 additions & 18 deletions src/hooks/user/helpers/Base/BaseUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = true> = T extends boolean ? string[] : undefined;

export interface UserInfo<T = true> {
username: string;
avatar: string;
displayName: string;
bio: string;
friends: string[];
friends: UserFriends<T>;
}

export const UserKeys = {
Expand Down Expand Up @@ -35,23 +37,32 @@ export class BaseUser {
_db: IGunInstance;

_user: GunUserInstance;
info?: UserInfo;
info?: UserInfo<true>;
_friends: string[] = []

constructor(db: IGunInstance, user: GunUserInstance, preventFetch = false) {
this._db = db;
this._user = user;

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<UserInfo> {
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;
}
Expand All @@ -62,7 +73,7 @@ export class BaseUser {
}

// WARN: ONLY CALL WHEN USER CHANGES THEIR DATA
async refetch(updateCache = false): Promise<UserInfo> {
async refetch(updateCache = false, friends: string[] = []): Promise<UserInfo> {
const bioPro = this.createPromiseGunGetUser(
UserKeys.Bio,
) as Promise<string>;
Expand All @@ -76,24 +87,13 @@ export class BaseUser {
const displaynamePro = this.createPromiseGunGetUser(
UserKeys.DisplayName,
) as Promise<string>;
const friendsPro = this.createPromiseGunGetUser(
UserKeys.Friends,
{} as Record<string, string>,
) as Promise<Record<string, string>>;

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,
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/user/helpers/User/ClientUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
});

Expand Down

0 comments on commit 4851956

Please sign in to comment.