Skip to content

feat(moderation): add names moderation #712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Web/Models/Entities/Name.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use openvk\Web\Util\DateTime;
use openvk\Web\Models\RowModel;
use Nette\Database\Table\ActiveRow;
use Chandler\Database\DatabaseConnection as DB;
use openvk\Web\Models\Repositories\{Users};
use openvk\Web\Models\Entities\User;

class Name extends RowModel
{
protected $tableName = "names";

function getId(): int
{
return $this->getRecord()->id;
}

function getCreationDate(): DateTime
{
return new DateTime($this->getRecord()->created);
}

function getFirstName(): ?string
{
return $this->getRecord()->new_fn;
}

function getLastName(): ?string
{
return $this->getRecord()->new_ln;
}

function getUser(): ?User
{
return (new Users)->get($this->getRecord()->author);
}

function getStatus(): int
{
return $this->getRecord()->state;
}
}
12 changes: 11 additions & 1 deletion Web/Models/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use openvk\Web\Util\DateTime;
use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\{Photo, Message, Correspondence, Gift};
use openvk\Web\Models\Repositories\{Users, Clubs, Albums, Gifts, Notifications};
use openvk\Web\Models\Repositories\{Users, Clubs, Albums, Gifts, Notifications, Names};
use openvk\Web\Models\Exceptions\InvalidUserNameException;
use Nette\Database\Table\ActiveRow;
use Chandler\Database\DatabaseConnection;
Expand Down Expand Up @@ -1038,6 +1038,16 @@ function canUnbanThemself(): bool

return true;
}

function getNamesRequests(int $status = 0, ?bool $actual = false): \Traversable
{
return (new Names)->getByUser($this->getId(), $status, $actual);
}

function hasNamesRequests(int $status = 0, ?bool $actual = false): bool
{
return sizeof(iterator_to_array($this->getNamesRequests($status, $actual))) > 0;
}

use Traits\TSubscribable;
}
50 changes: 50 additions & 0 deletions Web/Models/Repositories/Names.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types=1);
namespace openvk\Web\Models\Repositories;
use Nette\Database\Table\ActiveRow;
use Chandler\Database\DatabaseConnection as DB;

use openvk\Web\Models\Entities\Name;
use openvk\Web\Models\Entities\User;

class Names
{
private $context;
private $names;

public function __construct()
{
$this->context = DB::i()->getContext();
$this->names = $this->context->table("names");
$this->users = $this->context->table("profiles");
}

private function toName(?ActiveRow $ar): ?Name
{
return is_null($ar) ? NULL : new Name($ar);
}

function get(int $id): ?Name
{
return $this->toName($this->names->get($id));
}

function getCount(int $status = 0): int
{
return sizeof(DB::i()->getContext()->table("names")->where("state", $status));
}

function getList(int $page = 1, int $status = 0): \Traversable
{
foreach($this->names->where("state", $status)->order("created ASC")->page($page, 5) as $name)
yield $this->toName($name);
}

function getByUser(int $uid, int $status = 0, ?bool $actual = true): \Traversable
{
$filter = ["author" => $uid, "state" => $status];
$actual && $filter[] = "created >= " . (time() - 259200);

foreach($this->names->where($filter) as $name)
yield $this->toName($name);
}
}
73 changes: 73 additions & 0 deletions Web/Presenters/NamesPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php declare(strict_types=1);

namespace openvk\Web\Presenters;

use openvk\Web\Models\Entities\Name;
use openvk\Web\Models\Repositories\Names;

final class NamesPresenter extends OpenVKPresenter
{
private $names;

public function __construct(Names $names)
{
$this->names = $names;
}

function renderList(): void
{
$this->assertUserLoggedIn();
$this->assertPermission("openvk\Web\Models\Entities\TicketReply", "write", 0);

$this->template->mode = $this->queryParam("act") ?? "new";
$this->template->mode_status = 0;

if ($this->template->mode == "accepted")
$this->template->mode_status = 1;
else if ($this->template->mode == "rejected")
$this->template->mode_status = 2;

$this->template->page = (int) $this->queryParam("p") ?: 1;

$this->template->iterator = $this->names->getList($this->template->page, $this->template->mode_status);
$this->template->names = iterator_to_array($this->template->iterator);

$this->template->count = (clone $this->names)->getCount($this->template->mode_status);
}

function renderAction(int $id): void
{
$this->assertUserLoggedIn();
$this->assertPermission("openvk\Web\Models\Entities\TicketReply", "write", 0);

$act = $this->queryParam("act");

if(!$act)
$this->flashFail("err", tr("error"), tr("forbidden"));

$name = $this->names->get($id);

if(!$name)
$this->flashFail("err", tr("error"), "Заявка #$id не найдена.");

$user = $name->getUser();

if($act == "accept") {
$user->setFirst_name($name->getFirstName());
$user->setLast_name($name->getLastName());
$user->save();

$name->setState(1);
$name->save();

$this->flashFail("success", "Успех", "Заявка #$id была принята.");
} elseif($act == "reject") {
$name->setState(2);
$name->save();

$this->flashFail("success", "Успех", "Заявка #$id была отклонена.");
}

$this->flashFail("err", tr("error"), tr("forbidden"));
}
}
6 changes: 4 additions & 2 deletions Web/Presenters/OpenVKPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Latte\Engine as TemplatingEngine;
use openvk\Web\Models\Entities\IP;
use openvk\Web\Themes\Themepacks;
use openvk\Web\Models\Repositories\{IPs, Users, APITokens, Tickets};
use openvk\Web\Models\Repositories\{IPs, Users, APITokens, Tickets, Names};
use WhichBrowser;

