public static function addUserAction($note_id, $user_id, $action) { if ($note_id == null || $user_id == null || $action == null) { throw new Exception('Unable to add user action.'); } $toReturn = array('action' => null, 'likes' => -1, 'flags' => -1); $annotation = OldAnnotation::find($note_id); $meta = NoteMeta::where('user_id', $user_id)->where('annotation_id', '=', $note_id)->where('meta_key', '=', 'user_action'); //This user has no actions on this annotation if ($meta->count() == 0) { $meta = new NoteMeta(); $meta->user_id = Auth::user()->id; $meta->note_id = $note_id; $meta->meta_key = 'user_action'; $meta->meta_value = $action; $meta->save(); $toReturn['action'] = true; } elseif ($meta->count() == 1) { $meta = $meta->first(); //This user has already done this action. Removing the action if ($meta->meta_value == $action) { $meta->delete(); $toReturn['action'] = false; } else { $meta->meta_value = $action; $meta->save(); $toReturn['action'] = true; } } else { throw new Exception('Multiple user actions were found'); } $toReturn['likes'] = $annotation->likes(); $toReturn['flags'] = $annotation->flags(); return $toReturn; }