/** * Likes a post * @param Request $request The request object * @return string the json encoded list of likes */ public function like(Request $request) { $post_id = (int) $request->get('postId'); $user = $this->app['session']->get('user'); if (!$user) { return new Response($this->app->trans('MUST_BE_LOGGED_IN'), 500); } if (!$post_id) { return new Response($this->app->trans('UNKNOWN_ERROR'), 500); } $post = $this->findById(new Request(['id' => $post_id])); $post = json_decode($post, true); $likes = $this->app['db']->fetchAll('SELECT username FROM likes WHERE postId=?', array($post_id)); $_likes = array(); foreach ($likes as $like) { $_likes[] = $like['username']; } if (in_array($user['username'], $_likes)) { return new Response($this->app->trans('ALREADY_LIKED'), 400); } $this->app['db']->insert('likes', array('postId' => $post_id, 'username' => $user['username'], 'added' => date('Y-m-d H:i:s'))); $topic = $this->app['topic']->findById($post['topic']); $forum = $this->app['forum']->findById($post['forum']); $post_url = '/' . $this->app['board']['base'] . \ASF\Utils::toUrl($forum['name']) . '/' . \ASF\Utils::toUrl($topic['name']) . '-' . $topic['id'] . '/#' . $post['id']; if ($post['author'] !== $user['id']) { $this->app['notification']->add(new Request(['user_id' => $post['author'], 'notification' => '<a href="/' . $this->app['board']['base'] . 'user/' . $user['username'] . '/" class="user-link">' . $user['username'] . '</a> likes your <a href="' . $post_url . '">post</a>'])); } $_likes[] = $user['username']; $this->app['cache']->collection = $this->collection; $this->app['cache']->delete('post-' . $post_id . '-likes'); return json_encode($_likes); }
return str_repeat($string, $length); }); $to_date = new Twig_SimpleFilter('toDate', function ($string) use($app) { $user = $app['session']->get('user'); if (!$user) { // @todo add an admin setting for default date format return date('jS m y, H:i', strtotime($string)); } else { if (!$user['settings']['date_format']) { return date('jS m y, H:i', strtotime($string)); } return date($user['settings']['date_format'], strtotime($string)); } }); $to_url = new Twig_SimpleFilter('toUrl', function ($string) { return \ASF\Utils::toUrl($string); }); $app['twig']->addFunction($truncate); $app['twig']->addFunction($config_function); $app['twig']->addFunction($permissions_function); $app['twig']->addFunction($repeat_function); $app['twig']->addFilter($to_date); $app['twig']->addFilter($to_url); $app->register(new Silex\Provider\DoctrineServiceProvider(), array('db.options' => array('driver' => 'pdo_mysql', 'dbname' => $app['database']['name'], 'host' => $app['database']['host'], 'user' => $app['database']['user'], 'password' => $app['database']['password']))); if ($app['defaults']['cache'] === 'disk') { $app->register(new DiskCache\DiskCacheServiceProvider(), array('diskcache.cache_dir' => dirname(__DIR__) . '/cache')); $app['cache'] = $app['diskcache']; } else { $app->register(new Mongo\Silex\Provider\MongoServiceProvider(), array('mongo.connections' => array($app['database']['name'] => array('server' => 'mongodb://' . $app['mongo']['host'] . ':' . $app['mongo']['port'], 'options' => array("connect" => true))))); $app->register(new MongoCache\MongoCacheServiceProvider()); $app['cache'] = $app['mongocache'];