From 56c9a1ab41a208ca21a4fdd976599023d776b7b2 Mon Sep 17 00:00:00 2001 From: Jeremy Kauffman Date: Mon, 26 Aug 2019 17:31:17 -0400 Subject: [PATCH] fix metadata and add redirects add redirect, other fixes remove password clean up / final version clean up / final version --- .eslintrc.json | 6 +- config.js | 4 +- electron-builder.json | 2 +- package.json | 4 +- src/platforms/electron/createWindow.js | 2 +- src/platforms/web/server.js | 103 ++++++++++++++++++++++++- src/ui/page/settings/view.jsx | 2 +- static/index-electron.html | 12 +++ static/index-web.html | 12 +++ static/index.dev-electron.html | 18 ----- static/index.dev-web.html | 18 ----- static/index.html | 34 -------- webpack.electron.config.js | 6 +- webpack.web.config.js | 4 +- yarn.lock | 22 +++++- 15 files changed, 164 insertions(+), 85 deletions(-) create mode 100644 static/index-electron.html create mode 100644 static/index-web.html delete mode 100644 static/index.dev-electron.html delete mode 100644 static/index.dev-web.html delete mode 100644 static/index.html diff --git a/.eslintrc.json b/.eslintrc.json index dd7c42530..165343cec 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -39,7 +39,11 @@ "react/jsx-indent": 0, "react-hooks/exhaustive-deps": "warn", "react-hooks/rules-of-hooks": "error", - "space-before-function-paren": ["error", "never"], + "space-before-function-paren": ["error", { + "anonymous": "never", + "named": "never", + "asyncArrow": "always" + }], "standard/object-curly-even-spacing": 0, "standard/no-callback-literal": 0, "react/display-name": 0, diff --git a/config.js b/config.js index d7df27a70..15e164e16 100644 --- a/config.js +++ b/config.js @@ -1,6 +1,6 @@ const config = { - WEBPACK_WEB_PORT: 9090, // Also hardcoded in static/index.dev-web.html - WEBPACK_ELECTRON_PORT: 9091, // Also hardcoded in static/index.dev-electron.html + WEBPACK_WEB_PORT: 9090, + WEBPACK_ELECTRON_PORT: 9091, WEB_SERVER_PORT: 1337, DOMAIN: 'https://beta.lbry.tv', }; diff --git a/electron-builder.json b/electron-builder.json index 943c640f0..755411427 100644 --- a/electron-builder.json +++ b/electron-builder.json @@ -18,7 +18,7 @@ { "from": "dist/electron/static", "to": "./", - "filter": ["!dist/!electron/!static/index.html"] + "filter": ["!dist/!electron/!static/index-electron.html"] } ], "publish": [ diff --git a/package.json b/package.json index c4b0db386..1385d3b15 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,9 @@ "electron-updater": "^4.1.2", "express": "^4.16.4", "if-env": "^1.0.4", - "keytar": "^4.4.1" + "keytar": "^4.4.1", + "mysql": "^2.17.1", + "nodemon": "^1.19.1" }, "devDependencies": { "@babel/core": "^7.0.0", diff --git a/src/platforms/electron/createWindow.js b/src/platforms/electron/createWindow.js index b24be9c5a..dcb1b2543 100644 --- a/src/platforms/electron/createWindow.js +++ b/src/platforms/electron/createWindow.js @@ -37,7 +37,7 @@ export default appState => { }; const lbryProto = 'lbry://'; const lbryProtoQ = 'lbry://?'; - const rendererURL = isDev ? `http://localhost:${WEBPACK_ELECTRON_PORT}` : `file://${__dirname}/index.html`; + const rendererURL = isDev ? `http://localhost:${WEBPACK_ELECTRON_PORT}` : `file://${__dirname}/index-electron.html`; let window = new BrowserWindow(windowConfiguration); diff --git a/src/platforms/web/server.js b/src/platforms/web/server.js index 7b4e203e8..468093e03 100644 --- a/src/platforms/web/server.js +++ b/src/platforms/web/server.js @@ -1,12 +1,111 @@ +const { parseURI } = require('lbry-redux'); const { WEB_SERVER_PORT } = require('../../config'); +const { readFileSync } = require('fs'); const express = require('express'); const path = require('path'); const app = express(); +const mysql = require('mysql'); + +const pool = mysql.createPool({ + connectionLimit: 100, + host: 'chainquery.lbry.com', + user: 'lbrytv', + password: process.env.CHAINQUERY_MYSQL_PASSWORD, + database: 'chainquery', +}); + +const getClaim = (claimName, claimId, callback) => { + let params = [claimName]; + + let sql = + 'SELECT channel_claim.name as channel, claim.claim_id, claim.name, claim.description, claim.language, claim.thumbnail_url, claim.title ' + + 'FROM claim ' + + 'LEFT JOIN claim channel_claim on claim.publisher_id = channel_claim.claim_id ' + + 'WHERE claim.name = ?'; + + if (claimId) { + sql += ' AND claim.claim_id LIKE ?'; + params.push(claimId + '%'); + } else { + sql += ' AND claim.bid_state = "controlling"'; + } + + sql += ' LIMIT 1'; + + pool.query(sql, params, callback); +}; + app.use(express.static(__dirname)); -app.get('*', function(req, res) { - res.sendFile(path.join(__dirname, '/index.html')); +function truncateDescription(description) { + return description.length > 200 ? description.substr(0, 200) + '...' : description; +} + +function insertToHead(fullHtml, htmlToInsert) { + return fullHtml.replace('%%HEAD_TOKEN%%', htmlToInsert); +} + +const defaultHead = + 'lbry.tv\n' + + '\n' + + '\n' + + '\n' + + '\n' + + ''; + +app.get('*', async (req, res) => { + let html = readFileSync(path.join(__dirname, '/index-web.html'), 'utf8'); + const urlPath = req.path.substr(1); // trim leading slash + + if (urlPath.match(/^([^@/:]+)\/([^:/]+)$/)) { + return res.redirect(301, req.url.replace(/([^/:]+)\/([^:/]+)/, '$1:$2')); // test against urlPath, but use req.url to retain parameters + } + + if (urlPath.length > 0 && urlPath[0] !== '$') { + const { isChannel, streamName, channelName, channelClaimId, streamClaimId } = parseURI(urlPath.replace(/:/g, '#')); + const claimName = isChannel ? '@' + channelName : streamName; + const claimId = isChannel ? channelClaimId : streamClaimId; + + getClaim(claimName, claimId, (err, rows) => { + if (!err && rows && rows.length > 0) { + const claim = rows[0]; + const title = claim.title ? claim.title : claimName; + const claimDescription = + claim.description && claim.description.length > 0 + ? truncateDescription(claim.description) + : `Watch ${title} on LBRY.tv`; + const claimLanguage = claim.language || 'en_US'; + const claimThumbnail = claim.thumbnail_url || '/og.png'; + const claimTitle = + claim.channel && !isChannel ? `${title} from ${claim.channel} on LBRY.tv` : `${title} on LBRY.tv`; + + let head = ''; + + head += ''; + head += ``; + if (claim.tags) { + head += ``; + } + head += ``; + head += ``; + head += ``; + head += ``; + head += ``; + head += ``; + // below should be canonical_url, but not provided by chainquery yet + head += ``; + head += `${claimTitle}`; + + html = insertToHead(html, head); + } else { + html = insertToHead(html, defaultHead); + } + res.send(html); + }); + } else { + res.send(insertToHead(html, defaultHead)); + } }); app.listen(WEB_SERVER_PORT, () => console.log(`UI server listening at http://localhost:${WEB_SERVER_PORT}`)); // eslint-disable-line diff --git a/src/ui/page/settings/view.jsx b/src/ui/page/settings/view.jsx index 7a3e3a518..a5650c730 100644 --- a/src/ui/page/settings/view.jsx +++ b/src/ui/page/settings/view.jsx @@ -168,7 +168,7 @@ class SettingsPage extends React.PureComponent { }); const success = () => { this.setState({ clearingCache: false }); - window.location.href = 'index.html'; + window.location.reload(); }; const clear = () => this.props.clearCache().then(success); diff --git a/static/index-electron.html b/static/index-electron.html new file mode 100644 index 000000000..a8c304f8b --- /dev/null +++ b/static/index-electron.html @@ -0,0 +1,12 @@ + + + + + LBRY + + + +
+ + + diff --git a/static/index-web.html b/static/index-web.html new file mode 100644 index 000000000..f757cb3b8 --- /dev/null +++ b/static/index-web.html @@ -0,0 +1,12 @@ + + + + + %%HEAD_TOKEN%% + + + +
+ + + diff --git a/static/index.dev-electron.html b/static/index.dev-electron.html deleted file mode 100644 index d9761dba6..000000000 --- a/static/index.dev-electron.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - LBRY Dev - - - - -
- - - - - diff --git a/static/index.dev-web.html b/static/index.dev-web.html deleted file mode 100644 index f3d0265ec..000000000 --- a/static/index.dev-web.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - LBRY Dev - - - - -
- - - - - diff --git a/static/index.html b/static/index.html deleted file mode 100644 index 0a606a2ba..000000000 --- a/static/index.html +++ /dev/null @@ -1,34 +0,0 @@ - - - - - - LBRY - - - lbry.tv - - - - - - - - - -
- - - diff --git a/webpack.electron.config.js b/webpack.electron.config.js index 6553d2925..656a5d452 100644 --- a/webpack.electron.config.js +++ b/webpack.electron.config.js @@ -46,11 +46,11 @@ let mainConfig = { { from: `${STATIC_ROOT}/`, to: `${DIST_ROOT}/electron/static/`, - ignore: ['font/**/*', 'index.dev-web.html', 'index.html'], + ignore: ['font/**/*', 'index-electron.html'], }, { - from: ifProduction(`${STATIC_ROOT}/index.html`, `${STATIC_ROOT}/index.dev-electron.html`), - to: `${DIST_ROOT}/electron/static/index.html`, + from: `${STATIC_ROOT}/index-electron.html`, + to: `${DIST_ROOT}/electron/static/index-electron.html`, }, ]), ], diff --git a/webpack.web.config.js b/webpack.web.config.js index 18a0a6668..1a5b3530f 100644 --- a/webpack.web.config.js +++ b/webpack.web.config.js @@ -51,8 +51,8 @@ const webConfig = { plugins: [ new CopyWebpackPlugin([ { - from: `${STATIC_ROOT}/index.html`, - to: `${DIST_ROOT}/web/index.html`, + from: `${STATIC_ROOT}/index-web.html`, + to: `${DIST_ROOT}/web/index-web.html`, }, { from: `${STATIC_ROOT}/img/favicon.ico`, diff --git a/yarn.lock b/yarn.lock index e69e0f939..53f8cd913 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1852,6 +1852,11 @@ big.js@^5.2.2: resolved "https://registry.yarnpkg.com/big.js/-/big.js-5.2.2.tgz#65f0af382f578bcdc742bd9c281e9cb2d7768328" integrity sha512-vyL2OymJxmarO8gxMr0mhChsO9QGwhynfuu4+MHTAW6czfq9humCB7rKpUjDd9YUiDPU4mzpyupFSvOClAwbmQ== +bignumber.js@7.2.1: + version "7.2.1" + resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-7.2.1.tgz#80c048759d826800807c4bfd521e50edbba57a5f" + integrity sha512-S4XzBk5sMB+Rcb/LNcpzXr57VRTxgAvaAEDAl1AwRx27j00hT84O6OkteE7u8UB3NuaaygCRrEpqox4uDOrbdQ== + binary-extensions@^1.0.0: version "1.13.1" resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.13.1.tgz#598afe54755b2868a5330d2aff9d4ebb53209b65" @@ -7751,6 +7756,16 @@ mux.js@5.1.1: resolved "https://registry.yarnpkg.com/mux.js/-/mux.js-5.1.1.tgz#0e95f048b4ac51d413c9ddc2d78e4cefad8d06de" integrity sha512-Mf/UYmh5b8jvUP+jmrTbETnyFZprMdbT0RxKm/lJ/4d2Q3xdc5GaHaRPI1zVV5D3+6uxArVPm78QEb1RsrmaQw== +mysql@^2.17.1: + version "2.17.1" + resolved "https://registry.yarnpkg.com/mysql/-/mysql-2.17.1.tgz#62bba4a039a9b2f73638cd1652ce50fc6f682899" + integrity sha512-7vMqHQ673SAk5C8fOzTG2LpPcf3bNt0oL3sFpxPEEFp1mdlDcrLK0On7z8ZYKaaHrHwNcQ/MTUz7/oobZ2OyyA== + dependencies: + bignumber.js "7.2.1" + readable-stream "2.3.6" + safe-buffer "5.1.2" + sqlstring "2.3.1" + nan@2.13.2: version "2.13.2" resolved "https://registry.yarnpkg.com/nan/-/nan-2.13.2.tgz#f51dc7ae66ba7d5d55e1e6d4d8092e802c9aefe7" @@ -9953,7 +9968,7 @@ read-pkg@^4.0.1: parse-json "^4.0.0" pify "^3.0.0" -"readable-stream@1 || 2", readable-stream@^2.0.0, readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.1.5, readable-stream@^2.2.2, readable-stream@^2.3.0, readable-stream@^2.3.3, readable-stream@^2.3.5, readable-stream@^2.3.6, readable-stream@~2.3.6: +"readable-stream@1 || 2", readable-stream@2.3.6, readable-stream@^2.0.0, readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.1.5, readable-stream@^2.2.2, readable-stream@^2.3.0, readable-stream@^2.3.3, readable-stream@^2.3.5, readable-stream@^2.3.6, readable-stream@~2.3.6: version "2.3.6" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.6.tgz#b11c27d88b8ff1fbe070643cf94b0c79ae1b0aaf" integrity sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw== @@ -11017,6 +11032,11 @@ sprintf-js@~1.0.2: resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw= +sqlstring@2.3.1: + version "2.3.1" + resolved "https://registry.yarnpkg.com/sqlstring/-/sqlstring-2.3.1.tgz#475393ff9e91479aea62dcaf0ca3d14983a7fb40" + integrity sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A= + sshpk@^1.7.0: version "1.16.1" resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.16.1.tgz#fb661c0bef29b39db40769ee39fa70093d6f6877"