-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathdispatch.js
171 lines (150 loc) · 5.1 KB
/
dispatch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { get } from "@ember/object";
import { default as Evented, on } from "@ember/object/evented";
import Mixin from "@ember/object/mixin";
import { inject as service } from "@ember/service";
import { iconGroup, iconDownload } from "./icons";
import { logDebug } from "./logger";
import NotificationData from "./data";
import { showNotification } from "./provider";
import {
ATTR_NOTIFY_CLICK_NOOP,
ATTR_NOTIFY_CLICK_FOLLOWED,
ATTR_NOTIFY_CLICK_STREAM,
ATTR_NOTIFY_CLICK_STREAMANDCHAT
} from "data/models/settings/notification/fragment";
import { setMinimized, setVisibility, setFocused } from "nwjs/Window";
export default Mixin.create( Evented, {
chat: service(),
i18n: service(),
/** @type {RouterService} */
router: service(),
settings: service(),
streaming: service(),
dispatchNotifications: on(
"streams-filtered",
/**
* @param {TwitchStream[]} streams
* @returns {Promise}
*/
async function( streams ) {
if ( !streams ) { return; }
const length = get( streams, "length" );
if ( length > 1 && get( this, "settings.notification.grouping" ) ) {
// merge multiple notifications and show a single one
const data = this._getNotificationDataGroup( streams );
await this._showNotification( data );
} else if ( length > 0 ) {
await Promise.all( streams.map( async stream => {
// download channel icon first and save it into a local temp dir...
await iconDownload( stream );
// show notification
const data = this._getNotificationDataSingle( stream );
await this._showNotification( data );
}) );
}
}
),
/**
* Show multiple streams as one notification
* @param {TwitchStream[]} streams
* @returns {NotificationData}
*/
_getNotificationDataGroup( streams ) {
const i18n = get( this, "i18n" );
const settings = get( this, "settings.notification.click_group" );
return new NotificationData({
title: i18n.t( "services.notification.dispatch.group" ).toString(),
message: streams.map( stream => ({
title: get( stream, "channel.display_name" ),
message: get( stream, "channel.status" ) || ""
}) ),
icon: iconGroup,
click: () => this._notificationClick( streams, settings ),
settings
});
},
/**
* Show a notification for each stream
* @param {TwitchStream} stream
* @returns {NotificationData}
*/
_getNotificationDataSingle( stream ) {
const i18n = get( this, "i18n" );
const settings = get( this, "settings.notification.click" );
const name = get( stream, "channel.display_name" );
return new NotificationData({
title: i18n.t( "services.notification.dispatch.single", { name } ).toString(),
message: get( stream, "channel.status" ) || "",
icon: get( stream, "logo" ) || iconGroup,
click: () => this._notificationClick( [ stream ], settings ),
settings
});
},
/**
* Notfication click callback
* @param {TwitchStream[]} streams
* @param {Number} action
* @returns {Promise}
*/
async _notificationClick( streams, action ) {
if ( action === ATTR_NOTIFY_CLICK_NOOP ) {
return;
}
logDebug( "Notification click", () => ({
action,
streams: streams.mapBy( "id" )
}) );
// restore the window
if ( get( this, "settings.notification.click_restore" ) ) {
setMinimized( false );
setVisibility( true );
setFocused( true );
}
if ( action === ATTR_NOTIFY_CLICK_FOLLOWED ) {
this.router.transitionTo( "user.followedStreams" );
} else if ( action === ATTR_NOTIFY_CLICK_STREAM ) {
const streaming = get( this, "streaming" );
await Promise.all( streams.map( async stream => {
// don't await startStream promise and ignore errors
streaming.startStream( stream )
.catch( () => {} );
}) );
} else if ( action === ATTR_NOTIFY_CLICK_STREAMANDCHAT ) {
const streaming = get( this, "streaming" );
const openGlobal = get( this, "settings.streams.chat_open" );
const chat = get( this, "chat" );
await Promise.all( streams.map( async stream => {
const channel = get( stream, "channel" );
const { streams_chat_open: openChannel } = await channel.getChannelSettings();
// don't await startStream promise and ignore errors
streaming.startStream( stream )
.catch( () => {} );
// TODO: refactor startStream and add another parameter to force opening the chat
// chat was already opened by startStream() if
// 1. openGlobal is false and openChannel is true
// 2. openGlobal is true and openChannel is null
// 3. openGlobal is true and openChannel is true
// so open it only if
// 4. openGlobal is false and openChannel is null
// 5. openGlobal is false and openChannel is false
// 6. openGlobal is true and openChannel is false
if ( openGlobal && openChannel === null || openChannel === true ) {
return;
}
// don't await openChat promise and ignore errors
chat.openChat( channel )
.catch( () => {} );
}) );
}
},
/**
* @param {NotificationData} data
* @returns {Promise}
*/
async _showNotification( data ) {
const provider = get( this, "settings.notification.provider" );
// don't await the notification promise here
showNotification( provider, data, false )
.catch( () => {} );
}
});