예제 #1
0
 public function receive(Request $request)
 {
     $data = $request->json()->all();
     if (!$this->verifyArray($data, EmailController::EXPECTED_KEYS)) {
         return response(json_encode(['error' => 'Invalid data']), 400, ['Content-Type' => 'application/json']);
     }
     if ($data['signature'] != hash_hmac('sha256', $data['timestamp'] . $data['token'], env('CONTEXT_IO_SECRET'))) {
         return response(json_encode(['error' => 'Invalid signature']), 400, ['Content-Type' => 'application/json']);
     }
     $isForward = $this->isGmailForwardEmail($data);
     $body = null;
     if ($isForward) {
         $body = $this->downloadBody($data);
         $email = explode(' ', $body)[0];
     } else {
         // TODO: One day, support multiple to addresses and alert all verified addresses (once per account)
         $email = Arr::get($data, 'message_data.addresses.to.0.email');
     }
     // TODO: Make a different email for sending push notifications? Forwards keep the original message exactly as it
     // was. So, the forwarded message isn't from the account that initially received it, it's from the original
     // sender.
     /**
      * @var $email Email
      */
     $email = Email::whereEmail($email)->first();
     if (is_null($email)) {
         return response(json_encode(['error' => 'Unknown email']), 404, ['Content-Type' => 'application/json']);
     }
     if (!$email->user->pb_access_token) {
         return response(json_encode(['error' => 'Pushbullet not authenticated.']), 400, ['Content-Type' => 'application/json']);
     }
     if (!$email->verified) {
         return response(json_encode(['error' => 'Unverified email'], 400, ['Content-Type' => 'application/json']));
     }
     $notification = new Notification();
     if ($email->notifications()->count() < 1) {
         $contextIO = new ContextIO(env('CONTEXT_IO_KEY'), env('CONTEXT_IO_SECRET'));
         $message = $contextIO->getMessageBody(Arr::get($data, 'account_id'), ['label' => 0, 'folder' => Arr::get($data, 'message_data.folders.0'), 'message_id' => Arr::get($data, 'message_data.message_id'), 'type' => 'text/plain']);
         $notification->data = Arr::get($message->getData(), 'bodies.0.content');
     } else {
         $notification->data = null;
     }
     $notification->email_id = $email->id;
     $notification->subject = $this->replaceForwardPrefix(Arr::get($data, 'message_data.subject'));
     $notification->save();
     $this->dispatch(new PushJob($email->user->pb_access_token, $notification->subject));
     return response(json_encode(['status' => 'received']), 200, ['Content-Type' => 'application/json']);
 }