UserSession class [5] - Refactor src/Module/ files with DI

This commit is contained in:
Philipp 2022-10-20 22:59:12 +02:00
parent a729f3255d
commit eecc456e0c
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
78 changed files with 455 additions and 530 deletions

View file

@ -28,7 +28,6 @@ use Friendica\Content\Widget;
use Friendica\Core\L10n;
use Friendica\Core\Protocol;
use Friendica\Core\Renderer;
use Friendica\Core\Session;
use Friendica\Database\Database;
use Friendica\DI;
use Friendica\Model;
@ -57,7 +56,7 @@ class Advanced extends BaseModule
$this->dba = $dba;
$this->page = $page;
if (!Session::isAuthenticated()) {
if (!DI::userSession()->isAuthenticated()) {
throw new ForbiddenException($this->t('Permission denied.'));
}
}
@ -66,7 +65,7 @@ class Advanced extends BaseModule
{
$cid = $this->parameters['id'];
$contact = Model\Contact::selectFirst([], ['id' => $cid, 'uid' => Session::getLocalUser()]);
$contact = Model\Contact::selectFirst([], ['id' => $cid, 'uid' => DI::userSession()->getLocalUserId()]);
if (empty($contact)) {
throw new BadRequestException($this->t('Contact not found.'));
}
@ -87,7 +86,7 @@ class Advanced extends BaseModule
'nurl' => $nurl,
'poll' => $poll,
],
['id' => $contact['id'], 'uid' => Session::getLocalUser()]
['id' => $contact['id'], 'uid' => DI::userSession()->getLocalUserId()]
);
if ($photo) {
@ -105,7 +104,7 @@ class Advanced extends BaseModule
{
$cid = $this->parameters['id'];
$contact = Model\Contact::selectFirst([], ['id' => $cid, 'uid' => Session::getLocalUser()]);
$contact = Model\Contact::selectFirst([], ['id' => $cid, 'uid' => DI::userSession()->getLocalUserId()]);
if (empty($contact)) {
throw new BadRequestException($this->t('Contact not found.'));
}

View file

@ -25,7 +25,6 @@ use Friendica\BaseModule;
use Friendica\Content\Pager;
use Friendica\Content\Widget;
use Friendica\Core\Renderer;
use Friendica\Core\Session;
use Friendica\DI;
use Friendica\Model;
use Friendica\Model\User;
@ -36,7 +35,7 @@ class Contacts extends BaseModule
{
protected function content(array $request = []): string
{
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
throw new HTTPException\ForbiddenException();
}
@ -54,7 +53,7 @@ class Contacts extends BaseModule
throw new HTTPException\NotFoundException(DI::l10n()->t('Contact not found.'));
}
$localContactId = Model\Contact::getPublicIdByUserId(Session::getLocalUser());
$localContactId = Model\Contact::getPublicIdByUserId(DI::userSession()->getLocalUserId());
DI::page()['aside'] = Widget\VCard::getHTML($contact);

View file

@ -23,7 +23,6 @@ namespace Friendica\Module\Contact;
use Friendica\BaseModule;
use Friendica\Core\Renderer;
use Friendica\Core\Session;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
@ -41,7 +40,7 @@ class Hovercard extends BaseModule
$contact_url = $_REQUEST['url'] ?? '';
// Get out if the system doesn't have public access allowed
if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
if (DI::config()->get('system', 'block_public') && !DI::userSession()->isAuthenticated()) {
throw new HTTPException\ForbiddenException();
}
@ -70,8 +69,8 @@ class Hovercard extends BaseModule
// Search for contact data
// Look if the local user has got the contact
if (Session::isAuthenticated()) {
$contact = Contact::getByURLForUser($contact_url, Session::getLocalUser());
if (DI::userSession()->isAuthenticated()) {
$contact = Contact::getByURLForUser($contact_url, DI::userSession()->getLocalUserId());
} else {
$contact = Contact::getByURL($contact_url, false);
}
@ -81,7 +80,7 @@ class Hovercard extends BaseModule
}
// Get the photo_menu - the menu if possible contact actions
if (Session::isAuthenticated()) {
if (DI::userSession()->isAuthenticated()) {
$actions = Contact::photoMenu($contact);
} else {
$actions = [];

View file

@ -75,7 +75,7 @@ class Profile extends BaseModule
protected function post(array $request = [])
{
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return;
}
@ -83,7 +83,7 @@ class Profile extends BaseModule
// Backward compatibility: The update still needs a user-specific contact ID
// Change to user-contact table check by version 2022.03
$cdata = Contact::getPublicAndUserContactID($contact_id, Session::getLocalUser());
$cdata = Contact::getPublicAndUserContactID($contact_id, DI::userSession()->getLocalUserId());
if (empty($cdata['user']) || !DBA::exists('contact', ['id' => $cdata['user'], 'deleted' => false])) {
return;
}
@ -125,20 +125,20 @@ class Profile extends BaseModule
$fields['info'] = $_POST['info'];
}
if (!Contact::update($fields, ['id' => $cdata['user'], 'uid' => Session::getLocalUser()])) {
if (!Contact::update($fields, ['id' => $cdata['user'], 'uid' => DI::userSession()->getLocalUserId()])) {
DI::sysmsg()->addNotice($this->t('Failed to update contact record.'));
}
}
protected function content(array $request = []): string
{
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return Module\Security\Login::form($_SERVER['REQUEST_URI']);
}
// Backward compatibility: Ensure to use the public contact when the user contact is provided
// Remove by version 2022.03
$data = Contact::getPublicAndUserContactID(intval($this->parameters['id']), Session::getLocalUser());
$data = Contact::getPublicAndUserContactID(intval($this->parameters['id']), DI::userSession()->getLocalUserId());
if (empty($data)) {
throw new HTTPException\NotFoundException($this->t('Contact not found.'));
}
@ -153,7 +153,7 @@ class Profile extends BaseModule
throw new HTTPException\NotFoundException($this->t('Contact not found.'));
}
$localRelationship = $this->localRelationship->getForUserContact(Session::getLocalUser(), $contact['id']);
$localRelationship = $this->localRelationship->getForUserContact(DI::userSession()->getLocalUserId(), $contact['id']);
if ($localRelationship->rel === Contact::SELF) {
$this->baseUrl->redirect('profile/' . $contact['nick'] . '/profile');
@ -174,12 +174,12 @@ class Profile extends BaseModule
if ($cmd === 'block') {
if ($localRelationship->blocked) {
// @TODO Backward compatibility, replace with $localRelationship->unblock()
Contact\User::setBlocked($contact['id'], Session::getLocalUser(), false);
Contact\User::setBlocked($contact['id'], DI::userSession()->getLocalUserId(), false);
$message = $this->t('Contact has been unblocked');
} else {
// @TODO Backward compatibility, replace with $localRelationship->block()
Contact\User::setBlocked($contact['id'], Session::getLocalUser(), true);
Contact\User::setBlocked($contact['id'], DI::userSession()->getLocalUserId(), true);
$message = $this->t('Contact has been blocked');
}
@ -190,12 +190,12 @@ class Profile extends BaseModule
if ($cmd === 'ignore') {
if ($localRelationship->ignored) {
// @TODO Backward compatibility, replace with $localRelationship->unblock()
Contact\User::setIgnored($contact['id'], Session::getLocalUser(), false);
Contact\User::setIgnored($contact['id'], DI::userSession()->getLocalUserId(), false);
$message = $this->t('Contact has been unignored');
} else {
// @TODO Backward compatibility, replace with $localRelationship->block()
Contact\User::setIgnored($contact['id'], Session::getLocalUser(), true);
Contact\User::setIgnored($contact['id'], DI::userSession()->getLocalUserId(), true);
$message = $this->t('Contact has been ignored');
}
@ -224,8 +224,8 @@ class Profile extends BaseModule
'$baseurl' => $this->baseUrl->get(true),
]);
$contact['blocked'] = Contact\User::isBlocked($contact['id'], Session::getLocalUser());
$contact['readonly'] = Contact\User::isIgnored($contact['id'], Session::getLocalUser());
$contact['blocked'] = Contact\User::isBlocked($contact['id'], DI::userSession()->getLocalUserId());
$contact['readonly'] = Contact\User::isIgnored($contact['id'], DI::userSession()->getLocalUserId());
switch ($localRelationship->rel) {
case Contact::FRIEND: $relation_text = $this->t('You are mutual friends with %s', $contact['name']); break;
@ -486,7 +486,7 @@ class Profile extends BaseModule
*/
private static function updateContactFromProbe(int $contact_id)
{
$contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => [0, Session::getLocalUser()], 'deleted' => false]);
$contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => [0, DI::userSession()->getLocalUserId()], 'deleted' => false]);
if (!DBA::isResult($contact)) {
return;
}

View file

@ -27,7 +27,6 @@ use Friendica\Content\Nav;
use Friendica\Core\L10n;
use Friendica\Core\Protocol;
use Friendica\Core\Renderer;
use Friendica\Core\Session;
use Friendica\Database\Database;
use Friendica\DI;
use Friendica\Model;
@ -55,11 +54,11 @@ class Revoke extends BaseModule
$this->dba = $dba;
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return;
}
$data = Model\Contact::getPublicAndUserContactID($this->parameters['id'], Session::getLocalUser());
$data = Model\Contact::getPublicAndUserContactID($this->parameters['id'], DI::userSession()->getLocalUserId());
if (!$this->dba->isResult($data)) {
throw new HTTPException\NotFoundException($this->t('Unknown contact.'));
}
@ -81,7 +80,7 @@ class Revoke extends BaseModule
protected function post(array $request = [])
{
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
throw new HTTPException\UnauthorizedException();
}
@ -96,7 +95,7 @@ class Revoke extends BaseModule
protected function content(array $request = []): string
{
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return Login::form($_SERVER['REQUEST_URI']);
}