From 6835e388f1e9181f0a40062b5a549c0f4f6ad338 Mon Sep 17 00:00:00 2001 From: hackertron Date: Thu, 19 Oct 2023 20:11:58 +0530 Subject: [PATCH] get unverified urls of users in match making --- package-lock.json | 82 ++++++++++++++----------- package.json | 3 +- pnpm-lock.yaml | 47 +++++++++----- src/controllers/index.ts | 1 + src/controllers/match.ts | 3 + src/controllers/users.ts | 1 + src/{controllers => lib}/matchmaking.ts | 39 ++++++++---- src/models/match.ts | 5 ++ src/models/schema.ts | 5 ++ src/routes/contracts.ts | 2 +- tsconfig.json | 2 +- 11 files changed, 123 insertions(+), 67 deletions(-) create mode 100644 src/controllers/match.ts rename src/{controllers => lib}/matchmaking.ts (77%) diff --git a/package-lock.json b/package-lock.json index 2f3bd25..cca06c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "cors": "^2.8.5", "cross-env": "^7.0.3", "dotenv": "^16.0.3", - "ethers": "^6.1.0", + "ethers": "^6.8.0", "express": "^4.18.2", "express-jwt": "^8.4.1", "jest": "^29.6.1", @@ -34,6 +34,7 @@ "@types/supertest": "^2.0.13", "@types/swagger-jsdoc": "^6.0.1", "@types/swagger-ui-express": "^4.1.4", + "ethers-typescript-typings": "^0.0.4", "faker": "^5.5.3", "nodemon": "^2.0.22", "swagger-jsdoc": "^6.2.8", @@ -44,9 +45,9 @@ } }, "node_modules/@adraffy/ens-normalize": { - "version": "1.8.9", - "resolved": "https://registry.npmjs.org/@adraffy/ens-normalize/-/ens-normalize-1.8.9.tgz", - "integrity": "sha512-93OmGCV0vO8+JQ3FHG+gZk/MPHzzMPDRiCiFcCQNTCnHaaxsacO3ScTPGlu2wX2dOtgfalbchPcw1cOYYjHCYQ==" + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/@adraffy/ens-normalize/-/ens-normalize-1.10.0.tgz", + "integrity": "sha512-nA9XHtlAkYfJxY7bce8DcN7eKxWWCWkU+1GR9d+U6MbNpfwQp8TI7vqOsBsMcHoT4mBu2kypKoSKnghEzOOq5Q==" }, "node_modules/@ampproject/remapping": { "version": "2.2.1", @@ -1051,27 +1052,27 @@ "integrity": "sha512-4JQNk+3mVzK3xh2rqd6RB4J46qUR19azEHBneZyTZM+c456qOrbbM/5xcR8huNCCcbVt7+UmizG6GuUvPvKUYg==", "dev": true }, - "node_modules/@noble/hashes": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.1.2.tgz", - "integrity": "sha512-KYRCASVTv6aeUi1tsF8/vpyR7zpfs3FUzy2Jqm+MU+LmUKhQ0y2FpfwqkCcxSg2ua4GALJd8k2R76WxwZGbQpA==", - "funding": [ - { - "type": "individual", - "url": "https://paulmillr.com/funding/" - } - ] + "node_modules/@noble/curves": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.2.0.tgz", + "integrity": "sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==", + "dependencies": { + "@noble/hashes": "1.3.2" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } }, - "node_modules/@noble/secp256k1": { - "version": "1.7.1", - "resolved": "https://registry.npmjs.org/@noble/secp256k1/-/secp256k1-1.7.1.tgz", - "integrity": "sha512-hOUk6AyBFmqVrv7k5WAw/LpszxVbj9gGN4JRkIX52fdFAj1UA61KXmZDvqVEm+pOyec3+fIeZB02LYa/pWOArw==", - "funding": [ - { - "type": "individual", - "url": "https://paulmillr.com/funding/" - } - ] + "node_modules/@noble/hashes": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.2.tgz", + "integrity": "sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==", + "engines": { + "node": ">= 16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } }, "node_modules/@sinclair/typebox": { "version": "0.27.8", @@ -1296,9 +1297,9 @@ } }, "node_modules/@types/node": { - "version": "18.15.3", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.3.tgz", - "integrity": "sha512-p6ua9zBxz5otCmbpb5D3U4B5Nanw6Pk3PPyX05xnxbB/fRv71N7CPmORg7uAD5P70T0xmx1pzAx/FUfa5X+3cw==" + "version": "18.15.13", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.13.tgz", + "integrity": "sha512-N+0kuo9KgrUQ1Sn/ifDXsvg0TTleP7rIy4zOBGECxAljqvqfqpTfzx0Q1NUedOixRMBfe2Whhb056a42cWs26Q==" }, "node_modules/@types/nodemon": { "version": "1.19.3", @@ -1472,9 +1473,9 @@ } }, "node_modules/aes-js": { - "version": "4.0.0-beta.3", - "resolved": "https://registry.npmjs.org/aes-js/-/aes-js-4.0.0-beta.3.tgz", - "integrity": "sha512-/xJX0/VTPcbc5xQE2VUP91y1xN8q/rDfhEzLm+vLc3hYvb5+qHCnpJRuFcrKn63zumK/sCwYYzhG8HP78JYSTA==" + "version": "4.0.0-beta.5", + "resolved": "https://registry.npmjs.org/aes-js/-/aes-js-4.0.0-beta.5.tgz", + "integrity": "sha512-G965FqalsNyrPqgEGON7nIx1e/OVENSgiEIzyC63haUMuvNnwIgIjMs52hlTCKhkBny7A2ORNlfY9Zu+jmGk1Q==" }, "node_modules/ansi-escapes": { "version": "4.3.2", @@ -2341,9 +2342,9 @@ } }, "node_modules/ethers": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/ethers/-/ethers-6.1.0.tgz", - "integrity": "sha512-aC45YGbvgXt7Nses5WsdQwc1cUIrrQt32zeFShNW7ZT3RQCIHBnd4nmbE5sJmrp70uTdwkRHkr4cZr1D/YwFPg==", + "version": "6.8.0", + "resolved": "https://registry.npmjs.org/ethers/-/ethers-6.8.0.tgz", + "integrity": "sha512-zrFbmQRlraM+cU5mE4CZTLBurZTs2gdp2ld0nG/f3ecBK+x6lZ69KSxBqZ4NjclxwfTxl5LeNufcBbMsTdY53Q==", "funding": [ { "type": "individual", @@ -2355,10 +2356,11 @@ } ], "dependencies": { - "@adraffy/ens-normalize": "1.8.9", - "@noble/hashes": "1.1.2", - "@noble/secp256k1": "1.7.1", - "aes-js": "4.0.0-beta.3", + "@adraffy/ens-normalize": "1.10.0", + "@noble/curves": "1.2.0", + "@noble/hashes": "1.3.2", + "@types/node": "18.15.13", + "aes-js": "4.0.0-beta.5", "tslib": "2.4.0", "ws": "8.5.0" }, @@ -2366,6 +2368,12 @@ "node": ">=14.0.0" } }, + "node_modules/ethers-typescript-typings": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/ethers-typescript-typings/-/ethers-typescript-typings-0.0.4.tgz", + "integrity": "sha512-YvMFLq5JO4ge12UVAVL1NlDcGl4qbYR/fT7EqAXvnIUiwGNpSSEYUhKRKac4WXeb2Iti/TBJLMm7QgzEOt4hDQ==", + "dev": true + }, "node_modules/exec-limiter": { "version": "3.2.13", "resolved": "https://registry.npmjs.org/exec-limiter/-/exec-limiter-3.2.13.tgz", diff --git a/package.json b/package.json index 2d53a0e..a8fe7c3 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "cors": "^2.8.5", "cross-env": "^7.0.3", "dotenv": "^16.0.3", - "ethers": "^6.1.0", + "ethers": "^6.8.0", "express": "^4.18.2", "express-jwt": "^8.4.1", "jest": "^29.6.1", @@ -42,6 +42,7 @@ "@types/supertest": "^2.0.13", "@types/swagger-jsdoc": "^6.0.1", "@types/swagger-ui-express": "^4.1.4", + "ethers-typescript-typings": "^0.0.4", "faker": "^5.5.3", "nodemon": "^2.0.22", "swagger-jsdoc": "^6.2.8", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bb3d8af..42d817e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,8 +14,8 @@ dependencies: specifier: ^16.0.3 version: 16.0.3 ethers: - specifier: ^6.1.0 - version: 6.1.0 + specifier: ^6.8.0 + version: 6.8.0 express: specifier: ^4.18.2 version: 4.18.2 @@ -72,6 +72,9 @@ devDependencies: '@types/swagger-ui-express': specifier: ^4.1.4 version: 4.1.4 + ethers-typescript-typings: + specifier: ^0.0.4 + version: 0.0.4 faker: specifier: ^5.5.3 version: 5.5.3 @@ -96,8 +99,8 @@ devDependencies: packages: - /@adraffy/ens-normalize@1.8.9: - resolution: {integrity: sha512-93OmGCV0vO8+JQ3FHG+gZk/MPHzzMPDRiCiFcCQNTCnHaaxsacO3ScTPGlu2wX2dOtgfalbchPcw1cOYYjHCYQ==} + /@adraffy/ens-normalize@1.10.0: + resolution: {integrity: sha512-nA9XHtlAkYfJxY7bce8DcN7eKxWWCWkU+1GR9d+U6MbNpfwQp8TI7vqOsBsMcHoT4mBu2kypKoSKnghEzOOq5Q==} dev: false /@ampproject/remapping@2.2.1: @@ -686,12 +689,15 @@ packages: resolution: {integrity: sha512-4JQNk+3mVzK3xh2rqd6RB4J46qUR19azEHBneZyTZM+c456qOrbbM/5xcR8huNCCcbVt7+UmizG6GuUvPvKUYg==} dev: true - /@noble/hashes@1.1.2: - resolution: {integrity: sha512-KYRCASVTv6aeUi1tsF8/vpyR7zpfs3FUzy2Jqm+MU+LmUKhQ0y2FpfwqkCcxSg2ua4GALJd8k2R76WxwZGbQpA==} + /@noble/curves@1.2.0: + resolution: {integrity: sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==} + dependencies: + '@noble/hashes': 1.3.2 dev: false - /@noble/secp256k1@1.7.1: - resolution: {integrity: sha512-hOUk6AyBFmqVrv7k5WAw/LpszxVbj9gGN4JRkIX52fdFAj1UA61KXmZDvqVEm+pOyec3+fIeZB02LYa/pWOArw==} + /@noble/hashes@1.3.2: + resolution: {integrity: sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==} + engines: {node: '>= 16'} dev: false /@sinclair/typebox@0.27.8: @@ -856,6 +862,10 @@ packages: - snappy dev: false + /@types/node@18.15.13: + resolution: {integrity: sha512-N+0kuo9KgrUQ1Sn/ifDXsvg0TTleP7rIy4zOBGECxAljqvqfqpTfzx0Q1NUedOixRMBfe2Whhb056a42cWs26Q==} + dev: false + /@types/node@20.1.2: resolution: {integrity: sha512-CTO/wa8x+rZU626cL2BlbCDzydgnFNgc19h4YvizpTO88MFQxab8wqisxaofQJ/9bLGugRdWIuX/TbIs6VVF6g==} @@ -974,8 +984,8 @@ packages: engines: {node: '>=0.4.0'} hasBin: true - /aes-js@4.0.0-beta.3: - resolution: {integrity: sha512-/xJX0/VTPcbc5xQE2VUP91y1xN8q/rDfhEzLm+vLc3hYvb5+qHCnpJRuFcrKn63zumK/sCwYYzhG8HP78JYSTA==} + /aes-js@4.0.0-beta.5: + resolution: {integrity: sha512-G965FqalsNyrPqgEGON7nIx1e/OVENSgiEIzyC63haUMuvNnwIgIjMs52hlTCKhkBny7A2ORNlfY9Zu+jmGk1Q==} dev: false /ansi-escapes@4.3.2: @@ -1547,14 +1557,19 @@ packages: resolution: {integrity: sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==} engines: {node: '>= 0.6'} - /ethers@6.1.0: - resolution: {integrity: sha512-aC45YGbvgXt7Nses5WsdQwc1cUIrrQt32zeFShNW7ZT3RQCIHBnd4nmbE5sJmrp70uTdwkRHkr4cZr1D/YwFPg==} + /ethers-typescript-typings@0.0.4: + resolution: {integrity: sha512-YvMFLq5JO4ge12UVAVL1NlDcGl4qbYR/fT7EqAXvnIUiwGNpSSEYUhKRKac4WXeb2Iti/TBJLMm7QgzEOt4hDQ==} + dev: true + + /ethers@6.8.0: + resolution: {integrity: sha512-zrFbmQRlraM+cU5mE4CZTLBurZTs2gdp2ld0nG/f3ecBK+x6lZ69KSxBqZ4NjclxwfTxl5LeNufcBbMsTdY53Q==} engines: {node: '>=14.0.0'} dependencies: - '@adraffy/ens-normalize': 1.8.9 - '@noble/hashes': 1.1.2 - '@noble/secp256k1': 1.7.1 - aes-js: 4.0.0-beta.3 + '@adraffy/ens-normalize': 1.10.0 + '@noble/curves': 1.2.0 + '@noble/hashes': 1.3.2 + '@types/node': 18.15.13 + aes-js: 4.0.0-beta.5 tslib: 2.4.0 ws: 8.5.0 transitivePeerDependencies: diff --git a/src/controllers/index.ts b/src/controllers/index.ts index 7584942..c1698fd 100644 --- a/src/controllers/index.ts +++ b/src/controllers/index.ts @@ -4,3 +4,4 @@ export * as userControl from './users'; export * as urlControl from './urls'; export * as tagControl from './tags'; export * as likeControl from './likes'; +export * as matchControl from './match'; \ No newline at end of file diff --git a/src/controllers/match.ts b/src/controllers/match.ts new file mode 100644 index 0000000..870b0ca --- /dev/null +++ b/src/controllers/match.ts @@ -0,0 +1,3 @@ +export const createMatch = async (req: Request, res: Response) => { + +} \ No newline at end of file diff --git a/src/controllers/users.ts b/src/controllers/users.ts index ecbbeae..dc5655d 100644 --- a/src/controllers/users.ts +++ b/src/controllers/users.ts @@ -10,6 +10,7 @@ export const createUser = async (req: Request, res: Response) => { try { const user = await User.create({ walletAddress: address }); const token = generateToken(user); + const match = addToGroup(user, 1); res.status(201).json({ user: user, token, diff --git a/src/controllers/matchmaking.ts b/src/lib/matchmaking.ts similarity index 77% rename from src/controllers/matchmaking.ts rename to src/lib/matchmaking.ts index cca4b2b..155cfb5 100644 --- a/src/controllers/matchmaking.ts +++ b/src/lib/matchmaking.ts @@ -1,7 +1,7 @@ // create matches from MatchGroup import { MatchGroup } from "../models/matchGroup" import { Match, MatchDocument } from "../models/match"; -import { User } from "../models/schema"; +import { User, Url } from "../models/schema"; import mongoose from "mongoose"; import { Request, Response } from 'express'; /* @@ -21,30 +21,38 @@ export const createMatch = async(req: Request, res: Response) => { for (const groupKey in MatchGroups) { if (MatchGroups.hasOwnProperty(groupKey)) { const group = MatchGroups[groupKey]; - console.log("group : ", group); + console.log("group is ......... : ", group); if (group.users.length >= 2) { for(let i = 0; i < group.users.length; i++) { for (let j = i+ 1; j < group.users.length; j++) { const match = await Match.create( - { user1: group.users[i]._id, user2: group.users[j]._id }, + { user1: group.users[i]._id, user2: group.users[j]._id, threshold: parseInt(groupKey) }, ) // update users matched field await User.updateMany({ _id: { $in: [group.users[i]._id, group.users[j]._id] } }, { matched: true }); + // remove users from matchgroup + const res = await MatchGroup.updateMany({}, { $pull: { users: {$in: [group.users[i]._id, group.users[j]._id]} } }); + console.log("result : ", res); - // remove matched users from MatchGroups + // remove matched users from MatchGroups array MatchGroups[groupKey].users.splice(i, 1); MatchGroups[groupKey].users.splice(i, j); matches.push(match); } } } + else { + console.log("not enough users in group : ", group.users); + } } } // Match remaining users across groups let usersRemaining = []; for (const groupkey in MatchGroups) { const group = MatchGroups[groupkey]; - usersRemaining.push(group); + if(group.users.length > 0) { + usersRemaining.push(group); + } } while (usersRemaining.length >= 2) { @@ -59,17 +67,26 @@ export const createMatch = async(req: Request, res: Response) => { } +// populate match details from Match +export const populateMatch = async(matchObj : MatchDocument) => { + // get all the urls submitted by user1 where verified is false and limit is threshold + const user1Urls = await Url.find({ user: matchObj.user1, verified: false }).limit(matchObj.threshold); + const user2Urls = await Url.find({ user: matchObj.user2, verified: false }).limit(matchObj.threshold); + return { user1Urls: user1Urls, user2Urls: user2Urls }; + +} + + // get match by matchID export const getMatchbyMatchID = async (matchID: string) => { const match = await Match.findOne({ _id: matchID }); - return match; + if(!match) { + throw new Error("match with provided matchID not found"); + } + const urls = await populateMatch(match); + return { match: match, urls: urls }; } -// get match by userID -export const getMatchbyUserID = async (userID: string) => { - const match = await Match.findOne({ user1: userID }); - return match; -} export const updateMatchStatus = async(matchID: string, status: string) :Promise => { if (status in ['ready', 'running', 'completed', 'deadlock'] === false) { diff --git a/src/models/match.ts b/src/models/match.ts index e75f94e..8a37a25 100644 --- a/src/models/match.ts +++ b/src/models/match.ts @@ -6,6 +6,7 @@ interface MatchDocument extends Document { user2: mongoose.Types.ObjectId; user2Completed: boolean; status: string; + threshold: number; // threshold is max limit of urls allowed to be validated, depnds on key in MatchGroup createdAt: Date; updatedAt: Date; } @@ -32,6 +33,10 @@ const MatchSchema = new mongoose.Schema({ enum: ['ready', 'running', 'completed', 'deadlock'], default: 'ready', }, + threshold: { + type: Number, + default: 1, + }, createdAt: { type: Date, default: Date.now, diff --git a/src/models/schema.ts b/src/models/schema.ts index e5c6514..a2851b3 100644 --- a/src/models/schema.ts +++ b/src/models/schema.ts @@ -17,6 +17,7 @@ interface URLDocument extends Document { url: string; submittedBy: mongoose.Types.ObjectId; likes: number; + verified: boolean; tags: Array; createdAt: Date; updatedAt: Date; @@ -95,6 +96,10 @@ const URLSchema = new mongoose.Schema({ type: Number, default: 0, }, + verified: { + type: Boolean, + default: false, + }, tags: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Tag', diff --git a/src/routes/contracts.ts b/src/routes/contracts.ts index 481deba..14a567a 100644 --- a/src/routes/contracts.ts +++ b/src/routes/contracts.ts @@ -7,7 +7,7 @@ import { userControl, likeControl } from "../controllers/index"; -import { createMatch } from '../controllers/matchmaking'; +import { createMatch } from '../lib/matchmaking'; import { authenticate, diff --git a/tsconfig.json b/tsconfig.json index 825dc81..30e02a1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -33,7 +33,7 @@ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ "types": [ - "node", + //"node", "jest" ] /* Specify type package names to be included without being referenced in a source file. */, // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */