Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ookamiiixd committed Apr 10, 2022
1 parent 81878dd commit 58f0deb
Show file tree
Hide file tree
Showing 17 changed files with 2,051 additions and 4,596 deletions.
3 changes: 1 addition & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
package.json
package-lock.json
sessions/
10 changes: 9 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@
"useTabs": false,
"endOfLine": "lf",
"semi": false,
"singleQuote": true
"singleQuote": true,
"overrides": [
{
"files": "*.json",
"options": {
"tabWidth": 2
}
}
]
}
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

An implementation of [@adiwajshing/Baileys](https://github.com/adiwajshing/Baileys) as a simple RESTful API service with multiple device support. This project implements both **Legacy** (Normal WhatsApp Web) and **Beta Multi-Device** client so that you can choose and use one of them easily.

## Requirements

- **NodeJS** version **14.5.0** or higher.

## Installation

1. Download or clone this repo.
2. Enter to the project directory.
3. Execute `npm i` to install the dependencies.
3. Install the dependencies.

## `.env` Configurations

Expand Down Expand Up @@ -44,13 +48,16 @@ The server will respond in following JSON format:
```

## Known Issue
- Logging out from your phone manually when the session is still active **will kill the entire app** after a few minutes. As for now you should only destroy a session by using the **delete session endpoint** to avoid this issue. This issue only occurs for **Beta Multi-Device** users.

- ~~Logging out from your phone manually when the session is still active **will kill the entire app** after a few minutes. As for now you should only destroy a session by using the **delete session endpoint** to avoid this issue. This issue only occurs for **Beta Multi-Device** users~~. This issue should be solved on Baileys version **4.1.0** (Tested).

## Notes
- The app only provide a very simple validation, you may want to implement your own.
- There's no authentication, you may want to implement your own.
- The **Beta Multi-Device** client use provided baileys's `makeInMemoryStore` method which will store your data in memory and a json file, you may want to use a better data management.
- **There's no reading message occured before sending message**. The reading message only occurs when the client received message from someone, it will read them immediately. You should always read messages before starting the app and start sending messages to avoid abnormal detection.

- The app only provide a very simple validation, you may want to implement your own.
- There's no authentication, you may want to implement your own.
- The **Beta Multi-Device** client use provided Baileys's `makeInMemoryStore` method which will store your data in memory and a json file, you may want to use a better data management.
- Automatically reading incoming messages is now disabled by default. Uncomment `whatsapp.js:91-105` to enable this behaviour.
- If you have problems when deploying on **CPanel** or any other similar hosting, transpiling your code into **CommonJS** should fix the problems.

## Notice

Expand Down
File renamed without changes.
File renamed without changes.
38 changes: 0 additions & 38 deletions controllers/sessionController.js

This file was deleted.

46 changes: 46 additions & 0 deletions controllers/sessionsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { isSessionExists, createSession, getSession, deleteSession } from './../whatsapp.js'
import response from './../response.js'

const find = (req, res) => {
response(res, 200, true, 'Session found.')
}

const status = (req, res) => {
const states = ['connecting', 'connected', 'disconnecting', 'disconnected']

const session = getSession(res.locals.sessionId)
let state = states[session.ws.readyState]

state =
state === 'connected' && typeof (session.isLegacy ? session.state.legacy.user : session.user) !== 'undefined'
? 'authenticated'
: state

response(res, 200, true, '', { status: state })
}

const add = (req, res) => {
const { id, isLegacy } = req.body

if (isSessionExists(id)) {
return response(res, 409, false, 'Session already exists, please use another id.')
}

createSession(id, isLegacy === 'true', res)
}

const del = async (req, res) => {
const { id } = req.params
const session = getSession(id)

try {
await session.logout()
} catch {
} finally {
deleteSession(id, session.isLegacy)
}

response(res, 200, true, 'The session has been successfully deleted.')
}

export { find, status, add, del }
2 changes: 1 addition & 1 deletion middlewares/sessionValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isSessionExists } from '../whatsapp.js'
import response from './../response.js'

const validate = (req, res, next) => {
const sessionId = req.query.id
const sessionId = req.query.id ?? req.params.id

if (!isSessionExists(sessionId)) {
return response(res, 404, false, 'Session not found.')
Expand Down
Loading

0 comments on commit 58f0deb

Please sign in to comment.