-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Spotify connection, improve server-sent event calling
- Loading branch information
Showing
16 changed files
with
289 additions
and
9 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
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,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; |
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,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; |
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,13 @@ | ||
// ******************** // | ||
// Interfaces for the connectionsUpdated event file. | ||
// ******************** // | ||
|
||
export interface ConnectionsUpdatedParams { | ||
profileId: string; | ||
|
||
spotify: { | ||
hasSpotify: boolean; | ||
spotifyName: string; | ||
spotifyURL: string; | ||
}; | ||
} |
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,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 {} |
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,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 {} |
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.