attributes->get('user'); return response()->json(['channels' => $user->channels()->orderBy('id')->get()->map(self::present(...))->values()]); } public function store(Request $request): JsonResponse { /** @var User $user */ $user = $request->attributes->get('user'); $data = $request->validate([ 'type' => ['required', 'in:webhook'], 'config' => ['required', 'array'], 'config.deliver_url' => ['required', 'url'], 'config.presence_url' => ['required', 'url'], ], ['type.in' => 'Через API добавляется только webhook; Telegram подключается через /start.']); $channel = $user->channels()->create([ 'type' => ChannelType::Webhook, 'config' => ['deliver_url' => $data['config']['deliver_url'], 'presence_url' => $data['config']['presence_url']], 'enabled' => true, ]); return response()->json(self::present($channel), 201); } public function destroy(Request $request, int $channel): Response { /** @var User $user */ $user = $request->attributes->get('user'); $model = $user->channels()->whereKey($channel)->firstOrFail(); if ($model->type === ChannelType::Web) { throw ValidationException::withMessages(['channel' => 'Веб-канал удалить нельзя']); } $model->delete(); return response()->noContent(); } public function telegramLink(Request $request): JsonResponse { /** @var User $user */ $user = $request->attributes->get('user'); $code = Str::upper(Str::random(6)); Cache::put("tg:link:{$code}", $user->id, 900); return response()->json([ 'code' => $code, 'bot_url' => 'https://t.me/'.config('hado.telegram.username').'?start='.$code, ]); } /** @return array */ public static function present(Channel $channel): array { return [ 'id' => $channel->id, 'type' => $channel->type->value, 'config' => $channel->config, 'enabled' => $channel->enabled, ]; } }