Skip to content

Commit

Permalink
Merge pull request #84 from alurosu/main
Browse files Browse the repository at this point in the history
added daily limits for wallet likes
  • Loading branch information
zksquirrel authored Jul 30, 2024
2 parents 656a1b7 + 57fdac7 commit 5008594
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/components/WalletList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ const WalletList: React.FC<Props> = ({ allWallets }) => {
}));
setSuccess({ [walletTitle]: "We saved your review!" });
} else {
setError({ [walletTitle]: "You reviewed this in the past." });
const data = await response.json();
setError({ [walletTitle]: data.message });
}
} catch (error) {
setError({ [walletTitle]: "Error updating rating: " + error });
Expand Down Expand Up @@ -140,7 +141,8 @@ const WalletList: React.FC<Props> = ({ allWallets }) => {
}));
setSuccess({ [walletTitle]: "We saved your review!" });
} else {
setError({ [walletTitle]: "You reviewed this in the past." });
const data = await response.json();
setError({ [walletTitle]: data.message });
}
} catch (error) {
setError({ [walletTitle]: "Error updating rating: " + error });
Expand Down
36 changes: 33 additions & 3 deletions src/pages/api/wallet-likes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { Client } from 'pg';
import crypto from 'crypto';

const dailyLimit4WalletVotes = 5;

// Function to hash text using SHA-1
const hashSHA1 = (text) => {
return crypto.createHash('sha1').update(text).digest('hex');
Expand Down Expand Up @@ -38,22 +40,50 @@ export default async function handler(req, res) {
}, {});
return res.status(200).json(likesData);
} else {
const today = new Date().toISOString().split('T')[0];

// Check if the user has already voted for this title today
const resIp = await client.query('SELECT EXISTS (SELECT 1 FROM wallet_likes_proofs WHERE hash = $1)', [hashedIpTitleKey]);

if (resIp.rows[0].exists) {
return res.status(429).json({ message: "You have already updated likes for this title." });
return res.status(429).json({ message: "You reviewed this in the past." });
}

await client.query('BEGIN');

// Check the vote limit
const resLimit = await client.query('SELECT votes FROM wallet_likes_limits WHERE title = $1 AND day = $2', [title, today]);

let currentVotes = 0;
if (resLimit.rows.length > 0) {
currentVotes = resLimit.rows[0].votes;
}

if (currentVotes >= dailyLimit4WalletVotes) {
await client.query('ROLLBACK');
return res.status(429).json({ message: "" });
}

// Update the votes limit table
if (resLimit.rows.length === 0) {
await client.query('INSERT INTO wallet_likes_limits (title, day, votes) VALUES ($1, $2, $3)', [title, today, 1]);
} else {
await client.query('UPDATE wallet_likes_limits SET votes = votes + 1 WHERE title = $1 AND day = $2', [title, today]);
}

// Update the likes count
const resLikes = await client.query('SELECT likes FROM wallet_likes WHERE title = $1', [title]);
let newLikes;

if (delta > 0)
newLikes = 1;
else
newLikes = -1;

if (resLikes.rows.length === 0) {
newLikes = delta;
await client.query('INSERT INTO wallet_likes (title, likes) VALUES ($1, $2)', [title, newLikes]);
} else {
newLikes = resLikes.rows[0].likes + delta;
newLikes = resLikes.rows[0].likes + newLikes;
await client.query('UPDATE wallet_likes SET likes = $1 WHERE title = $2', [newLikes, title]);
}

Expand Down

0 comments on commit 5008594

Please sign in to comment.