Skip to content

Commit

Permalink
0.0.9 (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
narafrost authored Dec 30, 2023
2 parents e17b533 + d3cc8d6 commit c4f364c
Show file tree
Hide file tree
Showing 54 changed files with 2,897 additions and 1,903 deletions.
8 changes: 8 additions & 0 deletions app-back/Services/Account/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ export default class AbstractAuth extends Service {
return null;
}

async getProfileData(_id: string): Promise<any> {
return {};
}

async setUserData(_type: string, _data: any = {}): Promise<any> {
return null;
}

async changeAvatar(_type: string, _data: any = {}): Promise<boolean> {
return true;
}

async logout(_type: string): Promise<boolean> {
return true;
}
}
36 changes: 30 additions & 6 deletions app-back/Services/Account/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export default class SmokeAccount extends AbstractAccount {
#token: string = "";
#userData: any = {};

init() {
super.init();
// this.handleStart();
async init() {
await super.init();
await this.handleStart();
}

async handleStart() {
Expand All @@ -38,7 +38,10 @@ export default class SmokeAccount extends AbstractAccount {
}

private async saveSession(token) {
await ipcRenderer.invoke('set-session-storage', { key: "smoke-token", value: token });
this.#token = token;
this.services.Friends.reload();
this.services.Messages.reload();
}

private async getSession() {
Expand All @@ -57,7 +60,9 @@ export default class SmokeAccount extends AbstractAccount {

private async fetchUserData() {
this.#userData = await fetch(`/account`, {}, this.#token);
if(!this.#userData) this.#userData = {};
if(this.#userData.avatar) this.#userData.avatar = `${BASE_STORE}/${this.#userData.avatar}`;
this.#userData.smoke_id = this.#userData.id;
}

async getUserData(type: string, _options?: any) {
Expand Down Expand Up @@ -120,14 +125,14 @@ export default class SmokeAccount extends AbstractAccount {
case "email":
response = await fetch(`/auth`, {type, data:opts})
if(!response.access_token) return false;
this.saveSession(response.access_token);
this.saveCreditentials(opts.email, opts.password);
await this.saveSession(response.access_token);
await this.saveCreditentials(opts.email, opts.password);
this.fetchUserData();
return true
case "token":
response = await fetch(`/auth`, {type, data:opts})
if(!response.access_token) return false
this.saveSession(opts.token);
await this.saveSession(opts.token);
this.fetchUserData();
return true
case "discord": // need more here
Expand All @@ -147,4 +152,23 @@ export default class SmokeAccount extends AbstractAccount {
}
}

async getProfileData(id: string): Promise<any> {
const result = await fetch(`/profile`, {id}, this.#token);
if(result.avatar) result.avatar = `${BASE_STORE}/${result.avatar}`;
result.smoke_id = result.id;
return result;
}

async logout(type) {
switch(type) {
case "email":
case "token":
await this.saveSession("");
await this.saveCreditentials("", "");
this.#userData = {};
return true;
default:
return false;
}
}
}
2 changes: 1 addition & 1 deletion app-back/Services/Emulator/windows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class WindowsEmulatorManager extends AbstractEmulatorManager {
}

async init(){
super.init();
await super.init();
const customEmulators = await this.services.Storage.get('customEmulators', {}) as Record<string, Emulator>;
Object.values(customEmulators).forEach((emulator) => this.initCustomEmulator(emulator));
}
Expand Down
37 changes: 37 additions & 0 deletions app-back/Services/Friends/abstract.ts
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;
}

}
59 changes: 59 additions & 0 deletions app-back/Services/Friends/smoke.ts
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;
}



}
16 changes: 16 additions & 0 deletions app-back/Services/Messages/abstract.ts
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;
}
}
80 changes: 80 additions & 0 deletions app-back/Services/Messages/smoke.ts
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();
}

}
1 change: 1 addition & 0 deletions app-back/Services/Metadata/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class SmokeMetadata extends AbstractScanner {

return {
...game,
name: data?.result?.name || game.name,
image: cover,
banner: banner,
description: "No description yet",
Expand Down
4 changes: 2 additions & 2 deletions app-back/Services/Mods/windows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export default class WindowsModManager extends AbstractModManager {
// xdelta: new XDeltaInjection(),
}

init(){
super.init();
async init(){
await super.init();
Object.values(this.#injections).forEach(injection => injection.setServices(this.services));
}

Expand Down
2 changes: 1 addition & 1 deletion app-back/Services/Storage/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class AbstractStorage extends Service {
return null;
}

async clear(){
async clear(_location: string){
return null;
}
}
21 changes: 19 additions & 2 deletions app-back/Services/Storage/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,24 @@ export default class LocalStorage extends AbstractStorage {
this.#store.delete("storage."+id);
}

async clear(){
this.#store.clear();
async clear(location){
switch(location){
case 'games':
const currentGames = this.#store.get('games', {});
for (const game in currentGames) {
if(currentGames[game].image?.startsWith?.('file://')) ipcRenderer.invoke('delete-local-image', {
path: currentGames[game].image,
});
if(currentGames[game].banner?.startsWith?.('file://')) ipcRenderer.invoke('delete-local-image', {
path: currentGames[game].banner,
});
}
this.#store.set('games', {});
break;
case 'all':
this.clear('games');
break;
}

}
}
4 changes: 4 additions & 0 deletions app-back/Services/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import AbstractMetadata from "./Metadata/abstract";
import AbstractModManager from "./Mods/abstractManager";
import AbstractAccount from "./Account/abstract";
import AbstractController from "./Controller/abstract";
import AbstractFriends from "./Friends/abstract";
import AbstractMessages from "./Messages/abstract";

export const abstractServices = {
Account: AbstractAccount,
Expand All @@ -14,4 +16,6 @@ export const abstractServices = {
Scanner: AbstractScanner,
Storage: AbstractStorage,
Controller: AbstractController,
Friends: AbstractFriends,
Messages: AbstractMessages,
}
Loading

0 comments on commit c4f364c

Please sign in to comment.