[CORE][DB] Fix uses of db tables after previous restructure

This commit is contained in:
Hugo Sales 2020-08-13 01:23:22 +00:00 committed by Hugo Sales
parent 1111ee95f1
commit 8716d700a6
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
8 changed files with 38 additions and 31 deletions

View File

@ -36,9 +36,10 @@ class Avatar extends Controller
{ {
switch ($size) { switch ($size) {
case 'full': case 'full':
$result = DB::createQuery('select f.file_hash, f.mimetype, f.title from ' . $result = DB::createQuery('select f.file_hash, f.mimetype, f.title ' .
'App\\Entity\\File f join App\\Entity\\Avatar a with f.id = a.file_id ' . 'from App\\Entity\\File f ' .
'join App\\Entity\\Profile p with p.id = a.profile_id ' . 'join App\\Entity\\Avatar a with f.id = a.file_id ' .
'join App\\Entity\\GSActor p with p.id = a.gsactor_id ' .
'where p.nickname = :nickname') 'where p.nickname = :nickname')
->setParameter('nickname', $nickname) ->setParameter('nickname', $nickname)
->getResult(); ->getResult();

View File

@ -6,8 +6,8 @@ use App\Core\Controller;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Entity\GSActor;
use App\Entity\LocalUser; use App\Entity\LocalUser;
use App\Entity\Profile;
use App\Security\Authenticator; use App\Security\Authenticator;
use App\Security\EmailVerifier; use App\Security\EmailVerifier;
use app\Util\Common; use app\Util\Common;
@ -81,11 +81,11 @@ class Security extends Controller
throw new \Exception(_m('Invalid nickname')); throw new \Exception(_m('Invalid nickname'));
} }
$profile = Profile::create(['nickname' => $data['nickname']]); $actor = GSActor::create(['nickname' => $data['nickname']]);
$user = LocalUser::create(['nickname' => $data['nickname'], 'email' => $data['email'], 'password' => $data['password']]); $user = LocalUser::create(['nickname' => $data['nickname'], 'email' => $data['email'], 'password' => $data['password']]);
DB::persist($user); DB::persist($user);
DB::persist($profile); DB::persist($actor);
DB::flush(); DB::flush();
// generate a signed url and email it to the user // generate a signed url and email it to the user

View File

@ -66,8 +66,8 @@ class UserPanel extends AbstractController
public function personal_info(Request $request) public function personal_info(Request $request)
{ {
$user = Common::user(); $user = Common::user();
$profile = $user->getProfile(); $actor = $user->getActor();
$extra = ['self_tags' => $profile->getSelfTags()]; $extra = ['self_tags' => $actor->getSelfTags()];
$form_definition = [ $form_definition = [
['nickname', TextType::class, ['label' => _m('Nickname'), 'required' => true, 'help' => _m('1-64 lowercase letters or numbers, no punctuation or spaces.')]], ['nickname', TextType::class, ['label' => _m('Nickname'), 'required' => true, 'help' => _m('1-64 lowercase letters or numbers, no punctuation or spaces.')]],
['full_name', TextType::class, ['label' => _m('Full Name'), 'required' => false, 'help' => _m('A full name is required, if empty it will be set to your nickname.')]], ['full_name', TextType::class, ['label' => _m('Full Name'), 'required' => false, 'help' => _m('A full name is required, if empty it will be set to your nickname.')]],
@ -78,7 +78,7 @@ class UserPanel extends AbstractController
['save', SubmitType::class, ['label' => _m('Save')]], ['save', SubmitType::class, ['label' => _m('Save')]],
]; ];
$extra_step = function ($data, $extra_args) use ($user) { $user->setNickname($data['nickname']); }; $extra_step = function ($data, $extra_args) use ($user) { $user->setNickname($data['nickname']); };
$form = Form::handle($form_definition, $request, $profile, $extra, $extra_step, [['self_tags' => $extra['self_tags']]]); $form = Form::handle($form_definition, $request, $actor, $extra, $extra_step, [['self_tags' => $extra['self_tags']]]);
return ['_template' => 'settings/profile.html.twig', 'prof' => $form->createView()]; return ['_template' => 'settings/profile.html.twig', 'prof' => $form->createView()];
} }
@ -139,18 +139,18 @@ class UserPanel extends AbstractController
} else { } else {
throw new ClientException('Invalid form'); throw new ClientException('Invalid form');
} }
$profile_id = Common::profile()->getId(); $actor_id = Common::actor()->getId();
$file = Media::validateAndStoreFile($sfile, Common::config('avatar', 'dir'), $file_title); $file = Media::validateAndStoreFile($sfile, Common::config('avatar', 'dir'), $file_title);
$avatar = null; $avatar = null;
try { try {
$avatar = DB::find('avatar', ['profile_id' => $profile_id]); $avatar = DB::find('avatar', ['actor_id' => $actor_id]);
} catch (Exception $e) { } catch (Exception $e) {
} }
if ($avatar != null) { if ($avatar != null) {
$avatar->delete(); $avatar->delete();
} else { } else {
DB::persist($file); DB::persist($file);
DB::persist(Avatar::create(['profile_id' => $profile_id, 'file_id' => $file->getId()])); DB::persist(Avatar::create(['actor_id' => $actor_id, 'file_id' => $file->getId()]));
} }
DB::flush(); DB::flush();
// Only delete files if the commit went through // Only delete files if the commit went through
@ -173,12 +173,12 @@ class UserPanel extends AbstractController
$label = str_replace('_', ' ', ucfirst($name)); $label = str_replace('_', ' ', ucfirst($name));
$labels = [ $labels = [
'target_profile_id' => 'Target Profiles', 'target_actor_id' => 'Target Actors',
'dm' => 'DM', 'dm' => 'DM',
]; ];
$help = [ $help = [
'target_profile_id' => 'If specified, these settings apply only to these profiles (comma- or space-separated list)', 'target_actor_id' => 'If specified, these settings apply only to these profiles (comma- or space-separated list)',
'activity_by_followed' => 'Notify me when someone I follow has new activity', 'activity_by_followed' => 'Notify me when someone I follow has new activity',
'mention' => 'Notify me when mentions me in a notice', 'mention' => 'Notify me when mentions me in a notice',
'reply' => 'Notify me when someone replies to a notice made by me', 'reply' => 'Notify me when someone replies to a notice made by me',
@ -195,8 +195,8 @@ class UserPanel extends AbstractController
$form_defs['placeholder'][$name] = [$name, CheckboxType::class, ['data' => $val, 'label' => _m($labels[$name] ?? $label), 'help' => _m($help[$name])]]; $form_defs['placeholder'][$name] = [$name, CheckboxType::class, ['data' => $val, 'label' => _m($labels[$name] ?? $label), 'help' => _m($help[$name])]];
break; break;
case Types::INTEGER: case Types::INTEGER:
if ($name == 'target_profile_id') { if ($name == 'target_actor_id') {
$form_defs['placeholder'][$name] = ['target_profiles', TextType::class, ['data' => $val, 'label' => _m($labels[$name]), 'help' => _m($help[$name])], 'transformer' => ProfileArrayTransformer::class]; $form_defs['placeholder'][$name] = ['target_actors', TextType::class, ['data' => $val, 'label' => _m($labels[$name]), 'help' => _m($help[$name])], 'transformer' => ActorArrayTransformer::class];
break; break;
} }
// no break // no break

View File

@ -62,7 +62,7 @@ class Controller extends AbstractController implements EventSubscriberInterface
$controller = $event->getController(); $controller = $event->getController();
$request = $event->getRequest(); $request = $event->getRequest();
if (($user = Common::user()) !== null && ($avatar = DB::find('avatar', ['profile_id' => $user->getProfile()->getId()])) != null) { if (($user = Common::user()) !== null && ($avatar = DB::find('avatar', ['gsactor_id' => $user->getActor()->getId()])) != null) {
$avatar_filename = $avatar->getUrl(); $avatar_filename = $avatar->getUrl();
} else { } else {
$avatar_filename = '/public/assets/default_avatar.svg'; $avatar_filename = '/public/assets/default_avatar.svg';

View File

@ -100,6 +100,9 @@ abstract class DB
$args[0] = '\App\Entity\\' . ucfirst(Formatting::snakeCaseToCamelCase($args[0])); $args[0] = '\App\Entity\\' . ucfirst(Formatting::snakeCaseToCamelCase($args[0]));
} }
} }
if (($args[0] ?? '') === '\App\Entity\Gsactor') {
$args[0] = '\App\Entity\GSActor';
}
return self::$em->{$name}(...$args); return self::$em->{$name}(...$args);
} }

