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

View file

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

View file

@ -27,7 +27,8 @@ import {
toast, toast,
setTheme, setTheme,
getLanguage, getLanguage,
notify, notifyComment,
notifyPrivateMessage,
} from '../utils'; } from '../utils';
import { i18n } from '../i18next'; import { i18n } from '../i18next';
@ -434,7 +435,7 @@ export class Navbar extends Component<any, NavbarState> {
this.state.unreadCount++; this.state.unreadCount++;
this.setState(this.state); this.setState(this.state);
this.sendUnreadCount(); this.sendUnreadCount();
notify(data.comment, this.context.router); notifyComment(data.comment, this.context.router);
} }
} }
} else if (res.op == UserOperation.CreatePrivateMessage) { } else if (res.op == UserOperation.CreatePrivateMessage) {
@ -446,7 +447,7 @@ export class Navbar extends Component<any, NavbarState> {
this.state.unreadCount++; this.state.unreadCount++;
this.setState(this.state); this.setState(this.state);
this.sendUnreadCount(); this.sendUnreadCount();
notify(data.message, this.context.router); notifyPrivateMessage(data.message, this.context.router);
} }
} }
} else if (res.op == UserOperation.GetSite) { } 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 favIconUrl = '/static/assets/favicon.svg';
export const favIconPngUrl = '/static/assets/apple-touch-icon.png'; 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 repoUrl = 'https://github.com/LemmyNet/lemmy';
export const helpGuideUrl = '/docs/about_guide.html'; export const helpGuideUrl = '/docs/about_guide.html';
export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`; export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
@ -604,38 +605,37 @@ export function messageToastify(info: NotifyInfo, router: any) {
}).showToast(); }).showToast();
} }
export function notify(data: Comment | PrivateMessage | Post, router: any) { export function notifyPost(post: Post, router: any) {
let defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`; 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 export function notifyPrivateMessage(pm: PrivateMessage, router: any) {
if (isPostType(data)) { let info: NotifyInfo = {
let post = data; name: pm.creator_name,
info = { icon: pm.creator_avatar ? pm.creator_avatar : defaultFavIcon,
name: post.community_name, link: `/inbox`,
icon: post.community_icon ? post.community_icon : defaultFavIcon, body: pm.content,
link: `/post/${post.id}`, };
body: post.name, notify(info, router);
}; }
} 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,
};
}
function notify(info: NotifyInfo, router: any) {
messageToastify(info, router); messageToastify(info, router);
if (Notification.permission !== 'granted') Notification.requestPermission(); if (Notification.permission !== 'granted') Notification.requestPermission();