Skip to content

Commit

Permalink
feat: service-sdk refactor - moved key exchange inside (#314)
Browse files Browse the repository at this point in the history
* feat: service-sdk refactor - moved key exchange inside

* Update README.md

* fix: minor fixes

* Update README.md

* feat: take config in ctor

* Update README.md
  • Loading branch information
muke1908 committed Jul 9, 2023
1 parent c4babe1 commit 9d2b9ca
Show file tree
Hide file tree
Showing 23 changed files with 3,892 additions and 30,363 deletions.
12 changes: 7 additions & 5 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import path from "path";
import bodyParser from 'body-parser';
import cors from 'cors';
import express from 'express';
import path from 'path';

import apiController from './backend/api';

require("dotenv").config();

const app = express();
app.disable("x-powered-by");
import apiController from "./backend/api";
const corsOptions = {
origin: process.env.CHAT_LINK_DOMAIN || "localhost:3001"
};
Expand Down
12 changes: 6 additions & 6 deletions backend/api/chatLink/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import express from "express";
import asyncHandler from "../../middleware/asyncHandler";
import generateLink, { LinkType } from "./utils/link";
import channelValid, { CHANNEL_STATE } from "./utils/validateChannel";
import express from 'express';

import db from "../../db";
import { LINK_COLLECTION } from "../../db/const";
import db from '../../db';
import { LINK_COLLECTION } from '../../db/const';
import asyncHandler from '../../middleware/asyncHandler';
import generateLink, { LinkType } from './utils/link';
import channelValid, { CHANNEL_STATE } from './utils/validateChannel';

const router = express.Router({ mergeParams: true });

Expand Down
3 changes: 2 additions & 1 deletion backend/api/chatLink/utils/link.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { v4 } from 'uuid';

import generateLink from './link';
import { generatePIN } from './pin';
import { v4 } from 'uuid';

jest.mock('uuid', () => ({
v4: jest.fn().mockReturnValue('hash'),
Expand Down
3 changes: 2 additions & 1 deletion backend/api/chatLink/utils/pin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import crypto from "crypto";
import crypto from 'crypto';

const base36map = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

export const generatePIN = (uuid: string, pinLength = 4): string => {
Expand Down
4 changes: 2 additions & 2 deletions backend/api/chatLink/utils/validateChannel.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import channelValid, { CHANNEL_STATE } from './validateChannel';
import db from "../../../db";
import db from '../../../db';
import { LINK_COLLECTION } from '../../../db/const';
import channelValid, { CHANNEL_STATE } from './validateChannel';

jest.mock('../../../db');

Expand Down
6 changes: 3 additions & 3 deletions backend/api/chatLink/utils/validateChannel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LINK_COLLECTION } from "../../../db/const";
import db from "../../../db";
import { LinkType } from "./link";
import db from '../../../db';
import { LINK_COLLECTION } from '../../../db/const';
import { LinkType } from './link';

export enum CHANNEL_STATE {
"NOT_FOUND" = "NOT_FOUND",
Expand Down
6 changes: 3 additions & 3 deletions backend/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import express, { Request, Response } from 'express';

const router = express.Router({ mergeParams: true });
import chatLinkController from './chatLink';
import chatController from './messaging';

import chatController from "./messaging";
import chatLinkController from "./chatLink";
const router = express.Router({ mergeParams: true });

router.get("/", async (req: Request, res: Response) => {
res.send({ message: "/api is working!" });
Expand Down
26 changes: 15 additions & 11 deletions backend/api/messaging/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import express, { Request, Response } from "express";
import uploadImage from "../../external/uploadImage";
import db from "../../db";
import channelValid from "../chatLink/utils/validateChannel";
import { socketEmit, SOCKET_TOPIC } from "../../socket.io";
import getClientInstance from "../../socket.io/clients";
import asyncHandler from "../../middleware/asyncHandler";

import { PUBLIC_KEY_COLLECTION } from "../../db/const";
import { ChatMessageType, GetPublicKeyResponse, MessageResponse, SharePublicKeyResponse, UsersInChannelResponse } from "./types";
import express, { Request, Response } from 'express';

import db from '../../db';
import { PUBLIC_KEY_COLLECTION } from '../../db/const';
import uploadImage from '../../external/uploadImage';
import asyncHandler from '../../middleware/asyncHandler';
import { SOCKET_TOPIC, socketEmit } from '../../socket.io';
import getClientInstance from '../../socket.io/clients';
import channelValid from '../chatLink/utils/validateChannel';
import {
ChatMessageType, GetPublicKeyResponse, MessageResponse, SharePublicKeyResponse, UsersInChannelResponse
} from './types';

const router = express.Router({ mergeParams: true });
const clients = getClientInstance();
Expand Down Expand Up @@ -87,7 +89,9 @@ router.get(
}
const receiverID = clients.getReceiverIDBySenderID(userId as string, channel as string);
const data = await db.findOneFromDB<GetPublicKeyResponse>({ channel, user: receiverID }, PUBLIC_KEY_COLLECTION);
return res.send(data);
return res.send(data || {
public_key: null
});
})
);

Expand Down
9 changes: 4 additions & 5 deletions backend/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Db, MongoClient } from "mongodb";
import { Db, MongoClient } from 'mongodb';

import {
insertInDb as _insertInDb,
findOneFromDB as _findOneFromDB,
updateOneFromDb as _updateOneFromDb
} from "./inMemDB";
findOneFromDB as _findOneFromDB, insertInDb as _insertInDb, updateOneFromDb as _updateOneFromDb
} from './inMemDB';

const uri = process.env.MONGO_URI;
const dbName = process.env.MONGO_DB_NAME;
Expand Down
Loading

0 comments on commit 9d2b9ca

Please sign in to comment.