Skip to content

Commit

Permalink
Add Spotify connection, improve server-sent event calling
Browse files Browse the repository at this point in the history
  • Loading branch information
StamTsag committed Mar 15, 2024
1 parent 17544dc commit 968e006
Show file tree
Hide file tree
Showing 16 changed files with 289 additions and 9 deletions.
11 changes: 8 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ FRONVO_TENOR_KEY={str/Array[str], Required}
FRONVO_FIREBASE_CREDENTIALS={str, Required}
FRONVO_FIREBASE_PROJECT_ID={str, Required}

# Turbo secret for applying, UUID and something else
FRONVO_TURBO_SECRET={str, Required}
# Encrypted Fronvo keys
FRONVO_TURBO_SECRET={str}
FRONVO_GENERAL_SECRET={str}

# For the desktop app version checker
FRONVO_VERSION={str, Required}
Expand All @@ -60,4 +61,8 @@ FRONVO_AES_IV={str, Optional}
FRONVO_WEEKLY_RESTARTS={bool, Optional, Default: false}

# Default server to join upon account creation
FRONVO_OFFICIAL_SERVER={str, Optional}
FRONVO_OFFICIAL_SERVER={str, Optional}

# Spotify utilities
FRONVO_SPOTIFY_CLIENT_ID={str, Optional}
FRONVO_SPOTIFY_CLIENT_SECRET={str, Optional}
6 changes: 6 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ model Account {
turbo Boolean? @default(false)
turboCH String?
fcm String? @default("")
hasSpotify Boolean? @default(false)
spotifyName String? @default("")
spotifyURL String? @default("")
spotifyRefreshToken String? @default("")
spotifyAccessToken String? @default("")
spotifyTokenType String? @default("")
}

model Post {
Expand Down
4 changes: 4 additions & 0 deletions src/events/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ import leaveServer from 'events/account/leaveServer';
import kickMember from 'events/account/kickMember';
import banMember from 'events/account/banMember';
import unbanMember from 'events/account/unbanMember';
import updateConnectionSpotify from 'events/account/updateConnectionSpotify';
import removeConnectionSpotify from 'events/account/removeConnectionSpotify';

import { EventExportTemplate } from 'interfaces/all';

Expand Down Expand Up @@ -106,6 +108,8 @@ const accountTemplate: EventExportTemplate = {
kickMember,
banMember,
unbanMember,
updateConnectionSpotify,
removeConnectionSpotify,
};

export default accountTemplate;
6 changes: 1 addition & 5 deletions src/events/account/deletePost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ async function deletePost({
},
});

io.to(account.profileId).emit('postRemoved', {
postId,
});

