Browse Source

Start to handle comments in Movim

pull/617/head
Timothée Jaussoin 8 years ago
parent
commit
ea1e26395d
  1. 25
      app/Post.php
  2. 54
      app/widgets/Post/Post.php
  3. 2
      app/widgets/Post/_post.tpl
  4. 61
      app/widgets/Post/_post_comments.tpl
  5. 5
      database/migrations/20180402145559_create_posts_table.php

25
app/Post.php

@ -29,6 +29,18 @@ class Post extends Model
return $this->belongsToMany('App\Tag')->withTimestamps();
}
public function comments()
{
return $this->hasMany('App\Post', 'parent_id', 'id')
->where('like', false);
}
public function likes()
{
return $this->hasMany('App\Post', 'parent_id', 'id')
->where('like', true);
}
public function attachments()
{
return $this->hasMany('App\Attachment');
@ -284,6 +296,8 @@ class Post extends Model
}
}
$this->like = $this->isLike();
$this->setAttachments($entry->entry->link, $extra);
if ($this->isComment()) {
@ -355,14 +369,14 @@ class Post extends Model
$this->attachments[] = $att;
/*if ((string)$attachment->attributes()->title == 'comments') {
if ((string)$attachment->attributes()->title == 'comments') {
$url = parse_url(urldecode((string)$attachment->attributes()->href));
if ($url) {
$this->commentserver = $url['path'];
$this->commentnodeid = substr($url['query'], 36);
}
}*/
}
}
}
@ -584,15 +598,12 @@ class Post extends Model
public function countComments()
{
/*
return $pd->countComments($this->commentserver, $this->commentnodeid);*/
return 0;
return $this->comments()->count();
}
public function countLikes()
{
//return $pd->countLikes($this->commentserver, $this->commentnodeid);*/
return 0;
return $this->likes()->count();
}
public function isLiked()

54
app/widgets/Post/Post.php

