Skip to content

Commit

Permalink
Merge branch 'main' into prod
Browse files Browse the repository at this point in the history
  • Loading branch information
BaptisteLecat committed Jul 1, 2024
2 parents 60ddc08 + 6879068 commit de63f37
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/midjourney_cloud_function.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# MidJourney Cloud Function

This is a Google Cloud Function used to access the MidJourney Discord channel on my Discord Server and query the MidJourney bot to generate stuff. This function is triggered when a new document (generation) is created.

## How it works

```mermaid
graph TB;
weather_journey_app[Weather Journey App] -->|User selects a location| generation_process[Initiate Generation Process]
generation_process -->|API request| weather_journey_image_api[Weather Journey Image API]
weather_journey_image_api -->|Fetch weather data & craft ChatGPT prompt| midJourneyPrompt[Create Generation Document in Firestore]
midJourneyPrompt -->|Document creation triggers| cloudFunction[Cloud Function]
cloudFunction -->|Function invokes| midJourney[MidJourney Bot]
classDef classDefault fill:#fff,stroke:#333,stroke-width:1px;
classDef classLink stroke:#333,stroke-width:1px;
class weather_journey_app,generation_process,weather_journey_image_api,midJourneyPrompt,cloudFunction,midJourney classDefault;
linkStyle default classLink;
```

4 changes: 2 additions & 2 deletions functions/src/entities/generation.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Generation {
return new Generation(id, data.generatedImage, data.progress, data.prompt, data.createdAt);
}

static fromJson(data: any): Generation {
static fromJSON(data: any): Generation {
return new Generation(data.id, data.generatedImage, data.progress, data.prompt, data.createdAt);
}

Expand All @@ -34,7 +34,7 @@ export class Generation {
};
}

toJson(): any {
toJSON(): any {
return {
id: this.id,
generatedImage: (this.generatedImage == null) ? null : this.generatedImage.toJSON(),
Expand Down
39 changes: 39 additions & 0 deletions functions/src/entities/location.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export class Location {
id: string;
city: string;
latitude: number;
longitude: number;

public constructor(id: string, city: string, latitude: number, longitude: number) {
this.id = id;
this.city = city;
this.latitude = latitude;
this.longitude = longitude;
}

static fromFirestoreDocument(id: any, data: any): Location {
return new Location(id, data.city, data.latitude, data.longitude);
}

static fromJson(data: any): Location {
return new Location(data.id, data.city, data.latitude, data.longitude);
}

toFirestoreDocument(): any {
return {
id: this.id,
city: this.city,
latitude: this.latitude,
longitude: this.longitude,
};
}

toJson(): any {
return {
id: this.id,
city: this.city,
latitude: this.latitude,
longitude: this.longitude,
};
}
}
61 changes: 61 additions & 0 deletions functions/src/entities/root-generation-user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export class RootGenerationUser {
id: string;
firstname: string;
lastname: string;
email: string;

public constructor(
id: string,
firstname: string,
lastname: string,
email: string,
) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}

static fromFirestoreDocument(id: any, data: any): RootGenerationUser {
return new RootGenerationUser(
id,
data.firstname,
data.lastname,
data.email,
);
}

static fromJson(data: any): RootGenerationUser {
return new RootGenerationUser(
data.id,
data.firstname,
data.lastname,
data.email,
);
}

toFirestoreDocument(): any {
const firestoreDocument: any = {
id: this.id,
firstname: this.firstname,
lastname: this.lastname,
email: this.email,
};
//Remove all undefined values
Object.keys(firestoreDocument).forEach(
(key) =>
firestoreDocument[key] === undefined && delete firestoreDocument[key],
);

return firestoreDocument;
}

toJson(): any {
return {
id: this.id,
firstname: this.firstname,
lastname: this.lastname,
email: this.email,
};
}
}
43 changes: 43 additions & 0 deletions functions/src/entities/root-generation.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {Generation} from "./generation.entity";
import {Location} from "./location.entity";
import {RootGenerationUser} from "./root-generation-user.entity";

export class RootGeneration {
id: string;
generation: Generation;
location: Location;
user: RootGenerationUser;

public constructor(id: string, generation: Generation, location: Location, user: RootGenerationUser) {
this.id = id;
this.generation = generation;
this.location = location;
this.user = user;
}

static fromFirestoreDocument(id: any, data: any): RootGeneration {
return new RootGeneration(id, data.generation, data.location, data.user);
}

static fromJson(data: any): RootGeneration {
return new RootGeneration(data.id, data.generation, data.location, data.user);
}

toFirestoreDocument(): any {
return {
id: this.id,
generation: (this.generation == null) ? null : this.generation.toFirestoreDocument(),
location: (this.location == null) ? null : this.location.toFirestoreDocument(),
user: (this.user == null) ? null : this.user.toFirestoreDocument(),
};
}

toJson(): any {
return {
id: this.id,
generatedImage: (this.generation == null) ? null : this.generation.toJSON(),
location: (this.location == null) ? null : this.location.toJson(),
user: (this.user == null) ? null : this.user.toJson(),
};
}
}
16 changes: 16 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import * as functions from "firebase-functions";
import { Generation } from "./entities/generation.entity";
import { ImageGeneratorService } from "./services/image_generator.service";
import * as admin from 'firebase-admin';
import {RootGeneration} from "./entities/root-generation.entity";
import {Location} from "./entities/location.entity";
import {RootGenerationUser} from "./entities/root-generation-user.entity";

admin.initializeApp();
const db = admin.firestore();
Expand Down Expand Up @@ -36,6 +39,19 @@ export const onGenerationCreated = functions.runWith({ timeoutSeconds: 160 }).re

});
imageGeneratorService.offProgress();

const userRef = db.collection('users').doc(context.params.userId);
const user = await userRef.get();

const locationRef = db.collection('users').doc(context.params.userId).collection('locations').doc(context.params.locationId);
const location = await locationRef.get();

//Create a generation document in root generations collection for easy access
const generationRef = db.collection('generations').doc(generationId);
console.log("Saving generation to root generations collection")
console.log(new RootGeneration(generationId, generation, Location.fromFirestoreDocument(location.id, location.data()), RootGenerationUser.fromFirestoreDocument(user.id, user.data())).toFirestoreDocument());
await generationRef.set(new RootGeneration(generationId, generation, Location.fromFirestoreDocument(location.id, location.data()), RootGenerationUser.fromFirestoreDocument(user.id, user.data())).toFirestoreDocument());

});


Expand Down

0 comments on commit de63f37

Please sign in to comment.