Some refactoring of browser notifs

This commit is contained in:
Dessalines 2020-08-12 12:00:04 -04:00
parent ac187802b8
commit e04509bd63
4 changed files with 38 additions and 37 deletions

View file

@ -50,7 +50,7 @@ import {
commentsToFlatNodes,
setupTippy,
favIconUrl,
notify,
notifyPost,
} from '../utils';
import { i18n } from '../i18next';
@ -427,7 +427,7 @@ export class Community extends Component<any, State> {
} else if (res.op == UserOperation.CreatePost) {
let data = res.data as PostResponse;
this.state.posts.unshift(data.post);
notify(data.post, this.context.router);
notifyPost(data.post, this.context.router);
this.setState(this.state);
} else if (res.op == UserOperation.CreatePostLike) {
let data = res.data as PostResponse;

View file

@ -55,7 +55,7 @@ import {
commentsToFlatNodes,
setupTippy,
favIconUrl,
notify,
notifyPost,
} from '../utils';
import { i18n } from '../i18next';
import { T } from 'inferno-i18next';
@ -718,7 +718,7 @@ export class Main extends Component<any, MainState> {
.includes(data.post.community_id)
) {
this.state.posts.unshift(data.post);
notify(data.post, this.context.router);
notifyPost(data.post, this.context.router);
}
} else {
// NSFW posts
@ -732,7 +732,7 @@ export class Main extends Component<any, MainState> {
UserService.Instance.user.show_nsfw)
) {
this.state.posts.unshift(data.post);
notify(data.post, this.context.router);
notifyPost(data.post, this.context.router);
}
}
this.setState(this.state);

View file

@ -27,7 +27,8 @@ import {
toast,
setTheme,
getLanguage,
notify,
notifyComment,
notifyPrivateMessage,
} from '../utils';
import { i18n } from '../i18next';
@ -434,7 +435,7 @@ export class Navbar extends Component<any, NavbarState> {
this.state.unreadCount++;
this.setState(this.state);
this.sendUnreadCount();
notify(data.comment, this.context.router);
notifyComment(data.comment, this.context.router);
}
}
} else if (res.op == UserOperation.CreatePrivateMessage) {
@ -446,7 +447,7 @@ export class Navbar extends Component<any, NavbarState> {
this.state.unreadCount++;
this.setState(this.state);
this.sendUnreadCount();
notify(data.message, this.context.router);
notifyPrivateMessage(data.message, this.context.router);
}
}
} else if (res.op == UserOperation.GetSite) {

58
ui/src/utils.ts vendored
View file

@ -60,6 +60,7 @@ import moment from 'moment';
export const favIconUrl = '/static/assets/favicon.svg';
export const favIconPngUrl = '/static/assets/apple-touch-icon.png';
export const defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`;
export const repoUrl = 'https://github.com/LemmyNet/lemmy';
export const helpGuideUrl = '/docs/about_guide.html';
export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
@ -604,38 +605,37 @@ export function messageToastify(info: NotifyInfo, router: any) {
}).showToast();
}
export function notify(data: Comment | PrivateMessage | Post, router: any) {
let defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`;
export function notifyPost(post: Post, router: any) {
let info: NotifyInfo = {
name: post.community_name,
icon: post.community_icon ? post.community_icon : defaultFavIcon,
link: `/post/${post.id}`,
body: post.name,
};
notify(info, router);
}
let info: NotifyInfo;
export function notifyComment(comment: Comment, router: any) {
let info: NotifyInfo = {
name: comment.creator_name,
icon: comment.creator_avatar ? comment.creator_avatar : defaultFavIcon,
link: `/post/${comment.post_id}/comment/${comment.id}`,
body: comment.content,
};
notify(info, router);
}
// If its a post, do community info / avatars
if (isPostType(data)) {
let post = data;
info = {
name: post.community_name,
icon: post.community_icon ? post.community_icon : defaultFavIcon,
link: `/post/${post.id}`,
body: post.name,
};
} else if (isCommentType(data)) {
let comment = data;
info = {
name: comment.creator_name,
icon: comment.creator_avatar ? comment.creator_avatar : defaultFavIcon,
link: `/post/${comment.post_id}/comment/${comment.id}`,
body: comment.content,
};
} else {
let pm = data;
info = {
name: pm.creator_name,
icon: pm.creator_avatar ? pm.creator_avatar : defaultFavIcon,
link: `/inbox`,
body: pm.content,
};
}
export function notifyPrivateMessage(pm: PrivateMessage, router: any) {
let info: NotifyInfo = {
name: pm.creator_name,
icon: pm.creator_avatar ? pm.creator_avatar : defaultFavIcon,
link: `/inbox`,
body: pm.content,
};
notify(info, router);
}
function notify(info: NotifyInfo, router: any) {
messageToastify(info, router);
if (Notification.permission !== 'granted') Notification.requestPermission();