@ -56,12 +56,8 @@ class Post extends \Movim\Widget\Base
function onComments($packet)
{
list($server, $node, $id) = array_values($packet->content);
$pd = new \Modl\PostnDAO;
$p = $pd->get($server, $node, $id);
$this->rpc('MovimTpl.fill', '#comments', $this->prepareComments($p));
$post = \App\Post::find($packet->content);
$this->rpc('MovimTpl.fill', '#comments', $this->prepareComments($post));
}
function onCommentsError($packet)
@ -122,12 +118,14 @@ class Post extends \Movim\Widget\Base
function ajaxGetPostComments($server, $node, $id)
{
/*$pd = new \Modl\PostnDAO;
$p = $pd->get($server, $node, $id);
$post = \App\Post::where('server', $server)
->where('node', $node)
->where('nodeid', $id)
->first();
if ($p) {
$this->requestComments($p);
}*/
if ($post) {
$this->requestComments($post);
}
}
function ajaxShare($server, $node, $id)
@ -140,18 +138,15 @@ class Post extends \Movim\Widget\Base
}*/
}
function requestComments(\Modl\ContactPostn $post)
function requestComments(\App\Post $post)
{
/*$pd = new \Modl\PostnDAO;
$pd->deleteNode($post->commentserver, "urn:xmpp:microblog:0:comments/".$post->commentnodeid);
\App\Post::where('parent_id', $post->id)->delete();
$c = new CommentsGet;
$c->setTo($post->commentserver)
->setId($post->commentnodeid)
->setParentOrigin($post->server)
->setParentNode($post->node)
->setParentNodeId($post->nodeid)
->request();*/
->setParentId($post->id)
->request();
}
public function publishComment($comment, $to, $node, $id)
@ -184,26 +179,11 @@ class Post extends \Movim\Widget\Base
}
}
public function prepareComments($post)
public function prepareComments(\App\Post $post)
{
if ($post == null) return;
$emoji = \MovimEmoji::getInstance();
$comments = $post->getComments();
$likes = [];
foreach ($comments as $key => $comment) {
if ($comment->isLike()) {
$likes[] = $comment;
unset($comments[$key]);
}
}
$view = $this->tpl();
$view->assign('post', $post);
$view->assign('comments', $comments);
$view->assign('likes', $likes);
$view->assign('hearth', $emoji->replace('♥'));
return $view->draw('_post_comments', true);
@ -215,7 +195,7 @@ class Post extends \Movim\Widget\Base
return $view->draw('_post_not_found', true);
}
function preparePost($p, $external = false, $public = false, $card = false)
function preparePost(\App\Post $p, $external = false, $public = false, $card = false)
{
$view = $this->tpl();
$view->assign('external', $external);
@ -223,7 +203,7 @@ class Post extends \Movim\Widget\Base
if (isset($p)) {
if ($p->hasCommentsNode()
&& !$external) {
//$this->requestComments($p); // Broken in case of repost
$this->requestComments($p); // Broken in case of repost
$view->assign('commentsdisabled', false);
} else {
$viewd = $this->tpl();
@ -233,8 +213,6 @@ class Post extends \Movim\Widget\Base
$view->assign('repost', false);
$comments = $this->tpl();
//$comments->assign('comments', $p->getComments());
$view->assign('comments', $comments->draw('_post_comments_external', true));
$view->assign('public', $public);
$view->assign('reply', $p->isReply() ? $p->getReply() : false);

2
app/widgets/Post/_post.tpl

@ -218,8 +218,6 @@
{/if}
</footer>
{$comments}
{if="!$public"}
{if="$commentsdisabled"}
{$commentsdisabled}

61
app/widgets/Post/_post_comments.tpl

@ -1,18 +1,18 @@
{$liked = false}
{if="count($likes) > 0"}
{if="$post->likes()->count() > 0"}
<ul class="list divided spaced middle">
<li>
<span class="primary icon red">
<i class="zmdi zmdi-favorite"></i>
</span>
<p>{$likes|count}</span> {$c->__('button.like')}</p>
<p>{$post->likes()->count()}</span> {$c->__('button.like')}</p>
<p class="all">
{loop="$likes"}
{if="$value->isMine()"}{$liked = [$value->origin, $value->node, $value->nodeid]}{/if}
{loop="$post->likes"}
{if="$value->isMine()"}{$liked = [$value->server, $value->node, $value->nodeid]}{/if}
<a title="{$value->published|strtotime|prepareDate:true,true}"
href="{$c->route('contact', $value->getContact()->jid)}">
{$value->getContact()->getTrueName()}</a>{if="$key + 1 < count($likes)"},
href="{$c->route('contact', $value->contact->jid)}">
{$value->contact->truename}</a>{if="$key + 1 < count($likes)"},
{/if}
{/loop}
</p>
@ -21,35 +21,42 @@
{/if}
<ul class="list divided spaced middle">
{if="count($comments) > 0"}
{if="$post->comments()->count() > 0"}
<li class="subheader center">
<p>
<span class="info">{$comments|count}</span> {$c->__('post.comments')}
<span class="info">{$post->comments()->count()}</span> {$c->__('post.comments')}
</p>
</li>
{/if}
{loop="$comments"}
{loop="$post->comments"}
{if="$value->title || $value->contentraw"}
<li id="{$value->nodeid|cleanupId}"
{if="$value->isMine(true) && $value->isLike()"}class="mine"{/if}>
{if="$value->isMine() || $post->isMine()"}
<span class="control icon gray active"
onclick="PostActions_ajaxDelete('{$value->origin}', '{$value->node}', '{$value->nodeid}')">
onclick="PostActions_ajaxDelete('{$value->server}', '{$value->node}', '{$value->nodeid}')">
<i class="zmdi zmdi-delete"></i>
</span>
{/if}
{$url = $value->getContact()->getPhoto('s')}
{if="$url"}
<span class="primary icon bubble small">
<a href="{$c->route('contact', $value->getContact()->jid)}">
<img src="{$url}">
</a>
</span>
{if="$value->contact"}
{$url = $value->contact->getPhoto('s')}
{if="$url"}
<span class="primary icon bubble small">
<a href="{$c->route('contact', $value->contact->jid)}">
<img src="{$url}">
</a>
</span>
{else}
<span class="primary icon bubble color {$value->contact->jid|stringToColor} small">
<a href="{$c->route('contact', $value->contact->jid)}">
<i class="zmdi zmdi-account"></i>
</a>
</span>
{/if}
{else}
<span class="primary icon bubble color {$value->getContact()->jid|stringToColor} small">
<a href="{$c->route('contact', $value->getContact()->jid)}">
<span class="primary icon bubble color {$value->aid|stringToColor} small">
<a href="{$c->route('contact', $value->aid)}">
<i class="zmdi zmdi-account"></i>
</a>
</span>
@ -58,9 +65,15 @@
<span class="info" title="{$value->published|strtotime|prepareDate}">
{$value->published|strtotime|prepareDate:true,true}
</span>
<a href="{$c->route('contact', $value->getContact()->jid)}">
{$value->getContact()->getTrueName()}
{if="$value->contact"}
<a href="{$c->route('contact', $value->contact->jid)}">
{$value->contact->truename}
</a>
{else}
<a href="{$c->route('contact', $value->aid)}">
{$value->aid}
</a>
{/if}
</p>
<p class="all">
{if="$value->contentraw"}
@ -77,7 +90,7 @@
<span class="primary icon small gray">
<i class="zmdi zmdi-comment"></i>
</span>
<span class="control icon gray active" onclick="Post_ajaxPublishComment(MovimUtils.formToJson('comment'),'{$post->origin}', '{$post->node}', '{$post->nodeid}')">
<span class="control icon gray active" onclick="Post_ajaxPublishComment(MovimUtils.formToJson('comment'),'{$post->server}', '{$post->node}', '{$post->nodeid}')">
<i class="zmdi zmdi-mail-send"></i>
</span>
<form name="comment">
@ -103,7 +116,7 @@
{else}
<button class="button red flat"
id="like"
onclick="this.classList.add('disabled'); PostActions_ajaxLike('{$post->origin}', '{$post->node}', '{$post->nodeid}')">
onclick="this.classList.add('disabled'); PostActions_ajaxLike('{$post->server}', '{$post->node}', '{$post->nodeid}')">
<i class="zmdi zmdi-favorite"></i> {$c->__('button.like')}
</button>
{/if}

5
database/migrations/20180402145559_create_posts_table.php

@ -23,12 +23,11 @@ class CreatePostsTable extends Migration
$table->string('commentserver', 64)->nullable();
$table->string('commentnodeid', 192)->nullable();
$table->string('parentorigin', 64)->nullable();
$table->string('parentnode', 96)->nullable();
$table->string('parentnodeid', 96)->nullable();
$table->integer('parent_id')->nullable();
$table->boolean('open')->default(false);
$table->boolean('nsfw')->default(false);
$table->boolean('like')->default(false);
$table->datetime('published')->nullable();
$table->datetime('updated')->nullable();

Loading…
Cancel
Save