From ff99c865329623a99fa2180fd2389994afb78532 Mon Sep 17 00:00:00 2001 From: Michael V Date: Fri, 30 Nov 2018 19:23:33 +0100 Subject: [PATCH 1/8] Rename namespaces --- src/listeners/AddAssets.php | 8 ++++---- src/listeners/AddProfileViewHandler.php | 8 ++++---- src/listeners/AddUserApiAttributes.php | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/listeners/AddAssets.php b/src/listeners/AddAssets.php index eebfc10..ceb1ad3 100644 --- a/src/listeners/AddAssets.php +++ b/src/listeners/AddAssets.php @@ -4,7 +4,7 @@ namespace michaelbelgium\profileviews\listeners; use Flarum\Event\ConfigureLocales; -use Flarum\Event\ConfigureWebApp; +use Flarum\Frontend\Event\Rendering; use Illuminate\Contracts\Events\Dispatcher; use DirectoryIterator; @@ -15,14 +15,14 @@ class AddAssets */ public function subscribe(Dispatcher $events) { - $events->listen(ConfigureWebApp::class, [$this, 'configAssets']); + $events->listen(Rendering::class, [$this, 'configAssets']); $events->listen(ConfigureLocales::class, [$this, 'configLocales']); } /** - * @param ConfigureWebApp $event + * @param Rendering $event */ - public function configAssets(ConfigureWebApp $event) + public function configAssets(Rendering $event) { if($event->isForum()) { diff --git a/src/listeners/AddProfileViewHandler.php b/src/listeners/AddProfileViewHandler.php index b8e91e3..d2d070e 100644 --- a/src/listeners/AddProfileViewHandler.php +++ b/src/listeners/AddProfileViewHandler.php @@ -2,19 +2,19 @@ namespace michaelbelgium\profileviews\listeners; use Flarum\Api\Controller\ShowUserController; -use Flarum\Event\PrepareApiData; +use Flarum\Api\Event\WillSerializeData; use Illuminate\Contracts\Events\Dispatcher; -use Flarum\Core\User; +use Flarum\User\User; use Illuminate\Support\Facades\DB; class AddProfileViewHandler { public function subscribe(Dispatcher $events) { - $events->listen(PrepareApiData::class, [$this, "confViews"]); + $events->listen(WillSerializeData::class, [$this, "confViews"]); } - public function confViews(PrepareApiData $event) + public function confViews(WillSerializeData $event) { if($event->isController(ShowUserController::class)) { diff --git a/src/listeners/AddUserApiAttributes.php b/src/listeners/AddUserApiAttributes.php index 83bbd03..2505b71 100644 --- a/src/listeners/AddUserApiAttributes.php +++ b/src/listeners/AddUserApiAttributes.php @@ -2,7 +2,7 @@ namespace michaelbelgium\profileviews\listeners; use Flarum\Api\Serializer\UserSerializer; -use Flarum\Event\PrepareApiAttributes; +use Flarum\Api\Event\Serializing; use Illuminate\Contracts\Events\Dispatcher; class AddUserApiAttributes @@ -12,13 +12,13 @@ class AddUserApiAttributes */ public function subscribe(Dispatcher $events) { - $events->listen(PrepareApiAttributes::class, [$this, 'addApiAttributes']); + $events->listen(Serializing::class, [$this, 'addApiAttributes']); } /** - * @param PrepareApiAttributes $event + * @param Serializing $event */ - public function addApiAttributes(PrepareApiAttributes $event) + public function addApiAttributes(Serializing $event) { if ($event->isSerializer(UserSerializer::class)) $event->attributes['views'] = $event->model->views; From ac6201afdbe3b41f323e1ee03a135770367fa52b Mon Sep 17 00:00:00 2001 From: Michael V Date: Fri, 30 Nov 2018 19:43:58 +0100 Subject: [PATCH 2/8] Update js side for beta 8 + composer --- bootstrap.php | 10 ----- composer.json | 4 +- extend.php | 19 +++++++++ js/dist/forum.js | 2 + js/dist/forum.js.map | 1 + js/forum.js | 1 + js/forum/Gulpfile.js | 7 --- js/forum/dist/extension.js | 40 ----------------- js/forum/package.json | 7 --- js/package.json | 13 ++++++ js/{forum/src/main.js => src/forum/index.js} | 2 +- js/webpack.config.js | 3 ++ src/listeners/AddAssets.php | 45 -------------------- 13 files changed, 42 insertions(+), 112 deletions(-) delete mode 100644 bootstrap.php create mode 100644 extend.php create mode 100644 js/dist/forum.js create mode 100644 js/dist/forum.js.map create mode 100644 js/forum.js delete mode 100644 js/forum/Gulpfile.js delete mode 100644 js/forum/dist/extension.js delete mode 100644 js/forum/package.json create mode 100644 js/package.json rename js/{forum/src/main.js => src/forum/index.js} (94%) create mode 100644 js/webpack.config.js delete mode 100644 src/listeners/AddAssets.php diff --git a/bootstrap.php b/bootstrap.php deleted file mode 100644 index a845607..0000000 --- a/bootstrap.php +++ /dev/null @@ -1,10 +0,0 @@ -subscribe(listeners\AddProfileViewHandler::class); - $events->subscribe(listeners\AddUserApiAttributes::class); - $events->subscribe(listeners\AddAssets::class); -}; \ No newline at end of file diff --git a/composer.json b/composer.json index 2d6ba42..b42ed12 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Add views to profiles", "type": "flarum-extension", "require": { - "flarum/core": "^0.1.0-beta.7" + "flarum/core": "^0.1.0-beta.8" }, "license": "MIT", "authors": [ @@ -25,7 +25,7 @@ "flarum-extension": { "title": "Profile views", "icon": { - "name": "eye", + "name": "far fa-eye", "backgroundColor": "#90C3D4", "color": "#fff" } diff --git a/extend.php b/extend.php new file mode 100644 index 0000000..436406c --- /dev/null +++ b/extend.php @@ -0,0 +1,19 @@ +js(__DIR__. '/js/dist/forum.js'), + + new Locales(__DIR__ . '/locale'), + + function (Dispatcher $events) { + $events->subscribe(listeners\AddProfileViewHandler::class); + $events->subscribe(listeners\AddUserApiAttributes::class); + } +]; \ No newline at end of file diff --git a/js/dist/forum.js b/js/dist/forum.js new file mode 100644 index 0000000..33fb87e --- /dev/null +++ b/js/dist/forum.js @@ -0,0 +1,2 @@ +module.exports=function(e){var t={};function r(o){if(t[o])return t[o].exports;var n=t[o]={i:o,l:!1,exports:{}};return e[o].call(n.exports,n,n.exports,r),n.l=!0,n.exports}return r.m=e,r.c=t,r.d=function(e,t,o){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(r.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)r.d(o,n,function(t){return e[t]}.bind(null,n));return o},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=6)}([function(e,t){e.exports=flarum.core.compat.app},function(e,t){e.exports=flarum.core.compat["models/User"]},function(e,t){e.exports=flarum.core.compat["components/UserCard"]},function(e,t){e.exports=flarum.core.compat["helpers/icon"]},function(e,t){e.exports=flarum.core.compat.Model},function(e,t){e.exports=flarum.core.compat.extend},function(e,t,r){"use strict";r.r(t);var o=r(0),n=r.n(o),u=r(1),i=r.n(u),a=r(2),c=r.n(a),f=r(3),l=r.n(f),p=r(4),s=r.n(p),d=r(5);n.a.initializers.add("michaelbelgium-flarum-profile-views",function(){i.a.prototype.views=s.a.attribute("views"),Object(d.extend)(c.a.prototype,"infoItems",function(e){var t=this.props.user;e.add("profile-views",m("span",null,l()("far fa-eye")," ",n.a.translator.trans("flarum_profile_views.forum.user.views_count_text",{viewcount:t.views()})))})})}]); +//# sourceMappingURL=forum.js.map \ No newline at end of file diff --git a/js/dist/forum.js.map b/js/dist/forum.js.map new file mode 100644 index 0000000..478e11d --- /dev/null +++ b/js/dist/forum.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["webpack://@michaelbelgium/flarum-profile-views/webpack/bootstrap","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['app']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['models/User']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['components/UserCard']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['helpers/icon']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['Model']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['extend']\"","webpack://@michaelbelgium/flarum-profile-views/./src/forum/index.js"],"names":["installedModules","__webpack_require__","moduleId","exports","module","i","l","modules","call","m","c","d","name","getter","o","Object","defineProperty","enumerable","get","r","Symbol","toStringTag","value","t","mode","__esModule","ns","create","key","bind","n","object","property","prototype","hasOwnProperty","p","s","flarum","core","compat","app","initializers","add","User","views","Model","attribute","extend","UserCard","items","user","this","props","icon","translator","trans","viewcount"],"mappings":"2BACA,IAAAA,EAAA,GAGA,SAAAC,EAAAC,GAGA,GAAAF,EAAAE,GACA,OAAAF,EAAAE,GAAAC,QAGA,IAAAC,EAAAJ,EAAAE,GAAA,CACAG,EAAAH,EACAI,GAAA,EACAH,QAAA,IAUA,OANAI,EAAAL,GAAAM,KAAAJ,EAAAD,QAAAC,IAAAD,QAAAF,GAGAG,EAAAE,GAAA,EAGAF,EAAAD,QA0DA,OArDAF,EAAAQ,EAAAF,EAGAN,EAAAS,EAAAV,EAGAC,EAAAU,EAAA,SAAAR,EAAAS,EAAAC,GACAZ,EAAAa,EAAAX,EAAAS,IACAG,OAAAC,eAAAb,EAAAS,EAAA,CAA0CK,YAAA,EAAAC,IAAAL,KAK1CZ,EAAAkB,EAAA,SAAAhB,GACA,oBAAAiB,eAAAC,aACAN,OAAAC,eAAAb,EAAAiB,OAAAC,YAAA,CAAwDC,MAAA,WAExDP,OAAAC,eAAAb,EAAA,cAAiDmB,OAAA,KAQjDrB,EAAAsB,EAAA,SAAAD,EAAAE,GAEA,GADA,EAAAA,IAAAF,EAAArB,EAAAqB,IACA,EAAAE,EAAA,OAAAF,EACA,KAAAE,GAAA,iBAAAF,QAAAG,WAAA,OAAAH,EACA,IAAAI,EAAAX,OAAAY,OAAA,MAGA,GAFA1B,EAAAkB,EAAAO,GACAX,OAAAC,eAAAU,EAAA,WAAyCT,YAAA,EAAAK,UACzC,EAAAE,GAAA,iBAAAF,EAAA,QAAAM,KAAAN,EAAArB,EAAAU,EAAAe,EAAAE,EAAA,SAAAA,GAAgH,OAAAN,EAAAM,IAAqBC,KAAA,KAAAD,IACrI,OAAAF,GAIAzB,EAAA6B,EAAA,SAAA1B,GACA,IAAAS,EAAAT,KAAAqB,WACA,WAA2B,OAAArB,EAAA,SAC3B,WAAiC,OAAAA,GAEjC,OADAH,EAAAU,EAAAE,EAAA,IAAAA,GACAA,GAIAZ,EAAAa,EAAA,SAAAiB,EAAAC,GAAsD,OAAAjB,OAAAkB,UAAAC,eAAA1B,KAAAuB,EAAAC,IAGtD/B,EAAAkC,EAAA,GAIAlC,IAAAmC,EAAA,mBClFAhC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,mBCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,8BCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,sCCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,+BCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,qBCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,uICOAC,IAAIC,aAAaC,IAAI,sCAAuC,WACxDC,IAAKV,UAAUW,MAAQC,IAAMC,UAAU,SAEvCC,iBAAOC,IAASf,UAAW,YAAa,SAASgB,GAC7C,IAAMC,EAAOC,KAAKC,MAAMF,KAExBD,EAAMP,IAAI,gBACNjC,EAAA,YACK4C,IAAK,cACL,IACAb,IAAIc,WAAWC,MAAM,mDAAoD,CAACC,UAAWN,EAAKN","file":"forum.js","sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, { enumerable: true, get: getter });\n \t\t}\n \t};\n\n \t// define __esModule on exports\n \t__webpack_require__.r = function(exports) {\n \t\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n \t\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n \t\t}\n \t\tObject.defineProperty(exports, '__esModule', { value: true });\n \t};\n\n \t// create a fake namespace object\n \t// mode & 1: value is a module id, require it\n \t// mode & 2: merge all properties of value into the ns\n \t// mode & 4: return value when already ns object\n \t// mode & 8|1: behave like require\n \t__webpack_require__.t = function(value, mode) {\n \t\tif(mode & 1) value = __webpack_require__(value);\n \t\tif(mode & 8) return value;\n \t\tif((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;\n \t\tvar ns = Object.create(null);\n \t\t__webpack_require__.r(ns);\n \t\tObject.defineProperty(ns, 'default', { enumerable: true, value: value });\n \t\tif(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));\n \t\treturn ns;\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 6);\n","module.exports = flarum.core.compat['app'];","module.exports = flarum.core.compat['models/User'];","module.exports = flarum.core.compat['components/UserCard'];","module.exports = flarum.core.compat['helpers/icon'];","module.exports = flarum.core.compat['Model'];","module.exports = flarum.core.compat['extend'];","import app from 'flarum/app';\r\nimport User from 'flarum/models/User';\r\nimport UserCard from 'flarum/components/UserCard';\r\nimport icon from 'flarum/helpers/icon';\r\nimport Model from 'flarum/Model';\r\nimport { extend } from 'flarum/extend';\r\n\r\napp.initializers.add('michaelbelgium-flarum-profile-views', function() {\r\n User.prototype.views = Model.attribute('views');\r\n\r\n extend(UserCard.prototype, 'infoItems', function(items) {\r\n const user = this.props.user;\r\n\r\n items.add('profile-views',(\r\n \r\n {icon('far fa-eye')}\r\n {' '}\r\n {app.translator.trans('flarum_profile_views.forum.user.views_count_text', {viewcount: user.views()})}\r\n \r\n ));\r\n });\r\n});"],"sourceRoot":""} \ No newline at end of file diff --git a/js/forum.js b/js/forum.js new file mode 100644 index 0000000..d6c455c --- /dev/null +++ b/js/forum.js @@ -0,0 +1 @@ +export * from './src/forum'; \ No newline at end of file diff --git a/js/forum/Gulpfile.js b/js/forum/Gulpfile.js deleted file mode 100644 index f264b65..0000000 --- a/js/forum/Gulpfile.js +++ /dev/null @@ -1,7 +0,0 @@ -var flarum = require('flarum-gulp'); - -flarum({ - modules: { - 'michaelbelgium/flarum-profile-views': 'src/**/*.js' - } -}); \ No newline at end of file diff --git a/js/forum/dist/extension.js b/js/forum/dist/extension.js deleted file mode 100644 index eb21d6d..0000000 --- a/js/forum/dist/extension.js +++ /dev/null @@ -1,40 +0,0 @@ -'use strict'; - -System.register('michaelbelgium/flarum-profile-views/main', ['flarum/app', 'flarum/models/User', 'flarum/components/UserCard', 'flarum/helpers/icon', 'flarum/Model', 'flarum/extend'], function (_export, _context) { - "use strict"; - - var app, User, UserCard, icon, Model, extend; - return { - setters: [function (_flarumApp) { - app = _flarumApp.default; - }, function (_flarumModelsUser) { - User = _flarumModelsUser.default; - }, function (_flarumComponentsUserCard) { - UserCard = _flarumComponentsUserCard.default; - }, function (_flarumHelpersIcon) { - icon = _flarumHelpersIcon.default; - }, function (_flarumModel) { - Model = _flarumModel.default; - }, function (_flarumExtend) { - extend = _flarumExtend.extend; - }], - execute: function () { - - app.initializers.add('michaelbelgium-flarum-profile-views', function () { - User.prototype.views = Model.attribute('views'); - - extend(UserCard.prototype, 'infoItems', function (items) { - var user = this.props.user; - - items.add('profile-views', m( - 'span', - null, - icon('eye'), - ' ', - app.translator.trans('flarum_profile_views.forum.user.views_count_text', { viewcount: user.views() }) - )); - }); - }); - } - }; -}); \ No newline at end of file diff --git a/js/forum/package.json b/js/forum/package.json deleted file mode 100644 index 6216d68..0000000 --- a/js/forum/package.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "private": true, - "devDependencies": { - "flarum-gulp": "^0.2.0", - "gulp": "^3.9.1" - } -} diff --git a/js/package.json b/js/package.json new file mode 100644 index 0000000..648b6dc --- /dev/null +++ b/js/package.json @@ -0,0 +1,13 @@ +{ + "private": true, + "name": "@michaelbelgium/flarum-profile-views", + "dependencies": { + "flarum-webpack-config": "0.1.0-beta.10", + "webpack": "^4.0.0", + "webpack-cli": "^3.0.7" + }, + "scripts": { + "dev": "webpack --mode development --watch", + "build": "webpack --mode production" + } +} diff --git a/js/forum/src/main.js b/js/src/forum/index.js similarity index 94% rename from js/forum/src/main.js rename to js/src/forum/index.js index afc2a10..aa161a4 100644 --- a/js/forum/src/main.js +++ b/js/src/forum/index.js @@ -13,7 +13,7 @@ app.initializers.add('michaelbelgium-flarum-profile-views', function() { items.add('profile-views',( - {icon('eye')} + {icon('far fa-eye')} {' '} {app.translator.trans('flarum_profile_views.forum.user.views_count_text', {viewcount: user.views()})} diff --git a/js/webpack.config.js b/js/webpack.config.js new file mode 100644 index 0000000..fa68b09 --- /dev/null +++ b/js/webpack.config.js @@ -0,0 +1,3 @@ +const config = require('flarum-webpack-config'); + +module.exports = config(); \ No newline at end of file diff --git a/src/listeners/AddAssets.php b/src/listeners/AddAssets.php deleted file mode 100644 index ceb1ad3..0000000 --- a/src/listeners/AddAssets.php +++ /dev/null @@ -1,45 +0,0 @@ -listen(Rendering::class, [$this, 'configAssets']); - $events->listen(ConfigureLocales::class, [$this, 'configLocales']); - } - - /** - * @param Rendering $event - */ - public function configAssets(Rendering $event) - { - if($event->isForum()) - { - $event->addAssets(__DIR__.'/../../js/forum/dist/extension.js'); - $event->addBootstrapper('michaelbelgium/flarum-profile-views/main'); - } - } - - /** - * @param ConfigureLocales $event - */ - public function configLocales(ConfigureLocales $event) - { - foreach (new DirectoryIterator(__DIR__.'/../../locale') as $file) { - if ($file->isFile() && in_array($file->getExtension(), ['yml', 'yaml'], false)) { - $event->locales->addTranslations($file->getBasename('.'.$file->getExtension()), $file->getPathname()); - } - } - } -} \ No newline at end of file From 0b4935a851d85d407caaab449b4e3c9ed382ea57 Mon Sep 17 00:00:00 2001 From: Michael V Date: Fri, 30 Nov 2018 20:04:57 +0100 Subject: [PATCH 3/8] Fix the number zero not displaying ?! --- js/dist/forum.js | 2 +- js/dist/forum.js.map | 2 +- js/src/forum/index.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/js/dist/forum.js b/js/dist/forum.js index 33fb87e..2d23c6a 100644 --- a/js/dist/forum.js +++ b/js/dist/forum.js @@ -1,2 +1,2 @@ -module.exports=function(e){var t={};function r(o){if(t[o])return t[o].exports;var n=t[o]={i:o,l:!1,exports:{}};return e[o].call(n.exports,n,n.exports,r),n.l=!0,n.exports}return r.m=e,r.c=t,r.d=function(e,t,o){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(r.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)r.d(o,n,function(t){return e[t]}.bind(null,n));return o},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=6)}([function(e,t){e.exports=flarum.core.compat.app},function(e,t){e.exports=flarum.core.compat["models/User"]},function(e,t){e.exports=flarum.core.compat["components/UserCard"]},function(e,t){e.exports=flarum.core.compat["helpers/icon"]},function(e,t){e.exports=flarum.core.compat.Model},function(e,t){e.exports=flarum.core.compat.extend},function(e,t,r){"use strict";r.r(t);var o=r(0),n=r.n(o),u=r(1),i=r.n(u),a=r(2),c=r.n(a),f=r(3),l=r.n(f),p=r(4),s=r.n(p),d=r(5);n.a.initializers.add("michaelbelgium-flarum-profile-views",function(){i.a.prototype.views=s.a.attribute("views"),Object(d.extend)(c.a.prototype,"infoItems",function(e){var t=this.props.user;e.add("profile-views",m("span",null,l()("far fa-eye")," ",n.a.translator.trans("flarum_profile_views.forum.user.views_count_text",{viewcount:t.views()})))})})}]); +module.exports=function(e){var t={};function r(o){if(t[o])return t[o].exports;var n=t[o]={i:o,l:!1,exports:{}};return e[o].call(n.exports,n,n.exports,r),n.l=!0,n.exports}return r.m=e,r.c=t,r.d=function(e,t,o){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(r.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)r.d(o,n,function(t){return e[t]}.bind(null,n));return o},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=6)}([function(e,t){e.exports=flarum.core.compat.app},function(e,t){e.exports=flarum.core.compat["models/User"]},function(e,t){e.exports=flarum.core.compat["components/UserCard"]},function(e,t){e.exports=flarum.core.compat["helpers/icon"]},function(e,t){e.exports=flarum.core.compat.Model},function(e,t){e.exports=flarum.core.compat.extend},function(e,t,r){"use strict";r.r(t);var o=r(0),n=r.n(o),u=r(1),i=r.n(u),a=r(2),c=r.n(a),f=r(3),l=r.n(f),p=r(4),s=r.n(p),d=r(5);n.a.initializers.add("michaelbelgium-flarum-profile-views",function(){i.a.prototype.views=s.a.attribute("views"),Object(d.extend)(c.a.prototype,"infoItems",function(e){var t=this.props.user;e.add("profile-views",m("span",null,l()("far fa-eye")," ",n.a.translator.trans("flarum_profile_views.forum.user.views_count_text",{viewcount:0==t.views()?"0":t.views()})))})})}]); //# sourceMappingURL=forum.js.map \ No newline at end of file diff --git a/js/dist/forum.js.map b/js/dist/forum.js.map index 478e11d..2f816bc 100644 --- a/js/dist/forum.js.map +++ b/js/dist/forum.js.map @@ -1 +1 @@ -{"version":3,"sources":["webpack://@michaelbelgium/flarum-profile-views/webpack/bootstrap","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['app']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['models/User']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['components/UserCard']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['helpers/icon']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['Model']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['extend']\"","webpack://@michaelbelgium/flarum-profile-views/./src/forum/index.js"],"names":["installedModules","__webpack_require__","moduleId","exports","module","i","l","modules","call","m","c","d","name","getter","o","Object","defineProperty","enumerable","get","r","Symbol","toStringTag","value","t","mode","__esModule","ns","create","key","bind","n","object","property","prototype","hasOwnProperty","p","s","flarum","core","compat","app","initializers","add","User","views","Model","attribute","extend","UserCard","items","user","this","props","icon","translator","trans","viewcount"],"mappings":"2BACA,IAAAA,EAAA,GAGA,SAAAC,EAAAC,GAGA,GAAAF,EAAAE,GACA,OAAAF,EAAAE,GAAAC,QAGA,IAAAC,EAAAJ,EAAAE,GAAA,CACAG,EAAAH,EACAI,GAAA,EACAH,QAAA,IAUA,OANAI,EAAAL,GAAAM,KAAAJ,EAAAD,QAAAC,IAAAD,QAAAF,GAGAG,EAAAE,GAAA,EAGAF,EAAAD,QA0DA,OArDAF,EAAAQ,EAAAF,EAGAN,EAAAS,EAAAV,EAGAC,EAAAU,EAAA,SAAAR,EAAAS,EAAAC,GACAZ,EAAAa,EAAAX,EAAAS,IACAG,OAAAC,eAAAb,EAAAS,EAAA,CAA0CK,YAAA,EAAAC,IAAAL,KAK1CZ,EAAAkB,EAAA,SAAAhB,GACA,oBAAAiB,eAAAC,aACAN,OAAAC,eAAAb,EAAAiB,OAAAC,YAAA,CAAwDC,MAAA,WAExDP,OAAAC,eAAAb,EAAA,cAAiDmB,OAAA,KAQjDrB,EAAAsB,EAAA,SAAAD,EAAAE,GAEA,GADA,EAAAA,IAAAF,EAAArB,EAAAqB,IACA,EAAAE,EAAA,OAAAF,EACA,KAAAE,GAAA,iBAAAF,QAAAG,WAAA,OAAAH,EACA,IAAAI,EAAAX,OAAAY,OAAA,MAGA,GAFA1B,EAAAkB,EAAAO,GACAX,OAAAC,eAAAU,EAAA,WAAyCT,YAAA,EAAAK,UACzC,EAAAE,GAAA,iBAAAF,EAAA,QAAAM,KAAAN,EAAArB,EAAAU,EAAAe,EAAAE,EAAA,SAAAA,GAAgH,OAAAN,EAAAM,IAAqBC,KAAA,KAAAD,IACrI,OAAAF,GAIAzB,EAAA6B,EAAA,SAAA1B,GACA,IAAAS,EAAAT,KAAAqB,WACA,WAA2B,OAAArB,EAAA,SAC3B,WAAiC,OAAAA,GAEjC,OADAH,EAAAU,EAAAE,EAAA,IAAAA,GACAA,GAIAZ,EAAAa,EAAA,SAAAiB,EAAAC,GAAsD,OAAAjB,OAAAkB,UAAAC,eAAA1B,KAAAuB,EAAAC,IAGtD/B,EAAAkC,EAAA,GAIAlC,IAAAmC,EAAA,mBClFAhC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,mBCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,8BCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,sCCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,+BCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,qBCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,uICOAC,IAAIC,aAAaC,IAAI,sCAAuC,WACxDC,IAAKV,UAAUW,MAAQC,IAAMC,UAAU,SAEvCC,iBAAOC,IAASf,UAAW,YAAa,SAASgB,GAC7C,IAAMC,EAAOC,KAAKC,MAAMF,KAExBD,EAAMP,IAAI,gBACNjC,EAAA,YACK4C,IAAK,cACL,IACAb,IAAIc,WAAWC,MAAM,mDAAoD,CAACC,UAAWN,EAAKN","file":"forum.js","sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, { enumerable: true, get: getter });\n \t\t}\n \t};\n\n \t// define __esModule on exports\n \t__webpack_require__.r = function(exports) {\n \t\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n \t\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n \t\t}\n \t\tObject.defineProperty(exports, '__esModule', { value: true });\n \t};\n\n \t// create a fake namespace object\n \t// mode & 1: value is a module id, require it\n \t// mode & 2: merge all properties of value into the ns\n \t// mode & 4: return value when already ns object\n \t// mode & 8|1: behave like require\n \t__webpack_require__.t = function(value, mode) {\n \t\tif(mode & 1) value = __webpack_require__(value);\n \t\tif(mode & 8) return value;\n \t\tif((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;\n \t\tvar ns = Object.create(null);\n \t\t__webpack_require__.r(ns);\n \t\tObject.defineProperty(ns, 'default', { enumerable: true, value: value });\n \t\tif(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));\n \t\treturn ns;\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 6);\n","module.exports = flarum.core.compat['app'];","module.exports = flarum.core.compat['models/User'];","module.exports = flarum.core.compat['components/UserCard'];","module.exports = flarum.core.compat['helpers/icon'];","module.exports = flarum.core.compat['Model'];","module.exports = flarum.core.compat['extend'];","import app from 'flarum/app';\r\nimport User from 'flarum/models/User';\r\nimport UserCard from 'flarum/components/UserCard';\r\nimport icon from 'flarum/helpers/icon';\r\nimport Model from 'flarum/Model';\r\nimport { extend } from 'flarum/extend';\r\n\r\napp.initializers.add('michaelbelgium-flarum-profile-views', function() {\r\n User.prototype.views = Model.attribute('views');\r\n\r\n extend(UserCard.prototype, 'infoItems', function(items) {\r\n const user = this.props.user;\r\n\r\n items.add('profile-views',(\r\n \r\n {icon('far fa-eye')}\r\n {' '}\r\n {app.translator.trans('flarum_profile_views.forum.user.views_count_text', {viewcount: user.views()})}\r\n \r\n ));\r\n });\r\n});"],"sourceRoot":""} \ No newline at end of file +{"version":3,"sources":["webpack://@michaelbelgium/flarum-profile-views/webpack/bootstrap","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['app']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['models/User']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['components/UserCard']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['helpers/icon']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['Model']\"","webpack://@michaelbelgium/flarum-profile-views/external \"flarum.core.compat['extend']\"","webpack://@michaelbelgium/flarum-profile-views/./src/forum/index.js"],"names":["installedModules","__webpack_require__","moduleId","exports","module","i","l","modules","call","m","c","d","name","getter","o","Object","defineProperty","enumerable","get","r","Symbol","toStringTag","value","t","mode","__esModule","ns","create","key","bind","n","object","property","prototype","hasOwnProperty","p","s","flarum","core","compat","app","initializers","add","User","views","Model","attribute","extend","UserCard","items","user","this","props","icon","translator","trans","viewcount"],"mappings":"2BACA,IAAAA,EAAA,GAGA,SAAAC,EAAAC,GAGA,GAAAF,EAAAE,GACA,OAAAF,EAAAE,GAAAC,QAGA,IAAAC,EAAAJ,EAAAE,GAAA,CACAG,EAAAH,EACAI,GAAA,EACAH,QAAA,IAUA,OANAI,EAAAL,GAAAM,KAAAJ,EAAAD,QAAAC,IAAAD,QAAAF,GAGAG,EAAAE,GAAA,EAGAF,EAAAD,QA0DA,OArDAF,EAAAQ,EAAAF,EAGAN,EAAAS,EAAAV,EAGAC,EAAAU,EAAA,SAAAR,EAAAS,EAAAC,GACAZ,EAAAa,EAAAX,EAAAS,IACAG,OAAAC,eAAAb,EAAAS,EAAA,CAA0CK,YAAA,EAAAC,IAAAL,KAK1CZ,EAAAkB,EAAA,SAAAhB,GACA,oBAAAiB,eAAAC,aACAN,OAAAC,eAAAb,EAAAiB,OAAAC,YAAA,CAAwDC,MAAA,WAExDP,OAAAC,eAAAb,EAAA,cAAiDmB,OAAA,KAQjDrB,EAAAsB,EAAA,SAAAD,EAAAE,GAEA,GADA,EAAAA,IAAAF,EAAArB,EAAAqB,IACA,EAAAE,EAAA,OAAAF,EACA,KAAAE,GAAA,iBAAAF,QAAAG,WAAA,OAAAH,EACA,IAAAI,EAAAX,OAAAY,OAAA,MAGA,GAFA1B,EAAAkB,EAAAO,GACAX,OAAAC,eAAAU,EAAA,WAAyCT,YAAA,EAAAK,UACzC,EAAAE,GAAA,iBAAAF,EAAA,QAAAM,KAAAN,EAAArB,EAAAU,EAAAe,EAAAE,EAAA,SAAAA,GAAgH,OAAAN,EAAAM,IAAqBC,KAAA,KAAAD,IACrI,OAAAF,GAIAzB,EAAA6B,EAAA,SAAA1B,GACA,IAAAS,EAAAT,KAAAqB,WACA,WAA2B,OAAArB,EAAA,SAC3B,WAAiC,OAAAA,GAEjC,OADAH,EAAAU,EAAAE,EAAA,IAAAA,GACAA,GAIAZ,EAAAa,EAAA,SAAAiB,EAAAC,GAAsD,OAAAjB,OAAAkB,UAAAC,eAAA1B,KAAAuB,EAAAC,IAGtD/B,EAAAkC,EAAA,GAIAlC,IAAAmC,EAAA,mBClFAhC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,mBCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,8BCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,sCCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,+BCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,qBCAAnC,EAAAD,QAAAkC,OAAAC,KAAAC,OAAA,uICOAC,IAAIC,aAAaC,IAAI,sCAAuC,WACxDC,IAAKV,UAAUW,MAAQC,IAAMC,UAAU,SAEvCC,iBAAOC,IAASf,UAAW,YAAa,SAASgB,GAC7C,IAAMC,EAAOC,KAAKC,MAAMF,KAExBD,EAAMP,IAAI,gBACNjC,EAAA,YACK4C,IAAK,cACL,IACAb,IAAIc,WAAWC,MAAM,mDAAoD,CAACC,UAA2B,GAAhBN,EAAKN,QAAe,IAAMM,EAAKN","file":"forum.js","sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, { enumerable: true, get: getter });\n \t\t}\n \t};\n\n \t// define __esModule on exports\n \t__webpack_require__.r = function(exports) {\n \t\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n \t\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n \t\t}\n \t\tObject.defineProperty(exports, '__esModule', { value: true });\n \t};\n\n \t// create a fake namespace object\n \t// mode & 1: value is a module id, require it\n \t// mode & 2: merge all properties of value into the ns\n \t// mode & 4: return value when already ns object\n \t// mode & 8|1: behave like require\n \t__webpack_require__.t = function(value, mode) {\n \t\tif(mode & 1) value = __webpack_require__(value);\n \t\tif(mode & 8) return value;\n \t\tif((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;\n \t\tvar ns = Object.create(null);\n \t\t__webpack_require__.r(ns);\n \t\tObject.defineProperty(ns, 'default', { enumerable: true, value: value });\n \t\tif(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));\n \t\treturn ns;\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 6);\n","module.exports = flarum.core.compat['app'];","module.exports = flarum.core.compat['models/User'];","module.exports = flarum.core.compat['components/UserCard'];","module.exports = flarum.core.compat['helpers/icon'];","module.exports = flarum.core.compat['Model'];","module.exports = flarum.core.compat['extend'];","import app from 'flarum/app';\r\nimport User from 'flarum/models/User';\r\nimport UserCard from 'flarum/components/UserCard';\r\nimport icon from 'flarum/helpers/icon';\r\nimport Model from 'flarum/Model';\r\nimport { extend } from 'flarum/extend';\r\n\r\napp.initializers.add('michaelbelgium-flarum-profile-views', function() {\r\n User.prototype.views = Model.attribute('views');\r\n\r\n extend(UserCard.prototype, 'infoItems', function(items) {\r\n const user = this.props.user;\r\n\r\n items.add('profile-views',(\r\n \r\n {icon('far fa-eye')}\r\n {' '}\r\n {app.translator.trans('flarum_profile_views.forum.user.views_count_text', {viewcount: user.views() == 0 ? '0' : user.views()})}\r\n \r\n ));\r\n });\r\n});"],"sourceRoot":""} \ No newline at end of file diff --git a/js/src/forum/index.js b/js/src/forum/index.js index aa161a4..f422188 100644 --- a/js/src/forum/index.js +++ b/js/src/forum/index.js @@ -15,7 +15,7 @@ app.initializers.add('michaelbelgium-flarum-profile-views', function() { {icon('far fa-eye')} {' '} - {app.translator.trans('flarum_profile_views.forum.user.views_count_text', {viewcount: user.views()})} + {app.translator.trans('flarum_profile_views.forum.user.views_count_text', {viewcount: user.views() == 0 ? '0' : user.views()})} )); }); From 1183c40c504370d72c0dc933330a5357a49868a8 Mon Sep 17 00:00:00 2001 From: Michael V Date: Sat, 1 Dec 2018 10:54:00 +0100 Subject: [PATCH 4/8] Rename views to view_count --- migrations/2018_11_30_190115_users_rename_views.php | 5 +++++ src/listeners/AddProfileViewHandler.php | 2 +- src/listeners/AddUserApiAttributes.php | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 migrations/2018_11_30_190115_users_rename_views.php diff --git a/migrations/2018_11_30_190115_users_rename_views.php b/migrations/2018_11_30_190115_users_rename_views.php new file mode 100644 index 0000000..a732797 --- /dev/null +++ b/migrations/2018_11_30_190115_users_rename_views.php @@ -0,0 +1,5 @@ + 0) return; - $user->views++; + $user->view_count++; $user->save(); app('flarum.db')->table("users_profile_views")->insert(array("ip" => $ip, "user_id" => $user->id)); diff --git a/src/listeners/AddUserApiAttributes.php b/src/listeners/AddUserApiAttributes.php index 2505b71..a2d0899 100644 --- a/src/listeners/AddUserApiAttributes.php +++ b/src/listeners/AddUserApiAttributes.php @@ -21,6 +21,6 @@ public function subscribe(Dispatcher $events) public function addApiAttributes(Serializing $event) { if ($event->isSerializer(UserSerializer::class)) - $event->attributes['views'] = $event->model->views; + $event->attributes['views'] = $event->model->view_count; } } \ No newline at end of file From 2bb3991166cf458cb8f9e1c4d99539c7776c1dfa Mon Sep 17 00:00:00 2001 From: Michael V Date: Sat, 1 Dec 2018 10:54:10 +0100 Subject: [PATCH 5/8] Add update section in readme --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index bb479ab..3b4d404 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,15 @@ This is an extension for the [flarum forum software](http://flarum.org) where yo Execute this command in your flarum root: `composer require michaelbelgium/flarum-profile-views` +# Updating +Execute these commands to update the extension: + +``` +composer update michaelbelgium/flarum-profile-views +php flarum migrate +php flarum cache:clear +``` + # Features * Tracks and displays **unique** profileviews in the usercard From d7d114c769f385bd00d31dffb207706b1bc27508 Mon Sep 17 00:00:00 2001 From: Michael V Date: Sat, 1 Dec 2018 11:15:42 +0100 Subject: [PATCH 6/8] Improve database columns/table --- .../2017_11_29_204708_users_add_views.php | 19 +++----------- migrations/2017_12_28_091742_add_unique.php | 22 +++++----------- .../2018_11_30_190115_users_rename_views.php | 5 ---- ...2_01_095958_rename_table_profile_views.php | 5 ++++ .../2018_12_01_100540_rename_columns.php | 26 +++++++++++++++++++ src/listeners/AddProfileViewHandler.php | 6 ++--- 6 files changed, 45 insertions(+), 38 deletions(-) delete mode 100644 migrations/2018_11_30_190115_users_rename_views.php create mode 100644 migrations/2018_12_01_095958_rename_table_profile_views.php create mode 100644 migrations/2018_12_01_100540_rename_columns.php diff --git a/migrations/2017_11_29_204708_users_add_views.php b/migrations/2017_11_29_204708_users_add_views.php index 047dcf4..19f50ff 100644 --- a/migrations/2017_11_29_204708_users_add_views.php +++ b/migrations/2017_11_29_204708_users_add_views.php @@ -1,18 +1,7 @@ function (Builder $schema) { - $schema->table('users', function (Blueprint $table) { - $table->integer('views')->default(0); - }); - }, - - 'down' => function (Builder $schema) { - $schema->table('users', function (Blueprint $table) { - $table->dropColumn('views'); - }); - } -]; +return Migration::addColumns("users", [ + "views" => ["integer", "default" => 0] +]); \ No newline at end of file diff --git a/migrations/2017_12_28_091742_add_unique.php b/migrations/2017_12_28_091742_add_unique.php index 7a0ece3..7bf0a18 100644 --- a/migrations/2017_12_28_091742_add_unique.php +++ b/migrations/2017_12_28_091742_add_unique.php @@ -1,20 +1,12 @@ function (Builder $schema) { - $schema->create('users_profile_views', function (Blueprint $table) { - $table->increments('id'); - $table->string("ip"); - $table->integer("user_id")->unsigned(); + return Migration::createTable('users_profile_views', function (Blueprint $table) { + $table->increments('id'); + $table->string("ip"); + $table->integer("user_id")->unsigned(); - $table->foreign("user_id")->references("id")->on("users")->onDelete("CASCADE")->onUpdate("CASCADE"); - }); - }, - - 'down' => function (Builder $schema) { - $schema->drop('users_profile_views'); - } -]; + $table->foreign("user_id")->references("id")->on("users")->onDelete("CASCADE")->onUpdate("CASCADE"); +}); diff --git a/migrations/2018_11_30_190115_users_rename_views.php b/migrations/2018_11_30_190115_users_rename_views.php deleted file mode 100644 index a732797..0000000 --- a/migrations/2018_11_30_190115_users_rename_views.php +++ /dev/null @@ -1,5 +0,0 @@ - function (Builder $schema) { + $schema->table('users', function (Blueprint $table) { + $table->renameColumn('views', 'view_count'); + }); + + $schema->table('user_profile_views', function (Blueprint $table) { + $table->renameColumn('user_id', 'viewed_user_id'); + }); + }, + + 'down' => function (Builder $schema) { + $schema->table('users', function (Blueprint $table) { + $table->renameColumn('view_count', 'views'); + }); + + $schema->table('user_profile_views', function (Blueprint $table) { + $table->renameColumn('viewed_user_id', 'user_id'); + }); + } +]; diff --git a/src/listeners/AddProfileViewHandler.php b/src/listeners/AddProfileViewHandler.php index 14f8b62..ab158f1 100644 --- a/src/listeners/AddProfileViewHandler.php +++ b/src/listeners/AddProfileViewHandler.php @@ -31,9 +31,9 @@ public function confViews(WillSerializeData $event) $user = $event->data; $resultCount = app('flarum.db') - ->table("users_profile_views") + ->table("user_profile_views") ->where("ip", $ip) - ->where("user_id", $user->id) + ->where("viewed_user_id", $user->id) ->count() ; @@ -42,7 +42,7 @@ public function confViews(WillSerializeData $event) $user->view_count++; $user->save(); - app('flarum.db')->table("users_profile_views")->insert(array("ip" => $ip, "user_id" => $user->id)); + app('flarum.db')->table("user_profile_views")->insert(array("ip" => $ip, "viewed_user_id" => $user->id)); } } } From 6b221e2c3bb1125047a5d8d5224ba14b8dd950e7 Mon Sep 17 00:00:00 2001 From: Michael V Date: Sat, 1 Dec 2018 13:11:38 +0100 Subject: [PATCH 7/8] Remove column "views", use relationship to user_profile_views --- extend.php | 13 ++++++-- ...18_12_01_100540_rename_remove_columns.php} | 6 +--- .../2018_12_01_103657_add_viewer_id.php | 21 ++++++++++++ src/listeners/AddProfileViewHandler.php | 18 +++------- src/listeners/AddUserApiAttributes.php | 26 --------------- .../AddUserProfileViewsRelationship.php | 33 +++++++++++++++++++ 6 files changed, 70 insertions(+), 47 deletions(-) rename migrations/{2018_12_01_100540_rename_columns.php => 2018_12_01_100540_rename_remove_columns.php} (75%) create mode 100644 migrations/2018_12_01_103657_add_viewer_id.php delete mode 100644 src/listeners/AddUserApiAttributes.php create mode 100644 src/listeners/AddUserProfileViewsRelationship.php diff --git a/extend.php b/extend.php index 436406c..35766f4 100644 --- a/extend.php +++ b/extend.php @@ -1,8 +1,9 @@ subscribe(listeners\AddProfileViewHandler::class); - $events->subscribe(listeners\AddUserApiAttributes::class); + $events->subscribe(listeners\AddUserProfileViewsRelationship::class); + + $events->listen(Serializing::class, function (Serializing $event) { + if ($event->isSerializer(UserSerializer::class)) { + $event->attributes['views'] = $event->model->profileViews()->count(); + } + }); } ]; \ No newline at end of file diff --git a/migrations/2018_12_01_100540_rename_columns.php b/migrations/2018_12_01_100540_rename_remove_columns.php similarity index 75% rename from migrations/2018_12_01_100540_rename_columns.php rename to migrations/2018_12_01_100540_rename_remove_columns.php index 221da27..4d0d0e0 100644 --- a/migrations/2018_12_01_100540_rename_columns.php +++ b/migrations/2018_12_01_100540_rename_remove_columns.php @@ -6,7 +6,7 @@ return [ 'up' => function (Builder $schema) { $schema->table('users', function (Blueprint $table) { - $table->renameColumn('views', 'view_count'); + $table->dropColumn('views'); }); $schema->table('user_profile_views', function (Blueprint $table) { @@ -15,10 +15,6 @@ }, 'down' => function (Builder $schema) { - $schema->table('users', function (Blueprint $table) { - $table->renameColumn('view_count', 'views'); - }); - $schema->table('user_profile_views', function (Blueprint $table) { $table->renameColumn('viewed_user_id', 'user_id'); }); diff --git a/migrations/2018_12_01_103657_add_viewer_id.php b/migrations/2018_12_01_103657_add_viewer_id.php new file mode 100644 index 0000000..a7a8714 --- /dev/null +++ b/migrations/2018_12_01_103657_add_viewer_id.php @@ -0,0 +1,21 @@ + function (Builder $schema) { + $schema->table('user_profile_views', function (Blueprint $table) { + $table->integer("viewer_id")->unsigned()->nullable(); + + $table->foreign("viewer_id")->references("id")->on("users")->onDelete("CASCADE")->onUpdate("CASCADE"); + }); + }, + + 'down' => function (Builder $schema) { + $schema->table('user_profile_views', function (Blueprint $table) { + $table->dropForeign(['viewer_id']); + $table->dropColumn('viewer_id'); + }); + } +]; diff --git a/src/listeners/AddProfileViewHandler.php b/src/listeners/AddProfileViewHandler.php index ab158f1..50ae6eb 100644 --- a/src/listeners/AddProfileViewHandler.php +++ b/src/listeners/AddProfileViewHandler.php @@ -5,6 +5,7 @@ use Flarum\Api\Event\WillSerializeData; use Illuminate\Contracts\Events\Dispatcher; use Flarum\User\User; +use Flarum\User\Guest; use Illuminate\Support\Facades\DB; class AddProfileViewHandler @@ -26,23 +27,14 @@ public function confViews(WillSerializeData $event) if (isset($serverParams["HTTP_CF_CONNECTING_IP"])) $ip = $serverParams["HTTP_CF_CONNECTING_IP"]; + + $visited_user = $event->data; - /** @var User $profile */ - $user = $event->data; - - $resultCount = app('flarum.db') - ->table("user_profile_views") - ->where("ip", $ip) - ->where("viewed_user_id", $user->id) - ->count() - ; + $resultCount = $visited_user->profileViews()->wherePivot('ip', '=', $ip)->count(); if($resultCount > 0) return; - $user->view_count++; - $user->save(); - - app('flarum.db')->table("user_profile_views")->insert(array("ip" => $ip, "viewed_user_id" => $user->id)); + $visited_user->profileViews()->attach($user, ["ip" => $ip]); } } } diff --git a/src/listeners/AddUserApiAttributes.php b/src/listeners/AddUserApiAttributes.php deleted file mode 100644 index a2d0899..0000000 --- a/src/listeners/AddUserApiAttributes.php +++ /dev/null @@ -1,26 +0,0 @@ -listen(Serializing::class, [$this, 'addApiAttributes']); - } - - /** - * @param Serializing $event - */ - public function addApiAttributes(Serializing $event) - { - if ($event->isSerializer(UserSerializer::class)) - $event->attributes['views'] = $event->model->view_count; - } -} \ No newline at end of file diff --git a/src/listeners/AddUserProfileViewsRelationship.php b/src/listeners/AddUserProfileViewsRelationship.php new file mode 100644 index 0000000..c211386 --- /dev/null +++ b/src/listeners/AddUserProfileViewsRelationship.php @@ -0,0 +1,33 @@ +listen(GetModelRelationship::class, [$this, 'getModelRelationship']); + } + + public function getModelRelationship(GetModelRelationship $event) + { + if($event->isRelationship(User::class, self::RELATIONSHIP)) + { + return $event->model->belongsToMany(User::class, 'user_profile_views', 'viewed_user_id', 'viewer_id')->withPivot('ip'); + } + + if($event->isRelationship(User::class, self::RELATIONSHIP_OTHER)) + { + return $event->model->belongsToMany(User::class, 'user_profile_views', 'viewer_id', 'viewed_user_id')->withPivot('ip'); + } + } +} \ No newline at end of file From 940a7a70285cbe68bc300f8eed4eacb6a1733f32 Mon Sep 17 00:00:00 2001 From: Michael V Date: Sat, 1 Dec 2018 15:47:49 +0100 Subject: [PATCH 8/8] Exclude guests --- src/listeners/AddProfileViewHandler.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/listeners/AddProfileViewHandler.php b/src/listeners/AddProfileViewHandler.php index 50ae6eb..caf608f 100644 --- a/src/listeners/AddProfileViewHandler.php +++ b/src/listeners/AddProfileViewHandler.php @@ -29,10 +29,11 @@ public function confViews(WillSerializeData $event) $ip = $serverParams["HTTP_CF_CONNECTING_IP"]; $visited_user = $event->data; + $user = $event->actor; $resultCount = $visited_user->profileViews()->wherePivot('ip', '=', $ip)->count(); - if($resultCount > 0) return; + if($resultCount > 0 || $user->isGuest()) return; $visited_user->profileViews()->attach($user, ["ip" => $ip]); }