Skip to content

Commit

Permalink
Merge branch 'flarum-beta-8'
Browse files Browse the repository at this point in the history
closes #4
  • Loading branch information
MichaelBelgium committed Dec 1, 2018
2 parents 2fec6d6 + 940a7a7 commit 6efacf1
Show file tree
Hide file tree
Showing 22 changed files with 162 additions and 187 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 0 additions & 10 deletions bootstrap.php

This file was deleted.

4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -25,7 +25,7 @@
"flarum-extension": {
"title": "Profile views",
"icon": {
"name": "eye",
"name": "far fa-eye",
"backgroundColor": "#90C3D4",
"color": "#fff"
}
Expand Down
26 changes: 26 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
use michaelbelgium\profileviews\listeners;

use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Api\Serializer\UserSerializer;
use Flarum\Api\Event\Serializing;
use Flarum\Extend\Locales;
use Flarum\Extend\Frontend;

return [
(new Frontend('forum'))
->js(__DIR__. '/js/dist/forum.js'),

new Locales(__DIR__ . '/locale'),

function (Dispatcher $events) {
$events->subscribe(listeners\AddProfileViewHandler::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();
}
});
}
];
2 changes: 2 additions & 0 deletions js/dist/forum.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions js/dist/forum.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions js/forum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/forum';
7 changes: 0 additions & 7 deletions js/forum/Gulpfile.js

This file was deleted.

40 changes: 0 additions & 40 deletions js/forum/dist/extension.js

This file was deleted.

7 changes: 0 additions & 7 deletions js/forum/package.json

This file was deleted.

13 changes: 13 additions & 0 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
4 changes: 2 additions & 2 deletions js/forum/src/main.js → js/src/forum/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ app.initializers.add('michaelbelgium-flarum-profile-views', function() {

items.add('profile-views',(
<span>
{icon('eye')}
{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()})}
</span>
));
});
Expand Down
3 changes: 3 additions & 0 deletions js/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const config = require('flarum-webpack-config');

module.exports = config();
19 changes: 4 additions & 15 deletions migrations/2017_11_29_204708_users_add_views.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
use Flarum\Database\Migration;

return [
'up' => 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]
]);
22 changes: 7 additions & 15 deletions migrations/2017_12_28_091742_add_unique.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
<?php

use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;

return [
'up' => 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");
});
5 changes: 5 additions & 0 deletions migrations/2018_12_01_095958_rename_table_profile_views.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Flarum\Database\Migration;

return Migration::renameTable('users_profile_views', 'user_profile_views');
22 changes: 22 additions & 0 deletions migrations/2018_12_01_100540_rename_remove_columns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;

return [
'up' => function (Builder $schema) {
$schema->table('users', function (Blueprint $table) {
$table->dropColumn('views');
});

$schema->table('user_profile_views', function (Blueprint $table) {
$table->renameColumn('user_id', 'viewed_user_id');
});
},

'down' => function (Builder $schema) {
$schema->table('user_profile_views', function (Blueprint $table) {
$table->renameColumn('viewed_user_id', 'user_id');
});
}
];
21 changes: 21 additions & 0 deletions migrations/2018_12_01_103657_add_viewer_id.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Schema\Builder;
use Illuminate\Database\Schema\Blueprint;

return [
'up' => 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');
});
}
];
45 changes: 0 additions & 45 deletions src/listeners/AddAssets.php

This file was deleted.

29 changes: 11 additions & 18 deletions src/listeners/AddProfileViewHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
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 Flarum\User\Guest;
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))
{
Expand All @@ -26,23 +27,15 @@ public function confViews(PrepareApiData $event)

if (isset($serverParams["HTTP_CF_CONNECTING_IP"]))
$ip = $serverParams["HTTP_CF_CONNECTING_IP"];

$visited_user = $event->data;
$user = $event->actor;

/** @var User $profile */
$user = $event->data;
$resultCount = $visited_user->profileViews()->wherePivot('ip', '=', $ip)->count();

$resultCount = app('flarum.db')
->table("users_profile_views")
->where("ip", $ip)
->where("user_id", $user->id)
->count()
;
if($resultCount > 0 || $user->isGuest()) return;

if($resultCount > 0) return;

$user->views++;
$user->save();

app('flarum.db')->table("users_profile_views")->insert(array("ip" => $ip, "user_id" => $user->id));
$visited_user->profileViews()->attach($user, ["ip" => $ip]);
}
}
}
Loading

0 comments on commit 6efacf1

Please sign in to comment.