[PLUGIN][DeleteNote][Favourite][ProfileColor][RepeatNote][WebMonetization] Refactor, cleanup and cache results

This commit is contained in:
Hugo Sales 2022-01-08 17:15:00 +00:00
parent cdf1d67d0f
commit a697399a6f
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
11 changed files with 186 additions and 71 deletions

View File

@ -32,6 +32,7 @@ class Collection extends Component
} }
$note_qb = DB::createQueryBuilder(); $note_qb = DB::createQueryBuilder();
$actor_qb = DB::createQueryBuilder(); $actor_qb = DB::createQueryBuilder();
// TODO consider selecting note related stuff, to avoid separate queries (though they're cached, so maybe it's okay)
$note_qb->select('note')->from('App\Entity\Note', 'note')->orderBy('note.created', 'DESC')->addOrderBy('note.id', 'DESC'); $note_qb->select('note')->from('App\Entity\Note', 'note')->orderBy('note.created', 'DESC')->addOrderBy('note.id', 'DESC');
$actor_qb->select('actor')->from('App\Entity\Actor', 'actor')->orderBy('actor.created', 'DESC')->addOrderBy('actor.id', 'DESC'); $actor_qb->select('actor')->from('App\Entity\Actor', 'actor')->orderBy('actor.created', 'DESC')->addOrderBy('actor.id', 'DESC');
Event::handle('CollectionQueryAddJoins', [&$note_qb, &$actor_qb, $note_criteria, $actor_criteria]); Event::handle('CollectionQueryAddJoins', [&$note_qb, &$actor_qb, $note_criteria, $actor_criteria]);

View File