abstract class OpenVKPresenter extends SimplePresenter
Expand Down Expand Up @@ -258,8 +258,10 @@ function onStartup(): void
}

$this->template->ticketAnsweredCount = (new Tickets)->getTicketsCountByUserId($this->user->id, 1);
if($user->can("write")->model("openvk\Web\Models\Entities\TicketReply")->whichBelongsTo(0))
if($user->can("write")->model("openvk\Web\Models\Entities\TicketReply")->whichBelongsTo(0)) {
$this->template->helpdeskTicketNotAnsweredCount = (new Tickets)->getTicketCount(0);
$this->template->namesNotModeratedCount = (new Names)->getCount(0);
}
}

header("X-OpenVK-User-Validated: $userValidated");
Expand Down
17 changes: 14 additions & 3 deletions Web/Presenters/UserPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace openvk\Web\Presenters;
use openvk\Web\Util\Sms;
use openvk\Web\Themes\Themepacks;
use openvk\Web\Models\Entities\{Photo, Post, EmailChangeVerification};
use openvk\Web\Models\Entities\{Photo, Post, EmailChangeVerification, Name};
use openvk\Web\Models\Entities\Notifications\{CoinsTransferNotification, RatingUpNotification};
use openvk\Web\Models\Repositories\{Users, Clubs, Albums, Videos, Notes, Vouchers, EmailChangeVerifications};
use openvk\Web\Models\Exceptions\InvalidUserNameException;
Expand Down Expand Up @@ -142,8 +142,19 @@ function renderEdit(): void

