feat(notif): issue notifications (#2242)
* feat(notif): issue notifications * refactor: dedupe test notification strings * fix: webhook key parsing * fix(notif): skip send for admin who requested on behalf of another user * fix(notif): send comment notifs to admins when other admins reply * fix(notif): also send resolved notifs to admins, and reopened notifs to issue creator * fix: don't send duplicate notifications * fix(lang): tweak notification description strings * fix(notif): tweak Slack notification styling * fix(notif): tweak Pushbullet & Telegram notification styling * docs: reformat webhooks page * fix(notif): add missing issue_type & issue_status variables to LunaSea notif payloads * fix: explicitly attach media & issue objects where applicable * fix(notif): correctly notify both notifyUser and managers where applicable * fix: update default webhook payload for new installs * fix(notif): add missing comment_message to LunaSea notif payload * refactor(sw): simplify notificationclick event listener logic * fix(notif): add missing event description for MEDIA_AVAILABLE notifications
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { sortBy } from 'lodash';
|
||||
import {
|
||||
EntitySubscriberInterface,
|
||||
EventSubscriber,
|
||||
@@ -5,9 +6,12 @@ import {
|
||||
InsertEvent,
|
||||
} from 'typeorm';
|
||||
import TheMovieDb from '../api/themoviedb';
|
||||
import { IssueType, IssueTypeName } from '../constants/issue';
|
||||
import { MediaType } from '../constants/media';
|
||||
import IssueComment from '../entity/IssueComment';
|
||||
import Media from '../entity/Media';
|
||||
import notificationManager, { Notification } from '../lib/notifications';
|
||||
import { Permission } from '../lib/permissions';
|
||||
|
||||
@EventSubscriber()
|
||||
export class IssueCommentSubscriber
|
||||
@@ -18,41 +22,67 @@ export class IssueCommentSubscriber
|
||||
}
|
||||
|
||||
private async sendIssueCommentNotification(entity: IssueComment) {
|
||||
const issueCommentRepository = getRepository(IssueComment);
|
||||
let title: string;
|
||||
let image: string;
|
||||
const tmdb = new TheMovieDb();
|
||||
const issuecomment = await issueCommentRepository.findOne({
|
||||
where: { id: entity.id },
|
||||
relations: ['issue'],
|
||||
});
|
||||
|
||||
const issue = issuecomment?.issue;
|
||||
|
||||
const issue = (
|
||||
await getRepository(IssueComment).findOne({
|
||||
where: { id: entity.id },
|
||||
relations: ['issue'],
|
||||
})
|
||||
)?.issue;
|
||||
if (!issue) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (issue.media.mediaType === MediaType.MOVIE) {
|
||||
const movie = await tmdb.getMovie({ movieId: issue.media.tmdbId });
|
||||
const media = await getRepository(Media).findOne({
|
||||
where: { id: issue.media.id },
|
||||
});
|
||||
if (!media) {
|
||||
return;
|
||||
}
|
||||
|
||||
title = movie.title;
|
||||
if (media.mediaType === MediaType.MOVIE) {
|
||||
const movie = await tmdb.getMovie({ movieId: media.tmdbId });
|
||||
|
||||
title = `${movie.title}${
|
||||
movie.release_date ? ` (${movie.release_date.slice(0, 4)})` : ''
|
||||
}`;
|
||||
image = `https://image.tmdb.org/t/p/w600_and_h900_bestv2${movie.poster_path}`;
|
||||
} else {
|
||||
const tvshow = await tmdb.getTvShow({ tvId: issue.media.tmdbId });
|
||||
const tvshow = await tmdb.getTvShow({ tvId: media.tmdbId });
|
||||
|
||||
title = tvshow.name;
|
||||
title = `${tvshow.name}${
|
||||
tvshow.first_air_date ? ` (${tvshow.first_air_date.slice(0, 4)})` : ''
|
||||
}`;
|
||||
image = `https://image.tmdb.org/t/p/w600_and_h900_bestv2${tvshow.poster_path}`;
|
||||
}
|
||||
|
||||
notificationManager.sendNotification(Notification.ISSUE_COMMENT, {
|
||||
subject: `New Issue Comment: ${title}`,
|
||||
message: entity.message,
|
||||
issue,
|
||||
image,
|
||||
notifyUser:
|
||||
issue.createdBy.id !== entity.user.id ? issue.createdBy : undefined,
|
||||
});
|
||||
const [firstComment] = sortBy(issue.comments, 'id');
|
||||
|
||||
if (entity.id !== firstComment.id) {
|
||||
// Send notifications to all issue managers
|
||||
notificationManager.sendNotification(Notification.ISSUE_COMMENT, {
|
||||
event: `New Comment on ${
|
||||
issue.issueType !== IssueType.OTHER
|
||||
? `${IssueTypeName[issue.issueType]} `
|
||||
: ''
|
||||
}Issue`,
|
||||
subject: title,
|
||||
message: firstComment.message,
|
||||
comment: entity,
|
||||
issue,
|
||||
media,
|
||||
image,
|
||||
notifyAdmin: true,
|
||||
notifyUser:
|
||||
!issue.createdBy.hasPermission(Permission.MANAGE_ISSUES) &&
|
||||
issue.createdBy.id !== entity.user.id
|
||||
? issue.createdBy
|
||||
: undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public afterInsert(event: InsertEvent<IssueComment>): void {
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
import { sortBy } from 'lodash';
|
||||
import {
|
||||
EntitySubscriberInterface,
|
||||
EventSubscriber,
|
||||
InsertEvent,
|
||||
UpdateEvent,
|
||||
} from 'typeorm';
|
||||
import TheMovieDb from '../api/themoviedb';
|
||||
import { IssueStatus, IssueType, IssueTypeName } from '../constants/issue';
|
||||
import { MediaType } from '../constants/media';
|
||||
import Issue from '../entity/Issue';
|
||||
import notificationManager, { Notification } from '../lib/notifications';
|
||||
import { Permission } from '../lib/permissions';
|
||||
|
||||
@EventSubscriber()
|
||||
export class IssueSubscriber implements EntitySubscriberInterface<Issue> {
|
||||
@@ -14,29 +18,75 @@ export class IssueSubscriber implements EntitySubscriberInterface<Issue> {
|
||||
return Issue;
|
||||
}
|
||||
|
||||
private async sendIssueCreatedNotification(entity: Issue) {
|
||||
private async sendIssueNotification(entity: Issue, type: Notification) {
|
||||
let title: string;
|
||||
let image: string;
|
||||
const tmdb = new TheMovieDb();
|
||||
if (entity.media.mediaType === MediaType.MOVIE) {
|
||||
const movie = await tmdb.getMovie({ movieId: entity.media.tmdbId });
|
||||
|
||||
title = movie.title;
|
||||
title = `${movie.title}${
|
||||
movie.release_date ? ` (${movie.release_date.slice(0, 4)})` : ''
|
||||
}`;
|
||||
image = `https://image.tmdb.org/t/p/w600_and_h900_bestv2${movie.poster_path}`;
|
||||
} else {
|
||||
const tvshow = await tmdb.getTvShow({ tvId: entity.media.tmdbId });
|
||||
|
||||
title = tvshow.name;
|
||||
title = `${tvshow.name}${
|
||||
tvshow.first_air_date ? ` (${tvshow.first_air_date.slice(0, 4)})` : ''
|
||||
}`;
|
||||
image = `https://image.tmdb.org/t/p/w600_and_h900_bestv2${tvshow.poster_path}`;
|
||||
}
|
||||
|
||||
const [firstComment] = entity.comments;
|
||||
const [firstComment] = sortBy(entity.comments, 'id');
|
||||
const extra: { name: string; value: string }[] = [];
|
||||
|
||||
notificationManager.sendNotification(Notification.ISSUE_CREATED, {
|
||||
if (entity.media.mediaType === MediaType.TV && entity.problemSeason > 0) {
|
||||
extra.push({
|
||||
name: 'Affected Season',
|
||||
value: entity.problemSeason.toString(),
|
||||
});
|
||||
|
||||
if (entity.problemEpisode > 0) {
|
||||
extra.push({
|
||||
name: 'Affected Episode',
|
||||
value: entity.problemEpisode.toString(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
notificationManager.sendNotification(type, {
|
||||
event:
|
||||
type === Notification.ISSUE_CREATED
|
||||
? `New ${
|
||||
entity.issueType !== IssueType.OTHER
|
||||
? `${IssueTypeName[entity.issueType]} `
|
||||
: ''
|
||||
}Issue Reported`
|
||||
: type === Notification.ISSUE_RESOLVED
|
||||
? `${
|
||||
entity.issueType !== IssueType.OTHER
|
||||
? `${IssueTypeName[entity.issueType]} `
|
||||
: ''
|
||||
}Issue Resolved`
|
||||
: `${
|
||||
entity.issueType !== IssueType.OTHER
|
||||
? `${IssueTypeName[entity.issueType]} `
|
||||
: ''
|
||||
}Issue Reopened`,
|
||||
subject: title,
|
||||
message: firstComment.message,
|
||||
issue: entity,
|
||||
media: entity.media,
|
||||
image,
|
||||
extra,
|
||||
notifyAdmin: true,
|
||||
notifyUser:
|
||||
!entity.createdBy.hasPermission(Permission.MANAGE_ISSUES) &&
|
||||
(type === Notification.ISSUE_RESOLVED ||
|
||||
type === Notification.ISSUE_REOPENED)
|
||||
? entity.createdBy
|
||||
: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -45,6 +95,30 @@ export class IssueSubscriber implements EntitySubscriberInterface<Issue> {
|
||||
return;
|
||||
}
|
||||
|
||||
this.sendIssueCreatedNotification(event.entity);
|
||||
this.sendIssueNotification(event.entity, Notification.ISSUE_CREATED);
|
||||
}
|
||||
|
||||
public beforeUpdate(event: UpdateEvent<Issue>): void {
|
||||
if (!event.entity) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
event.entity.status === IssueStatus.RESOLVED &&
|
||||
event.databaseEntity.status !== IssueStatus.RESOLVED
|
||||
) {
|
||||
this.sendIssueNotification(
|
||||
event.entity as Issue,
|
||||
Notification.ISSUE_RESOLVED
|
||||
);
|
||||
} else if (
|
||||
event.entity.status === IssueStatus.OPEN &&
|
||||
event.databaseEntity.status !== IssueStatus.OPEN
|
||||
) {
|
||||
this.sendIssueNotification(
|
||||
event.entity as Issue,
|
||||
Notification.ISSUE_REOPENED
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,8 @@ export class MediaSubscriber implements EntitySubscriberInterface<Media> {
|
||||
|
||||
relatedRequests.forEach((request) => {
|
||||
notificationManager.sendNotification(Notification.MEDIA_AVAILABLE, {
|
||||
event: 'Movie Now Available',
|
||||
notifyAdmin: false,
|
||||
notifyUser: request.requestedBy,
|
||||
subject: `${movie.title}${
|
||||
movie.release_date ? ` (${movie.release_date.slice(0, 4)})` : ''
|
||||
@@ -42,7 +44,7 @@ export class MediaSubscriber implements EntitySubscriberInterface<Media> {
|
||||
}),
|
||||
media: entity,
|
||||
image: `https://image.tmdb.org/t/p/w600_and_h900_bestv2${movie.poster_path}`,
|
||||
request: request,
|
||||
request,
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -91,6 +93,7 @@ export class MediaSubscriber implements EntitySubscriberInterface<Media> {
|
||||
);
|
||||
const tv = await tmdb.getTvShow({ tvId: entity.tmdbId });
|
||||
notificationManager.sendNotification(Notification.MEDIA_AVAILABLE, {
|
||||
event: 'Series Now Available',
|
||||
subject: `${tv.name}${
|
||||
tv.first_air_date ? ` (${tv.first_air_date.slice(0, 4)})` : ''
|
||||
}`,
|
||||
@@ -99,18 +102,19 @@ export class MediaSubscriber implements EntitySubscriberInterface<Media> {
|
||||
separator: /\s/,
|
||||
omission: '…',
|
||||
}),
|
||||
notifyAdmin: false,
|
||||
notifyUser: request.requestedBy,
|
||||
image: `https://image.tmdb.org/t/p/w600_and_h900_bestv2${tv.poster_path}`,
|
||||
media: entity,
|
||||
extra: [
|
||||
{
|
||||
name: 'Seasons',
|
||||
name: 'Requested Seasons',
|
||||
value: request.seasons
|
||||
.map((season) => season.seasonNumber)
|
||||
.join(', '),
|
||||
},
|
||||
],
|
||||
request: request,
|
||||
request,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user