-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
2,897 additions
and
1,903 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Service } from "../service"; | ||
|
||
export default class AbstractFriends extends Service { | ||
|
||
async requestFriend(_id: string): Promise<boolean> { | ||
return false; | ||
} | ||
|
||
async acceptFriend(_id: string): Promise<boolean> { | ||
return false; | ||
} | ||
|
||
async rejectFriend(_id: string): Promise<boolean> { | ||
return false; | ||
} | ||
|
||
async removeFriend(_id: string): Promise<boolean> { | ||
return false; | ||
} | ||
|
||
async getFriends(): Promise<string[]> { | ||
return []; | ||
} | ||
|
||
async getFriendRequests(): Promise<string[]> { | ||
return []; | ||
} | ||
|
||
async getFriendRequestsSent(): Promise<string[]> { | ||
return []; | ||
} | ||
|
||
async isFriend(_id: string): Promise<boolean> { | ||
return false; | ||
} | ||
|
||
} |
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,59 @@ | ||
import { BASE_STORE, BASE_URL, anonKey } from "../smoke.config"; | ||
import AbstractFriends from "./abstract"; | ||
const { ipcRenderer } = require("electron"); | ||
|
||
const fetch = async (url, data, token = null): Promise<any> => { | ||
return await ipcRenderer.invoke('fetch',{ | ||
url: `${BASE_URL}${url}`, | ||
options: { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Authorization": `Bearer ${token || anonKey}`, | ||
...(token ? { "apiKey": anonKey } : {}) | ||
}, | ||
body: JSON.stringify(data || {} ) | ||
}, | ||
result: 'json' | ||
}) | ||
} | ||
|
||
export default class SmokeFriends extends AbstractFriends { | ||
|
||
#friends = []; | ||
|
||
async reload(){ | ||
await super.reload(); | ||
this.handleStart(); | ||
return null; | ||
} | ||
|
||
async handleStart() { | ||
await this.updateFriends(); | ||
} | ||
|
||
async updateFriends() { | ||
const token = await ipcRenderer.invoke('get-session-storage', 'smoke-token'); | ||
if(!token) return this.#friends = []; | ||
const result = await fetch(`/friends`, {}, token); | ||
this.#friends = result.friends.map(friend => ({ | ||
...friend, | ||
avatar: `${BASE_STORE}/${friend.avatar}`, | ||
smoke_id: friend.id, | ||
})) | ||
} | ||
|
||
async getFriends() { | ||
return this.#friends; | ||
} | ||
|
||
async requestFriend(username: string) { | ||
const token = await ipcRenderer.invoke('get-session-storage', 'smoke-token'); | ||
await fetch(`/friends-add`, { username }, token); | ||
await this.updateFriends();//!!! | ||
return true; | ||
} | ||
|
||
|
||
|
||
} |
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,16 @@ | ||
import { Service } from "../service"; | ||
|
||
export default class AbstractMessages extends Service { | ||
|
||
async sendPrivateMessage(_user: any, _message: string): Promise<boolean> { | ||
return false; | ||
} | ||
|
||
async getPrivateMessages(_user: any): Promise<any[]> { | ||
return []; | ||
} | ||
|
||
async setCallback(_target: any, _callback: Function): Promise<void> { | ||
return; | ||
} | ||
} |
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,80 @@ | ||
import { BASE_URL, anonKey } from "../smoke.config"; | ||
import AbstractMessages from "./abstract"; | ||
const { ipcRenderer } = require("electron"); | ||
|
||
const fetch = async (url, data, token = null): Promise<any> => { | ||
return await ipcRenderer.invoke('fetch',{ | ||
url: `${BASE_URL}${url}`, | ||
options: { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Authorization": `Bearer ${token || anonKey}`, | ||
...(token ? { "apiKey": anonKey } : {}) | ||
}, | ||
body: JSON.stringify(data || {} ) | ||
}, | ||
result: 'json' | ||
}) | ||
} | ||
|
||
export default class SmokeMessages extends AbstractMessages { | ||
|
||
#callback = null; | ||
#target = null; | ||
#lastId = null; | ||
|
||
async reload(){ | ||
await super.reload(); | ||
this.#lastId = null; | ||
this.#callback = null; | ||
this.#target = null; | ||
return null; | ||
} | ||
|
||
async getPrivateMessages(user) { | ||
if(!user.smoke_id) return []; | ||
const token = await ipcRenderer.invoke('get-session-storage', 'smoke-token'); | ||
const result = await fetch(`/chat-room`, { room: user.smoke_id, isPrivate: true}, token); | ||
if(!result.messages) return []; | ||
this.#lastId = result.messages[result.messages.length - 1].id; | ||
return result.messages; | ||
} | ||
|
||
|
||
async sendPrivateMessage(user, message) { | ||
if(!user.smoke_id) return false; | ||
const token = await ipcRenderer.invoke('get-session-storage', 'smoke-token'); | ||
const { message: newMessage } = await fetch(`/send-message`, { room: user.smoke_id, isPrivate: true, message: message}, token); | ||
if(!newMessage) return false; | ||
this.#lastId = newMessage.id; | ||
return true; | ||
} | ||
|
||
async setCallback(target, callback) { | ||
this.#target = target; | ||
this.#callback = callback; | ||
if(!this.#target || !this.#callback) return; | ||
this.startListening(); | ||
} | ||
|
||
async startListening() { | ||
if(!this.#callback) return; | ||
if(!this.#target) return; | ||
const token = await ipcRenderer.invoke('get-session-storage', 'smoke-token'); | ||
const room = this.#target.smoke_id ? this.#target.smoke_id : this.#target.smoke_id; // will have groups | ||
try{ | ||
const result = await fetch(`/chat-listen`, { room, isPrivate: !!this.#target.smoke_id, lastId: this.#lastId }, token); | ||
const { messages } = result; | ||
if(messages) { | ||
this.#callback(messages); | ||
this.#lastId = messages[messages.length - 1].id; | ||
} | ||
}catch(e) { | ||
console.error(e); | ||
await new Promise(resolve => setTimeout(resolve, 3000)); | ||
} | ||
this.startListening(); | ||
} | ||
|
||
} |
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
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.