Skip to content

Commit

Permalink
Change: add followers
Browse files Browse the repository at this point in the history
  • Loading branch information
Voktor Stolenets committed Jan 20, 2025
1 parent f277a6d commit 42c7eb4
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,55 @@ paths:
properties:
error:
type: string
/api/follow:
post:
summary: Add or remove a follower
description: Add a new follower or remove an existing one based on the request body.
parameters:
- $ref: '#/components/parameters/GlobalSignatureHeader'
- $ref: '#/components/parameters/GlobalAddressHeader'
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- userAddress
- add
properties:
userAddress:
type: string
description: The address of the user to follow or unfollow.
add:
type: boolean
description: Set to 'true' to follow or 'false' to unfollow the user.
responses:
"200":
description: Success response
content:
application/json:
schema:
type: object
additionalProperties: false
"400":
description: Invalid JSON payload or invalid/missing fields
content:
application/json:
schema:
type: object
properties:
error:
type: string
"500":
description: Server error
content:
application/json:
schema:
type: object
properties:
error:
type: string
/api/like:
post:
summary: Add or remove a like for a token
Expand Down Expand Up @@ -261,6 +310,51 @@ paths:
properties:
error:
type: string
/api/followers:
get:
summary: Get followers
description: Retrieve the list of followers for a specific user.
parameters:
- name: userAddress
in: query
required: true
description: The address of the user whose followers are being retrieved.
schema:
type: string
responses:
"200":
description: Successfully retrieved the list of followers.
content:
application/json:
schema:
type: array
items:
type: object
properties:
address:
type: string
description: The address of the follower.
userAddress:
type: string
description: The address of the user being followed.
"400":
description: Invalid query parameter or missing required field.
content:
application/json:
schema:
type: object
properties:
error:
type: string
"500":
description: Server error
content:
application/json:
schema:
type: object
properties:
error:
type: string
/api/token:
get:
summary: Get token by address
Expand Down
54 changes: 54 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ const startServer = async () => {
fastify.post("/api/user", addOrUpdateUser);
fastify.post("/api/message", addMessage);
fastify.post("/api/like", addOrDeleteLike);
fastify.post("/api/follow", addfollow);
fastify.get("/api/user", getUser);
fastify.get("/api/messages", getMessages);
fastify.get("/api/token", getToken);
fastify.get("/api/likes", getUserLikes);
fastify.get("/api/secret", getSecret);
fastify.get("/api/followers", getFollowers);

await fastify.listen({ host: HOST, port: PORT });
console.log(`Server running on http://${HOST}:${PORT}`);
Expand Down Expand Up @@ -388,4 +390,56 @@ async function uploadFile(request: FastifyRequest, reply: FastifyReply) {
}
}

async function addfollow(request: FastifyRequest, reply: FastifyReply) {
try {
const data = request.body;
if (!data || typeof data !== "object") {
return reply.status(400).send({ error: "Invalid JSON payload" });
}

const { address } = request.headers as { address?: string };
let { userAddress, add } = data as {
userAddress: string;
add: boolean;
};

userAddress = userAddress.toLowerCase();
if (!ethers.isAddress(userAddress)) throw new Error("Invalid address");

if (add) {
await request.server.mongo.db?.collection("followers").updateOne(
{ address, userAddress },
{
address,
userAddress,
},
{ upsert: true },
);
} else {
await request.server.mongo.db
?.collection("followers")
.deleteOne({ address, userAddress });
}
} catch (error) {
request.log.error(error);
return reply.status(500).send({ error: error.message });
}
return reply.send({});
}

async function getFollowers(request: FastifyRequest, reply: FastifyReply) {
try {
const { userAddress } = request.query as {
userAddress?: string;
};
const followers = await request.server.mongo.db
?.collection("followers")
.find({ userAddress }, { projection: { _id: 0 } })
.toArray();
return reply.send(followers);
} catch (error) {
request.log.error(error);
return reply.status(500).send({ error: error.message });
}
}
startServer();

0 comments on commit 42c7eb4

Please sign in to comment.