From bc9a8283c66d7588f931d4b802f7ab1fa7aa3226 Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 13 Nov 2017 18:05:35 +0900 Subject: なんかもうめっちゃ変えた MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/web/app/common/scripts/api.js | 46 ---------- src/web/app/common/scripts/api.ts | 46 ++++++++++ src/web/app/common/scripts/bytes-to-size.js | 6 -- src/web/app/common/scripts/bytes-to-size.ts | 6 ++ src/web/app/common/scripts/channel-stream.js | 16 ---- src/web/app/common/scripts/channel-stream.ts | 16 ++++ src/web/app/common/scripts/check-for-update.js | 14 --- src/web/app/common/scripts/check-for-update.ts | 16 ++++ src/web/app/common/scripts/config.js | 25 ------ src/web/app/common/scripts/config.ts | 25 ++++++ src/web/app/common/scripts/contains.js | 8 -- src/web/app/common/scripts/contains.ts | 8 ++ src/web/app/common/scripts/copy-to-clipboard.js | 13 --- src/web/app/common/scripts/copy-to-clipboard.ts | 13 +++ src/web/app/common/scripts/date-stringify.js | 13 --- src/web/app/common/scripts/date-stringify.ts | 13 +++ src/web/app/common/scripts/gcd.js | 2 - src/web/app/common/scripts/gcd.ts | 2 + src/web/app/common/scripts/get-kao.js | 5 -- src/web/app/common/scripts/get-kao.ts | 5 ++ src/web/app/common/scripts/home-stream.js | 29 ------ src/web/app/common/scripts/home-stream.ts | 29 ++++++ src/web/app/common/scripts/is-promise.js | 1 - src/web/app/common/scripts/is-promise.ts | 1 + src/web/app/common/scripts/loading.js | 21 ----- src/web/app/common/scripts/loading.ts | 21 +++++ src/web/app/common/scripts/messaging-stream.js | 23 ----- src/web/app/common/scripts/messaging-stream.ts | 23 +++++ .../app/common/scripts/server-stream-manager.ts | 31 +------ src/web/app/common/scripts/server-stream.js | 14 --- src/web/app/common/scripts/server-stream.ts | 14 +++ src/web/app/common/scripts/signout.js | 7 -- src/web/app/common/scripts/signout.ts | 7 ++ src/web/app/common/scripts/stream-manager.ts | 33 +++++++ src/web/app/common/scripts/stream.js | 100 --------------------- src/web/app/common/scripts/stream.ts | 99 ++++++++++++++++++++ src/web/app/common/scripts/text-compiler.js | 47 ---------- src/web/app/common/scripts/text-compiler.ts | 47 ++++++++++ 38 files changed, 426 insertions(+), 419 deletions(-) delete mode 100644 src/web/app/common/scripts/api.js create mode 100644 src/web/app/common/scripts/api.ts delete mode 100644 src/web/app/common/scripts/bytes-to-size.js create mode 100644 src/web/app/common/scripts/bytes-to-size.ts delete mode 100644 src/web/app/common/scripts/channel-stream.js create mode 100644 src/web/app/common/scripts/channel-stream.ts delete mode 100644 src/web/app/common/scripts/check-for-update.js create mode 100644 src/web/app/common/scripts/check-for-update.ts delete mode 100644 src/web/app/common/scripts/config.js create mode 100644 src/web/app/common/scripts/config.ts delete mode 100644 src/web/app/common/scripts/contains.js create mode 100644 src/web/app/common/scripts/contains.ts delete mode 100644 src/web/app/common/scripts/copy-to-clipboard.js create mode 100644 src/web/app/common/scripts/copy-to-clipboard.ts delete mode 100644 src/web/app/common/scripts/date-stringify.js create mode 100644 src/web/app/common/scripts/date-stringify.ts delete mode 100644 src/web/app/common/scripts/gcd.js create mode 100644 src/web/app/common/scripts/gcd.ts delete mode 100644 src/web/app/common/scripts/get-kao.js create mode 100644 src/web/app/common/scripts/get-kao.ts delete mode 100644 src/web/app/common/scripts/home-stream.js create mode 100644 src/web/app/common/scripts/home-stream.ts delete mode 100644 src/web/app/common/scripts/is-promise.js create mode 100644 src/web/app/common/scripts/is-promise.ts delete mode 100644 src/web/app/common/scripts/loading.js create mode 100644 src/web/app/common/scripts/loading.ts delete mode 100644 src/web/app/common/scripts/messaging-stream.js create mode 100644 src/web/app/common/scripts/messaging-stream.ts delete mode 100644 src/web/app/common/scripts/server-stream.js create mode 100644 src/web/app/common/scripts/server-stream.ts delete mode 100644 src/web/app/common/scripts/signout.js create mode 100644 src/web/app/common/scripts/signout.ts create mode 100644 src/web/app/common/scripts/stream-manager.ts delete mode 100644 src/web/app/common/scripts/stream.js create mode 100644 src/web/app/common/scripts/stream.ts delete mode 100644 src/web/app/common/scripts/text-compiler.js create mode 100644 src/web/app/common/scripts/text-compiler.ts (limited to 'src/web/app/common/scripts') diff --git a/src/web/app/common/scripts/api.js b/src/web/app/common/scripts/api.js deleted file mode 100644 index 4855f736c7..0000000000 --- a/src/web/app/common/scripts/api.js +++ /dev/null @@ -1,46 +0,0 @@ -/** - * API Request - */ - -import CONFIG from './config'; - -let spinner = null; -let pending = 0; - -/** - * Send a request to API - * @param {string|Object} i Credential - * @param {string} endpoint Endpoint - * @param {any} [data={}] Data - * @return {Promise} Response - */ -export default (i, endpoint, data = {}) => { - if (++pending === 1) { - spinner = document.createElement('div'); - spinner.setAttribute('id', 'wait'); - document.body.appendChild(spinner); - } - - // Append the credential - if (i != null) data.i = typeof i === 'object' ? i.token : i; - - return new Promise((resolve, reject) => { - // Send request - fetch(endpoint.indexOf('://') > -1 ? endpoint : `${CONFIG.apiUrl}/${endpoint}`, { - method: 'POST', - body: JSON.stringify(data), - credentials: endpoint === 'signin' ? 'include' : 'omit' - }).then(res => { - if (--pending === 0) spinner.parentNode.removeChild(spinner); - if (res.status === 200) { - res.json().then(resolve); - } else if (res.status === 204) { - resolve(); - } else { - res.json().then(err => { - reject(err.error); - }); - } - }).catch(reject); - }); -}; diff --git a/src/web/app/common/scripts/api.ts b/src/web/app/common/scripts/api.ts new file mode 100644 index 0000000000..2a9d78e87d --- /dev/null +++ b/src/web/app/common/scripts/api.ts @@ -0,0 +1,46 @@ +/** + * API Request + */ + +import CONFIG from './config'; + +let spinner = null; +let pending = 0; + +/** + * Send a request to API + * @param {string|Object} i Credential + * @param {string} endpoint Endpoint + * @param {any} [data={}] Data + * @return {Promise} Response + */ +export default (i, endpoint, data = {}): Promise => { + if (++pending === 1) { + spinner = document.createElement('div'); + spinner.setAttribute('id', 'wait'); + document.body.appendChild(spinner); + } + + // Append the credential + 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}`, { + method: 'POST', + body: JSON.stringify(data), + credentials: endpoint === 'signin' ? 'include' : 'omit' + }).then(res => { + if (--pending === 0) spinner.parentNode.removeChild(spinner); + if (res.status === 200) { + res.json().then(resolve); + } else if (res.status === 204) { + resolve(); + } else { + res.json().then(err => { + reject(err.error); + }); + } + }).catch(reject); + }); +}; diff --git a/src/web/app/common/scripts/bytes-to-size.js b/src/web/app/common/scripts/bytes-to-size.js deleted file mode 100644 index af0268dbd0..0000000000 --- a/src/web/app/common/scripts/bytes-to-size.js +++ /dev/null @@ -1,6 +0,0 @@ -export default (bytes, digits = 0) => { - var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; - if (bytes == 0) return '0Byte'; - var i = parseInt(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/bytes-to-size.ts b/src/web/app/common/scripts/bytes-to-size.ts new file mode 100644 index 0000000000..1d2b1e7ce3 --- /dev/null +++ b/src/web/app/common/scripts/bytes-to-size.ts @@ -0,0 +1,6 @@ +export default (bytes, digits = 0) => { + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; + if (bytes == 0) return '0Byte'; + 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/channel-stream.js b/src/web/app/common/scripts/channel-stream.js deleted file mode 100644 index 17944dbe45..0000000000 --- a/src/web/app/common/scripts/channel-stream.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict'; - -import Stream from './stream'; - -/** - * Channel stream connection - */ -class Connection extends Stream { - constructor(channelId) { - super('channel', { - channel: channelId - }); - } -} - -export default Connection; diff --git a/src/web/app/common/scripts/channel-stream.ts b/src/web/app/common/scripts/channel-stream.ts new file mode 100644 index 0000000000..17944dbe45 --- /dev/null +++ b/src/web/app/common/scripts/channel-stream.ts @@ -0,0 +1,16 @@ +'use strict'; + +import Stream from './stream'; + +/** + * Channel stream connection + */ +class Connection extends Stream { + constructor(channelId) { + super('channel', { + channel: channelId + }); + } +} + +export default Connection; 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..99d8b5d059 --- /dev/null +++ b/src/web/app/common/scripts/check-for-update.ts @@ -0,0 +1,16 @@ +import CONFIG from './config'; + +declare var VERSION: string; + +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/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/config.ts b/src/web/app/common/scripts/config.ts new file mode 100644 index 0000000000..c5015622f0 --- /dev/null +++ b/src/web/app/common/scripts/config.ts @@ -0,0 +1,25 @@ +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.js deleted file mode 100644 index a5071b3f25..0000000000 --- a/src/web/app/common/scripts/contains.js +++ /dev/null @@ -1,8 +0,0 @@ -export default (parent, child) => { - let node = child.parentNode; - while (node) { - if (node == parent) return true; - node = node.parentNode; - } - return false; -}; diff --git a/src/web/app/common/scripts/contains.ts b/src/web/app/common/scripts/contains.ts new file mode 100644 index 0000000000..a5071b3f25 --- /dev/null +++ b/src/web/app/common/scripts/contains.ts @@ -0,0 +1,8 @@ +export default (parent, child) => { + let node = child.parentNode; + while (node) { + if (node == parent) return true; + node = node.parentNode; + } + return false; +}; diff --git a/src/web/app/common/scripts/copy-to-clipboard.js b/src/web/app/common/scripts/copy-to-clipboard.js deleted file mode 100644 index 3d2741f8d7..0000000000 --- a/src/web/app/common/scripts/copy-to-clipboard.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Clipboardに値をコピー(TODO: 文字列以外も対応) - */ -export default val => { - const form = document.createElement('textarea'); - form.textContent = val; - document.body.appendChild(form); - form.select(); - const result = document.execCommand('copy'); - document.body.removeChild(form); - - return result; -}; diff --git a/src/web/app/common/scripts/copy-to-clipboard.ts b/src/web/app/common/scripts/copy-to-clipboard.ts new file mode 100644 index 0000000000..3d2741f8d7 --- /dev/null +++ b/src/web/app/common/scripts/copy-to-clipboard.ts @@ -0,0 +1,13 @@ +/** + * Clipboardに値をコピー(TODO: 文字列以外も対応) + */ +export default val => { + const form = document.createElement('textarea'); + form.textContent = val; + document.body.appendChild(form); + form.select(); + const result = document.execCommand('copy'); + document.body.removeChild(form); + + return result; +}; diff --git a/src/web/app/common/scripts/date-stringify.js b/src/web/app/common/scripts/date-stringify.js deleted file mode 100644 index e51de8833d..0000000000 --- a/src/web/app/common/scripts/date-stringify.js +++ /dev/null @@ -1,13 +0,0 @@ -export default date => { - if (typeof date == 'string') date = new Date(date); - return ( - date.getFullYear() + '年' + - (date.getMonth() + 1) + '月' + - date.getDate() + '日' + - ' ' + - date.getHours() + '時' + - date.getMinutes() + '分' + - ' ' + - `(${['日', '月', '火', '水', '木', '金', '土'][date.getDay()]})` - ); -}; diff --git a/src/web/app/common/scripts/date-stringify.ts b/src/web/app/common/scripts/date-stringify.ts new file mode 100644 index 0000000000..e51de8833d --- /dev/null +++ b/src/web/app/common/scripts/date-stringify.ts @@ -0,0 +1,13 @@ +export default date => { + if (typeof date == 'string') date = new Date(date); + return ( + date.getFullYear() + '年' + + (date.getMonth() + 1) + '月' + + date.getDate() + '日' + + ' ' + + date.getHours() + '時' + + date.getMinutes() + '分' + + ' ' + + `(${['日', '月', '火', '水', '木', '金', '土'][date.getDay()]})` + ); +}; diff --git a/src/web/app/common/scripts/gcd.js b/src/web/app/common/scripts/gcd.js deleted file mode 100644 index 9a19f9da66..0000000000 --- a/src/web/app/common/scripts/gcd.js +++ /dev/null @@ -1,2 +0,0 @@ -const gcd = (a, b) => !b ? a : gcd(b, a % b); -export default gcd; diff --git a/src/web/app/common/scripts/gcd.ts b/src/web/app/common/scripts/gcd.ts new file mode 100644 index 0000000000..9a19f9da66 --- /dev/null +++ b/src/web/app/common/scripts/gcd.ts @@ -0,0 +1,2 @@ +const gcd = (a, b) => !b ? a : gcd(b, a % b); +export default gcd; diff --git a/src/web/app/common/scripts/get-kao.js b/src/web/app/common/scripts/get-kao.js deleted file mode 100644 index 0b77ee285a..0000000000 --- a/src/web/app/common/scripts/get-kao.js +++ /dev/null @@ -1,5 +0,0 @@ -export default () => [ - '(=^・・^=)', - 'v(‘ω’)v', - '🐡( '-' 🐡 )フグパンチ!!!!' -][Math.floor(Math.random() * 3)]; diff --git a/src/web/app/common/scripts/get-kao.ts b/src/web/app/common/scripts/get-kao.ts new file mode 100644 index 0000000000..2168c5be88 --- /dev/null +++ b/src/web/app/common/scripts/get-kao.ts @@ -0,0 +1,5 @@ +export default () => [ + '(=^・・^=)', + 'v(‘ω’)v', + '🐡( \'-\' 🐡 )フグパンチ!!!!' +][Math.floor(Math.random() * 3)]; diff --git a/src/web/app/common/scripts/home-stream.js b/src/web/app/common/scripts/home-stream.js deleted file mode 100644 index de9ceb3b51..0000000000 --- a/src/web/app/common/scripts/home-stream.js +++ /dev/null @@ -1,29 +0,0 @@ -'use strict'; - -import Stream from './stream'; -import signout from './signout'; - -/** - * Home stream connection - */ -class Connection extends Stream { - constructor(me) { - super('', { - i: me.token - }); - - // 最終利用日時を更新するため定期的にaliveメッセージを送信 - setInterval(() => { - this.send({ type: 'alive' }); - }, 1000 * 60); - - this.on('i_updated', me.update); - - this.on('my_token_regenerated', () => { - alert('%i18n:common.my-token-regenerated%'); - signout(); - }); - } -} - -export default Connection; diff --git a/src/web/app/common/scripts/home-stream.ts b/src/web/app/common/scripts/home-stream.ts new file mode 100644 index 0000000000..c549f2b936 --- /dev/null +++ b/src/web/app/common/scripts/home-stream.ts @@ -0,0 +1,29 @@ +'use strict'; + +import Stream from './stream'; +import signout from './signout'; + +/** + * Home stream connection + */ +class Connection extends Stream { + constructor(me) { + super('', { + i: me.token + }); + + // 最終利用日時を更新するため定期的にaliveメッセージを送信 + setInterval(() => { + this.send({ type: 'alive' }); + }, 1000 * 60); + + (this as any).on('i_updated', me.update); + + (this as any).on('my_token_regenerated', () => { + alert('%i18n:common.my-token-regenerated%'); + signout(); + }); + } +} + +export default Connection; diff --git a/src/web/app/common/scripts/is-promise.js b/src/web/app/common/scripts/is-promise.js deleted file mode 100644 index 3b4cd70b49..0000000000 --- a/src/web/app/common/scripts/is-promise.js +++ /dev/null @@ -1 +0,0 @@ -export default x => typeof x.then == 'function'; diff --git a/src/web/app/common/scripts/is-promise.ts b/src/web/app/common/scripts/is-promise.ts new file mode 100644 index 0000000000..3b4cd70b49 --- /dev/null +++ b/src/web/app/common/scripts/is-promise.ts @@ -0,0 +1 @@ +export default x => typeof x.then == 'function'; diff --git a/src/web/app/common/scripts/loading.js b/src/web/app/common/scripts/loading.js deleted file mode 100644 index c48e626648..0000000000 --- a/src/web/app/common/scripts/loading.js +++ /dev/null @@ -1,21 +0,0 @@ -const NProgress = require('nprogress'); -NProgress.configure({ - trickleSpeed: 500, - showSpinner: false -}); - -const root = document.getElementsByTagName('html')[0]; - -export default { - start: () => { - root.classList.add('progress'); - NProgress.start(); - }, - done: () => { - root.classList.remove('progress'); - NProgress.done(); - }, - set: val => { - NProgress.set(val); - } -}; diff --git a/src/web/app/common/scripts/loading.ts b/src/web/app/common/scripts/loading.ts new file mode 100644 index 0000000000..c48e626648 --- /dev/null +++ b/src/web/app/common/scripts/loading.ts @@ -0,0 +1,21 @@ +const NProgress = require('nprogress'); +NProgress.configure({ + trickleSpeed: 500, + showSpinner: false +}); + +const root = document.getElementsByTagName('html')[0]; + +export default { + start: () => { + root.classList.add('progress'); + NProgress.start(); + }, + done: () => { + root.classList.remove('progress'); + NProgress.done(); + }, + set: val => { + NProgress.set(val); + } +}; diff --git a/src/web/app/common/scripts/messaging-stream.js b/src/web/app/common/scripts/messaging-stream.js deleted file mode 100644 index 261525d5f6..0000000000 --- a/src/web/app/common/scripts/messaging-stream.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict'; - -import Stream from './stream'; - -/** - * Messaging stream connection - */ -class Connection extends Stream { - constructor(me, otherparty) { - super('messaging', { - i: me.token, - otherparty - }); - - this.on('_connected_', () => { - this.send({ - i: me.token - }); - }); - } -} - -export default Connection; diff --git a/src/web/app/common/scripts/messaging-stream.ts b/src/web/app/common/scripts/messaging-stream.ts new file mode 100644 index 0000000000..63830f7b17 --- /dev/null +++ b/src/web/app/common/scripts/messaging-stream.ts @@ -0,0 +1,23 @@ +'use strict'; + +import Stream from './stream'; + +/** + * Messaging stream connection + */ +class Connection extends Stream { + constructor(me, otherparty) { + super('messaging', { + i: me.token, + otherparty + }); + + (this as any).on('_connected_', () => { + this.send({ + i: me.token + }); + }); + } +} + +export default Connection; diff --git a/src/web/app/common/scripts/server-stream-manager.ts b/src/web/app/common/scripts/server-stream-manager.ts index 54333c8cf5..a170daebb9 100644 --- a/src/web/app/common/scripts/server-stream-manager.ts +++ b/src/web/app/common/scripts/server-stream-manager.ts @@ -1,14 +1,7 @@ +import StreamManager from './stream-manager'; import Connection from './server-stream'; -import * as uuid from 'uuid'; - -export default class ServerStreamManager { - private connection = null; - - /** - * コネクションを必要としているユーザー - */ - private users = []; +export default class ServerStreamManager extends StreamManager { public getConnection() { if (this.connection == null) { this.connection = new Connection(); @@ -16,24 +9,4 @@ export default class ServerStreamManager { return this.connection; } - - public use() { - // ユーザーID生成 - const userId = uuid(); - - this.users.push(userId); - - return userId; - } - - public dispose(userId) { - this.users = this.users.filter(id => id != userId); - - // 誰もコネクションの利用者がいなくなったら - if (this.users.length == 0) { - // コネクションを切断する - this.connection.close(); - this.connection = null; - } - } } diff --git a/src/web/app/common/scripts/server-stream.js b/src/web/app/common/scripts/server-stream.js deleted file mode 100644 index a1c466b35d..0000000000 --- a/src/web/app/common/scripts/server-stream.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -import Stream from './stream'; - -/** - * Server stream connection - */ -class Connection extends Stream { - constructor() { - super('server'); - } -} - -export default Connection; diff --git a/src/web/app/common/scripts/server-stream.ts b/src/web/app/common/scripts/server-stream.ts new file mode 100644 index 0000000000..a1c466b35d --- /dev/null +++ b/src/web/app/common/scripts/server-stream.ts @@ -0,0 +1,14 @@ +'use strict'; + +import Stream from './stream'; + +/** + * Server stream connection + */ +class Connection extends Stream { + constructor() { + super('server'); + } +} + +export default Connection; 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..6c95cfbc9c --- /dev/null +++ b/src/web/app/common/scripts/signout.ts @@ -0,0 +1,7 @@ +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/stream-manager.ts b/src/web/app/common/scripts/stream-manager.ts new file mode 100644 index 0000000000..4eaf0f9a45 --- /dev/null +++ b/src/web/app/common/scripts/stream-manager.ts @@ -0,0 +1,33 @@ +import * as uuid from 'uuid'; +import Connection from './stream'; + +export default abstract class StreamManager { + protected connection: T = null; + + /** + * コネクションを必要としているユーザー + */ + private users = []; + + public abstract getConnection(): T; + + public use() { + // ユーザーID生成 + const userId = uuid(); + + this.users.push(userId); + + return userId; + } + + public dispose(userId) { + this.users = this.users.filter(id => id != userId); + + // 誰もコネクションの利用者がいなくなったら + if (this.users.length == 0) { + // コネクションを切断する + this.connection.close(); + this.connection = null; + } + } +} diff --git a/src/web/app/common/scripts/stream.js b/src/web/app/common/scripts/stream.js deleted file mode 100644 index a03b7bf200..0000000000 --- a/src/web/app/common/scripts/stream.js +++ /dev/null @@ -1,100 +0,0 @@ -'use strict'; - -import * as ReconnectingWebsocket from 'reconnecting-websocket'; -import * as riot from 'riot'; -import CONFIG from './config'; - -/** - * Misskey stream connection - */ -class Connection { - constructor(endpoint, params) { - // 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); - - this.state = 'initializing'; - this.buffer = []; - - const host = CONFIG.apiUrl.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.addEventListener('open', this.onOpen); - this.socket.addEventListener('close', this.onClose); - this.socket.addEventListener('message', this.onMessage); - } - - /** - * Callback of when open connection - * @private - */ - onOpen() { - this.state = 'connected'; - this.trigger('_connected_'); - - // バッファーを処理 - const _buffer = [].concat(this.buffer); // Shallow copy - this.buffer = []; // Clear buffer - _buffer.forEach(message => { - this.send(message); // Resend each buffered messages - }); - } - - /** - * Callback of when close connection - * @private - */ - onClose() { - this.state = 'reconnecting'; - this.trigger('_closed_'); - } - - /** - * Callback of when received a message from connection - * @private - */ - onMessage(message) { - try { - const msg = JSON.parse(message.data); - if (msg.type) this.trigger(msg.type, msg.body); - } catch(e) { - // noop - } - } - - /** - * Send a message to connection - * @public - */ - send(message) { - // まだ接続が確立されていなかったらバッファリングして次に接続した時に送信する - if (this.state != 'connected') { - this.buffer.push(message); - return; - }; - - this.socket.send(JSON.stringify(message)); - } - - /** - * Close this connection - * @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/stream.ts b/src/web/app/common/scripts/stream.ts new file mode 100644 index 0000000000..9595246879 --- /dev/null +++ b/src/web/app/common/scripts/stream.ts @@ -0,0 +1,99 @@ +'use strict'; + +import * as ReconnectingWebsocket from 'reconnecting-websocket'; +import * as riot from 'riot'; +import CONFIG from './config'; + +/** + * Misskey stream connection + */ +class Connection { + private state: string; + private buffer: any[]; + private socket: ReconnectingWebsocket; + + constructor(endpoint, params?) { + // 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); + + this.state = 'initializing'; + this.buffer = []; + + const host = CONFIG.apiUrl.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.addEventListener('open', this.onOpen); + this.socket.addEventListener('close', this.onClose); + this.socket.addEventListener('message', this.onMessage); + } + + /** + * Callback of when open connection + */ + private onOpen() { + this.state = 'connected'; + (this as any).trigger('_connected_'); + + // バッファーを処理 + const _buffer = [].concat(this.buffer); // Shallow copy + this.buffer = []; // Clear buffer + _buffer.forEach(message => { + this.send(message); // Resend each buffered messages + }); + } + + /** + * Callback of when close connection + */ + private onClose() { + this.state = 'reconnecting'; + (this as any).trigger('_closed_'); + } + + /** + * Callback of when received a message from connection + */ + private onMessage(message) { + try { + const msg = JSON.parse(message.data); + if (msg.type) (this as any).trigger(msg.type, msg.body); + } catch (e) { + // noop + } + } + + /** + * Send a message to connection + */ + public send(message) { + // まだ接続が確立されていなかったらバッファリングして次に接続した時に送信する + if (this.state != 'connected') { + this.buffer.push(message); + return; + } + + this.socket.send(JSON.stringify(message)); + } + + /** + * Close this connection + */ + 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.js deleted file mode 100644 index 0a9b8022df..0000000000 --- a/src/web/app/common/scripts/text-compiler.js +++ /dev/null @@ -1,47 +0,0 @@ -import * as riot from 'riot'; -const pictograph = require('pictograph'); -import CONFIG from './config'; - -const escape = text => - text - .replace(/>/g, '>') - .replace(/ { - if (shouldBreak == null) { - shouldBreak = true; - } - - const me = riot.mixin('i').me; - - let text = tokens.map(token => { - switch (token.type) { - case 'text': - return escape(token.content) - .replace(/(\r\n|\n|\r)/g, shouldBreak ? '
' : ' '); - case 'bold': - return `${escape(token.bold)}`; - case 'url': - return ``; - case 'link': - return `${escape(token.title)}`; - case 'mention': - return `${token.content}`; - case 'hashtag': // TODO - return `${escape(token.content)}`; - case 'code': - return `
${token.html}
`; - case 'inline-code': - return `${token.html}`; - case 'emoji': - return pictograph.dic[token.emoji] || token.content; - } - }).join(''); - - // Remove needless whitespaces - text = text - .replace(/ /g, '').replace(/<\/code> /g, '') - .replace(/
/g, '
').replace(/<\/code><\/pre>
/g, '
'); - - return text; -}; diff --git a/src/web/app/common/scripts/text-compiler.ts b/src/web/app/common/scripts/text-compiler.ts new file mode 100644 index 0000000000..8c65d6a068 --- /dev/null +++ b/src/web/app/common/scripts/text-compiler.ts @@ -0,0 +1,47 @@ +import * as riot from 'riot'; +import * as pictograph from 'pictograph'; +import CONFIG from './config'; + +const escape = text => + text + .replace(/>/g, '>') + .replace(/ { + if (shouldBreak == null) { + shouldBreak = true; + } + + const me = (riot as any).mixin('i').me; + + let text = tokens.map(token => { + switch (token.type) { + case 'text': + return escape(token.content) + .replace(/(\r\n|\n|\r)/g, shouldBreak ? '
' : ' '); + case 'bold': + return `${escape(token.bold)}`; + case 'url': + return ``; + case 'link': + return `${escape(token.title)}`; + case 'mention': + return `${token.content}`; + case 'hashtag': // TODO + return `${escape(token.content)}`; + case 'code': + return `
${token.html}
`; + case 'inline-code': + return `${token.html}`; + case 'emoji': + return pictograph.dic[token.emoji] || token.content; + } + }).join(''); + + // Remove needless whitespaces + text = text + .replace(/ /g, '').replace(/<\/code> /g, '') + .replace(/
/g, '
').replace(/<\/code><\/pre>
/g, '
'); + + return text; +}; -- cgit v1.2.3-freya