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) {

32
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 = {
let info: NotifyInfo;
// If its a post, do community info / avatars
if (isPostType(data)) {
let post = data;
info = {
name: post.community_name, name: post.community_name,
icon: post.community_icon ? post.community_icon : defaultFavIcon, icon: post.community_icon ? post.community_icon : defaultFavIcon,
link: `/post/${post.id}`, link: `/post/${post.id}`,
body: post.name, body: post.name,
}; };
} else if (isCommentType(data)) { notify(info, router);
let comment = data; }
info = {
export function notifyComment(comment: Comment, router: any) {
let info: NotifyInfo = {
name: comment.creator_name, name: comment.creator_name,
icon: comment.creator_avatar ? comment.creator_avatar : defaultFavIcon, icon: comment.creator_avatar ? comment.creator_avatar : defaultFavIcon,
link: `/post/${comment.post_id}/comment/${comment.id}`, link: `/post/${comment.post_id}/comment/${comment.id}`,
body: comment.content, body: comment.content,
}; };
} else { notify(info, router);
let pm = data; }
info = {
export function notifyPrivateMessage(pm: PrivateMessage, router: any) {
let info: NotifyInfo = {
name: pm.creator_name, name: pm.creator_name,
icon: pm.creator_avatar ? pm.creator_avatar : defaultFavIcon, icon: pm.creator_avatar ? pm.creator_avatar : defaultFavIcon,
link: `/inbox`, link: `/inbox`,
body: pm.content, body: pm.content,
}; };
} notify(info, router);
}
function notify(info: NotifyInfo, router: any) {
messageToastify(info, router); messageToastify(info, router);
if (Notification.permission !== 'granted') Notification.requestPermission(); if (Notification.permission !== 'granted') Notification.requestPermission();