if($_GET['act'] === "main" || $_GET['act'] == NULL) {
try {
$user->setFirst_Name(empty($this->postParam("first_name")) ? $user->getFirstName() : $this->postParam("first_name"));
$user->setLast_Name(empty($this->postParam("last_name")) ? "" : $this->postParam("last_name"));
if(!OPENVK_ROOT_CONF["openvk"]["preferences"]["namesModeration"]) {
$user->setFirst_Name(empty($this->postParam("first_name")) ? $user->getFirstName() : $this->postParam("first_name"));
$user->setLast_Name(empty($this->postParam("last_name")) ? "" : $this->postParam("last_name"));
} else {
if(!$user->hasNamesRequests(0, true) AND !$user->hasNamesRequests(2, true)) {
$name = new Name;
$name->setNew_fn(empty($this->postParam("first_name")) ? $user->getFirstName() : $this->postParam("first_name"));
$name->setNew_ln(empty($this->postParam("last_name")) ? $user->getFirstName() : $this->postParam("last_name"));
$name->setAuthor($user->getId());
$name->setCreated(time());
$name->save();
}
}
} catch(InvalidUserNameException $ex) {
$this->flashFail("err", tr("error"), tr("invalid_real_name"));
}
Expand Down
5 changes: 5 additions & 0 deletions Web/Presenters/templates/@layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@
(<b>{$helpdeskTicketNotAnsweredCount}</b>)
{/if}
</a>
<a href="/names_admin.php" class="link" n:if="$canAccessHelpdesk">Имена
{if $namesNotModeratedCount > 0}
(<b>{$namesNotModeratedCount}</b>)
{/if}
</a>

<a n:if="$thisUser->getLeftMenuItemStatus('links')" n:foreach="OPENVK_ROOT_CONF['openvk']['preferences']['menu']['links'] as $menuItem" href="{$menuItem['url']}" target="_blank" class="link">{strpos($menuItem["name"], "@") === 0 ? tr(substr($menuItem["name"], 1)) : $menuItem["name"]}</a>

Expand Down
121 changes: 121 additions & 0 deletions Web/Presenters/templates/Names/List.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{extends "../@layout.xml"}

{block header}
Администрирование
{/block}

{block content}
{var $isNew = $mode === 'new'}
{var $isAccepted = $mode === 'accepted'}
{var $isRejected = $mode === 'rejected'}

{var $amount = sizeof($names)}

<div class="tabs">
<div n:attr="id => ($isNew ? 'activetabs' : 'ki')" class="tab">
<a n:attr="id => ($isNew ? 'act_tab_a' : 'ki')" href="/names_admin.php">{_names_new}</a>
</div>
<div n:attr="id => ($isAccepted ? 'activetabs' : 'ki')" class="tab">
<a n:attr="id => ($isAccepted ? 'act_tab_a' : 'ki')" href="/names_admin.php?act=accepted">{_names_approved}</a>
</div>
<div n:attr="id => ($isRejected ? 'activetabs' : 'ki')" class="tab">
<a n:attr="id => ($isRejected ? 'act_tab_a' : 'ki')" href="/names_admin.php?act=rejected">{_names_rejected}</a>
</div>
</div>
<br/>
<div
class="user-alert"
style="padding: 10px 0; background-color: #F5F7F9; border: 1px solid #E9E9EB; color: #000; font-weight: normal;"
>
{presenter "openvk!Support->knowledgeBaseArticle", "names"}
</div>

{if $count < 1}
{include "../components/nothing.xml"}
{else}
<h4>{tr("names_showed", $amount, $count)}</h4>
<div class="container_gray">
<div
n:foreach="$names as $name"
class="content"
>
<table>
<tbody>
<tr>
<td valign="top">
<a href="{$name->getUser()->getURL()}">
<img src="{$name->getUser()->getAvatarURL()}" width="100" height="100" />
</a>
<center>{$name->getCreationDate()}</center>
</td>
<td valign="top" style="width: 100%">
<table id="basicInfo" class="ugc-table group_info" cellspacing="0" cellpadding="0" border="0">
<tbody style="display: flex; justify-content: space-between;">
<tr>
<td class="data">
<a>{$name->getUser()->getFirstName()} {$name->getUser()->getLastName()}</a>
</td>
</tr>
<tr style="margin-left: 20%;">
<td class="data">
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
width="30px" height="20px" viewBox="0 0 512.000000 512.000000"
preserveAspectRatio="xMidYMid meet">
<g transform="translate(0.000000,512.000000) scale(0.100000,-0.100000)"
fill="#2B587A" stroke="none">
<path d="M3806 3840 c-62 -19 -97 -83 -81 -147 4 -19 171 -192 498 -520 l491
-493 -2316 -2 -2317 -3 -28 -21 c-15 -11 -35 -39 -43 -62 -13 -37 -12 -45 3
-76 9 -19 29 -43 44 -53 26 -17 133 -18 2342 -21 l2315 -2 -486 -488 c-268
-268 -492 -500 -497 -515 -25 -65 0 -126 62 -152 70 -29 56 -41 708 612 410
410 610 617 614 636 4 15 4 39 0 55 -4 18 -203 224 -608 631 -514 515 -644
638 -669 630 -2 0 -16 -5 -32 -9z"
/>
</g>
</svg>
</td>
</tr>
<tr style="text-align: right;">
<td class="data">
<a>{$name->getFirstName()} {$name->getLastName()}</a>
</td>
</tr>
</tbody>
</table>
<div
class="user-alert"
style="background-color: #EDEDED; border: 1px solid #D8D8D8; font-weight: normal; color: #000; padding: 10px;"
>
<b style="color: #858787;">{_city}:</b> {$name->getUser()->getCity() ?? "Не указан"}
</div>
<center style="margin-top: 20px;">
<div n:if="$name->getStatus() == 0">
<a href="/name{$name->getId()}?act=accept" class="button">{_approve}</a>
<a href="/name{$name->getId()}?act=reject" class="button">{_reject}</a>
</div>
<div n:if="$name->getStatus() == 1">
<b style="color: green;">{_approved}</b>
</div>
<div n:if="$name->getStatus() == 2">
<b style="color: red;">{_rejected}</b>
</div>
</center>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div style="padding: 8px;">
{include "../components/paginator.xml", conf => (object) [
"page" => $page,
"count" => $count,
"amount" => $amount,
"perPage" => 5,
"atBottom" => true,
]}
</div>
{/if}
{/block}
11 changes: 10 additions & 1 deletion Web/Presenters/templates/User/Edit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@

<div class="container_gray">
{if $isMain}

<h4>{_main_information}</h4>
<div class="msg msg_info" n:if="$user->hasNamesRequests() AND !$user->hasNamesRequests(2, true)">
<b>{_name_change_request}</b><br>
{_name_change_request_pending}
</div>
<div class="msg msg_err" n:if="$user->hasNamesRequests(2, true)">
<b>{_name_change_request}</b><br>
{_name_change_request_rejected}
</div>

<form action="/edit?act=main" method="POST" enctype="multipart/form-data">
<table cellspacing="7" cellpadding="0" width="60%" border="0" align="center">
<tbody>
Expand Down
Loading