summaryrefslogtreecommitdiff
path: root/src/web/app/desktop/script.js
blob: f9f0896dddf98d54a312575099ed1249cd30aafa (plain)
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
/**
 * Desktop Client
 */

// Style
import './style.styl';

require('./tags');
require('./mixins');
import * as riot from 'riot';
import init from '../init';
import route from './router';
import fuckAdBlock from './scripts/fuck-ad-block';
import getPostSummary from '../common/scripts/get-post-summary';

/**
 * init
 */
init(async (me, stream) => {
	/**
	 * Fuck AD Block
	 */
	fuckAdBlock();

	/**
	 * Init Notification
	 */
	if ('Notification' in window) {
		// 許可を得ていなかったらリクエスト
		if (Notification.permission == 'default') {
			await Notification.requestPermission();
		}

		if (Notification.permission == 'granted') {
			registerNotifications(stream);
		}
	}

	// Start routing
	route(me);
});

function registerNotifications(stream) {
	if (stream == null) return;

	stream.on('drive_file_created', file => {
		const n = new Notification('ファイルがアップロードされました', {
			body: file.name,
			icon: file.url + '?thumbnail&size=64'
		});
		setTimeout(n.close.bind(n), 5000);
	});

	stream.on('mention', post => {
		const n = new Notification(`${post.user.name}さんから:`, {
			body: getPostSummary(post),
			icon: post.user.avatar_url + '?thumbnail&size=64'
		});
		setTimeout(n.close.bind(n), 6000);
	});

	stream.on('reply', post => {
		const n = new Notification(`${post.user.name}さんから返信:`, {
			body: getPostSummary(post),
			icon: post.user.avatar_url + '?thumbnail&size=64'
		});
		setTimeout(n.close.bind(n), 6000);
	});

	stream.on('quote', post => {
		const n = new Notification(`${post.user.name}さんが引用:`, {
			body: getPostSummary(post),
			icon: post.user.avatar_url + '?thumbnail&size=64'
		});
		setTimeout(n.close.bind(n), 6000);
	});
}