Added alert editing.
This commit is contained in:
@@ -237,6 +237,12 @@
|
||||
title="Alert ID"
|
||||
>#{{ alert.id }}</span>
|
||||
|
||||
<Icon
|
||||
v-tooltip="'Edit alert'"
|
||||
icon="pencil5"
|
||||
@click="editAlert = alert"
|
||||
/>
|
||||
|
||||
<Icon
|
||||
icon="bin"
|
||||
@click="removeAlert(alert)"
|
||||
@@ -250,6 +256,12 @@
|
||||
v-if="showAlertDialog"
|
||||
@close="showAlertDialog = false; reloadAlerts();"
|
||||
/>
|
||||
|
||||
<AlertDialog
|
||||
v-if="editAlert"
|
||||
:alert="editAlert"
|
||||
@close="editAlert = null; reloadAlerts();"
|
||||
/>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
@@ -265,6 +277,7 @@ const pageContext = inject('pageContext');
|
||||
|
||||
const alerts = ref(pageContext.pageProps.alerts);
|
||||
const showAlertDialog = ref(false);
|
||||
const editAlert = ref(null);
|
||||
const done = ref(true);
|
||||
|
||||
const query = ref('');
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<template>
|
||||
<Dialog
|
||||
title="New alert"
|
||||
:title="alert ? `Edit alert #${alert.id}` : 'New alert'"
|
||||
@close="emit('close')"
|
||||
>
|
||||
<form
|
||||
@@ -24,9 +24,11 @@
|
||||
<span class="scene-title">{{ scene.title }}</span>
|
||||
|
||||
<span class="scene-details">
|
||||
<strong class="scene-date"><time :datetime="scene.date.toISOString()">{{ format(scene.date, 'yyyy-MM-dd') }}</time></strong> on
|
||||
<strong class="scene-date"><time :datetime="scene.date.toISOString()">{{ format(scene.date, 'yyyy-MM-dd') }}</time></strong>
|
||||
|
||||
<span class="scene-channel">{{ scene.channel?.name || scene.network?.name }}</span>
|
||||
<template v-if="scene.channel || scene.network">
|
||||
on <span class="scene-channel">{{ scene.channel?.name || scene.network?.name }}</span>
|
||||
</template>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
@@ -501,7 +503,7 @@
|
||||
<div class="dialog-section dialog-actions">
|
||||
<button
|
||||
class="button button-submit"
|
||||
>Set alert</button>
|
||||
>{{ alert ? 'Save alert' : 'Set alert' }}</button>
|
||||
</div>
|
||||
</form>
|
||||
</Dialog>
|
||||
@@ -514,7 +516,7 @@ import { computed, inject, ref, useId } from 'vue';
|
||||
import Dialog from '#/components/dialog/dialog.vue';
|
||||
import Checkbox from '#/components/form/checkbox.vue';
|
||||
|
||||
import { get, post } from '#/src/api.js';
|
||||
import { get, patch, post } from '#/src/api.js';
|
||||
import events from '#/src/events.js';
|
||||
import { getGlobalFilterTags } from '#/src/filter-tags.js';
|
||||
import getPath from '#/src/get-path.js';
|
||||
@@ -532,6 +534,11 @@ const props = defineProps({
|
||||
type: Array,
|
||||
default: () => [],
|
||||
},
|
||||
// an existing alert to edit instead of creating a new one
|
||||
alert: {
|
||||
type: Object,
|
||||
default: null,
|
||||
},
|
||||
presetScenes: {
|
||||
type: Array,
|
||||
default: () => [],
|
||||
@@ -546,28 +553,29 @@ const {
|
||||
env,
|
||||
} = inject('pageContext');
|
||||
|
||||
const actors = ref(props.presetActors);
|
||||
const actors = ref(props.alert ? [...props.alert.actors] : props.presetActors);
|
||||
const actorResults = ref([]);
|
||||
const actorQuery = ref('');
|
||||
|
||||
const entities = ref(props.presetEntities);
|
||||
const entities = ref(props.alert ? [...props.alert.entities] : props.presetEntities);
|
||||
const entityResults = ref([]);
|
||||
const entityQuery = ref('');
|
||||
|
||||
const tags = ref(props.presetTags);
|
||||
const tags = ref(props.alert ? [...props.alert.tags] : props.presetTags);
|
||||
const tagResults = ref([]);
|
||||
const tagQuery = ref('');
|
||||
|
||||
const scenes = ref(props.presetScenes);
|
||||
const scenes = ref(props.alert ? props.alert.scenes.map((scene) => ({ ...scene, date: new Date(scene.date) })) : props.presetScenes);
|
||||
|
||||
const matches = ref([]);
|
||||
// stored expressions are already regular expressions, wrapped in slashes so saving doesn't escape them again
|
||||
const matches = ref(props.alert ? props.alert.matches.map((match) => ({ property: match.property, expression: `/${match.expression}/` })) : []);
|
||||
const matchProperty = ref('title');
|
||||
const matchExpression = ref('');
|
||||
|
||||
const fieldsAnd = ref(true);
|
||||
const actorAnd = ref(true);
|
||||
const tagAnd = ref(true);
|
||||
const matchAnd = ref(true);
|
||||
const fieldsAnd = ref(props.alert?.and.fields ?? true);
|
||||
const actorAnd = ref(props.alert?.and.actors ?? true);
|
||||
const tagAnd = ref(props.alert?.and.tags ?? true);
|
||||
const matchAnd = ref(props.alert?.and.matches ?? true);
|
||||
|
||||
const inputs = {};
|
||||
|
||||
@@ -579,13 +587,13 @@ const addMatchDropdownId = useId();
|
||||
const addStashDropdownId = useId();
|
||||
const addFilterTagDropdownId = useId();
|
||||
|
||||
const notify = ref(JSON.parse(localStorage.getItem('alert_notify')) ?? true);
|
||||
const email = ref(JSON.parse(localStorage.getItem('alert_email')) ?? false);
|
||||
const notify = ref(props.alert ? props.alert.notify : JSON.parse(localStorage.getItem('alert_notify')) ?? true);
|
||||
const email = ref(props.alert ? props.alert.email : JSON.parse(localStorage.getItem('alert_email')) ?? false);
|
||||
|
||||
const stashes = ref([]);
|
||||
const stashes = ref(props.alert ? [...props.alert.stashes] : []);
|
||||
|
||||
// prefilled with the global tag filter
|
||||
const filterTags = ref(getGlobalFilterTags());
|
||||
const filterTags = ref(props.alert ? [...props.alert.filters.tags] : getGlobalFilterTags());
|
||||
const filterTagResults = ref([]);
|
||||
const filterTagQuery = ref('');
|
||||
|
||||
@@ -605,7 +613,7 @@ async function createAlert() {
|
||||
...scenes.value.map((scene) => scene.title),
|
||||
].filter(Boolean).join(', ');
|
||||
|
||||
const newAlert = await post('/alerts', {
|
||||
const body = {
|
||||
all: fieldsAnd.value,
|
||||
allActors: actorAnd.value,
|
||||
allTags: tagAnd.value,
|
||||
@@ -619,11 +627,19 @@ async function createAlert() {
|
||||
email: email.value,
|
||||
stashes: stashes.value.map((stash) => stash.id),
|
||||
filterTags: scenes.value.length === 0 ? filterTags.value.map((tag) => tag.slug) : [],
|
||||
}, {
|
||||
successFeedback: `Alert for '${alertLabel}' set`,
|
||||
errorFeedback: `Failed to set alert for '${alertLabel}'`,
|
||||
appendErrorMessage: true,
|
||||
});
|
||||
};
|
||||
|
||||
const newAlert = props.alert
|
||||
? await patch(`/alerts/${props.alert.id}`, body, {
|
||||
successFeedback: `Alert for '${alertLabel}' updated`,
|
||||
errorFeedback: `Failed to update alert for '${alertLabel}'`,
|
||||
appendErrorMessage: true,
|
||||
})
|
||||
: await post('/alerts', body, {
|
||||
successFeedback: `Alert for '${alertLabel}' set`,
|
||||
errorFeedback: `Failed to set alert for '${alertLabel}'`,
|
||||
appendErrorMessage: true,
|
||||
});
|
||||
|
||||
emit('alert', newAlert);
|
||||
emit('close', true);
|
||||
@@ -726,6 +742,10 @@ function selectStash(selectedStash) {
|
||||
}
|
||||
|
||||
function rememberThen(domain, checked) {
|
||||
if (props.alert) {
|
||||
return; // editing an alert shouldn't change the defaults for new ones
|
||||
}
|
||||
|
||||
localStorage.setItem(`alert_${domain}`, checked);
|
||||
}
|
||||
</script>
|
||||
|
||||
+119
-32
@@ -45,7 +45,7 @@ function curateAlert(alert, context = {}) {
|
||||
actors: alert.all_actors,
|
||||
tags: alert.all_tags,
|
||||
entities: alert.all_entities,
|
||||
matches: alert.all_tags,
|
||||
matches: alert.all_matches,
|
||||
},
|
||||
actors: context.actors?.map((actor) => ({
|
||||
id: actor.actor_id,
|
||||
@@ -203,11 +203,8 @@ export async function fetchAlerts(user, alertIds) {
|
||||
return curatedAlerts;
|
||||
}
|
||||
|
||||
export async function createAlert(alert, reqUser) {
|
||||
if (!reqUser) {
|
||||
throw new HttpError('You are not authenthicated', 401);
|
||||
}
|
||||
|
||||
// validate the input and resolve slugs, shared by creating and updating
|
||||
async function resolveAlert(alert) {
|
||||
const tagIds = (await getIdsBySlug(alert.tags, 'tags') || []).concat(alert.tagIds || []);
|
||||
const entityIds = (await getIdsBySlug(alert.entities || [], 'entities')).concat(alert.entityIds || []);
|
||||
const actorIds = [...(alert.actors || []), ...(alert.actorIds || [])]; // for consistency with tagIds and entityIds
|
||||
@@ -235,68 +232,158 @@ export async function createAlert(alert, reqUser) {
|
||||
throw new HttpError('Failed to resolve all filter tags');
|
||||
}
|
||||
|
||||
const [{ id: alertId }] = await knex('alerts')
|
||||
.insert({
|
||||
user_id: reqUser.id,
|
||||
notify: alert.notify,
|
||||
email: alert.email,
|
||||
all: alert.all,
|
||||
all_actors: alert.allActors,
|
||||
all_entities: alert.allEntities,
|
||||
all_tags: alert.allTags,
|
||||
all_matches: alert.allMatches,
|
||||
from_preset: alert.preset,
|
||||
comment: alert.comment,
|
||||
meta: alert.meta,
|
||||
})
|
||||
.returning('id');
|
||||
return {
|
||||
tagIds,
|
||||
entityIds,
|
||||
actorIds,
|
||||
stashIds,
|
||||
sceneIds,
|
||||
filterTagIds,
|
||||
matches: alert.matches || [],
|
||||
};
|
||||
}
|
||||
|
||||
async function insertAlertRelations(trx, alertId, alert, resolved) {
|
||||
const {
|
||||
actorIds,
|
||||
tagIds,
|
||||
stashIds,
|
||||
entityIds,
|
||||
sceneIds,
|
||||
filterTagIds,
|
||||
matches,
|
||||
} = resolved;
|
||||
|
||||
await Promise.all([
|
||||
actorIds?.length > 0 && knex('alerts_actors').insert(actorIds.map((actorId) => ({
|
||||
actorIds.length > 0 && trx('alerts_actors').insert(actorIds.map((actorId) => ({
|
||||
alert_id: alertId,
|
||||
actor_id: actorId,
|
||||
}))),
|
||||
tagIds?.length > 0 && knex('alerts_tags').insert(tagIds.map((tagId) => ({
|
||||
tagIds.length > 0 && trx('alerts_tags').insert(tagIds.map((tagId) => ({
|
||||
alert_id: alertId,
|
||||
tag_id: tagId,
|
||||
}))),
|
||||
alert.matches?.length > 0 && knex('alerts_matches').insert(alert.matches.map((match) => ({
|
||||
matches.length > 0 && trx('alerts_matches').insert(matches.map((match) => ({
|
||||
alert_id: alertId,
|
||||
property: match.property,
|
||||
expression: /\/.*\//.test(match.expression)
|
||||
? match.expression.slice(1, -1)
|
||||
: escapeRegexp(match.expression),
|
||||
}))),
|
||||
stashIds?.length > 0 && knex('alerts_stashes').insert(stashIds.map((stashId) => ({
|
||||
stashIds.length > 0 && trx('alerts_stashes').insert(stashIds.map((stashId) => ({
|
||||
alert_id: alertId,
|
||||
stash_id: stashId,
|
||||
}))),
|
||||
entityIds?.length > 0 && knex('alerts_entities').insert(entityIds.map((entityId) => ({
|
||||
entityIds.length > 0 && trx('alerts_entities').insert(entityIds.map((entityId) => ({
|
||||
alert_id: alertId,
|
||||
entity_id: entityId,
|
||||
})).slice(0, alert.allEntities ? 1 : undefined)), // one scene can never match multiple entities in AND mode
|
||||
sceneIds?.length > 0 && knex('alerts_scenes').insert(sceneIds.map((sceneId) => ({
|
||||
sceneIds.length > 0 && trx('alerts_scenes').insert(sceneIds.map((sceneId) => ({
|
||||
alert_id: alertId,
|
||||
scene_id: sceneId,
|
||||
}))),
|
||||
filterTagIds.length > 0 && knex('alerts_filters').insert(filterTagIds.map((tagId) => ({
|
||||
filterTagIds.length > 0 && trx('alerts_filters').insert(filterTagIds.map((tagId) => ({
|
||||
alert_id: alertId,
|
||||
tag_id: tagId,
|
||||
}))),
|
||||
]);
|
||||
}
|
||||
|
||||
async function refreshAlertViews(...resolvedAlerts) {
|
||||
await Promise.all([
|
||||
actorIds?.length > 0 && knex.schema.refreshMaterializedView('alerts_users_actors'),
|
||||
tagIds?.length > 0 && knex.schema.refreshMaterializedView('alerts_users_tags'),
|
||||
entityIds?.length > 0 && knex.schema.refreshMaterializedView('alerts_users_entities'),
|
||||
sceneIds?.length > 0 && knex.schema.refreshMaterializedView('alerts_users_scenes'),
|
||||
resolvedAlerts.some((resolved) => resolved.actorIds.length > 0) && knex.schema.refreshMaterializedView('alerts_users_actors'),
|
||||
resolvedAlerts.some((resolved) => resolved.tagIds.length > 0) && knex.schema.refreshMaterializedView('alerts_users_tags'),
|
||||
resolvedAlerts.some((resolved) => resolved.entityIds.length > 0) && knex.schema.refreshMaterializedView('alerts_users_entities'),
|
||||
resolvedAlerts.some((resolved) => resolved.sceneIds.length > 0) && knex.schema.refreshMaterializedView('alerts_users_scenes'),
|
||||
]);
|
||||
}
|
||||
|
||||
export async function createAlert(alert, reqUser) {
|
||||
if (!reqUser) {
|
||||
throw new HttpError('You are not authenthicated', 401);
|
||||
}
|
||||
|
||||
const resolved = await resolveAlert(alert);
|
||||
|
||||
const alertId = await knex.transaction(async (trx) => {
|
||||
const [{ id }] = await trx('alerts')
|
||||
.insert({
|
||||
user_id: reqUser.id,
|
||||
notify: alert.notify,
|
||||
email: alert.email,
|
||||
all: alert.all,
|
||||
all_actors: alert.allActors,
|
||||
all_entities: alert.allEntities,
|
||||
all_tags: alert.allTags,
|
||||
all_matches: alert.allMatches,
|
||||
from_preset: alert.preset,
|
||||
comment: alert.comment,
|
||||
meta: alert.meta,
|
||||
})
|
||||
.returning('id');
|
||||
|
||||
await insertAlertRelations(trx, id, alert, resolved);
|
||||
|
||||
return id;
|
||||
});
|
||||
|
||||
await refreshAlertViews(resolved);
|
||||
|
||||
const [newAlert] = await fetchAlerts(reqUser, [alertId]);
|
||||
|
||||
return newAlert;
|
||||
}
|
||||
|
||||
// replaces the alert's fields and relations wholesale, the client sends the complete alert like it does on creation
|
||||
export async function updateAlert(alertId, alert, reqUser) {
|
||||
if (!reqUser) {
|
||||
throw new HttpError('You are not authenthicated', 401);
|
||||
}
|
||||
|
||||
const [existingAlert] = await fetchAlerts(reqUser, [Number(alertId)]);
|
||||
|
||||
if (!existingAlert) {
|
||||
throw new HttpError(`Could not find alert ${alertId}`, 404);
|
||||
}
|
||||
|
||||
const resolved = await resolveAlert(alert);
|
||||
|
||||
await knex.transaction(async (trx) => {
|
||||
await trx('alerts')
|
||||
.where('id', existingAlert.id)
|
||||
.where('user_id', reqUser.id)
|
||||
.update({
|
||||
notify: alert.notify,
|
||||
email: alert.email,
|
||||
all: alert.all,
|
||||
all_actors: alert.allActors,
|
||||
all_entities: alert.allEntities,
|
||||
all_tags: alert.allTags,
|
||||
all_matches: alert.allMatches,
|
||||
...(alert.comment !== undefined && { comment: alert.comment }),
|
||||
...(alert.meta !== undefined && { meta: alert.meta }),
|
||||
});
|
||||
|
||||
await Promise.all(['alerts_actors', 'alerts_tags', 'alerts_matches', 'alerts_stashes', 'alerts_entities', 'alerts_scenes', 'alerts_filters'].map((table) => trx(table)
|
||||
.where('alert_id', existingAlert.id)
|
||||
.delete()));
|
||||
|
||||
await insertAlertRelations(trx, existingAlert.id, alert, resolved);
|
||||
});
|
||||
|
||||
// the views need refreshing for what the alert had as well as what it has now
|
||||
await refreshAlertViews(resolved, {
|
||||
actorIds: existingAlert.actors,
|
||||
tagIds: existingAlert.tags,
|
||||
entityIds: existingAlert.entities,
|
||||
sceneIds: existingAlert.scenes,
|
||||
});
|
||||
|
||||
const [updatedAlert] = await fetchAlerts(reqUser, [existingAlert.id]);
|
||||
|
||||
return updatedAlert;
|
||||
}
|
||||
|
||||
export async function removeAlert(alertId, reqUser) {
|
||||
const alert = await knex('alerts')
|
||||
.where('alerts.id', alertId)
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
fetchAlerts,
|
||||
fetchNotifications,
|
||||
removeAlert,
|
||||
updateAlert,
|
||||
updateNotification,
|
||||
updateNotifications,
|
||||
} from '../alerts.js';
|
||||
@@ -40,6 +41,29 @@ export const alertsSchema = `
|
||||
email: Boolean = false
|
||||
stashes: [Int!]
|
||||
stashIds: [Int!]
|
||||
filterTags: [String!]
|
||||
comment: String
|
||||
meta: JSON
|
||||
): Alert
|
||||
|
||||
updateAlert(
|
||||
id: Int!
|
||||
all: Boolean = true
|
||||
allActors: Boolean = true
|
||||
allTags: Boolean = true
|
||||
allMatches: Boolean = true
|
||||
actors: [Int!]
|
||||
actorIds: [Int!]
|
||||
tags: [String!]
|
||||
tagIds: [Int!]
|
||||
entities: [String!]
|
||||
entityIds: [Int!]
|
||||
matches: [AlertMatchInput!]
|
||||
notify: Boolean = true
|
||||
email: Boolean = false
|
||||
stashes: [Int!]
|
||||
stashIds: [Int!]
|
||||
filterTags: [String!]
|
||||
comment: String
|
||||
meta: JSON
|
||||
): Alert
|
||||
@@ -108,6 +132,10 @@ export const alertsSchema = `
|
||||
isPrimary: Boolean
|
||||
}
|
||||
|
||||
type AlertFilters {
|
||||
tags: [AlertTag]
|
||||
}
|
||||
|
||||
type Alert {
|
||||
id: Int
|
||||
notify: Boolean
|
||||
@@ -121,6 +149,7 @@ export const alertsSchema = `
|
||||
entities: [AlertEntity]
|
||||
matches: [AlertMatch]
|
||||
stashes: [AlertStash]
|
||||
filters: AlertFilters
|
||||
comment: String
|
||||
meta: JSON
|
||||
}
|
||||
@@ -167,6 +196,18 @@ export async function createAlertGraphql(query, req) {
|
||||
return alert;
|
||||
}
|
||||
|
||||
export async function updateAlertApi(req, res) {
|
||||
const alert = await updateAlert(req.params.alertId, req.body, req.user);
|
||||
|
||||
res.send(alert);
|
||||
}
|
||||
|
||||
export async function updateAlertGraphql(query, req) {
|
||||
const alert = await updateAlert(query.id, query, req.user);
|
||||
|
||||
return alert;
|
||||
}
|
||||
|
||||
export async function removeAlertApi(req, res) {
|
||||
await Promise.all(req.params.alertId.split(',').map(async (alertId) => removeAlert(alertId, req.user)));
|
||||
|
||||
@@ -223,6 +264,7 @@ export const alertsRouter = Router();
|
||||
|
||||
alertsRouter.get('/api/alerts', fetchAlertsApi);
|
||||
alertsRouter.post('/api/alerts', createAlertApi);
|
||||
alertsRouter.patch('/api/alerts/:alertId', updateAlertApi);
|
||||
alertsRouter.delete('/api/alerts/:alertId', removeAlertApi);
|
||||
|
||||
alertsRouter.get('/api/users/:userId/notifications', fetchNotificationsApi);
|
||||
|
||||
@@ -26,6 +26,7 @@ import {
|
||||
fetchAlertsGraphql,
|
||||
fetchNotificationsGraphql,
|
||||
removeAlertGraphql,
|
||||
updateAlertGraphql,
|
||||
updateNotificationGraphql,
|
||||
updateNotificationsGraphql,
|
||||
} from './alerts.js';
|
||||
@@ -158,6 +159,7 @@ export async function graphqlApi(req, res) {
|
||||
unstashMovie: async (query) => unstashMovieGraphql(query, req),
|
||||
// alert mutation
|
||||
createAlert: async (query) => createAlertGraphql(query, req),
|
||||
updateAlert: async (query) => updateAlertGraphql(query, req),
|
||||
removeAlert: async (query) => removeAlertGraphql(query, req),
|
||||
updateNotification: async (query) => updateNotificationGraphql(query, req),
|
||||
updateNotifications: async (query) => updateNotificationsGraphql(query, req),
|
||||
|
||||
Reference in New Issue
Block a user