-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
main.js
167 lines (148 loc) · 4.97 KB
/
main.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
/* eslint-disable import/first */
if (
(window.location.protocol === 'https:' || window.location.hostname === 'localhost')
&& navigator.serviceWorker
) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/fiora-sw.js');
});
}
import 'babel-polyfill';
import React from 'react';
import ReactDom from 'react-dom';
import { Provider } from 'react-redux';
import platform from 'platform';
import fetch from 'utils/fetch';
import App from './App';
import store from './state/store';
import action from './state/action';
import socket from './socket';
import notification from '../utils/notification';
import sound from '../utils/sound';
import getFriendId from '../utils/getFriendId';
import convertMessage from '../utils/convertMessage';
import voice from '../utils/voice';
if (window.Notification && (window.Notification.permission === 'default' || window.Notification.permission === 'denied')) {
window.Notification.requestPermission();
}
let windowStatus = 'focus';
window.onfocus = () => windowStatus = 'focus';
window.onblur = () => windowStatus = 'blur';
async function guest() {
const [err, res] = await fetch('guest', {
os: platform.os.family,
browser: platform.name,
environment: platform.description,
});
if (!err) {
action.setGuest(res);
}
}
socket.on('connect', async () => {
const token = window.localStorage.getItem('token');
if (token) {
const [err, res] = await fetch('loginByToken', {
token,
os: platform.os.family,
browser: platform.name,
environment: platform.description,
}, { toast: false });
if (err) {
guest();
} else {
action.setUser(res);
}
} else {
guest();
}
});
socket.on('disconnect', () => {
action.disconnect();
});
let prevFrom = '';
let prevName = '';
socket.on('message', (message) => {
convertMessage(message);
const state = store.getState();
const isSelfMessage = message.from._id === state.getIn(['user', '_id']);
const linkman = state.getIn(['user', 'linkmans']).find(l => l.get('_id') === message.to);
let title = '';
if (linkman) {
action.addLinkmanMessage(message.to, message);
if (linkman.get('type') === 'group') {
title = `${message.from.username} 在 ${linkman.get('name')} 对大家说:`;
} else {
title = `${message.from.username} 对你说:`;
}
} else {
// 联系人不存在并且是自己发的消息, 不创建新联系人
if (isSelfMessage) {
return;
}
const newLinkman = {
_id: getFriendId(
state.getIn(['user', '_id']),
message.from._id,
),
type: 'temporary',
createTime: Date.now(),
avatar: message.from.avatar,
name: message.from.username,
messages: [],
unread: 1,
};
action.addLinkman(newLinkman);
title = `${message.from.username} 对你说:`;
fetch('getLinkmanHistoryMessages', { linkmanId: newLinkman._id }).then(([err, res]) => {
if (!err) {
action.addLinkmanMessages(newLinkman._id, res);
}
});
}
if (windowStatus === 'blur' && state.getIn(['ui', 'notificationSwitch'])) {
notification(
title,
message.from.avatar,
message.type === 'text' ? message.content.replace(/</g, '<').replace(/>/g, '>') : `[${message.type}]`,
Math.random(),
);
}
if (state.getIn(['ui', 'soundSwitch'])) {
const soundType = state.getIn(['ui', 'sound']);
sound(soundType);
}
if (state.getIn(['ui', 'voiceSwitch'])) {
if (message.type === 'text') {
const text = message.content
.replace(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g, '')
.replace(/#/g, '');
if (text.length > 100) {
return;
}
const from = linkman && linkman.get('type') === 'group' ?
`${message.from.username}${linkman.get('name') === prevName ? '' : `在${linkman.get('name')}`}说`
:
`${message.from.username}对你说`;
if (text) {
voice.push(from !== prevFrom ? from + text : text, message.from.username);
}
prevFrom = from;
prevName = linkman.get('name');
} else if (message.type === 'system') {
voice.push(message.from.originUsername + message.content);
prevFrom = null;
}
}
});
socket.on('changeGroupName', ({ groupId, name }) => {
action.setGroupName(groupId, name);
});
socket.on('deleteGroup', ({ groupId }) => {
action.removeLinkman(groupId);
});
ReactDom.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('app'),
);