/** * memo/create API. * * @Any("create", as="api.memo.create") * * @return \Illuminate\Http\JsonResponse */ public function anyCreate() { try { $id = $this->request('id'); if (empty($id) || !is_string($id)) { throw new RuntimeException('id is empty.'); } $info = $this->TwistOAuth->get('statuses/show', compact('id')); $data = ['user_sn' => $info->user->screen_name, 'user_id' => $info->user->id_str, 'content' => $info->text]; foreach ($info->entities->urls as $url) { $data['content'] = str_replace($url->url, $url->expanded_url, $data['content']); } if (isset($info->extended_entities)) { foreach ($info->extended_entities->media as $m) { $data['content'] = str_replace($m->url, $m->media_url_https, $data['content']); } } $validate = $this->memo->insert($data, ['save' => true]); if ($validate === true) { $id = $this->memo->select('id')->where('user_sn', '=', $data['user_sn'])->where('user_id', '=', $data['user_id'])->where('content', '=', $data['content'])->get()->last()->id; $json = ['id' => $id, 'user_sn' => $data['user_sn'], 'user_id' => $data['user_id'], 'content' => $data['content']]; return $this->onSuccess($json); } throw new RuntimeException($validate->errors()->first(), 400); } catch (Exception $e) { return $this->onError($e); } }
/** * Create sitemap data. * * @return array */ protected function createSiteMapData() { $data = []; $pasteBin = new M\PasteBin(); $last = $pasteBin->orderBy('id')->get()->last(); $data[] = ['loc' => '/pastebin', 'lastmod' => $last->created_at->format('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.7']; $all = $pasteBin->select(['hash', 'created_at'])->where('protect', '=', false)->get(); foreach ($all as $p) { $data[] = ['loc' => "/pastebin/show/{$p->hash}", 'lastmod' => $p->created_at->format('Y-m-d'), 'changefreq' => 'never', 'priority' => '0.4']; } $memo = new M\Memo(); $last = $memo->orderBy('id')->get()->last(); $data[] = ['loc' => '/memo', 'lastmod' => $last->created_at->format('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.7']; foreach ($memo->select(['id', 'created_at'])->get() as $m) { $data[] = ['loc' => "/memo/show/{$m->id}", 'lastmod' => $m->created_at->format('Y-m-d'), 'changefreq' => 'never', 'priority' => '0.4']; } foreach ($memo->select('user_sn')->distinct()->get() as $m) { $updated_at = $memo->where('user_sn', '=', $m->user_sn)->get()->last()->updated_at; $data[] = ['loc' => "/memo/user/{$m->user_sn}", 'lastmod' => $updated_at->format('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.4']; } return $data; }
/** * Update memo user_sn. * * @param string $userId * @param string $userSn */ protected function updateMemo($userId, $userSn) { $memo = new Memo(); $all = $memo->select(['user_sn', 'user_id'])->whereUserId($userId)->get(); foreach ($all as $memo) { if ($memo->user_sn === $userSn) { continue; } $memo->user_sn = $userSn; $memo->update(); } }