io.to(socket.id).emit('postRemoved', {
io.to(account.profileId).to(socket.id).emit('postRemoved', {
postId,
});

Expand Down
7 changes: 7 additions & 0 deletions src/events/account/fetchConvos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ async function fetchConvos({
turbo: true,
status: true,
statusUpdatedTime: true,
hasSpotify: true,
spotifyName: true,
spotifyURL: true,
},
}
)),
Expand Down Expand Up @@ -128,6 +131,10 @@ async function fetchConvos({
: '',
online:
isFriend && targetDMUserData.online,
hasSpotify: targetDMUserData.hasSpotify,
spotifyName:
targetDMUserData.spotifyName,
spotifyURL: targetDMUserData.spotifyURL,
},
dmHiddenFor: convo.dmHiddenFor,
totalMessages,
Expand Down
3 changes: 3 additions & 0 deletions src/events/account/fetchProfileData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ async function fetchProfileData({
status: showStatus ? targetAccount.status : '',
totalPosts,
isTurbo: targetAccount.turbo || targetAccount.profileId == 'fronvo',
hasSpotify: targetAccount.hasSpotify,
spotifyName: targetAccount.spotifyName,
spotifyURL: targetAccount.spotifyURL,
};

// More data if our profile
Expand Down
60 changes: 60 additions & 0 deletions src/events/account/removeConnectionSpotify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// ******************** //
// The removeConnectionSpotify account event file.
// ******************** //

import {
RemoveConnectionSpotifyResult,
RemoveConnectionSpotifyServerParams,
} from 'interfaces/account/removeConnectionSpotify';
import { EventTemplate, FronvoError } from 'interfaces/all';
import { prismaClient } from 'variables/global';

async function removeConnectionSpotify({
io,
socket,
account,
}: RemoveConnectionSpotifyServerParams): Promise<
RemoveConnectionSpotifyResult | FronvoError
> {
if (!account.hasSpotify) {
return;
}

try {
await prismaClient.account.update({
where: {
profileId: account.profileId,
},

data: {
hasSpotify: false,
spotifyName: '',
spotifyRefreshToken: '',
spotifyAccessToken: '',
spotifyTokenType: '',
spotifyURL: '',
},
});

io.to(account.profileId)
.to(socket.id)
.emit('connectionsUpdated', {
profileId: account.profileId,

spotify: {
hasSpotify: false,
spotifyName: '',
spotifyURL: '',
},
});
} catch (e) {}

return {};
}

const removeConnectionSpotifyTemplate: EventTemplate = {
func: removeConnectionSpotify,
template: [],
};

export default removeConnectionSpotifyTemplate;
5 changes: 5 additions & 0 deletions src/events/account/requestData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ async function requestData({
friends: true,
seenStates: true,
dataSentTime: true,
fcm: true,
hasSpotify: true,
spotifyName: true,
spotifyURL: true,
turbo: true,
},
})
)
Expand Down
113 changes: 113 additions & 0 deletions src/events/account/updateConnectionSpotify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// ******************** //
// The updateConnectionSpotify account event file.
// ******************** //

import { StringSchema } from '@ezier/validate';
import {
UpdateConnectionSpotifyResult,
UpdateConnectionSpotifyServerParams,
} from 'interfaces/account/updateConnectionSpotify';
import { EventTemplate, FronvoError } from 'interfaces/all';
import { generateError, getLoggedInSockets } from 'utilities/global';
import { prismaClient } from 'variables/global';
import { getEnv } from 'variables/varUtils';

async function updateConnectionSpotify({
io,
socket,
account,
name,
url,
refreshToken,
accessToken,
tokenType,
secret,
}: UpdateConnectionSpotifyServerParams): Promise<
UpdateConnectionSpotifyResult | FronvoError
> {
if (secret != getEnv('GENERAL_SECRET')) {
return generateError('UNKNOWN');
}

try {
await prismaClient.account.update({
where: {
profileId: account.profileId,
},

data: {
hasSpotify: true,
spotifyName: name,
spotifyURL: url,
spotifyRefreshToken: refreshToken,
spotifyAccessToken: accessToken,
spotifyTokenType: tokenType,
},
});

io.to(account.profileId)
.to(socket.id)
.emit('connectionsUpdated', {
profileId: account.profileId,

spotify: {
hasSpotify: true,
spotifyName: name,
spotifyURL: url,
},
});

// To all sockets in this account
const targetSockets = getLoggedInSockets();

for (const socketIndex in targetSockets) {
const socketObj = targetSockets[socketIndex];

if (
socketObj.accountId == account.profileId &&
socketObj.socket.id != socket.id
) {
io.to(socketObj.socket.id).emit('connectionsUpdated', {
profileId: account.profileId,

spotify: {
hasSpotify: true,
spotifyName: name,
spotifyURL: url,
},
});
}
}
} catch (e) {}

return {};
}

const updateConnectionsTemplate: EventTemplate = {
func: updateConnectionSpotify,
template: [
'name',
'url',
'refreshToken',
'accessToken',
'tokenType',
'secret',
],
schema: new StringSchema({
name: {},

url: {},

refreshToken: {},

accessToken: {},

tokenType: {},

secret: {
minLength: 36,
},
}),
};

