Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Websockets on updates #30

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ REDIS_PASSWORD=""
REDIS_URL="redis://${REDIS_USER}:${REDIS_PASSWORD}@${REDIS_HOST}:${REDIS_PORT}"

# Vercel Blob Storage
BLOB_READ_WRITE_TOKEN="vercel_blob_rw_0511953119b0c1167283c7453a088727" # Fake vercel token
BLOB_READ_WRITE_TOKEN="vercel_blob_rw_0511953119b0c1167283c7453a088727" # Fake vercel token

# Pusher
PUSHER_APP_ID="1684269"
PUSHER_APP_KEY="3a4575271634ad5a09ef"
PUSHER_APP_SECRET="486036ded3c32d02bac3"
PUSHER_APP_CLUSTER="eu"
7 changes: 6 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ module.exports = {
'^.+\\.(t|j)sx?$': ['@swc/jest'],
},
setupFilesAfterEnv: ['./src/test/setupTest.ts'],
collectCoverageFrom: ['./src/**', '!./src/plugins/prisma.ts', '!./src/server.ts'],
collectCoverageFrom: [
'./src/**',
'!./src/server.ts',
'!./src/plugins/prisma.ts',
'!./src/plugins/pusher.ts',
],
coverageReporters: ['json-summary', 'text', 'html', 'json'],
coveragePathIgnorePatterns: ['node_modules'],
};
47 changes: 46 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"dotenv": "^16.3.1",
"fastify": "^4.23.2",
"fastify-i18n": "^1.1.1",
"fastify-plugin": "^4.5.1"
"fastify-plugin": "^4.5.1",
"pusher": "^5.1.3"
},
"devDependencies": {
"@swc/core": "^1.3.85",
Expand Down
32 changes: 16 additions & 16 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ model Item {
ownerId Int
parentId Int?

owner User @relation(fields: [ownerId], references: [id])
parentItem Item? @relation("ItemToItem", fields: [parentId], references: [id])
Items Item[] @relation("ItemToItem")
ItemFolder ItemFolder?
ItemBlob ItemBlob?
ItemDocs ItemDocs?
ItemSharing ItemSharing[]
ItemShortcut ItemShortcut? @relation("shortcutItem")
LinkedItemShortcut ItemShortcut[] @relation("linkedItem")
ItemStarred ItemStarred[]
owner User @relation(fields: [ownerId], references: [id])
parentItem Item? @relation("ItemToItem", fields: [parentId], references: [id])
Items Item[] @relation("ItemToItem")
ItemFolder ItemFolder?
ItemBlob ItemBlob?
ItemDocs ItemDocs?
ItemSharing ItemSharing[]
ItemShortcut ItemShortcut? @relation("shortcutItem")
LinkedItemShortcut ItemShortcut[] @relation("linkedItem")
ItemStarred ItemStarred[]

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down Expand Up @@ -117,21 +117,21 @@ model ItemShortcut {

shortcutItem Item @relation("shortcutItem", fields: [itemId], references: [id], onDelete: Cascade)
linkedItem Item @relation("linkedItem", fields: [linkedItemId], references: [id], onDelete: Cascade)

@@index([linkedItemId])
}

model ItemStarred {
id Int @id @default(autoincrement())
itemId Int
userId Int
id Int @id @default(autoincrement())
itemId Int
userId Int

item Item @relation(fields: [itemId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

@@unique([itemId, userId])
@@index([itemId])
@@index([userId])
Expand Down
3 changes: 2 additions & 1 deletion src/modules/auth/__test__/login.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { User } from '@prisma/client';
import { jwt } from '../../../plugins/jwt';
import UserService from '../user.service';
import { UserServiceFactory } from '../auth.factory';

describe('POST /api/auth/login', () => {
let userService: UserService;
Expand All @@ -9,7 +10,7 @@ describe('POST /api/auth/login', () => {
const userPassword = '1234';

beforeAll(async () => {
userService = new UserService();
userService = UserServiceFactory.make();

user = await userService.createUser({
name: 'Joe Biden the 1st',
Expand Down
5 changes: 3 additions & 2 deletions src/modules/auth/__test__/logout.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { prisma } from '../../../plugins/prisma';
import UserService from '../user.service';
import AuthService from '../auth.service';
import { AuthServiceFactory, UserServiceFactory } from '../auth.factory';

describe('POST /api/auth/logout', () => {
let userService: UserService;
let authService: AuthService;

beforeAll(async () => {
userService = new UserService();
authService = new AuthService();
userService = UserServiceFactory.make();
authService = AuthServiceFactory.make();
});

beforeEach(async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/modules/auth/__test__/refresh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { jwt } from '../../../plugins/jwt';
import TimeUtil from '../../../utils/time';
import AuthService from '../auth.service';
import UserService from '../user.service';
import { AuthServiceFactory, UserServiceFactory } from '../auth.factory';

describe('POST /api/auth/refresh', () => {
let authService: AuthService;
Expand All @@ -13,8 +14,8 @@ describe('POST /api/auth/refresh', () => {
let user: User;

beforeAll(async () => {
authService = new AuthService();
userService = new UserService();
authService = AuthServiceFactory.make();
userService = UserServiceFactory.make();

user = await userService.createUser({
name: 'Joe Biden the 1st',
Expand Down
3 changes: 2 additions & 1 deletion src/modules/auth/__test__/register.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { prisma } from '../../../plugins/prisma';
import { UserServiceFactory } from '../auth.factory';
import UserService from '../user.service';

describe('POST /api/auth/register', () => {
let userService: UserService;

beforeAll(async () => {
userService = new UserService();
userService = UserServiceFactory.make();
});

beforeEach(async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/modules/auth/__test__/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import TimeUtil from '../../../utils/time';
import UserService from '../user.service';
import AuthService from '../auth.service';
import { v4 } from 'uuid';
import { AuthServiceFactory, UserServiceFactory } from '../auth.factory';

describe('GET /api/auth/user', () => {
let userService: UserService;
Expand All @@ -12,8 +13,8 @@ describe('GET /api/auth/user', () => {
let user: User;

beforeAll(async () => {
authService = new AuthService();
userService = new UserService();
authService = AuthServiceFactory.make();
userService = UserServiceFactory.make();

user = await userService.createUser({
name: 'Joe Biden the 1st',
Expand Down
21 changes: 21 additions & 0 deletions src/modules/auth/auth.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import AuthController from './auth.controller';
import AuthService from './auth.service';
import UserService from './user.service';

export class UserServiceFactory {
static make() {
return new UserService();
}
}

export class AuthServiceFactory {
static make() {
return new AuthService();
}
}

export class AuthControllerFactory {
static make() {
return new AuthController(AuthServiceFactory.make(), UserServiceFactory.make());
}
}
6 changes: 2 additions & 4 deletions src/modules/auth/auth.route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { FastifyInstance } from 'fastify';
import AuthController from './auth.controller';
import AuthService from './auth.service';
import UserService from './user.service';
import { AuthControllerFactory } from './auth.factory';

export default async (fastify: FastifyInstance) => {
const authController = new AuthController(new AuthService(), new UserService());
const authController = AuthControllerFactory.make();

fastify.post(
'/register',
Expand Down
17 changes: 11 additions & 6 deletions src/modules/item/__test__/item.read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import AuthService from '../../auth/auth.service';
import BlobService from '../blob/blob.service';
import DocsService from '../docs/docs.service';
import ShortcutService from '../shortcut/shortcut.service';
import { AuthServiceFactory, UserServiceFactory } from '../../auth/auth.factory';
import { FolderServiceFactory } from '../folder/folder.factory';
import { BlobServiceFactory } from '../blob/blob.factory';
import { DocsServiceFactory } from '../docs/docs.factory';
import { ShortcutServiceFactory } from '../shortcut/shortcut.factory';

describe('GET /api/item/:parentId', () => {
let userService: UserService;
Expand All @@ -18,12 +23,12 @@ describe('GET /api/item/:parentId', () => {
let otherUser: User;

beforeAll(async () => {
userService = new UserService();
folderService = new FolderService();
authService = new AuthService();
blobService = new BlobService();
docsService = new DocsService();
shortcutService = new ShortcutService();
userService = UserServiceFactory.make();
folderService = FolderServiceFactory.make();
authService = AuthServiceFactory.make();
blobService = BlobServiceFactory.make();
docsService = DocsServiceFactory.make();
shortcutService = ShortcutServiceFactory.make();

user = await userService.createUser({
name: 'Joe Biden the 1st',
Expand Down
17 changes: 11 additions & 6 deletions src/modules/item/__test__/item.root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import AuthService from '../../auth/auth.service';
import BlobService from '../blob/blob.service';
import DocsService from '../docs/docs.service';
import ShortcutService from '../shortcut/shortcut.service';
import { AuthServiceFactory, UserServiceFactory } from '../../auth/auth.factory';
import { FolderServiceFactory } from '../folder/folder.factory';
import { BlobServiceFactory } from '../blob/blob.factory';
import { DocsServiceFactory } from '../docs/docs.factory';
import { ShortcutServiceFactory } from '../shortcut/shortcut.factory';

describe('GET /api/item', () => {
let userService: UserService;
Expand All @@ -17,12 +22,12 @@ describe('GET /api/item', () => {
let user: User;

beforeAll(async () => {
userService = new UserService();
folderService = new FolderService();
authService = new AuthService();
blobService = new BlobService();
docsService = new DocsService();
shortcutService = new ShortcutService();
userService = UserServiceFactory.make();
folderService = FolderServiceFactory.make();
authService = AuthServiceFactory.make();
blobService = BlobServiceFactory.make();
docsService = DocsServiceFactory.make();
shortcutService = ShortcutServiceFactory.make();

user = await userService.createUser({
name: 'Joe Biden the 1st',
Expand Down
Loading