-
Notifications
You must be signed in to change notification settings - Fork 932
feat: browser push notifications for assignment, reopen and mentions #3755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { views } from "@/composables/useView"; | ||
| import { useAuthStore } from "@/stores/auth"; | ||
| import { useConfigStore } from "@/stores/config"; | ||
| import { globalStore } from "@/stores/globalStore"; | ||
| import { __ } from "@/translation"; | ||
| import { isCustomerPortal } from "@/utils"; | ||
| import { ref } from "vue"; | ||
| import { useRouter } from "vue-router"; | ||
|
|
||
| type PushPayload = { | ||
| notification_type: "Assignment" | "Mention" | "Reaction"; | ||
| user_from: string; | ||
| reference_ticket?: string; | ||
| }; | ||
|
|
||
| const isSupported = typeof window !== "undefined" && "Notification" in window; | ||
| const permission = ref<NotificationPermission>( | ||
| isSupported ? Notification.permission : "denied" | ||
| ); | ||
|
|
||
| // Module-level guard so the socket handler is registered only once. | ||
| let listening = false; | ||
|
|
||
| export function usePushNotifications() { | ||
| const router = useRouter(); | ||
| const auth = useAuthStore(); | ||
| const { $socket } = globalStore(); | ||
| const configStore = useConfigStore(); | ||
|
|
||
| function enable() { | ||
| if (!isSupported) return; | ||
| Notification.requestPermission().then((result) => { | ||
| permission.value = result; | ||
| }); | ||
| } | ||
|
|
||
| function show(payload: PushPayload) { | ||
| if (permission.value !== "granted" || isCustomerPortal.value) return; | ||
| // Assignment/reopen land in the agent's "my open tickets" list, so skip the | ||
| // popup while they're already looking at such a view. Mentions always fire. | ||
| if (payload.notification_type !== "Mention" && onMyOpenTicketsView()) return; | ||
|
|
||
| const notification = new Notification(getTitle(payload), { | ||
| body: payload.reference_ticket | ||
| ? `${__("Ticket")} #${payload.reference_ticket}` | ||
| : "", | ||
| icon: configStore.favicon, | ||
| // tag collapses repeats for the same ticket; omit it when absent | ||
| // (exactOptionalPropertyTypes disallows an explicit undefined). | ||
| ...(payload.reference_ticket ? { tag: payload.reference_ticket } : {}), | ||
| }); | ||
|
|
||
| notification.onclick = () => { | ||
| window.focus(); | ||
| notification.close(); | ||
| if (payload.reference_ticket) { | ||
| router.push({ | ||
| name: "TicketAgent", | ||
| params: { ticketId: payload.reference_ticket }, | ||
| }); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| // True when the current route is a HD Ticket list whose active view already | ||
| // surfaces the agent's freshly assigned/reopened tickets (assignee = me + open). | ||
| function onMyOpenTicketsView(): boolean { | ||
| const route = router.currentRoute.value; | ||
| if (route.name !== "TicketsAgent") return false; | ||
|
|
||
| const viewName = route.query.view as string | undefined; | ||
| const view = viewName | ||
| ? views.data?.find((v: any) => v.name === viewName) | ||
| : views.data?.find( | ||
| (v: any) => | ||
| v.is_default && v.user === auth.userId && v.dt === "HD Ticket" | ||
| ); | ||
|
|
||
| const filters = view?.filters; | ||
| return !!filters && assignedToMe(filters) && showsOpen(filters); | ||
| } | ||
|
|
||
| function assignedToMe(filters: Record<string, any>): boolean { | ||
| if (!filters._assign) return false; | ||
| const value = JSON.stringify(filters._assign); | ||
| return value.includes("@me") || value.includes(auth.userId); | ||
| } | ||
|
|
||
| function showsOpen(filters: Record<string, any>): boolean { | ||
| const status = filters.status_category ?? filters.status; | ||
| if (status == null) return true; // no status filter -> open tickets show too | ||
| return JSON.stringify(status).includes("Open"); | ||
| } | ||
|
|
||
| if (isSupported && !listening) { | ||
| listening = true; | ||
| $socket.on("helpdesk:new-notification", show); | ||
| } | ||
|
|
||
| return { isSupported, permission, enable }; | ||
| } | ||
|
|
||
| function getTitle(payload: PushPayload): string { | ||
| switch (payload.notification_type) { | ||
| case "Mention": | ||
| return `${payload.user_from} ${__("mentioned you in a ticket")}`; | ||
| case "Assignment": | ||
| return `${payload.user_from} ${__("assigned you a ticket")}`; | ||
| case "Reaction": | ||
| return `${payload.user_from} ${__("reopened a ticket")}`; | ||
| default: | ||
| return __("New notification"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 14 additions & 2 deletions
16
helpdesk/helpdesk/doctype/hd_notification/test_hd_notification.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,21 @@ | ||
| # Copyright (c) 2022, Frappe Technologies and Contributors | ||
| # See license.txt | ||
|
|
||
| # import frappe | ||
| import frappe | ||
| from frappe.tests.utils import FrappeTestCase | ||
|
|
||
|
|
||
| class TestHDNotification(FrappeTestCase): | ||
| pass | ||
| def test_should_push(self): | ||
| # Push assignment, mention and ticket reopen; skip comment emoji-reactions. | ||
| cases = [ | ||
| ("Assignment", None, True), | ||
| ("Mention", "comment-1", True), | ||
| ("Reaction", None, True), # ticket reopen | ||
| ("Reaction", "comment-1", False), # emoji reaction on a comment | ||
| ] | ||
| for notification_type, reference_comment, expected in cases: | ||
| doc = frappe.new_doc("HD Notification") | ||
| doc.notification_type = notification_type | ||
| doc.reference_comment = reference_comment | ||
| self.assertEqual(doc.should_push(), expected, msg=notification_type) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.