-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
memory optimization, introduce redis
Signed-off-by: Hoang Pham <[email protected]>
- Loading branch information
Showing
11 changed files
with
1,003 additions
and
1,412 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* eslint-disable no-console */ | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import fetch from 'node-fetch' | ||
import https from 'https' | ||
import dotenv from 'dotenv' | ||
import Utils from './utils.js' | ||
dotenv.config() | ||
|
||
class ApiService { | ||
|
||
constructor(authManager) { | ||
this.NEXTCLOUD_URL = process.env.NEXTCLOUD_URL | ||
this.IS_DEV = Utils.parseBooleanFromEnv(process.env.IS_DEV) | ||
this.agent = this.IS_DEV ? new https.Agent({ rejectUnauthorized: false }) : null | ||
this.authManager = authManager | ||
} | ||
|
||
fetchOptions(method, token, body = null, roomId = null, lastEditedUser = null) { | ||
return { | ||
method, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
...(method === 'GET' && { Authorization: `Bearer ${token}` }), | ||
...(method === 'PUT' && { | ||
'X-Whiteboard-Auth': this.authManager.generateSharedToken(roomId), | ||
'X-Whiteboard-User': lastEditedUser || 'unknown', | ||
}), | ||
}, | ||
...(body && { body: JSON.stringify(body) }), | ||
...(this.agent && { agent: this.agent }), | ||
} | ||
} | ||
|
||
async fetchData(url, options) { | ||
try { | ||
const response = await fetch(url, options) | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}: ${await response.text()}`) | ||
} | ||
return response.json() | ||
} catch (error) { | ||
console.error(error) | ||
return null | ||
} | ||
} | ||
|
||
async getRoomDataFromServer(roomID, jwtToken) { | ||
const url = `${this.NEXTCLOUD_URL}/index.php/apps/whiteboard/${roomID}` | ||
const options = this.fetchOptions('GET', jwtToken) | ||
return this.fetchData(url, options) | ||
} | ||
|
||
async saveRoomDataToServer(roomID, roomData, lastEditedUser) { | ||
console.log('Saving room data to file') | ||
|
||
const url = `${this.NEXTCLOUD_URL}/index.php/apps/whiteboard/${roomID}` | ||
const body = { data: { elements: roomData } } | ||
const options = this.fetchOptions('PUT', null, body, roomID, lastEditedUser) | ||
return this.fetchData(url, options) | ||
} | ||
|
||
} | ||
|
||
export default ApiService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.