View File

@ -35,8 +35,8 @@ namespace App\Util;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Router; use App\Core\Router;
use App\Core\Security; use App\Core\Security;
use App\Entity\GSActor;
use App\Entity\LocalUser; use App\Entity\LocalUser;
use App\Entity\Profile;
use Functional as F; use Functional as F;
abstract class Common abstract class Common
@ -75,9 +75,9 @@ abstract class Common
return Security::getUser(); return Security::getUser();
} }
public static function profile(): ?Profile public static function actor(): ?GSActor
{ {
return self::user()->getProfile(); return self::user()->getActor();
} }
/** /**

View File

@ -32,7 +32,7 @@
namespace App\Util\Form; namespace App\Util\Form;
class ProfileArrayTransformer extends ArrayTransformer class ActorArrayTransformer extends ArrayTransformer
{ {
/** /**
* @param array $a * @param array $a

View File

@ -22,6 +22,9 @@
namespace App\Util; namespace App\Util;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Entity\GSActor;
use App\Entity\LocalGroup;
use App\Entity\LocalUser;
use Normalizer; use Normalizer;
/** /**
@ -146,9 +149,9 @@ class Nickname
} elseif (self::isReserved($nickname) || Common::isSystemPath($nickname)) { } elseif (self::isReserved($nickname) || Common::isSystemPath($nickname)) {
throw new NicknameReservedException(); throw new NicknameReservedException();
} elseif ($check_already_used) { } elseif ($check_already_used) {
$profile = self::isTaken($nickname); $actor = self::isTaken($nickname);
if ($profile instanceof Profile) { if ($actor instanceof GSActor) {
throw new NicknameTakenException($profile); throw new NicknameTakenException($actor);
} }
} }
@ -197,9 +200,9 @@ class Nickname
/** /**
* Is the nickname already in use locally? Checks the User table. * Is the nickname already in use locally? Checks the User table.
* *
* @return null|Profile Returns Profile if nickname found, otherwise null * @return null|GSActor Returns GSActor if nickname found
*/ */
public static function isTaken(string $nickname): ?Profile public static function isTaken(string $nickname): ?GSActor
{ {
$found = DB::findBy('local_user', ['nickname' => $nickname]); $found = DB::findBy('local_user', ['nickname' => $nickname]);
if ($found instanceof LocalUser) { if ($found instanceof LocalUser) {
@ -207,7 +210,7 @@ class Nickname
} }
$found = DB::findBy('local_group', ['nickname' => $nickname]); $found = DB::findBy('local_group', ['nickname' => $nickname]);
if ($found instanceof Local_group) { if ($found instanceof LocalGroup) {
return $found->getProfile(); return $found->getProfile();
} }