Browse Source

Introduce a view counter for Posts, show the counter above 3 views

pull/1218/head
Timothée Jaussoin 2 years ago
parent
commit
749a86812e
  1. 1
      CHANGELOG.md
  2. 6
      app/Post.php
  3. 5
      app/User.php
  4. 2
      app/widgets/Post/Post.php
  5. 4
      app/widgets/Post/_post.tpl
  6. 5
      app/widgets/Post/_post_card.tpl
  7. 5
      app/widgets/Post/_post_ticket.tpl
  8. 32
      database/migrations/20230612194157_create_post_user_views_table.php

1
CHANGELOG.md

@ -22,6 +22,7 @@ v0.22 (trunk)
* New Colors and AvatarPlaceholder widgets
* Refactor and cleanup the Moxl Stanza classes
* Fixing XEP-0077: In-Band Registration support and cleaning old code
* Introduce a view counter for Posts, show the counter above 3 views
v0.21.1
---------------------------

6
app/Post.php

@ -16,6 +16,7 @@ class Post extends Model
'contact', 'openlink', 'embed',
'links', 'files', 'pictures', 'picture',
'attachment'];
public $withCount = ['userViews'];
private $titleLimit = 200;
private $changed = false; // Detect if the set post was different from the cache
@ -51,6 +52,11 @@ class Post extends Model
->where('node', $this->node);
}
public function userViews()
{
return $this->belongsToMany(User::class, 'post_user_views', 'post_id', 'user_id')->withTimestamps();
}
public function likes()
{
return $this->hasMany('App\Post', 'parent_id', 'id')

5
app/User.php

@ -72,6 +72,11 @@ class User extends Model
return $this->belongsToMany('App\Reported')->withTimestamps();
}
public function postViews()
{
return $this->belongsToMany(User::class, 'post_user_views', 'user_id', 'post_id')->withTimestamps();
}
public function getResolvedNicknameAttribute()
{
return $this->nickname ?? $this->id;

2
app/widgets/Post/Post.php

@ -106,6 +106,8 @@ class Post extends Base
->request();
if ($p) {
$p->userViews()->syncWithoutDetaching($this->user->id);
$html = $this->preparePost($p, false, false, false);
$this->rpc('MovimTpl.fill', '#post_widget.'.cleanupId($p->nodeid), $html);

4
app/widgets/Post/_post.tpl

@ -133,6 +133,10 @@
{if="$post->contentcleaned && readTime($post->contentcleaned)"}
· {$post->contentcleaned|readTime}
{/if}
{$count = $post->user_views_count}
{if="$count > 2"}
· {$count} <i class="material-icons">visibility</i>
{/if}
</p>
{if="$post->isBrief()"}
<p class="normal">

5
app/widgets/Post/_post_card.tpl

@ -65,6 +65,7 @@
edit
</i>
{/if}
{if="!$post->openlink"}
<i class="material-icons on_mobile" title="{$c->__('post.public_no')}">
lock
@ -73,6 +74,10 @@
{if="$post->contentcleaned && readTime($post->contentcleaned)"}
· {$post->contentcleaned|readTime}
{/if}
{$count = $post->user_views_count}
{if="$count > 2"}
· {$count} <i class="material-icons">visibility</i>
{/if}
</p>
{if="$post->isBrief()"}
<p class="normal">

5
app/widgets/Post/_post_ticket.tpl

@ -73,6 +73,11 @@
<i class="material-icons">movie</i> ·
{/if}
{$count = $post->user_views_count}
{if="$count > 2"}
{$count} <i class="material-icons">visibility</i> ·
{/if}
{$count = $post->likes->count()}
{if="$count > 0"}
{$count} <i class="material-icons">favorite_border</i> ·

32
database/migrations/20230612194157_create_post_user_views_table.php

@ -0,0 +1,32 @@
<?php
use Movim\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreatePostUserViewsTable extends Migration
{
public function up()
{
$this->schema->create('post_user_views', function (Blueprint $table) {
$table->string('user_id', 256);
$table->integer('post_id')->unsigned();
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade');
$table->timestamps();
$table->unique(['user_id', 'post_id']);
});
}
public function down()
{
$this->schema->drop('post_user_views');
}
}
Loading…
Cancel
Save