export default updateConnectionsTemplate;
5 changes: 5 additions & 0 deletions src/events/dispatchers/eventDispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ async function runEventFunc(
status: true,
statusUpdatedTime: true,
username: true,
hasSpotify: true,
spotifyAccessToken: true,
spotifyName: true,
spotifyTokenType: true,
spotifyURL: true,
},
});
} catch (e) {
Expand Down
13 changes: 13 additions & 0 deletions src/interfaces/account/connectionsUpdated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ******************** //
// Interfaces for the connectionsUpdated event file.
// ******************** //

export interface ConnectionsUpdatedParams {
profileId: string;

spotify: {
hasSpotify: boolean;
spotifyName: string;
spotifyURL: string;
};
}
17 changes: 17 additions & 0 deletions src/interfaces/account/removeConnectionSpotify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ******************** //
// Interfaces for the removeConnectionSpotify event file.
// ******************** //

import { FronvoError, EventArguments } from 'interfaces/all';

export interface RemoveConnectionSpotifyParams {}

export interface RemoveConnectionSpotifyServerParams
extends EventArguments,
RemoveConnectionSpotifyParams {}

export interface RemoveConnectionSpotifyResult {}

export interface RemoveConnectionSpotifyTestResult
extends FronvoError,
RemoveConnectionSpotifyResult {}
24 changes: 24 additions & 0 deletions src/interfaces/account/updateConnectionSpotify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ******************** //
// Interfaces for the updateConnectionSpotify event file.
// ******************** //

import { FronvoError, EventArguments } from 'interfaces/all';

export interface UpdateConnectionSpotifyParams {
name: string;
url: string;
refreshToken: string;
accessToken: string;
tokenType: string;
secret: string;
}

export interface UpdateConnectionSpotifyServerParams
extends EventArguments,
UpdateConnectionSpotifyParams {}

export interface UpdateConnectionSpotifyResult {}

export interface UpdateConnectionSpotifyTestResult
extends FronvoError,
UpdateConnectionSpotifyResult {}
16 changes: 16 additions & 0 deletions src/interfaces/events/c2s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ import {
UnbanMemberParams,
UnbanMemberTestResult,
} from 'interfaces/account/unbanMember';
import {
UpdateConnectionSpotifyParams,
UpdateConnectionSpotifyTestResult,
} from 'interfaces/account/updateConnectionSpotify';
import {
RemoveConnectionSpotifyParams,
RemoveConnectionSpotifyTestResult,
} from 'interfaces/account/removeConnectionSpotify';

export interface ClientToServerEvents {
register: (
Expand Down Expand Up @@ -427,4 +435,12 @@ export interface ClientToServerEvents {
{}: UnbanMemberParams,
callback?: ({}: UnbanMemberTestResult) => void
) => void;
updateConnectionSpotify: (
{}: UpdateConnectionSpotifyParams,
callback?: ({}: UpdateConnectionSpotifyTestResult) => void
) => void;
removeConnectionSpotify: (
{}: RemoveConnectionSpotifyParams,
callback?: ({}: RemoveConnectionSpotifyTestResult) => void
) => void;
}
3 changes: 3 additions & 0 deletions src/interfaces/events/s2c.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { ServerInviteRegeneratedParams } from 'interfaces/account/serverInviteRe
import { ChannelRenamedParams } from 'interfaces/account/channelRenamed';
import { MemberBannedParams } from 'interfaces/account/memberBanned';
import { MemberUnbannedParams } from 'interfaces/account/memberUnbanned';
import { ConnectionsUpdatedParams } from 'interfaces/account/connectionsUpdated';

export interface ServerToClientEvents {
registerVerify: (
Expand Down Expand Up @@ -106,4 +107,6 @@ export interface ServerToClientEvents {
serverInvitesToggled: ({}: ServerInvitesToggledParams) => void;

serverInviteRegenerated: ({}: ServerInviteRegeneratedParams) => void;

connectionsUpdated: ({}: ConnectionsUpdatedParams) => void;
}
Loading

0 comments on commit 968e006

Please sign in to comment.