[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

@ -36,11 +36,6 @@ 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,13 +34,6 @@ 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();
// Only show buttons if a user is logged in
if ($user == null) {
return Event::next;
}
$to_repeat = DB::find('note', ['id' => $note->getId()]);
$is_set = false; $is_set = false;
$form = Form::create([ $form = Form::create([
['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']], ['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']],
@ -52,17 +45,23 @@ 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;
} } else {
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 ($form->isValid()) {
if (!$data['is_set']) { if (!$data['is_set']) {
DB::persist(Note::create(['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId(), 'content' => $note->getContent(), 'is_local' => true])); DB::persist(Note::create(['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId(), 'content' => $note->getContent(), 'is_local' => true]));
DB::flush(); DB::flush();
} else { } else {
DB::remove($to_repeat); DB::remove(DB::findOneBy('note', ['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId()]));
DB::flush(); DB::flush();
} }
return Event::stop; return Event::stop;
@ -71,6 +70,8 @@ class Repeat extends Module
} }
} }
} }
}
}
$actions[] = $form->createView(); $actions[] = $form->createView();
return Event::next; return Event::next;

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;