Cloud Functions - ZachAlba/305_Project GitHub Wiki

Firebase Cloud Functions

This application uses Firebase Cloud Functions to delegate some computing tasks to the server, mainly for interacting with the database, as well as retrieve data from the Spotify API.

Accessing the Database with Cloud Functions

Firebase Cloud Functions are utilized in this application to interact with the Firebase Realtime Database (RTDB). This section outlines how cloud functions are employed to retrieve and manipulate data in the database.

Retrieving Posts

One of the primary functionalities of our cloud functions is to retrieve posts from the database. We have a cloud function named getPosts which accepts various query parameters to tailor the returned posts.

Query Parameters

  • single: If set to true, only the posts belonging to a specific user are retrieved.
  • userId: The unique identifier of the user whose posts are to be fetched.
  • replies: If set to true, retrieves the replies to a specific post.
  • postId: The identifier of the post for which replies are to be fetched.

Function Logic

  • The function first checks the provided parameters and ensures that essential parameters are present.
  • If single is set to true, it retrieves posts belonging only to the specified user.
  • If replies is set to true, it retrieves replies to a specific post.
  • If neither single nor replies is specified, it retrieves posts from users that the specified user is following.
  • The function logs relevant information to Google Cloud console during execution and handles errors gracefully.

Finding Close Users

Another functionality provided by our cloud functions is finding users with usernames similar to a given search term. This is achieved using the Levenshtein distance algorithm.

Algorithm Used

  • We utilize the Levenshtein distance algorithm to calculate the similarity between the search term and usernames in the database.
  • This allows for flexibility in searching, accommodating for misspellings or slight variations in the search term.

Function Logic

  • The cloud function getCloseUsers implements the logic for finding users with usernames similar to the provided search term.
  • It retrieves all usernames from the database, calculates the Levenshtein distance between each username and the search term, and returns the usernames with the closest matches.
  • The function logs relevant information to Google Cloud console during execution and handles errors gracefully.

Spotify API Functions

This section outlines the cloud functions responsible for interacting with the Spotify API.

Retrieving Spotify Access Token

The cloud function getSpotifyToken is responsible for retrieving the access token required to make requests to the Spotify API.

Function Logic

  • The function retrieves the client ID and client secret from Firebase environment variables.
  • It sends a request to the Spotify API to obtain the access token using client credentials flow.
  • The access token is then returned to the client for further use.

Retrieving New Releases

The cloud function returnNewReleases fetches the latest new releases from the Spotify API.

Function Logic

  • The function receives a token as a query parameter, which is used to authenticate the request to the Spotify API.
  • It sends a request to the Spotify API to retrieve the latest new releases.
  • The response is parsed and formatted before being sent back to the client.

Retrieving Artist Data

The cloud function getArtistData retrieves data about a specific artist from the Spotify API.

Function Logic

  • The function receives a token and an artist ID as query parameters.
  • It sends requests to the Spotify API to fetch information about the artist, including top tracks and albums.
  • The responses are parsed and combined before being sent back to the client.

Searching Spotify

The cloud function searchSpotify allows users to search for artists on Spotify.

Function Logic

  • The function receives a token and an artist name as query parameters.
  • It sends a request to the Spotify API to search for the specified artist.
  • The response is parsed and formatted before being sent back to the client.