[NoteActions] Refactor note actions and fix bug in favourite

This commit is contained in:
Hugo Sales 2020-09-10 20:29:57 +00:00 committed by Hugo Sales
parent 4c15271d36
commit 72208b066c
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 30 additions and 29 deletions

View File

@ -35,12 +35,7 @@ class Favourite extends Module
{ {
public function onAddNoteActions(Request $request, Note $note, array &$actions) public function onAddNoteActions(Request $request, Note $note, array &$actions)
{ {
$user = Common::user(); $user = Common::user();
// Only show buttons if a user is logged in
if ($user == null) {
return Event::next;
}
$opts = ['note_id' => $note->getId(), 'gsactor_id' => $user->getId()]; $opts = ['note_id' => $note->getId(), 'gsactor_id' => $user->getId()];
$is_set = DB::find('favourite', $opts) != null; $is_set = DB::find('favourite', $opts) != null;
$form = Form::create([ $form = Form::create([

View File

@ -34,15 +34,8 @@ class Repeat extends Module
{ {
public function onAddNoteActions(Request $request, Note $note, array &$actions) public function onAddNoteActions(Request $request, Note $note, array &$actions)
{ {
$user = Common::user(); $is_set = false;
// Only show buttons if a user is logged in $form = Form::create([
if ($user == null) {
return Event::next;
}
$to_repeat = DB::find('note', ['id' => $note->getId()]);
$is_set = false;
$form = Form::create([
['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']], ['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']],
['note_id', HiddenType::class, ['data' => $note->getId()]], ['note_id', HiddenType::class, ['data' => $note->getId()]],
['repeat', SubmitType::class, ['label' => ' ']], ['repeat', SubmitType::class, ['label' => ' ']],
@ -52,22 +45,30 @@ class Repeat extends Module
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted()) { if ($form->isSubmitted()) {
$data = $form->getData(); $data = $form->getData();
// Loose comparison if ($data['note_id'] != $note . getId()) {
if ($data['note_id'] != $to_repeat) { // ^ Loose comparison
return Event::next; return Event::next;
}
if ($form->isValid()) {
if (!$data['is_set']) {
DB::persist(Note::create(['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId(), 'content' => $note->getContent(), 'is_local' => true]));
DB::flush();
} else {
DB::remove($to_repeat);
DB::flush();
}
return Event::stop;
} else { } else {
throw new InvalidFormException(); if (!$note->isVisibleTo(Common::user())) {
// ^ Ensure user isn't trying to trip us up
Log::error('Suspicious activity: user ' . $user->getNickname() .
' tried to repeat note ' . $note->getId() .
', but they shouldn\'t have access to it');
throw new NoSuchNoteException();
} else {
if ($form->isValid()) {
if (!$data['is_set']) {
DB::persist(Note::create(['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId(), 'content' => $note->getContent(), 'is_local' => true]));
DB::flush();
} else {
DB::remove(DB::findOneBy('note', ['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId()]));
DB::flush();
}
return Event::stop;
} else {
throw new InvalidFormException();
}
}
} }
} }
} }

View File

@ -60,6 +60,11 @@ class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface
public function getNoteActions(Note $note) public function getNoteActions(Note $note)
{ {
// Only show buttons if a user is logged in
if (Common::user() == null) {
return [];
}
$actions = []; $actions = [];
Event::handle('add_note_actions', [$this->request, $note, &$actions]); Event::handle('add_note_actions', [$this->request, $note, &$actions]);
return $actions; return $actions;