Skip to content

Commit

Permalink
0.0.12 (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
narafrost authored Jan 3, 2024
2 parents b5f5a84 + cb393e9 commit aca9cb5
Show file tree
Hide file tree
Showing 20 changed files with 671 additions and 141 deletions.
Binary file added app-back/Assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions app-back/Services/Friends/abstract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Service } from "../service";

export default class AbstractFriends extends Service {
export type FriendsEvents = {
event: "friends-updated";
listener: () => void;
}

export default class AbstractFriends extends Service<FriendsEvents>{

async requestFriend(_id: string): Promise<boolean> {
return false;
Expand All @@ -18,7 +23,7 @@ export default class AbstractFriends extends Service {
return false;
}

async getFriends(): Promise<string[]> {
async getFriends(): Promise<any[]> {
return [];
}

Expand Down
12 changes: 10 additions & 2 deletions app-back/Services/Friends/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ const fetch = async (url, data, token = null): Promise<any> => {
})
}

export default class SmokeFriends extends AbstractFriends {
export default class SmokeFriends extends AbstractFriends{

#friends = [];
#checkInterval = null;

async reload(){
await super.reload();
Expand All @@ -30,6 +31,9 @@ export default class SmokeFriends extends AbstractFriends {

async handleStart() {
await this.updateFriends();
this.#checkInterval = setInterval(() => {
this.updateFriends();
}, 60000);
}

async updateFriends() {
Expand All @@ -41,6 +45,7 @@ export default class SmokeFriends extends AbstractFriends {
avatar: `${BASE_STORE}/${friend.avatar}`,
smoke_id: friend.id,
}))
this.emit('friends-updated')
}

async getFriends() {
Expand All @@ -54,6 +59,9 @@ export default class SmokeFriends extends AbstractFriends {
return true;
}


async clean() {
await super.clean();
clearInterval(this.#checkInterval);
}

}
7 changes: 7 additions & 0 deletions app-back/Services/Messages/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class SmokeMessages extends AbstractMessages {
#callback = null;
#target = null;
#lastId = null;
#stoped = false;

async reload(){
await super.reload();
Expand Down Expand Up @@ -59,9 +60,11 @@ export default class SmokeMessages extends AbstractMessages {
}

async startListening() {
if(this.#stoped) return;
if(!this.#callback) return;
if(!this.#target) return;
const token = await ipcRenderer.invoke('get-session-storage', 'smoke-token');
if(!token) return;
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);
Expand All @@ -77,4 +80,8 @@ export default class SmokeMessages extends AbstractMessages {
this.startListening();
}

async clean() {
await super.clean();
this.#stoped = true;
}
}
17 changes: 17 additions & 0 deletions app-back/Services/Notifications/abstract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Service } from "../service";

export type NotificationsEvents = {
event: "notification-updated";
listener: () => void;
}

export default class AbstractNotifications extends Service<NotificationsEvents>{

async getNotifications(): Promise<any[]> {
return [];
}

async readNotification(_notification: any): Promise<boolean> {
return false;
}
}
89 changes: 89 additions & 0 deletions app-back/Services/Notifications/smoke.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { BASE_URL, anonKey } from "../smoke.config";
import AbstractNotifications 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 SmokeNotifications extends AbstractNotifications{

#notifications = [];
#lastId = null;
#stoped = false;

async reload(){
await super.reload();
this.handleStart();
return null;
}

async handleStart() {
this.startListening();
}

async getNotifications() {
return this.#notifications;
}

async readNotification(_notification: any) {
if(!_notification.smoke_id) return false;
const token = await ipcRenderer.invoke('get-session-storage', 'smoke-token');
await fetch(`/notifications-read`, { id: _notification.smoke_id }, token);
this.#notifications = this.#notifications.filter(notification => notification.smoke_id !== _notification.smoke_id);
this.emit('notification-updated')
return true;
}

#firstCall = true;
async startListening() {
if(this.#stoped) return;
const token = await ipcRenderer.invoke('get-session-storage', 'smoke-token');
if(!token) return;
try{
const result = await fetch(`/notifications`, { lastId: this.#lastId }, token);
const notifications = result.notifications.map(notification => ({
...notification,
smoke_id: notification.id,
}))

if(result.newNotifications && notifications && notifications.length > 0) {
this.#notifications = notifications;
this.#lastId = notifications[notifications.length - 1].smoke_id;
this.emit('notification-updated')
if(!this.#firstCall){
const appNotification = new Notification(`${notifications.length} new notification${notifications.length > 1 ? 's' : ''}`, {
icon: await ipcRenderer.invoke('get-icon-path', 'icon.png'),
body: `${notifications[notifications.length - 1].content}`,
});
appNotification.onclick = () => {
ipcRenderer.invoke('wake-up');
}
}else{
this.#firstCall = false;
}
}
}catch(e) {
console.error(e);
await new Promise(resolve => setTimeout(resolve, 3000));
}
this.startListening();
}

async clean() {
await super.clean();
this.#stoped = true;
}
}
7 changes: 6 additions & 1 deletion app-back/Services/Storage/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ export type Game = {
mapping?: any;
};

export default class AbstractStorage extends Service {
export type StorageEvents = {
event: "game-added";
listener: (game: Game) => void;
}

export default class AbstractStorage extends Service<StorageEvents>{

async getGames(): Promise<Record<string,Game>> {
return {}
Expand Down
1 change: 1 addition & 0 deletions app-back/Services/Storage/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class LocalStorage extends AbstractStorage {
...this.#store.get('games', {}),
[game.id]: game,
});
this.emit('game-added', game)
}

async setGame(game){
Expand Down
13 changes: 12 additions & 1 deletion app-back/Services/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import AbstractAccount from "./Account/abstract";
import AbstractController from "./Controller/abstract";
import AbstractFriends from "./Friends/abstract";
import AbstractMessages from "./Messages/abstract";
import AbstractNotifications from "./Notifications/abstract";

export const abstractServices = {
Account: AbstractAccount,
Expand All @@ -18,4 +19,14 @@ export const abstractServices = {
Controller: AbstractController,
Friends: AbstractFriends,
Messages: AbstractMessages,
}
Notifications: AbstractNotifications,
}

export const hiddenProperties = [
"constructor",
"setServices",
"setEventEmitter",
"events",
"emit",
"clean",
] as const;
Loading

0 comments on commit aca9cb5

Please sign in to comment.