Skip to content

Commit e062556

Browse files
CopilotBreadMotion
andcommitted
Update post-interactions to use GAS Web App API
Co-authored-by: BreadMotion <89498861+BreadMotion@users.noreply.github.com>
1 parent ead02d8 commit e062556

2 files changed

Lines changed: 20 additions & 19 deletions

File tree

WebSite/assets/js/post-interactions.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
* post-interactions.js
33
* - いいね(Like)とブックマークのクライアント実装
44
* - localStorage を利用したフォールバック(サーバーが無くても動作)
5-
* - window.__POST_INTERACTIONS_CONFIG に API のベース URL が埋め込まれていれば
6-
* 可能な限り API と同期を試みる(API の仕様に応じて修正してください)
5+
* - window.__POST_INTERACTIONS_CONFIG に GAS Web App URL が埋め込まれていれば
6+
* GET クエリ(action=get|like|unlike&postId=...)を使って API と同期
77
*
88
* 実装方針(軽量):
99
* - Bookmark: localStorage に保存(ユーザー毎のブックマーク)
10-
* - Like: ユーザー単体の「自分がいいねしたか」は localStorage。総数は localStorage に保存(グローバル永続化には API が必要
10+
* - Like: ユーザー単体の「自分がいいねしたか」は localStorage。総数は API から取得(グローバル永続化
1111
*
1212
* 依存なし(Vanilla JS)
1313
*/
@@ -96,16 +96,18 @@
9696
else btn.classList.remove("is-bookmarked");
9797
}
9898

99-
// API 呼び出し(あくまで「試みる」実装。実際のエンドポイント仕様に合わせて調整してください)
99+
// GAS Web App API 呼び出し
100+
// GET: action=get|like|unlike&postId=...
101+
// レスポンス: { likes: number, ... }
100102
async function fetchLikeCountFromApi(postId) {
101103
if (!API_BASE) return null;
102104
try {
103-
const res = await fetch(
104-
`${API_BASE.replace(/\/$/, "")}/posts/${encodeURIComponent(postId)}`,
105-
);
105+
const url = new URL(API_BASE);
106+
url.searchParams.set("action", "get");
107+
url.searchParams.set("postId", postId);
108+
const res = await fetch(url.toString());
106109
if (!res.ok) return null;
107110
const json = await res.json();
108-
// 期待: { likes: number, ... }
109111
return typeof json.likes === "number"
110112
? json.likes
111113
: null;
@@ -117,14 +119,10 @@
117119
async function sendLikeToggleToApi(postId, action) {
118120
if (!API_BASE) return null;
119121
try {
120-
const res = await fetch(
121-
`${API_BASE.replace(/\/$/, "")}/posts/${encodeURIComponent(postId)}/like`,
122-
{
123-
method: "POST",
124-
headers: { "Content-Type": "application/json" },
125-
body: JSON.stringify({ action }), // action: 'like' or 'unlike'
126-
},
127-
);
122+
const url = new URL(API_BASE);
123+
url.searchParams.set("action", action); // 'like' or 'unlike'
124+
url.searchParams.set("postId", postId);
125+
const res = await fetch(url.toString());
128126
if (!res.ok) return null;
129127
const json = await res.json();
130128
return typeof json.likes === "number"

WebSite/tools/build-blog.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ const GISCUS_ENABLED =
7777
(GISCUS_REPO_ID && GISCUS_CATEGORY_ID);
7878

7979
// ---------------------------
80-
// POST / Like API ベース URL(任意)
81-
// 環境変数 POST_API_URL を設定するとクライアントが API に接続を試みます。
80+
// POST / Like API ベース URL
81+
// 環境変数 POST_API_URL を設定すると上書きします。
82+
// デフォルトは GAS Web App URL を使用。
8283
// ---------------------------
83-
const POST_API_URL = process.env.POST_API_URL || "";
84+
const POST_API_URL =
85+
process.env.POST_API_URL ||
86+
"https://script.google.com/macros/s/AKfycby8h3ZR4vBDs6fOk3hIF8jed8BhnzU0IesQmPgSqkOyzYK1TfkhX9HDyixyrBH7N_SR/exec";
8487

8588
// ---------------------------
8689
// helper: XML/HTML escape

0 commit comments

Comments
 (0)