Skip to content

Commit

Permalink
feat(*): single btn to trigger notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnisDa committed Jul 30, 2023
1 parent 7fdbf22 commit 83da069
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 74 deletions.
9 changes: 4 additions & 5 deletions apps/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ use tower_http::{

use crate::{
background::{
general_media_cleanup_jobs, general_user_cleanup, import_media,
recalculate_user_summary_job, update_exercise_job, update_metadata_job, user_created_job,
yank_integrations_data,
import_media, media_jobs, recalculate_user_summary_job, update_exercise_job,
update_metadata_job, user_created_job, user_jobs, yank_integrations_data,
},
config::get_app_config,
config::AppConfig,
Expand Down Expand Up @@ -259,7 +258,7 @@ async fn main() -> Result<()> {
)
.layer(ApalisTraceLayer::new())
.layer(ApalisExtension(media_service_1.clone()))
.build_fn(general_user_cleanup)
.build_fn(user_jobs)
})
.register_with_count(1, move |c| {
WorkerBuilder::new(format!("general_media_cleanup_job-{c}"))
Expand All @@ -272,7 +271,7 @@ async fn main() -> Result<()> {
.layer(ApalisTraceLayer::new())
.layer(ApalisExtension(importer_service_2.clone()))
.layer(ApalisExtension(media_service_2.clone()))
.build_fn(general_media_cleanup_jobs)
.build_fn(media_jobs)
})
.register_with_count(1, move |c| {
WorkerBuilder::new(format!("yank_integrations_data-{c}"))
Expand Down
61 changes: 37 additions & 24 deletions apps/backend/src/miscellaneous/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,17 +972,11 @@ impl MiscellaneousMutation {
.await
}

/// Test a notification platform for the currently logged in user.
async fn test_user_notification_platform(
&self,
gql_ctx: &Context<'_>,
notification_id: usize,
) -> Result<bool> {
/// Test all notification platforms for the currently logged in user.
async fn test_user_notification_platforms(&self, gql_ctx: &Context<'_>) -> Result<bool> {
let service = gql_ctx.data_unchecked::<Arc<MiscellaneousService>>();
let user_id = service.user_id_from_ctx(gql_ctx).await?;
service
.test_user_notification_platform(user_id, notification_id)
.await
service.test_user_notification_platforms(user_id).await
}

/// Delete a notification platform for the currently logged in user.
Expand Down Expand Up @@ -1979,11 +1973,28 @@ impl MiscellaneousService {
production_status: String,
publish_year: Option<i32>,
) -> Result<()> {
let mut notifications = vec![];

let meta = Metadata::find_by_id(metadata_id)
.one(&self.db)
.await
.unwrap()
.unwrap();

if meta.production_status != production_status {
notifications.push(format!(
"Status changed from {:#?} to {:#?}",
meta.production_status, production_status
));
}

if meta.publish_year != publish_year {
notifications.push(format!(
"Publish year from {:#?} to {:#?}",
meta.publish_year, publish_year
));
}

let mut meta: metadata::ActiveModel = meta.into();
meta.title = ActiveValue::Set(title);
meta.description = ActiveValue::Set(description);
Expand Down Expand Up @@ -3509,22 +3520,14 @@ impl MiscellaneousService {
Ok(new_notification_id)
}

async fn test_user_notification_platform(
&self,
user_id: i32,
notification_id: usize,
) -> Result<bool> {
async fn test_user_notification_platforms(&self, user_id: i32) -> Result<bool> {
let user = self.user_by_id(user_id).await?;
let notifications = user.notifications.clone().0;
let notification = notifications.into_iter().find(|i| i.id == notification_id);
if let Some(nt) = notification {
nt.settings
.send_message(format!("Test notification message triggered."))
.await?;
Ok(true)
} else {
Ok(false)
}
self.send_notifications_to_user_platforms(
user_id,
format!("Test notification message triggered."),
)
.await?;
Ok(true)
}

async fn delete_user_notification_platform(
Expand Down Expand Up @@ -3944,6 +3947,16 @@ impl MiscellaneousService {
};
Ok(())
}

pub async fn send_notifications_to_user_platforms(
&self,
user_id: i32,
msg: String,
) -> Result<()> {
let user = self.user_by_id(user_id).await?;
for notification in user.notifications.0 {}
Ok(())
}
}

fn modify_seen_elements(all_seen: &mut [seen::Model]) {
Expand Down
53 changes: 25 additions & 28 deletions apps/frontend/src/pages/settings/notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ import {
type CreateUserNotificationPlatformMutationVariables,
DeleteUserNotificationPlatformDocument,
type DeleteUserNotificationPlatformMutationVariables,
TestUserNotificationPlatformDocument,
type TestUserNotificationPlatformMutationVariables,
UserNotificationPlatformLot,
UserNotificationPlatformsDocument,
type TestUserNotificationPlatformsMutationVariables,
TestUserNotificationPlatformsDocument,
} from "@ryot/generated/graphql/backend/graphql";
import { changeCase, formatTimeAgo } from "@ryot/utilities";
import { IconPlayerPlay, IconTrash } from "@tabler/icons-react";
import { IconTrash } from "@tabler/icons-react";
import { useMutation, useQuery } from "@tanstack/react-query";
import Head from "next/head";
import { type ReactElement, useState } from "react";
Expand Down Expand Up @@ -92,15 +92,15 @@ const Page: NextPageWithLayout = () => {
},
});

const testUserNotificationPlatform = useMutation({
const testUserNotificationPlatforms = useMutation({
mutationFn: async (
variables: TestUserNotificationPlatformMutationVariables,
variables: TestUserNotificationPlatformsMutationVariables,
) => {
const { testUserNotificationPlatform } = await gqlClient.request(
TestUserNotificationPlatformDocument,
const { testUserNotificationPlatforms } = await gqlClient.request(
TestUserNotificationPlatformsDocument,
variables,
);
return testUserNotificationPlatform;
return testUserNotificationPlatforms;
},
onSuccess: (data) => {
if (data)
Expand Down Expand Up @@ -150,19 +150,6 @@ const Page: NextPageWithLayout = () => {
<Text size="xs">{formatTimeAgo(notif.timestamp)}</Text>
</Box>
<Group>
<Tooltip label="Send test notification">
<ActionIcon
color="green"
variant="outline"
onClick={() => {
testUserNotificationPlatform.mutate({
notificationId: notif.id,
});
}}
>
<IconPlayerPlay size="1rem" />
</ActionIcon>
</Tooltip>
<Tooltip label="Delete">
<ActionIcon
color="red"
Expand All @@ -188,13 +175,23 @@ const Page: NextPageWithLayout = () => {
<Text>No notification platforms configured</Text>
)}
<Box ml="auto">
<Button
size="xs"
variant="light"
onClick={openCreateUserNotificationPlatformModal}
>
Add new notification platform
</Button>
<Group>
<Button
size="xs"
variant="light"
color="green"
onClick={() => testUserNotificationPlatforms.mutate({})}
>
Trigger test notification
</Button>
<Button
size="xs"
variant="light"
onClick={openCreateUserNotificationPlatformModal}
>
Add new notification platform
</Button>
</Group>
<Modal
opened={createUserNotificationPlatformModalOpened}
onClose={closeCreateUserNotificationPlatformModal}
Expand Down
4 changes: 2 additions & 2 deletions libs/generated/src/graphql/backend/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const documents = {
"mutation RegenerateUserSummary {\n regenerateUserSummary\n}": types.RegenerateUserSummaryDocument,
"mutation RegisterUser($input: UserInput!) {\n registerUser(input: $input) {\n __typename\n ... on RegisterError {\n error\n }\n ... on IdObject {\n id\n }\n }\n}": types.RegisterUserDocument,
"mutation RemoveMediaFromCollection($metadataId: Int!, $collectionName: String!) {\n removeMediaFromCollection(\n metadataId: $metadataId\n collectionName: $collectionName\n ) {\n id\n }\n}": types.RemoveMediaFromCollectionDocument,
"mutation TestUserNotificationPlatform($notificationId: Int!) {\n testUserNotificationPlatform(notificationId: $notificationId)\n}": types.TestUserNotificationPlatformDocument,
"mutation TestUserNotificationPlatforms {\n testUserNotificationPlatforms\n}": types.TestUserNotificationPlatformsDocument,
"mutation UpdateAllMetadata {\n updateAllMetadata\n}": types.UpdateAllMetadataDocument,
"mutation UpdateUser($input: UpdateUserInput!) {\n updateUser(input: $input) {\n id\n }\n}": types.UpdateUserDocument,
"mutation UpdateUserPreference($input: UpdateUserPreferenceInput!) {\n updateUserPreference(input: $input)\n}": types.UpdateUserPreferenceDocument,
Expand Down Expand Up @@ -184,7 +184,7 @@ export function graphql(source: "mutation RemoveMediaFromCollection($metadataId:
/**
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
export function graphql(source: "mutation TestUserNotificationPlatform($notificationId: Int!) {\n testUserNotificationPlatform(notificationId: $notificationId)\n}"): (typeof documents)["mutation TestUserNotificationPlatform($notificationId: Int!) {\n testUserNotificationPlatform(notificationId: $notificationId)\n}"];
export function graphql(source: "mutation TestUserNotificationPlatforms {\n testUserNotificationPlatforms\n}"): (typeof documents)["mutation TestUserNotificationPlatforms {\n testUserNotificationPlatforms\n}"];
/**
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
Expand Down
17 changes: 5 additions & 12 deletions libs/generated/src/graphql/backend/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,8 @@ export type MutationRoot = {
registerUser: RegisterResult;
/** Remove a media item from a collection if it is not there, otherwise do nothing. */
removeMediaFromCollection: IdObject;
/** Test a notification platform for the currently logged in user. */
testUserNotificationPlatform: Scalars['Boolean'];
/** Test all notification platforms for the currently logged in user. */
testUserNotificationPlatforms: Scalars['Boolean'];
/** Deploy jobs to update all media item's metadata. */
updateAllMetadata: Scalars['Boolean'];
/** Update a user's profile details. */
Expand Down Expand Up @@ -730,11 +730,6 @@ export type MutationRootRemoveMediaFromCollectionArgs = {
};


export type MutationRootTestUserNotificationPlatformArgs = {
notificationId: Scalars['Int'];
};


export type MutationRootUpdateUserArgs = {
input: UpdateUserInput;
};
Expand Down Expand Up @@ -1368,12 +1363,10 @@ export type RemoveMediaFromCollectionMutationVariables = Exact<{

export type RemoveMediaFromCollectionMutation = { removeMediaFromCollection: { id: number } };

export type TestUserNotificationPlatformMutationVariables = Exact<{
notificationId: Scalars['Int'];
}>;
export type TestUserNotificationPlatformsMutationVariables = Exact<{ [key: string]: never; }>;


export type TestUserNotificationPlatformMutation = { testUserNotificationPlatform: boolean };
export type TestUserNotificationPlatformsMutation = { testUserNotificationPlatforms: boolean };

export type UpdateAllMetadataMutationVariables = Exact<{ [key: string]: never; }>;

Expand Down Expand Up @@ -1557,7 +1550,7 @@ export const ProgressUpdateDocument = {"kind":"Document","definitions":[{"kind":
export const RegenerateUserSummaryDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"RegenerateUserSummary"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"regenerateUserSummary"}}]}}]} as unknown as DocumentNode<RegenerateUserSummaryMutation, RegenerateUserSummaryMutationVariables>;
export const RegisterUserDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"RegisterUser"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"input"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"UserInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"registerUser"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"input"},"value":{"kind":"Variable","name":{"kind":"Name","value":"input"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"__typename"}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"RegisterError"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"error"}}]}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"IdObject"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}}]}}]}}]}}]} as unknown as DocumentNode<RegisterUserMutation, RegisterUserMutationVariables>;
export const RemoveMediaFromCollectionDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"RemoveMediaFromCollection"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"metadataId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Int"}}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"collectionName"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"removeMediaFromCollection"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"metadataId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"metadataId"}}},{"kind":"Argument","name":{"kind":"Name","value":"collectionName"},"value":{"kind":"Variable","name":{"kind":"Name","value":"collectionName"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}}]}}]}}]} as unknown as DocumentNode<RemoveMediaFromCollectionMutation, RemoveMediaFromCollectionMutationVariables>;
export const TestUserNotificationPlatformDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"TestUserNotificationPlatform"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"notificationId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Int"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"testUserNotificationPlatform"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"notificationId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"notificationId"}}}]}]}}]} as unknown as DocumentNode<TestUserNotificationPlatformMutation, TestUserNotificationPlatformMutationVariables>;
export const TestUserNotificationPlatformsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"TestUserNotificationPlatforms"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"testUserNotificationPlatforms"}}]}}]} as unknown as DocumentNode<TestUserNotificationPlatformsMutation, TestUserNotificationPlatformsMutationVariables>;
export const UpdateAllMetadataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"UpdateAllMetadata"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"updateAllMetadata"}}]}}]} as unknown as DocumentNode<UpdateAllMetadataMutation, UpdateAllMetadataMutationVariables>;
export const UpdateUserDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"UpdateUser"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"input"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"UpdateUserInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"updateUser"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"input"},"value":{"kind":"Variable","name":{"kind":"Name","value":"input"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}}]}}]}}]} as unknown as DocumentNode<UpdateUserMutation, UpdateUserMutationVariables>;
export const UpdateUserPreferenceDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"UpdateUserPreference"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"input"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"UpdateUserPreferenceInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"updateUserPreference"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"input"},"value":{"kind":"Variable","name":{"kind":"Name","value":"input"}}}]}]}}]} as unknown as DocumentNode<UpdateUserPreferenceMutation, UpdateUserPreferenceMutationVariables>;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mutation TestUserNotificationPlatforms {
testUserNotificationPlatforms
}

0 comments on commit 83da069

Please sign in to comment.