2020-12-30 18:35:18 +00:00
|
|
|
import { AppContext, KoaNext, NotificationView } from '../utils/types';
|
2020-12-30 23:50:44 +00:00
|
|
|
import { isApiRequest } from '../utils/requestUtils';
|
|
|
|
import { defaultAdminEmail, defaultAdminPassword, NotificationLevel } from '../db';
|
2020-12-30 18:35:18 +00:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
import Logger from '@joplin/lib/Logger';
|
|
|
|
import * as MarkdownIt from 'markdown-it';
|
|
|
|
|
|
|
|
const logger = Logger.create('notificationHandler');
|
|
|
|
|
|
|
|
export default async function(ctx: AppContext, next: KoaNext): Promise<void> {
|
|
|
|
ctx.notifications = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (isApiRequest(ctx)) return next();
|
|
|
|
|
2020-12-30 23:50:44 +00:00
|
|
|
const user = ctx.owner;
|
2020-12-30 18:35:18 +00:00
|
|
|
if (!user) return next();
|
|
|
|
|
|
|
|
const notificationModel = ctx.models.notification({ userId: user.id });
|
|
|
|
|
|
|
|
if (user.is_admin) {
|
|
|
|
const defaultAdmin = await ctx.models.user().login(defaultAdminEmail, defaultAdminPassword);
|
|
|
|
|
|
|
|
if (defaultAdmin) {
|
|
|
|
await notificationModel.add(
|
|
|
|
'change_admin_password',
|
|
|
|
NotificationLevel.Important,
|
|
|
|
_('The default admin password is insecure and has not been changed! [Change it now](%s)', await ctx.models.user().profileUrl())
|
|
|
|
);
|
2021-01-12 16:02:44 +00:00
|
|
|
} else {
|
|
|
|
await notificationModel.markAsRead('change_admin_password');
|
2020-12-30 18:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const markdownIt = new MarkdownIt();
|
|
|
|
const notifications = await notificationModel.allUnreadByUserId(user.id);
|
|
|
|
const views: NotificationView[] = [];
|
|
|
|
for (const n of notifications) {
|
|
|
|
views.push({
|
|
|
|
id: n.id,
|
|
|
|
messageHtml: markdownIt.render(n.message),
|
|
|
|
level: n.level === NotificationLevel.Important ? 'warning' : 'info',
|
|
|
|
closeUrl: notificationModel.closeUrl(n.id),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.notifications = views;
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|