summaryrefslogtreecommitdiff
path: root/src/web/app/common/scripts
diff options
context:
space:
mode:
authorha-dai <contact@haradai.net>2017-11-27 03:41:47 +0900
committerha-dai <contact@haradai.net>2017-11-27 03:41:47 +0900
commit6c75bc6d5188cbbf80fe1086fa0e8828f4edb873 (patch)
tree3ffedcc3a06e53269e92d2990cf0b3bb247ac04a /src/web/app/common/scripts
parentMerge branch 'master' of https://github.com/syuilo/misskey (diff)
parentUpdate dependencies :rocket: (diff)
downloadmisskey-6c75bc6d5188cbbf80fe1086fa0e8828f4edb873.tar.gz
misskey-6c75bc6d5188cbbf80fe1086fa0e8828f4edb873.tar.bz2
misskey-6c75bc6d5188cbbf80fe1086fa0e8828f4edb873.zip
Merge branch 'master' of github.com:syuilo/misskey
Diffstat (limited to 'src/web/app/common/scripts')
-rw-r--r--src/web/app/common/scripts/api.ts (renamed from src/web/app/common/scripts/api.js)8
-rw-r--r--src/web/app/common/scripts/bytes-to-size.ts (renamed from src/web/app/common/scripts/bytes-to-size.js)4
-rw-r--r--src/web/app/common/scripts/check-for-update.js14
-rw-r--r--src/web/app/common/scripts/check-for-update.ts12
-rw-r--r--src/web/app/common/scripts/compose-notification.ts60
-rw-r--r--src/web/app/common/scripts/config.js25
-rw-r--r--src/web/app/common/scripts/contains.ts (renamed from src/web/app/common/scripts/contains.js)0
-rw-r--r--src/web/app/common/scripts/copy-to-clipboard.ts (renamed from src/web/app/common/scripts/copy-to-clipboard.js)0
-rw-r--r--src/web/app/common/scripts/date-stringify.ts (renamed from src/web/app/common/scripts/date-stringify.js)0
-rw-r--r--src/web/app/common/scripts/gcd.ts (renamed from src/web/app/common/scripts/gcd.js)0
-rw-r--r--src/web/app/common/scripts/generate-default-userdata.js45
-rw-r--r--src/web/app/common/scripts/get-kao.ts (renamed from src/web/app/common/scripts/get-kao.js)2
-rw-r--r--src/web/app/common/scripts/get-median.ts11
-rw-r--r--src/web/app/common/scripts/is-promise.ts (renamed from src/web/app/common/scripts/is-promise.js)0
-rw-r--r--src/web/app/common/scripts/loading.ts (renamed from src/web/app/common/scripts/loading.js)0
-rw-r--r--src/web/app/common/scripts/signout.js7
-rw-r--r--src/web/app/common/scripts/signout.ts7
-rw-r--r--src/web/app/common/scripts/streaming/channel-stream.ts (renamed from src/web/app/common/scripts/channel-stream.js)6
-rw-r--r--src/web/app/common/scripts/streaming/drive-stream-manager.ts20
-rw-r--r--src/web/app/common/scripts/streaming/drive-stream.ts12
-rw-r--r--src/web/app/common/scripts/streaming/home-stream-manager.ts20
-rw-r--r--src/web/app/common/scripts/streaming/home-stream.ts (renamed from src/web/app/common/scripts/home-stream.js)11
-rw-r--r--src/web/app/common/scripts/streaming/messaging-index-stream-manager.ts20
-rw-r--r--src/web/app/common/scripts/streaming/messaging-index-stream.ts12
-rw-r--r--src/web/app/common/scripts/streaming/messaging-stream.ts (renamed from src/web/app/common/scripts/messaging-stream.js)8
-rw-r--r--src/web/app/common/scripts/streaming/requests-stream-manager.ts12
-rw-r--r--src/web/app/common/scripts/streaming/requests-stream.ts10
-rw-r--r--src/web/app/common/scripts/streaming/server-stream-manager.ts12
-rw-r--r--src/web/app/common/scripts/streaming/server-stream.ts (renamed from src/web/app/common/scripts/server-stream.js)6
-rw-r--r--src/web/app/common/scripts/streaming/stream-manager.ts89
-rw-r--r--src/web/app/common/scripts/streaming/stream.ts (renamed from src/web/app/common/scripts/stream.js)54
-rw-r--r--src/web/app/common/scripts/text-compiler.ts (renamed from src/web/app/common/scripts/text-compiler.js)9
-rw-r--r--src/web/app/common/scripts/uuid.js18
33 files changed, 343 insertions, 171 deletions
diff --git a/src/web/app/common/scripts/api.js b/src/web/app/common/scripts/api.ts
index 4855f736c7..e62447b0a0 100644
--- a/src/web/app/common/scripts/api.js
+++ b/src/web/app/common/scripts/api.ts
@@ -2,7 +2,7 @@
* API Request
*/
-import CONFIG from './config';
+declare const _API_URL_: string;
let spinner = null;
let pending = 0;
@@ -14,7 +14,7 @@ let pending = 0;
* @param {any} [data={}] Data
* @return {Promise<any>} Response
*/
-export default (i, endpoint, data = {}) => {
+export default (i, endpoint, data = {}): Promise<{ [x: string]: any }> => {
if (++pending === 1) {
spinner = document.createElement('div');
spinner.setAttribute('id', 'wait');
@@ -22,11 +22,11 @@ export default (i, endpoint, data = {}) => {
}
// Append the credential
- if (i != null) data.i = typeof i === 'object' ? i.token : i;
+ if (i != null) (data as any).i = typeof i === 'object' ? i.token : i;
return new Promise((resolve, reject) => {
// Send request
- fetch(endpoint.indexOf('://') > -1 ? endpoint : `${CONFIG.apiUrl}/${endpoint}`, {
+ fetch(endpoint.indexOf('://') > -1 ? endpoint : `${_API_URL_}/${endpoint}`, {
method: 'POST',
body: JSON.stringify(data),
credentials: endpoint === 'signin' ? 'include' : 'omit'
diff --git a/src/web/app/common/scripts/bytes-to-size.js b/src/web/app/common/scripts/bytes-to-size.ts
index af0268dbd0..1d2b1e7ce3 100644
--- a/src/web/app/common/scripts/bytes-to-size.js
+++ b/src/web/app/common/scripts/bytes-to-size.ts
@@ -1,6 +1,6 @@
export default (bytes, digits = 0) => {
- var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
+ const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0Byte';
- var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
+ const i = Math.floor(Math.log(bytes) / Math.log(1024));
return (bytes / Math.pow(1024, i)).toFixed(digits).replace(/\.0+$/, '') + sizes[i];
};
diff --git a/src/web/app/common/scripts/check-for-update.js b/src/web/app/common/scripts/check-for-update.js
deleted file mode 100644
index 7cb7839d29..0000000000
--- a/src/web/app/common/scripts/check-for-update.js
+++ /dev/null
@@ -1,14 +0,0 @@
-import CONFIG from './config';
-
-export default function() {
- fetch(CONFIG.apiUrl + '/meta', {
- method: 'POST'
- }).then(res => {
- res.json().then(meta => {
- if (meta.version != VERSION) {
- localStorage.setItem('should-refresh', 'true');
- alert('%i18n:common.update-available%'.replace('{newer}', meta.version).replace('{current}', VERSION));
- }
- });
- });
-};
diff --git a/src/web/app/common/scripts/check-for-update.ts b/src/web/app/common/scripts/check-for-update.ts
new file mode 100644
index 0000000000..c447a517fa
--- /dev/null
+++ b/src/web/app/common/scripts/check-for-update.ts
@@ -0,0 +1,12 @@
+import MiOS from '../mios';
+
+declare const _VERSION_: string;
+
+export default async function(mios: MiOS) {
+ const meta = await mios.getMeta();
+
+ if (meta.version != _VERSION_) {
+ localStorage.setItem('should-refresh', 'true');
+ alert('%i18n:common.update-available%'.replace('{newer}', meta.version).replace('{current}', _VERSION_));
+ }
+}
diff --git a/src/web/app/common/scripts/compose-notification.ts b/src/web/app/common/scripts/compose-notification.ts
new file mode 100644
index 0000000000..d0e0c2098d
--- /dev/null
+++ b/src/web/app/common/scripts/compose-notification.ts
@@ -0,0 +1,60 @@
+import getPostSummary from '../../../../common/get-post-summary';
+import getReactionEmoji from '../../../../common/get-reaction-emoji';
+
+type Notification = {
+ title: string;
+ body: string;
+ icon: string;
+ onclick?: any;
+};
+
+// TODO: i18n
+
+export default function(type, data): Notification {
+ switch (type) {
+ case 'drive_file_created':
+ return {
+ title: 'ファイルがアップロードされました',
+ body: data.name,
+ icon: data.url + '?thumbnail&size=64'
+ };
+
+ case 'mention':
+ return {
+ title: `${data.user.name}さんから:`,
+ body: getPostSummary(data),
+ icon: data.user.avatar_url + '?thumbnail&size=64'
+ };
+
+ case 'reply':
+ return {
+ title: `${data.user.name}さんから返信:`,
+ body: getPostSummary(data),
+ icon: data.user.avatar_url + '?thumbnail&size=64'
+ };
+
+ case 'quote':
+ return {
+ title: `${data.user.name}さんが引用:`,
+ body: getPostSummary(data),
+ icon: data.user.avatar_url + '?thumbnail&size=64'
+ };
+
+ case 'reaction':
+ return {
+ title: `${data.user.name}: ${getReactionEmoji(data.reaction)}:`,
+ body: getPostSummary(data.post),
+ icon: data.user.avatar_url + '?thumbnail&size=64'
+ };
+
+ case 'unread_messaging_message':
+ return {
+ title: `${data.user.name}さんからメッセージ:`,
+ body: data.text, // TODO: getMessagingMessageSummary(data),
+ icon: data.user.avatar_url + '?thumbnail&size=64'
+ };
+
+ default:
+ return null;
+ }
+}
diff --git a/src/web/app/common/scripts/config.js b/src/web/app/common/scripts/config.js
deleted file mode 100644
index c5015622f0..0000000000
--- a/src/web/app/common/scripts/config.js
+++ /dev/null
@@ -1,25 +0,0 @@
-const Url = new URL(location.href);
-
-const isRoot = Url.host.split('.')[0] == 'misskey';
-
-const host = isRoot ? Url.host : Url.host.substring(Url.host.indexOf('.') + 1, Url.host.length);
-const scheme = Url.protocol;
-const url = `${scheme}//${host}`;
-const apiUrl = `${scheme}//api.${host}`;
-const chUrl = `${scheme}//ch.${host}`;
-const devUrl = `${scheme}//dev.${host}`;
-const aboutUrl = `${scheme}//about.${host}`;
-const statsUrl = `${scheme}//stats.${host}`;
-const statusUrl = `${scheme}//status.${host}`;
-
-export default {
- host,
- scheme,
- url,
- apiUrl,
- chUrl,
- devUrl,
- aboutUrl,
- statsUrl,
- statusUrl
-};
diff --git a/src/web/app/common/scripts/contains.js b/src/web/app/common/scripts/contains.ts
index a5071b3f25..a5071b3f25 100644
--- a/src/web/app/common/scripts/contains.js
+++ b/src/web/app/common/scripts/contains.ts
diff --git a/src/web/app/common/scripts/copy-to-clipboard.js b/src/web/app/common/scripts/copy-to-clipboard.ts
index 3d2741f8d7..3d2741f8d7 100644
--- a/src/web/app/common/scripts/copy-to-clipboard.js
+++ b/src/web/app/common/scripts/copy-to-clipboard.ts
diff --git a/src/web/app/common/scripts/date-stringify.js b/src/web/app/common/scripts/date-stringify.ts
index e51de8833d..e51de8833d 100644
--- a/src/web/app/common/scripts/date-stringify.js
+++ b/src/web/app/common/scripts/date-stringify.ts
diff --git a/src/web/app/common/scripts/gcd.js b/src/web/app/common/scripts/gcd.ts
index 9a19f9da66..9a19f9da66 100644
--- a/src/web/app/common/scripts/gcd.js
+++ b/src/web/app/common/scripts/gcd.ts
diff --git a/src/web/app/common/scripts/generate-default-userdata.js b/src/web/app/common/scripts/generate-default-userdata.js
deleted file mode 100644
index 1200563e1a..0000000000
--- a/src/web/app/common/scripts/generate-default-userdata.js
+++ /dev/null
@@ -1,45 +0,0 @@
-import uuid from './uuid';
-
-const home = {
- left: [
- 'profile',
- 'calendar',
- 'rss-reader',
- 'photo-stream',
- 'version'
- ],
- right: [
- 'broadcast',
- 'notifications',
- 'user-recommendation',
- 'donation',
- 'nav',
- 'tips'
- ]
-};
-
-export default () => {
- const homeData = [];
-
- home.left.forEach(widget => {
- homeData.push({
- name: widget,
- id: uuid(),
- place: 'left'
- });
- });
-
- home.right.forEach(widget => {
- homeData.push({
- name: widget,
- id: uuid(),
- place: 'right'
- });
- });
-
- const data = {
- home: JSON.stringify(homeData)
- };
-
- return data;
-};
diff --git a/src/web/app/common/scripts/get-kao.js b/src/web/app/common/scripts/get-kao.ts
index 0b77ee285a..2168c5be88 100644
--- a/src/web/app/common/scripts/get-kao.js
+++ b/src/web/app/common/scripts/get-kao.ts
@@ -1,5 +1,5 @@
export default () => [
'(=^・・^=)',
'v(‘ω’)v',
- '🐡( '-' 🐡 )フグパンチ!!!!'
+ '🐡( \'-\' 🐡 )フグパンチ!!!!'
][Math.floor(Math.random() * 3)];
diff --git a/src/web/app/common/scripts/get-median.ts b/src/web/app/common/scripts/get-median.ts
new file mode 100644
index 0000000000..91a415d5b2
--- /dev/null
+++ b/src/web/app/common/scripts/get-median.ts
@@ -0,0 +1,11 @@
+/**
+ * 中央値を求めます
+ * @param samples サンプル
+ */
+export default function(samples) {
+ if (!samples.length) return 0;
+ const numbers = samples.slice(0).sort((a, b) => a - b);
+ const middle = Math.floor(numbers.length / 2);
+ const isEven = numbers.length % 2 === 0;
+ return isEven ? (numbers[middle] + numbers[middle - 1]) / 2 : numbers[middle];
+}
diff --git a/src/web/app/common/scripts/is-promise.js b/src/web/app/common/scripts/is-promise.ts
index 3b4cd70b49..3b4cd70b49 100644
--- a/src/web/app/common/scripts/is-promise.js
+++ b/src/web/app/common/scripts/is-promise.ts
diff --git a/src/web/app/common/scripts/loading.js b/src/web/app/common/scripts/loading.ts
index c48e626648..c48e626648 100644
--- a/src/web/app/common/scripts/loading.js
+++ b/src/web/app/common/scripts/loading.ts
diff --git a/src/web/app/common/scripts/signout.js b/src/web/app/common/scripts/signout.js
deleted file mode 100644
index 6c95cfbc9c..0000000000
--- a/src/web/app/common/scripts/signout.js
+++ /dev/null
@@ -1,7 +0,0 @@
-import CONFIG from './config';
-
-export default () => {
- localStorage.removeItem('me');
- document.cookie = `i=; domain=.${CONFIG.host}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
- location.href = '/';
-};
diff --git a/src/web/app/common/scripts/signout.ts b/src/web/app/common/scripts/signout.ts
new file mode 100644
index 0000000000..2923196549
--- /dev/null
+++ b/src/web/app/common/scripts/signout.ts
@@ -0,0 +1,7 @@
+declare const _HOST_: string;
+
+export default () => {
+ localStorage.removeItem('me');
+ document.cookie = `i=; domain=.${_HOST_}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
+ location.href = '/';
+};
diff --git a/src/web/app/common/scripts/channel-stream.js b/src/web/app/common/scripts/streaming/channel-stream.ts
index 17944dbe45..434b108b9e 100644
--- a/src/web/app/common/scripts/channel-stream.js
+++ b/src/web/app/common/scripts/streaming/channel-stream.ts
@@ -1,16 +1,12 @@
-'use strict';
-
import Stream from './stream';
/**
* Channel stream connection
*/
-class Connection extends Stream {
+export default class Connection extends Stream {
constructor(channelId) {
super('channel', {
channel: channelId
});
}
}
-
-export default Connection;
diff --git a/src/web/app/common/scripts/streaming/drive-stream-manager.ts b/src/web/app/common/scripts/streaming/drive-stream-manager.ts
new file mode 100644
index 0000000000..8acdd7cbba
--- /dev/null
+++ b/src/web/app/common/scripts/streaming/drive-stream-manager.ts
@@ -0,0 +1,20 @@
+import StreamManager from './stream-manager';
+import Connection from './drive-stream';
+
+export default class DriveStreamManager extends StreamManager<Connection> {
+ private me;
+
+ constructor(me) {
+ super();
+
+ this.me = me;
+ }
+
+ public getConnection() {
+ if (this.connection == null) {
+ this.connection = new Connection(this.me);
+ }
+
+ return this.connection;
+ }
+}
diff --git a/src/web/app/common/scripts/streaming/drive-stream.ts b/src/web/app/common/scripts/streaming/drive-stream.ts
new file mode 100644
index 0000000000..0da3f12554
--- /dev/null
+++ b/src/web/app/common/scripts/streaming/drive-stream.ts
@@ -0,0 +1,12 @@
+import Stream from './stream';
+
+/**
+ * Drive stream connection
+ */
+export default class Connection extends Stream {
+ constructor(me) {
+ super('drive', {
+ i: me.token
+ });
+ }
+}
diff --git a/src/web/app/common/scripts/streaming/home-stream-manager.ts b/src/web/app/common/scripts/streaming/home-stream-manager.ts
new file mode 100644
index 0000000000..ad1dc870eb
--- /dev/null
+++ b/src/web/app/common/scripts/streaming/home-stream-manager.ts
@@ -0,0 +1,20 @@
+import StreamManager from './stream-manager';
+import Connection from './home-stream';
+
+export default class HomeStreamManager extends StreamManager<Connection> {
+ private me;
+
+ constructor(me) {
+ super();
+
+ this.me = me;
+ }
+
+ public getConnection() {
+ if (this.connection == null) {
+ this.connection = new Connection(this.me);
+ }
+
+ return this.connection;
+ }
+}
diff --git a/src/web/app/common/scripts/home-stream.js b/src/web/app/common/scripts/streaming/home-stream.ts
index de9ceb3b51..11ad754ef0 100644
--- a/src/web/app/common/scripts/home-stream.js
+++ b/src/web/app/common/scripts/streaming/home-stream.ts
@@ -1,12 +1,10 @@
-'use strict';
-
import Stream from './stream';
-import signout from './signout';
+import signout from '../signout';
/**
* Home stream connection
*/
-class Connection extends Stream {
+export default class Connection extends Stream {
constructor(me) {
super('', {
i: me.token
@@ -17,13 +15,14 @@ class Connection extends Stream {
this.send({ type: 'alive' });
}, 1000 * 60);
+ // 自分の情報が更新されたとき
this.on('i_updated', me.update);
+ // トークンが再生成されたとき
+ // このままではAPIが利用できないので強制的にサインアウトさせる
this.on('my_token_regenerated', () => {
alert('%i18n:common.my-token-regenerated%');
signout();
});
}
}
-
-export default Connection;
diff --git a/src/web/app/common/scripts/streaming/messaging-index-stream-manager.ts b/src/web/app/common/scripts/streaming/messaging-index-stream-manager.ts
new file mode 100644
index 0000000000..0f08b01481
--- /dev/null
+++ b/src/web/app/common/scripts/streaming/messaging-index-stream-manager.ts
@@ -0,0 +1,20 @@
+import StreamManager from './stream-manager';
+import Connection from './messaging-index-stream';
+
+export default class MessagingIndexStreamManager extends StreamManager<Connection> {
+ private me;
+
+ constructor(me) {
+ super();
+
+ this.me = me;
+ }
+
+ public getConnection() {
+ if (this.connection == null) {
+ this.connection = new Connection(this.me);
+ }
+
+ return this.connection;
+ }
+}
diff --git a/src/web/app/common/scripts/streaming/messaging-index-stream.ts b/src/web/app/common/scripts/streaming/messaging-index-stream.ts
new file mode 100644
index 0000000000..8015c840b4
--- /dev/null
+++ b/src/web/app/common/scripts/streaming/messaging-index-stream.ts
@@ -0,0 +1,12 @@
+import Stream from './stream';
+
+/**
+ * Messaging index stream connection
+ */
+export default class Connection extends Stream {
+ constructor(me) {
+ super('messaging-index', {
+ i: me.token
+ });
+ }
+}
diff --git a/src/web/app/common/scripts/messaging-stream.js b/src/web/app/common/scripts/streaming/messaging-stream.ts
index 261525d5f6..68dfc5ec09 100644
--- a/src/web/app/common/scripts/messaging-stream.js
+++ b/src/web/app/common/scripts/streaming/messaging-stream.ts
@@ -1,23 +1,19 @@
-'use strict';
-
import Stream from './stream';
/**
* Messaging stream connection
*/
-class Connection extends Stream {
+export default class Connection extends Stream {
constructor(me, otherparty) {
super('messaging', {
i: me.token,
otherparty
});
- this.on('_connected_', () => {
+ (this as any).on('_connected_', () => {
this.send({
i: me.token
});
});
}
}
-
-export default Connection;
diff --git a/src/web/app/common/scripts/streaming/requests-stream-manager.ts b/src/web/app/common/scripts/streaming/requests-stream-manager.ts
new file mode 100644
index 0000000000..44db913e78
--- /dev/null
+++ b/src/web/app/common/scripts/streaming/requests-stream-manager.ts
@@ -0,0 +1,12 @@
+import StreamManager from './stream-manager';
+import Connection from './requests-stream';
+
+export default class RequestsStreamManager extends StreamManager<Connection> {
+ public getConnection() {
+ if (this.connection == null) {
+ this.connection = new Connection();
+ }
+
+ return this.connection;
+ }
+}
diff --git a/src/web/app/common/scripts/streaming/requests-stream.ts b/src/web/app/common/scripts/streaming/requests-stream.ts
new file mode 100644
index 0000000000..22ecea6c07
--- /dev/null
+++ b/src/web/app/common/scripts/streaming/requests-stream.ts
@@ -0,0 +1,10 @@
+import Stream from './stream';
+
+/**
+ * Requests stream connection
+ */
+export default class Connection extends Stream {
+ constructor() {
+ super('requests');
+ }
+}
diff --git a/src/web/app/common/scripts/streaming/server-stream-manager.ts b/src/web/app/common/scripts/streaming/server-stream-manager.ts
new file mode 100644
index 0000000000..a170daebb9
--- /dev/null
+++ b/src/web/app/common/scripts/streaming/server-stream-manager.ts
@@ -0,0 +1,12 @@
+import StreamManager from './stream-manager';
+import Connection from './server-stream';
+
+export default class ServerStreamManager extends StreamManager<Connection> {
+ public getConnection() {
+ if (this.connection == null) {
+ this.connection = new Connection();
+ }
+
+ return this.connection;
+ }
+}
diff --git a/src/web/app/common/scripts/server-stream.js b/src/web/app/common/scripts/streaming/server-stream.ts
index a1c466b35d..b9e0684465 100644
--- a/src/web/app/common/scripts/server-stream.js
+++ b/src/web/app/common/scripts/streaming/server-stream.ts
@@ -1,14 +1,10 @@
-'use strict';
-
import Stream from './stream';
/**
* Server stream connection
*/
-class Connection extends Stream {
+export default class Connection extends Stream {
constructor() {
super('server');
}
}
-
-export default Connection;
diff --git a/src/web/app/common/scripts/streaming/stream-manager.ts b/src/web/app/common/scripts/streaming/stream-manager.ts
new file mode 100644
index 0000000000..5bb0dc701c
--- /dev/null
+++ b/src/web/app/common/scripts/streaming/stream-manager.ts
@@ -0,0 +1,89 @@
+import { EventEmitter } from 'eventemitter3';
+import * as uuid from 'uuid';
+import Connection from './stream';
+
+/**
+ * ストリーム接続を管理するクラス
+ * 複数の場所から同じストリームを利用する際、接続をまとめたりする
+ */
+export default abstract class StreamManager<T extends Connection> extends EventEmitter {
+ private _connection: T = null;
+
+ private disposeTimerId: any;
+
+ /**
+ * コネクションを必要としているユーザー
+ */
+ private users = [];
+
+ protected set connection(connection: T) {
+ this._connection = connection;
+
+ if (this._connection == null) {
+ this.emit('disconnected');
+ } else {
+ this.emit('connected', this._connection);
+ }
+ }
+
+ protected get connection() {
+ return this._connection;
+ }
+
+ /**
+ * コネクションを持っているか否か
+ */
+ public get hasConnection() {
+ return this._connection != null;
+ }
+
+ /**
+ * コネクションを要求します
+ */
+ public abstract getConnection(): T;
+
+ /**
+ * 現在接続しているコネクションを取得します
+ */
+ public borrow() {
+ return this._connection;
+ }
+
+ /**
+ * コネクションを要求するためのユーザーIDを発行します
+ */
+ public use() {
+ // タイマー解除
+ if (this.disposeTimerId) {
+ clearTimeout(this.disposeTimerId);
+ this.disposeTimerId = null;
+ }
+
+ // ユーザーID生成
+ const userId = uuid();
+
+ this.users.push(userId);
+
+ return userId;
+ }
+
+ /**
+ * コネクションを利用し終わってもう必要ないことを通知します
+ * @param userId use で発行したユーザーID
+ */
+ public dispose(userId) {
+ this.users = this.users.filter(id => id != userId);
+
+ // 誰もコネクションの利用者がいなくなったら
+ if (this.users.length == 0) {
+ // また直ぐに再利用される可能性があるので、一定時間待ち、
+ // 新たな利用者が現れなければコネクションを切断する
+ this.disposeTimerId = setTimeout(() => {
+ this.disposeTimerId = null;
+
+ this.connection.close();
+ this.connection = null;
+ }, 3000);
+ }
+ }
+}
diff --git a/src/web/app/common/scripts/stream.js b/src/web/app/common/scripts/streaming/stream.ts
index 981118b5de..770d77510f 100644
--- a/src/web/app/common/scripts/stream.js
+++ b/src/web/app/common/scripts/streaming/stream.ts
@@ -1,35 +1,38 @@
-'use strict';
+declare const _API_URL_: string;
-const ReconnectingWebSocket = require('reconnecting-websocket');
-import * as riot from 'riot';
-import CONFIG from './config';
+import { EventEmitter } from 'eventemitter3';
+import * as ReconnectingWebsocket from 'reconnecting-websocket';
/**
* Misskey stream connection
*/
-class Connection {
- constructor(endpoint, params) {
- // BIND -----------------------------------
+export default class Connection extends EventEmitter {
+ private state: string;
+ private buffer: any[];
+ private socket: ReconnectingWebsocket;
+
+ constructor(endpoint, params?) {
+ super();
+
+ //#region BIND
this.onOpen = this.onOpen.bind(this);
this.onClose = this.onClose.bind(this);
this.onMessage = this.onMessage.bind(this);
this.send = this.send.bind(this);
this.close = this.close.bind(this);
- // ----------------------------------------
-
- riot.observable(this);
+ //#endregion
this.state = 'initializing';
this.buffer = [];
- const host = CONFIG.apiUrl.replace('http', 'ws');
+ const host = _API_URL_.replace('http', 'ws');
const query = params
? Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&')
: null;
- this.socket = new ReconnectingWebSocket(`${host}/${endpoint}${query ? '?' + query : ''}`);
+ this.socket = new ReconnectingWebsocket(`${host}/${endpoint}${query ? '?' + query : ''}`);
this.socket.addEventListener('open', this.onOpen);
this.socket.addEventListener('close', this.onClose);
this.socket.addEventListener('message', this.onMessage);
@@ -37,11 +40,10 @@ class Connection {
/**
* Callback of when open connection
- * @private
*/
- onOpen() {
+ private onOpen() {
this.state = 'connected';
- this.trigger('_connected_');
+ this.emit('_connected_');
// バッファーを処理
const _buffer = [].concat(this.buffer); // Shallow copy
@@ -53,48 +55,42 @@ class Connection {
/**
* Callback of when close connection
- * @private
*/
- onClose() {
+ private onClose() {
this.state = 'reconnecting';
- this.trigger('_closed_');
+ this.emit('_closed_');
}
/**
* Callback of when received a message from connection
- * @private
*/
- onMessage(message) {
+ private onMessage(message) {
try {
const msg = JSON.parse(message.data);
- if (msg.type) this.trigger(msg.type, msg.body);
- } catch(e) {
+ if (msg.type) this.emit(msg.type, msg.body);
+ } catch (e) {
// noop
}
}
/**
* Send a message to connection
- * @public
*/
- send(message) {
+ public send(message) {
// まだ接続が確立されていなかったらバッファリングして次に接続した時に送信する
if (this.state != 'connected') {
this.buffer.push(message);
return;
- };
+ }
this.socket.send(JSON.stringify(message));
}
/**
* Close this connection
- * @public
*/
- close() {
+ public close() {
this.socket.removeEventListener('open', this.onOpen);
this.socket.removeEventListener('message', this.onMessage);
}
}
-
-export default Connection;
diff --git a/src/web/app/common/scripts/text-compiler.js b/src/web/app/common/scripts/text-compiler.ts
index 0a9b8022df..e0ea47df26 100644
--- a/src/web/app/common/scripts/text-compiler.js
+++ b/src/web/app/common/scripts/text-compiler.ts
@@ -1,6 +1,7 @@
+declare const _URL_: string;
+
import * as riot from 'riot';
-const pictograph = require('pictograph');
-import CONFIG from './config';
+import * as pictograph from 'pictograph';
const escape = text =>
text
@@ -12,7 +13,7 @@ export default (tokens, shouldBreak) => {
shouldBreak = true;
}
- const me = riot.mixin('i').me;
+ const me = (riot as any).mixin('i').me;
let text = tokens.map(token => {
switch (token.type) {
@@ -26,7 +27,7 @@ export default (tokens, shouldBreak) => {
case 'link':
return `<a class="link" href="${escape(token.url)}" target="_blank" title="${escape(token.url)}">${escape(token.title)}</a>`;
case 'mention':
- return `<a href="${CONFIG.url + '/' + escape(token.username)}" target="_blank" data-user-preview="${token.content}" ${me && me.username == token.username ? 'data-is-me' : ''}>${token.content}</a>`;
+ return `<a href="${_URL_ + '/' + escape(token.username)}" target="_blank" data-user-preview="${token.content}" ${me && me.username == token.username ? 'data-is-me' : ''}>${token.content}</a>`;
case 'hashtag': // TODO
return `<a>${escape(token.content)}</a>`;
case 'code':
diff --git a/src/web/app/common/scripts/uuid.js b/src/web/app/common/scripts/uuid.js
deleted file mode 100644
index ff016e18ad..0000000000
--- a/src/web/app/common/scripts/uuid.js
+++ /dev/null
@@ -1,18 +0,0 @@
-/**
- * Generate a UUID
- */
-export default () => {
- let uuid = '';
-
- for (let i = 0; i < 32; i++) {
- const random = Math.random() * 16 | 0;
-
- if (i == 8 || i == 12 || i == 16 || i == 20) {
- uuid += '-';
- }
-
- uuid += (i == 12 ? 4 : (i == 16 ? (random & 3 | 8) : random)).toString(16);
- }
-
- return uuid;
-};