Skip to content

TorusPlatforms/Backend

Repository files navigation

Torus Backend API

A comprehensive social media backend API built with Node.js and Express, designed for college communities with features including posts, loops (communities), events, messaging, and real-time notifications.

Tech Stack

  • Runtime: Node.js
  • Framework: Express.js 4.18
  • Database: PostgreSQL (Google Cloud SQL)
  • ORM: Sequelize 6.37
  • Authentication: Firebase Admin SDK
  • File Storage: AWS S3 (via AWS SDK)
  • File Upload: Multer
  • Scheduling: node-cron
  • Additional: CORS, dotenv, axios, cheerio

Architecture

The project follows an MVC (Model-View-Controller) pattern with a service layer:

src/
├── controllers/    # Request handlers and route logic
├── services/       # Business logic layer
├── models/         # Sequelize database models and associations
├── utils/          # Utilities (error handling, tokens, helpers)
└── router.js       # Route definitions

Key Patterns

  • Async Error Handling: Custom catchAsync wrapper for promise-based error handling
  • Custom Error Classes: APIError utility for standardized error responses
  • Service Layer: Business logic separated from controllers
  • Database Associations: Complex Sequelize relationships between users, posts, loops, events, and more

Features

Core Functionality

  • User Management: Registration, profiles, search, follow/unfollow, blocking
  • Posts: Create, like, comment, polls with voting, college-specific feeds
  • Loops: Community groups with membership, admin roles, join requests, announcements, and group chats
  • Events: Create, join/leave, attendee management, automated notifications
  • Comments & Replies: Nested comments with like functionality
  • Direct Messaging: Thread-based messaging between users
  • Notifications: Real-time notifications with read/unread status
  • Media Uploads: Image uploads via AWS S3 (10MB limit)
  • College Management: University/college association and filtering

Automated Tasks

  • Database Cleanup: Daily cron job removes events older than 7 days and notifications older than 7 days
  • Event Notifications: Hourly and daily checks for upcoming events with automated attendee notifications
  • System Posts: Automatic welcome posts for college feeds

Database Schema

Key models and relationships:

  • Users (torus_users): Core user profiles with college associations
  • Posts: User-generated content with loop/college associations, polls, and likes
  • Loops: Community groups with members, admins, join requests, and announcements
  • Events: Scheduled events with attendee tracking
  • Comments & Replies: Nested comment system with likes
  • Followings: User-to-user follow relationships
  • Blocked Users: User blocking functionality
  • Notifications: In-app notification system
  • Votes: Poll voting system

API Endpoints

User Routes

  • GET /api/user - Get current user
  • GET /api/user/:USERNAME - Get user by username
  • POST /api/user/register - Register new user
  • POST /api/user/update/:FIELD - Update user field
  • GET /api/users - Search users
  • GET /api/user/:USERNAME/followers - Get user followers
  • GET /api/user/:USERNAME/following - Get user following
  • POST /api/user/:USERNAME/follow - Follow user
  • POST /api/user/:USERNAME/unfollow - Unfollow user
  • POST /api/user/:USERNAME/block - Block user

Post Routes

  • GET /api/posts/get/:POSTID - Get post by ID
  • GET /api/posts/user/:USERNAME - Get user posts
  • GET /api/posts/following - Get posts from followed users
  • GET /api/posts/loop/:LOOPID - Get loop posts
  • GET /api/posts/college - Get college posts
  • POST /api/posts/add - Create post
  • POST /api/posts/like - Like post
  • POST /api/posts/unlike - Unlike post
  • POST /api/posts/vote - Vote on poll
  • DELETE /api/posts/delete/:POSTID - Delete post

Loop Routes

  • GET /api/loops - Discover loops
  • GET /api/loops/joined - Get joined loops
  • GET /api/loops/:LOOPID - Get loop details
  • POST /api/loops/add - Create loop
  • POST /api/loops/:LOOPID/join - Join loop
  • POST /api/loops/:LOOPID/leave - Leave loop
  • GET /api/loops/:LOOPID/members - Get loop members
  • POST /api/loops/:LOOPID/announcement - Create announcement
  • GET /api/loops/:LOOPID/messages - Get loop messages

Event Routes

  • POST /api/events/create - Create event
  • GET /api/events/get/:EVENTID - Get event
  • GET /api/events/get - Get public events
  • POST /api/events/:EVENTID/join - Join event
  • POST /api/events/:EVENTID/leave - Leave event
  • DELETE /api/events/:EVENTID/delete - Delete event

Comment Routes

  • POST /api/comments/add - Create comment
  • POST /api/comments/reply - Create reply
  • GET /api/comments/post/:POSTID - Get post comments
  • POST /api/comments/:COMMENTID/like - Like comment
  • DELETE /api/comments/:COMMENTID/delete - Delete comment

Other Routes

  • POST /api/upload - Upload media (multipart/form-data)
  • GET /api/notifications/get - Get notifications
  • POST /api/messages/add - Send message
  • GET /api/messages/threads - Get message threads
  • GET /api/colleges - Search colleges

Database Connection

The application uses Google Cloud SQL Connector for secure database connections. In development mode, it automatically configures the connection with the provided instance connection name.

Error Handling

  • Custom APIError class for standardized error responses
  • catchAsync wrapper for automatic error catching in async route handlers
  • Centralized error handling via handleError utility

Security

  • Firebase Admin SDK for authentication token verification
  • CORS enabled for cross-origin requests
  • File upload size limits (10MB)
  • User blocking and content reporting features

Notes

  • Requires firebasekey.json in the root directory for Firebase authentication
  • Database models are auto-loaded from the models/ directory
  • Sequelize associations are configured in models/index.js
  • Automated cron jobs run for database cleanup and event notifications

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 6