public static function generateId($length) { $idExists = true; while ($idExists) { $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $id = ''; for ($i = 0; $i < $length; $i++) { $id .= $chars[rand(0, strlen($chars) - 1)]; } $idExists = ChannelPost::exists(array('id' => $id)); } return $id; }
public function postMessage($messageContent) { $post = ChannelPost::create(array('id' => ChannelPost::generateId(6), 'channel_id' => $this->id, 'content' => $messageContent, 'timestamp' => Utils::tps())); ChannelAction::create(array('id' => ChannelAction::generateId(6), 'channel_id' => $this->id, 'recipients_ids' => ';' . implode(';', $this->getSubscribedUsersAsList()) . ';', 'type' => 'message', 'target' => $messageContent, 'complementary_id' => $post->id, 'timestamp' => Utils::tps())); return $post; }
public function destroy($id, $request) { if (Session::isActive()) { $user = Session::get(); if (ChannelPost::exists($id)) { $post = ChannelPost::find($id); if (UserChannel::find($post->channel_id) && UserChannel::find($post->channel_id)->belongToUser($user->id)) { $post->erase(); } } } return new Response(200); }