Skip to content

Commit

Permalink
Change: fix mongo atomic operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Voktor Stolenets committed Jan 21, 2025
1 parent 0b09a5e commit 8c83633
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
8 changes: 6 additions & 2 deletions src/documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ paths:
get:
summary: Get user by address
parameters:
- $ref: '#/components/parameters/GlobalAddressHeader'
- name: address
in: query
required: true
schema:
type: string
responses:
"200":
description: User data
Expand Down Expand Up @@ -469,7 +473,7 @@ paths:
description: Retrieve the list of followed.
parameters:
- name: address
in: query
in: query
required: true
description: The address of the user whose followers are being retrieved.
schema:
Expand Down
35 changes: 21 additions & 14 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ const startServer = async () => {
};
address = address.toLowerCase();
const secret = mapSecret.get(address);

if (!signature || !address || !secret) {
return reply.status(400).send({
error: "Missing address or signature in headers",
Expand Down Expand Up @@ -132,9 +131,11 @@ async function checkTokenExist(
await request.server.mongo.db?.collection("token").updateOne(
{ tokenAddress },
{
tokenAddress,
like: 0,
timestamp: new Date(),
$set: {
tokenAddress,
like: 0,
timestamp: new Date(),
},
},
{ upsert: true },
);
Expand Down Expand Up @@ -202,10 +203,12 @@ async function addOrUpdateUser(request: FastifyRequest, reply: FastifyReply) {
await request.server.mongo.db?.collection("user").updateOne(
{ address },
{
address,
userName,
image,
timestamp: new Date(),
$set: {
address,
userName,
image,
timestamp: new Date(),
},
},
{ upsert: true },
);
Expand Down Expand Up @@ -244,9 +247,11 @@ async function addOrDeleteLike(request: FastifyRequest, reply: FastifyReply) {
.updateOne(
{ address, tokenAddress },
{
address,
tokenAddress,
timestamp: new Date(),
$set: {
address,
tokenAddress,
timestamp: new Date(),
},
},
{ upsert: true },
);
Expand Down Expand Up @@ -274,7 +279,7 @@ async function addOrDeleteLike(request: FastifyRequest, reply: FastifyReply) {

async function getUser(request: FastifyRequest, reply: FastifyReply) {
try {
let { address } = request.headers as { address?: string };
let { address } = request.query as { address?: string };
address = address.toLowerCase();
const user = await request.server.mongo.db
?.collection("user")
Expand Down Expand Up @@ -458,8 +463,10 @@ async function addfollow(request: FastifyRequest, reply: FastifyReply) {
await request.server.mongo.db?.collection("followers").updateOne(
{ address, userAddress },
{
address,
userAddress,
$set: {
address,
userAddress,
},
},
{ upsert: true },
);
Expand Down

0 comments on commit 8c83633

Please sign in to comment.