@ -22,6 +22,7 @@ declare(strict_types = 1);
namespace Plugin\DeleteNote; namespace Plugin\DeleteNote;
use ActivityPhp\Type\AbstractObject; use ActivityPhp\Type\AbstractObject;
use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
@ -51,6 +52,14 @@ use Symfony\Component\HttpFoundation\Request;
*/ */
class DeleteNote extends NoteHandlerPlugin class DeleteNote extends NoteHandlerPlugin
{ {
public static function cacheKeys(int|Note $note_id): array
{
$note_id = \is_int($note_id) ? $note_id : $note_id->getId();
return [
'activity' => "deleted-note-activity-{$note_id}",
];
}
/** /**
* **Checks actor permissions for the DeleteNote action, deletes given Note * **Checks actor permissions for the DeleteNote action, deletes given Note
* and creates respective Activity and Notification** * and creates respective Activity and Notification**
@ -85,6 +94,7 @@ class DeleteNote extends NoteHandlerPlugin
// Undertaker believes the actor can terminate this note // Undertaker believes the actor can terminate this note
$activity = $note->delete(actor: $actor, source: 'web'); $activity = $note->delete(actor: $actor, source: 'web');
Cache::delete(self::cacheKeys($note)['activity']);
// Undertaker successful // Undertaker successful
Event::handle('NewNotification', [$actor, $activity, [], _m('{nickname} deleted note {note_id}.', ['nickname' => $actor->getNickname(), 'note_id' => $activity->getObjectId()])]); Event::handle('NewNotification', [$actor, $activity, [], _m('{nickname} deleted note {note_id}.', ['nickname' => $actor->getNickname(), 'note_id' => $activity->getObjectId()])]);
@ -106,8 +116,13 @@ class DeleteNote extends NoteHandlerPlugin
{ {
$actor = \is_int($actor) ? Actor::getById($actor) : $actor; $actor = \is_int($actor) ? Actor::getById($actor) : $actor;
$note = \is_int($note) ? Note::getById($note) : $note; $note = \is_int($note) ? Note::getById($note) : $note;
// Try and find if note was already deleted // Try to find if note was already deleted
if (\is_null(DB::findOneBy(Activity::class, ['verb' => 'delete', 'object_type' => 'note', 'object_id' => $note->getId()], return_null: true))) { if (\is_null(
Cache::get(
self::cacheKeys($note)['activity'],
fn () => DB::findOneBy(Activity::class, ['verb' => 'delete', 'object_type' => 'note', 'object_id' => $note->getId()], return_null: true),
),
)) {
// If none found, then undertaker has a job to do // If none found, then undertaker has a job to do
return self::undertaker($actor, $note); return self::undertaker($actor, $note);
} else { } else {
@ -145,8 +160,12 @@ class DeleteNote extends NoteHandlerPlugin
if (\is_null($actor = Common::actor())) { if (\is_null($actor = Common::actor())) {
return Event::next; return Event::next;
} }
// Only add action if note wasn't already deleted! if (
if (\is_null(DB::findOneBy(Activity::class, ['verb' => 'delete', 'object_type' => 'note', 'object_id' => $note->getId()], return_null: true)) // Only add action if note wasn't already deleted!
\is_null(Cache::get(
self::cacheKeys($note)['activity'],
fn () => DB::findOneBy(Activity::class, ['verb' => 'delete', 'object_type' => 'note', 'object_id' => $note->getId()], return_null: true),
))
// And has permissions // And has permissions
&& $actor->canAdmin($note->getActor())) { && $actor->canAdmin($note->getActor())) {
$delete_action_url = Router::url('delete_note_action', ['note_id' => $note->getId()]); $delete_action_url = Router::url('delete_note_action', ['note_id' => $note->getId()]);

View File

@ -21,8 +21,11 @@ declare(strict_types = 1);
namespace Plugin\Favourite\Entity; namespace Plugin\Favourite\Entity;
use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Entity\Actor;
use App\Entity\LocalUser;
use App\Entity\Note; use App\Entity\Note;
use DateTimeInterface; use DateTimeInterface;
@ -82,22 +85,39 @@ class NoteFavourite extends Entity
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
// }}} Autocode // }}} Autocode
public static function cacheKeys(int|Note $note_id, int|Actor|LocalUser|null $actor_id = null): array
{
$note_id = \is_int($note_id) ? $note_id : $note_id->getId();
$actor_id = \is_null($actor_id) ? null : (\is_int($actor_id) ? $actor_id : $actor_id->getId());
return [
'favourite' => "note-favourite-{$note_id}-{$actor_id}",
'favourites' => "note-favourites-{$note_id}",
'favourites-actors' => "note-favourites-actors-{$note_id}",
];
}
public static function getNoteFavourites(Note $note): array public static function getNoteFavourites(Note $note): array
{ {
return DB::findBy('note_favourite', ['note_id' => $note->getId()]); return Cache::getList(
self::cacheKeys($note)['favourites'],
fn () => DB::findBy('note_favourite', ['note_id' => $note->getId()]),
);
} }
public static function getNoteFavouriteActors(Note $note): array public static function getNoteFavouriteActors(Note $note): array
{ {
return DB::dql( return Cache::getList(
<<<'EOF' self::cacheKeys($note)['favourites-actors'],
select a from actor as a fn () => DB::dql(
inner join note_favourite as nf <<<'EOF'
with nf.note_id = :note_id select a from actor a
where a.id = nf.actor_id inner join note_favourite nf
order by nf.created DESC with a.id = nf.actor_id
EOF, where nf.note_id = :note_id
['note_id' => $note->getId()], order by nf.created DESC
EOF,
['note_id' => $note->getId()],
),
); );
} }

View File

@ -23,6 +23,7 @@ declare(strict_types = 1);
namespace Plugin\Favourite; namespace Plugin\Favourite;
use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
@ -56,10 +57,14 @@ class Favourite extends NoteHandlerPlugin
public static function favourNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity public static function favourNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
{ {
$opts = ['note_id' => $note_id, 'actor_id' => $actor_id]; $opts = ['note_id' => $note_id, 'actor_id' => $actor_id];
$note_already_favoured = DB::findOneBy('note_favourite', $opts, return_null: true); $note_already_favoured = Cache::get(
$activity = null; FavouriteEntity::cacheKeys($note_id, $actor_id)['favourite'],
fn () => DB::findOneBy('note_favourite', $opts, return_null: true),
);
$activity = null;
if (\is_null($note_already_favoured)) { if (\is_null($note_already_favoured)) {
DB::persist(FavouriteEntity::create($opts)); DB::persist(FavouriteEntity::create($opts));
Cache::delete(FavouriteEntity::cacheKeys($note_id, $actor_id)['favourite']);
$activity = Activity::create([ $activity = Activity::create([
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'verb' => 'favourite', 'verb' => 'favourite',
@ -86,11 +91,15 @@ class Favourite extends NoteHandlerPlugin
*/ */
public static function unfavourNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity public static function unfavourNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
{ {
$note_already_favoured = DB::findOneBy('note_favourite', ['note_id' => $note_id, 'actor_id' => $actor_id], return_null: true); $note_already_favoured = Cache::get(
$activity = null; FavouriteEntity::cacheKeys($note_id, $actor_id)['favourite'],
fn () => DB::findOneBy('note_favourite', ['note_id' => $note_id, 'actor_id' => $actor_id], return_null: true),
);
$activity = null;
if (!\is_null($note_already_favoured)) { if (!\is_null($note_already_favoured)) {
DB::remove($note_already_favoured); DB::remove($note_already_favoured);
$favourite_activity = DB::findBy('activity', ['verb' => 'favourite', 'object_type' => 'note', 'object_id' => $note_id], order_by: ['created' => 'DESC'])[0]; Cache::delete(FavouriteEntity::cacheKeys($note_id, $actor_id)['favourite']);
$favourite_activity = DB::findBy('activity', ['verb' => 'favourite', 'object_type' => 'note', 'actor_id' => $actor_id, 'object_id' => $note_id], order_by: ['created' => 'DESC'])[0];
$activity = Activity::create([ $activity = Activity::create([
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'verb' => 'undo', // 'undo_favourite', 'verb' => 'undo', // 'undo_favourite',
@ -123,7 +132,12 @@ class Favourite extends NoteHandlerPlugin
// If note is favourite, "is_favourite" is 1 // If note is favourite, "is_favourite" is 1
$opts = ['note_id' => $note->getId(), 'actor_id' => $user->getId()]; $opts = ['note_id' => $note->getId(), 'actor_id' => $user->getId()];
$is_favourite = !\is_null(DB::findOneBy('note_favourite', $opts, return_null: true)); $is_favourite = !\is_null(
Cache::get(
FavouriteEntity::cacheKeys($note->getId(), $user->getId())['favourite'],
fn () => DB::findOneBy('note_favourite', $opts, return_null: true),
),
);
// Generating URL for favourite action route // Generating URL for favourite action route
$args = ['id' => $note->getId()]; $args = ['id' => $note->getId()];

View File

@ -103,6 +103,7 @@ class ProfileColor extends Entity
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
// }}} Autocode // }}} Autocode
public static function schemaDef(): array public static function schemaDef(): array
{ {
return [ return [

View File

@ -27,7 +27,6 @@ use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Modules\Plugin; use App\Core\Modules\Plugin;
use App\Core\Router\RouteLoader; use App\Core\Router\RouteLoader;
use App\Util\Exception\NotFoundException;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use App\Util\Formatting; use App\Util\Formatting;
@ -84,16 +83,9 @@ class ProfileColor extends Plugin
if ($actor !== null) { if ($actor !== null) {
$actor_id = $actor->getId(); $actor_id = $actor->getId();
try { $profile_color = Cache::get("profile-color-{$actor_id}", fn () => DB::findOneBy('profile_color', ['actor_id' => $actor_id], return_null: true));
$profile_color_tab = Cache::get("profile-color-{$actor_id}", fn () => DB::findOneBy('profile_color', ['actor_id' => $actor_id])); if (!\is_null($profile_color)) {
} catch (NotFoundException $e) { $res[] = Formatting::twigRenderFile('/profileColor/profileColorView.html.twig', ['profile_color' => $profile_color, 'actor' => $actor_id]);
return Event::next;
}
$color = DB::findBy('profile_color', ['actor_id' => $actor_id])[0];
if ($color !== null) {
$color = $color->getColor();
$res[] = Formatting::twigRenderFile('/profileColor/profileColorView.html.twig', ['profile_color' => $profile_color_tab, 'actor' => $actor_id]);
} }
} }

View File

@ -23,6 +23,7 @@ declare(strict_types = 1);
namespace Plugin\RepeatNote\Entity; namespace Plugin\RepeatNote\Entity;
use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Entity\Note; use App\Entity\Note;
@ -39,6 +40,8 @@ use App\Entity\Note;
*/ */
class NoteRepeat extends Entity class NoteRepeat extends Entity
{ {
// {{{ Autocode
// @codeCoverageIgnoreStart
private int $note_id; private int $note_id;
private int $actor_id; private int $actor_id;
private int $repeat_of; private int $repeat_of;
@ -75,26 +78,40 @@ class NoteRepeat extends Entity
{ {
return $this->repeat_of; return $this->repeat_of;
} }
// @codeCoverageIgnoreEnd
// }}} Autocode
public static function cacheKeys(int|Note $note_id): array
{
$note_id = \is_int($note_id) ? $note_id : $note_id->getId();
return [
'is_repeat' => "note-repeat-is-{$note_id}",
'repeats' => "note-repeats-{$note_id}",
];
}
/** /**
* @return bool Returns true if Note provided is a repeat of another Note * @return bool Returns true if Note provided is a repeat of another Note
*/ */
public static function isNoteRepeat(Note $note): bool public static function isNoteRepeat(Note $note): bool
{ {
return DB::count(self::class, ['note_id' => $note->getId()]) > 0; return Cache::get(self::cacheKeys($note)['is_repeat'], fn () => DB::count(self::class, ['note_id' => $note->getId()]) > 0);
} }
public static function getNoteRepeats(Note $note): array public static function getNoteRepeats(Note $note): array
{ {
return DB::dql( return Cache::getList(
<<<'EOF' self::cacheKeys($note)['repeats'],
select n from note as n fn () => DB::dql(
inner join note_repeat as nr <<<'EOF'
with nr.note_id = n.id select n from note as n
where nr.repeat_of = :note_id inner join note_repeat as nr
order by n.created DESC, n.id DESC with nr.note_id = n.id
EOF, where nr.repeat_of = :note_id
['note_id' => $note->getId()], order by n.created DESC, n.id DESC
EOF,
['note_id' => $note->getId()],
),
); );
} }

View File

@ -21,6 +21,7 @@ declare(strict_types = 1);
namespace Plugin\RepeatNote; namespace Plugin\RepeatNote;
use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
@ -29,6 +30,7 @@ use App\Core\Router\RouteLoader;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Entity\Activity; use App\Entity\Activity;
use App\Entity\Actor; use App\Entity\Actor;
use App\Entity\LocalUser;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\BugFoundException; use App\Util\Exception\BugFoundException;
@ -44,6 +46,15 @@ use Symfony\Component\HttpFoundation\Request;
class RepeatNote extends NoteHandlerPlugin class RepeatNote extends NoteHandlerPlugin
{ {
public static function cacheKeys(int|Note $note_id, int|Actor|LocalUser $actor_id): array
{
$note_id = \is_int($note_id) ? $note_id : $note_id->getId();
$actor_id = \is_int($actor_id) ? $actor_id : $actor_id->getId();
return [
'repeat' => "note-repeat-{$note_id}-{$actor_id}",
];
}
/** /**
* **Repeats a Note** * **Repeats a Note**
* *
@ -63,12 +74,15 @@ class RepeatNote extends NoteHandlerPlugin
*/ */
public static function repeatNote(Note $note, int $actor_id, string $source = 'web'): ?Activity public static function repeatNote(Note $note, int $actor_id, string $source = 'web'): ?Activity
{ {
$repeat_entity = DB::findBy('note_repeat', [ $note_repeat = Cache::get(
'actor_id' => $actor_id, self::cacheKeys($note->getId(), $actor_id)['repeat'],
'note_id' => $note->getId(), fn () => DB::findOneBy('note_repeat', [
])[0] ?? null; 'actor_id' => $actor_id,
'note_id' => $note->getId(),
], return_null: true),
);
if (!\is_null($repeat_entity)) { if (!\is_null($note_repeat)) {
return null; return null;
} }
@ -99,6 +113,7 @@ class RepeatNote extends NoteHandlerPlugin
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'repeat_of' => $original_note_id, 'repeat_of' => $original_note_id,
])); ]));
Cache::delete(self::cacheKeys($note->getId(), $actor_id)['repeat']);
// Log an activity // Log an activity
$repeat_activity = Activity::create([ $repeat_activity = Activity::create([
@ -128,23 +143,30 @@ class RepeatNote extends NoteHandlerPlugin
*/ */
public static function unrepeatNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity public static function unrepeatNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
{ {
$already_repeated = DB::findBy(RepeatEntity::class, ['actor_id' => $actor_id, 'repeat_of' => $note_id])[0] ?? null; $note_repeat = Cache::get(
self::cacheKeys($note_id, $actor_id)['repeat'],
fn () => DB::findOneBy('note_repeat', [
'actor_id' => $actor_id,
'note_id' => $note_id,
], return_null: true),
);
if (!\is_null($already_repeated)) { // If it was repeated, then we can undo it if (!\is_null($note_repeat)) { // If it was repeated, then we can undo it
// Find previous repeat activity // Find previous repeat activity
$already_repeated_activity = DB::findBy(Activity::class, [ $already_repeated_activity = DB::findOneBy(Activity::class, [
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'verb' => 'repeat', 'verb' => 'repeat',
'object_type' => 'note', 'object_type' => 'note',
'object_id' => $already_repeated->getRepeatOf(), 'object_id' => $note_repeat->getRepeatOf(),
])[0] ?? null; ], return_null: true);
// Remove the clone note // Remove the clone note
DB::findBy(Note::class, ['id' => $already_repeated->getNoteId()])[0]->delete(actor: Actor::getById($actor_id)); DB::findOneBy(Note::class, ['id' => $note_repeat->getNoteId()])->delete(actor: Actor::getById($actor_id));
DB::flush(); DB::flush();
// Remove from the note_repeat table // Remove from the note_repeat table
DB::remove(DB::findBy(RepeatEntity::class, ['note_id' => $already_repeated->getNoteId()])[0]); DB::removeBy(RepeatEntity::class, ['note_id' => $note_repeat->getNoteId()]);
Cache::delete(self::cacheKeys($note_id, $actor_id)['repeat']);
// Log an activity // Log an activity
$undo_repeat_activity = Activity::create([ $undo_repeat_activity = Activity::create([
@ -161,18 +183,18 @@ class RepeatNote extends NoteHandlerPlugin
return $undo_repeat_activity; return $undo_repeat_activity;
} else { } else {
// Either was undoed already // Either was undoed already
if (!\is_null($already_repeated_activity = DB::findBy('activity', [ if (!\is_null($already_repeated_activity = DB::findOneBy('activity', [
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'verb' => 'repeat', 'verb' => 'repeat',
'object_type' => 'note', 'object_type' => 'note',
'object_id' => $note_id, 'object_id' => $note_id,
])[0] ?? null)) { ], return_null: true))) {
return DB::findBy('activity', [ return DB::findOneBy('activity', [
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'verb' => 'undo', 'verb' => 'undo',
'object_type' => 'activity', 'object_type' => 'activity',
'object_id' => $already_repeated_activity->getId(), 'object_id' => $already_repeated_activity->getId(),
])[0] ?? null; // null if not undoed ], return_null: true); // null if not undoed
} else { } else {
// or it's an attempt to undo something that wasn't repeated in the first place, // or it's an attempt to undo something that wasn't repeated in the first place,
return null; return null;
@ -216,14 +238,19 @@ class RepeatNote extends NoteHandlerPlugin
return Event::next; return Event::next;
} }
$note_repeat = Cache::get(
self::cacheKeys($note->getId(), $user->getId())['repeat'],
fn () => DB::findOneBy('note_repeat', [
'actor_id' => $user->getId(),
'note_id' => $note->getId(),
], return_null: true),
);
// If note is repeated, "is_repeated" is 1, 0 otherwise. // If note is repeated, "is_repeated" is 1, 0 otherwise.
$is_repeat = ($note_repeat = DB::findBy('note_repeat', [ $is_repeat = !\is_null($note_repeat);
'actor_id' => $user->getId(),
'repeat_of' => $note->getId(),
])) !== [] ? 1 : 0;
// Generating URL for repeat action route // Generating URL for repeat action route
$args = ['note_id' => $is_repeat === 0 ? $note->getId() : $note_repeat[0]->getRepeatOf()]; $args = ['note_id' => !$is_repeat ? $note->getId() : $note_repeat->getRepeatOf()];
$type = Router::ABSOLUTE_PATH; $type = Router::ABSOLUTE_PATH;
$repeat_action_url = $is_repeat $repeat_action_url = $is_repeat
? Router::url('repeat_remove', $args, $type) ? Router::url('repeat_remove', $args, $type)
@ -290,8 +317,8 @@ class RepeatNote extends NoteHandlerPlugin
public function onNoteDeleteRelated(Note &$note, Actor $actor): bool public function onNoteDeleteRelated(Note &$note, Actor $actor): bool
{ {
$note_repeats_list = RepeatEntity::getNoteRepeats($note); $note_repeats_list = RepeatEntity::getNoteRepeats($note);
foreach ($note_repeats_list as $repeat_entity) { foreach ($note_repeats_list as $note_repeat) {
DB::remove($repeat_entity); DB::remove($note_repeat);
} }
return Event::next; return Event::next;

View File

@ -50,6 +50,7 @@ class Wallet extends Entity
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
// }}} Autocode // }}} Autocode
public static function schemaDef() public static function schemaDef()
{ {
return [ return [

View File

@ -74,6 +74,7 @@ class WebMonetization extends Entity
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
// }}} Autocode // }}} Autocode
public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array
{ {
if (\array_key_exists('object', $ids_already_known)) { if (\array_key_exists('object', $ids_already_known)) {
@ -88,6 +89,7 @@ class WebMonetization extends Entity
} }
return $target_ids; return $target_ids;
} }
public static function schemaDef() public static function schemaDef()
{ {
return [ return [

View File

@ -31,12 +31,14 @@ declare(strict_types = 1);
namespace Plugin\WebMonetization; namespace Plugin\WebMonetization;
use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Core\Modules\Plugin; use App\Core\Modules\Plugin;
use App\Entity\Activity; use App\Entity\Activity;
use App\Entity\Actor;
use App\Entity\LocalUser; use App\Entity\LocalUser;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
@ -195,21 +197,40 @@ class WebMonetization extends Plugin
} }
return Event::next; return Event::next;
} }
public static function cacheKeys(int|LocalUser|Actor $id): array
{
if (!\is_int($id)) {
$id = $id->getId();
}
return [
'wallets' => "webmonetization-wallets-sender-{$id}",
];
}
public function onAppendToHead(Request $request, &$res): bool public function onAppendToHead(Request $request, &$res): bool
{ {
$user = Common::actor(); $user = Common::user();
if (\is_null($user)) { if (\is_null($user)) {
return Event::next; return Event::next;
} }
// donate to everyone! // donate to everyone!
// Using Javascript, it can be improved to donate only // Using Javascript, it can be improved to donate only
// to actors owning notes rendered on current page. // to actors owning notes rendered on current page.
$entries = DB::dql(<<<'EOF' $entries = Cache::getList(
SELECT wallet FROM \Plugin\WebMonetization\Entity\Wallet wallet self::cacheKeys($user->getId())['wallets'],
INNER JOIN \Plugin\WebMonetization\Entity\WebMonetization wm fn () => DB::dql(
WITH wallet.actor_id = wm.receiver <<<'EOF'
WHERE wm.active = :active AND wm.sender = :sender SELECT wallet FROM webmonetizationWallet wallet
EOF, ['sender' => $user->getId(), 'active' => true]); INNER JOIN webmonetization wm
WITH wallet.actor_id = wm.receiver
WHERE wm.active = :active AND wm.sender = :sender
EOF,
['sender' => $user->getId(), 'active' => true],
),
);
foreach ($entries as $entry) { foreach ($entries as $entry) {
$res[] = Formatting::twigRenderString( $res[] = Formatting::twigRenderString(
'<meta name="monetization" content="{{ address }}">', '<meta name="monetization" content="{{ address }}">',