Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d97abc00e2 | |||
| cada5487bf | |||
| 2f9c65512b | |||
| edd87375c3 | |||
| b2d5b84435 | |||
| c460dc5385 | |||
| f54e76ab72 | |||
| fb1555338d | |||
| c98c917594 | |||
| cfb72d1772 | |||
| 01d07d65d1 | |||
| c6ad01b710 | |||
| 113cff5512 | |||
| 2c91b73a60 | |||
| 866c8a1838 | |||
| 926a28b7f9 | |||
| 9d6a18dda4 | |||
| 76f573023f |
@@ -45,13 +45,11 @@ jobs:
|
||||
with:
|
||||
node-version: '24'
|
||||
|
||||
- name: Create .env file
|
||||
run: |
|
||||
echo "API_URL=http://api:3000" >> .env
|
||||
echo "CDN_URL=http://cdn:4000" >> .env
|
||||
echo "WS_URL=ws://api:3000" >> .env
|
||||
|
||||
- name: Run Vitest
|
||||
env:
|
||||
API_URL: http://api:3000
|
||||
CDN_URL: http://cdn:4000
|
||||
WS_URL: ws://api:3000
|
||||
run: |
|
||||
npm install
|
||||
npm test --experimental-websocket
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@chatenium/chatenium-sdk",
|
||||
"version": "1.1.1",
|
||||
"version": "1.2.0",
|
||||
"description": "A library for interacting with the Chatenium API",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
@@ -39,7 +39,6 @@
|
||||
"dependencies": {
|
||||
"@faker-js/faker": "^10.4.0",
|
||||
"axios": "^1.14.0",
|
||||
"dotenv": "^17.4.0",
|
||||
"msw": "^2.12.14",
|
||||
"uuid": "^13.0.0"
|
||||
}
|
||||
|
||||
@@ -5,19 +5,12 @@ export interface SDKConfig {
|
||||
}
|
||||
|
||||
declare const process: any;
|
||||
declare const require: any;
|
||||
|
||||
const isNode =
|
||||
typeof process !== 'undefined' &&
|
||||
typeof process.versions !== 'undefined' &&
|
||||
typeof process.versions.node !== 'undefined';
|
||||
|
||||
if (isNode) {
|
||||
try {
|
||||
require('dotenv').config();
|
||||
} catch { }
|
||||
}
|
||||
|
||||
const getEnv = (key: string): string | undefined => {
|
||||
if (!isNode) return undefined;
|
||||
return process.env?.[key];
|
||||
|
||||
32
src/core/permissions.ts
Normal file
32
src/core/permissions.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
export const permissions = {
|
||||
createAndEditCategories: 2,
|
||||
deleteCategories: 4,
|
||||
createAndEditChannels: 8,
|
||||
deleteChannels: 16,
|
||||
deleteAnyMessage: 32,
|
||||
pinMessages: 64,
|
||||
createAndEditRanks: 128,
|
||||
deleteRanks: 256,
|
||||
changeNetworkNamePictureAndVisibility: 512,
|
||||
createEmojis: 1024,
|
||||
deleteEmojis: 2048,
|
||||
manageEmbed: 4096,
|
||||
createWebhooks: 8192,
|
||||
deleteWebhooks: 16384,
|
||||
createInvites: 32768,
|
||||
deleteInvites: 65536,
|
||||
sendMessages: 131072,
|
||||
seeChannels: 262144,
|
||||
banMembers: 524288,
|
||||
kickMembers: 1048576,
|
||||
unAndAssignRanksToMember: 2097152,
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether the set of permissions includes the permission. Note that network owners have regular permissions just like the other members, so if the userid matches with the network's creator ID, then the permission is automatically granted. This logic is not included in this function.
|
||||
* @param permissions
|
||||
* @param permission
|
||||
*/
|
||||
export function permissionGranted(permissions: number, permission: number): Boolean {
|
||||
return (permissions & permission) === permission;
|
||||
}
|
||||
@@ -43,6 +43,13 @@ export class WebSocketHandler {
|
||||
this.connection = new WebSocket(`${environment.get().wsUrl}/v2/ws?userid=${userid}&access_token=${resp.data.token}`)
|
||||
console.log("Connected to websocket successfully")
|
||||
this.startListening()
|
||||
|
||||
this.connection.onclose = () => {
|
||||
console.error("The WebSocket connection was closed unexpectedly. Reconnecting...")
|
||||
setTimeout(() => {
|
||||
this.connect(userid, token)
|
||||
}, 3000)
|
||||
}
|
||||
return
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
|
||||
@@ -24,4 +24,5 @@ export interface Attachment {
|
||||
path: string
|
||||
height: number
|
||||
width: number
|
||||
extraMetaData: Record<string, any> // Used by clients
|
||||
}
|
||||
@@ -208,9 +208,11 @@ export interface GetMembersReq {
|
||||
}
|
||||
|
||||
export interface UploadNewPictureReq {
|
||||
picId: string
|
||||
userid: string
|
||||
networkId: string
|
||||
data: string
|
||||
isImage: boolean
|
||||
monogramColors: RGB | null
|
||||
}
|
||||
|
||||
export interface ChangeVisibilityReq {
|
||||
|
||||
@@ -31,7 +31,7 @@ export class BroadcastChannelService {
|
||||
})
|
||||
WebSocketHandler.getInstance().registerService({
|
||||
identifier: channelId,
|
||||
onNewConnId: this.onNewConnId,
|
||||
onNewConnId: newConnId => this.onNewConnId(newConnId),
|
||||
onNewMessage: wsMessageListener,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ export class ChatService {
|
||||
})
|
||||
WebSocketHandler.getInstance().registerService({
|
||||
identifier: userid,
|
||||
onNewConnId: this.onNewConnId,
|
||||
onNewConnId: newConnId => this.onNewConnId(newConnId),
|
||||
onNewMessage: wsMessageListener,
|
||||
})
|
||||
}
|
||||
@@ -58,7 +58,7 @@ export class ChatService {
|
||||
}
|
||||
}
|
||||
|
||||
async getQuick(): Promise<Message[]> {
|
||||
async getQuick(): Promise<Chat[]> {
|
||||
const chats = await this.database.get("chats", this.userid)
|
||||
if (chats) {
|
||||
return JSON.parse(chats)
|
||||
|
||||
@@ -36,7 +36,7 @@ export class DMService {
|
||||
})
|
||||
WebSocketHandler.getInstance().registerService({
|
||||
identifier: chatid,
|
||||
onNewConnId: this.onNewConnId,
|
||||
onNewConnId: newConnId => this.onNewConnId(newConnId),
|
||||
onNewMessage: wsMessageListener,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ export class NetworkService {
|
||||
})
|
||||
WebSocketHandler.getInstance().registerService({
|
||||
identifier: networkId,
|
||||
onNewConnId: this.onNewConnId,
|
||||
onNewConnId: newConnId => this.onNewConnId(newConnId),
|
||||
onNewMessage: wsMessageListener,
|
||||
})
|
||||
}
|
||||
@@ -108,7 +108,7 @@ export class NetworkService {
|
||||
}
|
||||
}
|
||||
|
||||
async getQuick(): Promise<Message[]> {
|
||||
async getQuick(): Promise<Network[]> {
|
||||
const networks = await this.database.get("networks", this.userid)
|
||||
if (networks) {
|
||||
return JSON.parse(networks)
|
||||
@@ -711,14 +711,18 @@ export class NetworkService {
|
||||
|
||||
/**
|
||||
* Uploads a new network picture
|
||||
* @param picId
|
||||
* @param isImage
|
||||
* @param image
|
||||
* @param colors
|
||||
*/
|
||||
async uploadNewPic(picId: string): Promise<void> {
|
||||
async uploadNewPic(isImage: boolean, image: string | null, colors: RGB | null): Promise<void> {
|
||||
try {
|
||||
await this.client.patch<PublicUserData[]>("network/uploadNewPic", <UploadNewPictureReq>{
|
||||
await this.client.patch<PublicUserData[]>("v2/network/uploadNewPic", <UploadNewPictureReq>{
|
||||
userid: this.userid,
|
||||
networkId: this.networkId,
|
||||
picId: picId,
|
||||
data: image,
|
||||
isImage: isImage,
|
||||
monogramColors: colors
|
||||
});
|
||||
return
|
||||
} catch (e) {
|
||||
|
||||
@@ -36,14 +36,21 @@ export class SessionManager {
|
||||
async loadSessions(): Promise<Session[]> {
|
||||
const tokens = await this.keyring.getAll()
|
||||
const sessions: Session[] = []
|
||||
|
||||
for (const tokenKey of tokens) {
|
||||
const token = await this.keyring.get(tokenKey)
|
||||
const userData = await this.database.get("sessions", tokenKey)
|
||||
if (userData) {
|
||||
sessions.push({
|
||||
token: token,
|
||||
userData: JSON.parse(userData)
|
||||
})
|
||||
try {
|
||||
const token = await this.keyring.get(tokenKey)
|
||||
const userData = await this.database.get("sessions", tokenKey)
|
||||
|
||||
if (userData && userData.trim().length > 0) {
|
||||
sessions.push({
|
||||
token: token,
|
||||
userData: JSON.parse(userData)
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`Failed to parse session for ${tokenKey}:`, e)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +77,11 @@ export class SessionManager {
|
||||
*/
|
||||
async loadPreferredSession() {
|
||||
const sessions = await this.loadSessions()
|
||||
|
||||
if (sessions.length == 0) {
|
||||
throw new Error("No sessions found")
|
||||
}
|
||||
|
||||
let preferredUser = await this.getPreferredUser()
|
||||
if (preferredUser == "") {
|
||||
preferredUser = sessions[0].userData.userid
|
||||
|
||||
@@ -40,7 +40,7 @@ export class TextChannelServiceService {
|
||||
})
|
||||
WebSocketHandler.getInstance().registerService({
|
||||
identifier: channelId,
|
||||
onNewConnId: this.onNewConnId,
|
||||
onNewConnId: newConnId => this.onNewConnId(newConnId),
|
||||
onNewMessage: wsMessageListener,
|
||||
})
|
||||
}
|
||||
@@ -48,6 +48,7 @@ export class TextChannelServiceService {
|
||||
private onNewConnId(newConnId: string) {
|
||||
console.log("NetworkService: New connection id")
|
||||
this.client.defaults.headers["X-WS-ID"] = newConnId;
|
||||
this.joinWebSocketRoom().then()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -263,7 +264,7 @@ export class TextChannelServiceService {
|
||||
*/
|
||||
async joinWebSocketRoom(): Promise<void> {
|
||||
try {
|
||||
const resp = await this.client.patch("network/channel/joinWebSocketRoom", <JoinWsRoomReq>{
|
||||
const resp = await this.client.post("v2/network/channel/joinWebSocketRoom", <JoinWsRoomReq>{
|
||||
networkId: this.networkId,
|
||||
channelId: this.channelId,
|
||||
categoryId: this.categoryId,
|
||||
|
||||
Reference in New Issue
Block a user