# Mayaspace Backend

Mayaspace Backend is a federated social network backend implementation that follows the **ActivityPub** protocol. It includes user registration, password authentication using JWT tokens, post creation, and following activities. It uses **SQLite** via the `quick.db` library for data storage.

## Features

- **Federated**: Supports ActivityPub protocol to allow federated communication with other ActivityPub-compatible platforms.
- **Password Authentication**: User authentication with password hashing (using bcrypt) and JWT tokens.
- **SQLite Backend**: Data storage using SQLite via `quick.db`.
- **RESTful API**: Basic routes for user registration, login, post creation, and followers management.

## Technologies

- **Node.js** for backend.
- **Express.js** for routing and middleware.
- **ActivityPub-Express** for ActivityPub protocol handling.
- **quick.db** for persistent SQLite storage.
- **bcryptjs** for password hashing.
- **jsonwebtoken** for user authentication and token management.
- **uuid** for unique post IDs.
- **node-fetch** for making federated requests.

## Installation and Setup

Follow these steps to set up the Mayaspace Backend locally:

### Prerequisites

- Install **Node.js** (v14.0.0 or above).
- Ensure **npm** is installed.

### 1. Clone the Repository

First, clone the project to your local machine:

```bash
git clone https://github.com/yourusername/mayaspace-backend.git
cd mayaspace-backend
```

### 2. Install Dependencies
Install the required dependencies using npm:

```bash
npm install
```

This will install the following dependencies:

activitypub-express: For handling ActivityPub protocol.
bcryptjs: For securely hashing passwords.
body-parser: For parsing incoming request bodies.
jsonwebtoken: For managing JWT tokens.
quick.db: For SQLite storage.
uuid: For generating unique post IDs.
node-fetch: For federated network communication.
express: For building the API server.

### 3. Run the Server
Start the backend server:

```bash
npm start
```

This will run the backend on http://localhost:3000.

### 4. API Endpoints
Here are the main API endpoints available:

User Registration
Create a new user (actor) with a username, display name, and password.

```bash
POST /users
```

Request Body:

```json
{
  "username": "alice",
  "displayName": "Alice",
  "password": "password123"
}
```

#### User Login
Authenticate the user and get a JWT token.

```bash
POST /login
```

Request Body:

```json
{
  "username": "alice",
  "password": "password123"
}
```

Response:

```json
{
  "token": "<JWT_TOKEN>"
}
```
#### Create a Post (Status Update)
Create a post (note) for the authenticated user. Authentication is required using the JWT token in the Authorization header.

```bash
POST /users/:username/posts
````
Request Body:

```json
{
  "content": "Hello, federated world!"
}
```
#### Get User's Posts
Fetch posts for the authenticated user. Authentication is required using the JWT token in the Authorization header.

```bash
GET /users/:username/posts
```
#### ActivityPub - Follow
This endpoint handles incoming follow activities for federated interactions.

```bash
POST /users/:username/inbox
Request Body:
```

```json
{
  "type": "Follow",
  "actor": {
    "id": "http://example.com/users/bob"
  }
}
```

### 5. Testing the API
To test the API, you can use Postman or cURL to make requests.

Here are some example cURL commands to test the basic functionality:

### Create a new user:
```bash
curl -X POST http://localhost:3000/users -H "Content-Type: application/json" -d '{"username": "alice", "displayName": "Alice", "password": "password123"}'```

#### Login to get a token:
```bash
curl -X POST http://localhost:3000/login -H "Content-Type: application/json" -d '{"username": "alice", "password": "password123"}'```

#### Create a post (Authenticated):
```bash
curl -X POST http://localhost:3000/users/alice/posts -H "Content-Type: application/json" -H "Authorization: Bearer <YOUR_JWT_TOKEN>" -d '{"content": "Hello, federated world!"}'
```

#### Get posts (Authenticated):
```bash
curl http://localhost:3000/users/alice/posts -H "Authorization: Bearer <YOUR_JWT_TOKEN>"
```
### 6. Federated Interaction
If you want to integrate with other federated servers, you can send ActivityPub requests to follow users on other servers or receive activities through the /users/:username/inbox endpoint.

### License
The license is the SPL-R5, see the file named "LICENSE" for details.

### Contributing
Feel free to contribute to this project. Fork the repository, make changes, and submit pull requests to improve Mayaspace Backend.