feat(ntfy): Phase 2 — sendPushToUsers() ntfy 연동 + 구독 관리 UI

- ntfy_subscriptions 테이블 마이그레이션 추가
- ntfySender.js 유틸 (ntfy HTTP POST 래퍼)
- sendPushToUsers() ntfy 우선 분기 (ntfy 구독자 → ntfy, 나머지 → Web Push)
- ntfy subscribe/unsubscribe/status API 엔드포인트
- notification-bell.js ntfy 토글 버튼 + 앱 설정 안내 모달
- docker-compose system1-api에 NTFY 환경변수 추가

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Hyungi Ahn
2026-03-17 15:01:03 +09:00
parent e50ff3fb63
commit 1cef745cc9
8 changed files with 302 additions and 13 deletions

View File

@@ -43,6 +43,64 @@ const pushSubscriptionController = {
console.error('Push 구독 해제 오류:', error);
res.status(500).json({ success: false, message: 'Push 구독 해제 중 오류가 발생했습니다.' });
}
},
// === ntfy ===
// ntfy 구독 등록
async ntfySubscribe(req, res) {
try {
const userId = req.user?.id;
await pushSubscriptionModel.ntfySubscribe(userId);
const topic = `tkfactory-user-${userId}`;
res.json({
success: true,
message: 'ntfy 구독이 등록되었습니다.',
data: {
topic,
serverUrl: process.env.NTFY_EXTERNAL_URL || 'https://ntfy.technicalkorea.net',
username: 'subscriber',
password: 'tkfactory-sub-2026'
}
});
} catch (error) {
console.error('ntfy 구독 오류:', error);
res.status(500).json({ success: false, message: 'ntfy 구독 중 오류가 발생했습니다.' });
}
},
// ntfy 구독 해제
async ntfyUnsubscribe(req, res) {
try {
const userId = req.user?.id;
await pushSubscriptionModel.ntfyUnsubscribe(userId);
res.json({ success: true, message: 'ntfy 구독이 해제되었습니다.' });
} catch (error) {
console.error('ntfy 구독 해제 오류:', error);
res.status(500).json({ success: false, message: 'ntfy 구독 해제 중 오류가 발생했습니다.' });
}
},
// ntfy 구독 상태 확인
async ntfyStatus(req, res) {
try {
const userId = req.user?.id;
const subscribed = await pushSubscriptionModel.isNtfySubscribed(userId);
const topic = `tkfactory-user-${userId}`;
res.json({
success: true,
data: {
subscribed,
topic,
serverUrl: process.env.NTFY_EXTERNAL_URL || 'https://ntfy.technicalkorea.net'
}
});
} catch (error) {
console.error('ntfy 상태 확인 오류:', error);
res.status(500).json({ success: false, message: 'ntfy 상태 확인 중 오류가 발생했습니다.' });
}
}
};

View File

@@ -0,0 +1,5 @@
-- ntfy 구독 테이블
CREATE TABLE IF NOT EXISTS ntfy_subscriptions (
user_id INT PRIMARY KEY,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

View File

@@ -24,19 +24,44 @@ function getWebPush() {
return vapidConfigured ? webpush : null;
}
// Push 전송 헬퍼 — 알림 생성 후 호출
// Push 전송 헬퍼 — 알림 생성 후 호출 (ntfy 우선, 나머지 Web Push)
async function sendPushToUsers(userIds, payload) {
const wp = getWebPush();
if (!wp) return;
const pushModel = require('./pushSubscriptionModel');
const { sendNtfy } = require('../utils/ntfySender');
try {
const pushModel = require('./pushSubscriptionModel');
const subscriptions = userIds && userIds.length > 0
? await pushModel.getByUserIds(userIds)
: await pushModel.getAll(); // broadcast
// 1) ntfy 구독자 분리
const ntfyUserIds = userIds && userIds.length > 0
? await pushModel.getNtfyUserIds(userIds)
: await pushModel.getAllNtfyUserIds();
const ntfySet = new Set(ntfyUserIds);
// 2) ntfy 병렬 발송
if (ntfyUserIds.length > 0) {
await Promise.allSettled(
ntfyUserIds.map(uid =>
sendNtfy(uid, { title: payload.title, body: payload.body, url: payload.url })
)
);
}
// 3) Web Push — ntfy 구독자 제외
const wp = getWebPush();
if (!wp) return;
let subscriptions;
if (userIds && userIds.length > 0) {
const webPushUserIds = userIds.filter(id => !ntfySet.has(id));
subscriptions = webPushUserIds.length > 0
? await pushModel.getByUserIds(webPushUserIds)
: [];
} else {
// broadcast: 전체 구독 가져온 뒤 ntfy 사용자 제외
subscriptions = (await pushModel.getAll()).filter(s => !ntfySet.has(s.user_id));
}
const payloadStr = JSON.stringify(payload);
for (const sub of subscriptions) {
try {
await wp.sendNotification({
@@ -44,7 +69,6 @@ async function sendPushToUsers(userIds, payload) {
keys: { p256dh: sub.p256dh, auth: sub.auth }
}, payloadStr);
} catch (err) {
// 만료 구독 (410 Gone, 404 Not Found) 자동 정리
if (err.statusCode === 410 || err.statusCode === 404) {
await pushModel.deleteByEndpoint(sub.endpoint).catch(() => {});
}

View File

@@ -46,6 +46,46 @@ const pushSubscriptionModel = {
async deleteByEndpoint(endpoint) {
const db = await getDb();
await db.query('DELETE FROM push_subscriptions WHERE endpoint = ?', [endpoint]);
},
// === ntfy 구독 관련 ===
async getNtfyUserIds(userIds) {
if (!userIds || userIds.length === 0) return [];
const db = await getDb();
const [rows] = await db.query(
'SELECT user_id FROM ntfy_subscriptions WHERE user_id IN (?)',
[userIds]
);
return rows.map(r => r.user_id);
},
async getAllNtfyUserIds() {
const db = await getDb();
const [rows] = await db.query('SELECT user_id FROM ntfy_subscriptions');
return rows.map(r => r.user_id);
},
async ntfySubscribe(userId) {
const db = await getDb();
await db.query(
'INSERT IGNORE INTO ntfy_subscriptions (user_id) VALUES (?)',
[userId]
);
},
async ntfyUnsubscribe(userId) {
const db = await getDb();
await db.query('DELETE FROM ntfy_subscriptions WHERE user_id = ?', [userId]);
},
async isNtfySubscribed(userId) {
const db = await getDb();
const [rows] = await db.query(
'SELECT 1 FROM ntfy_subscriptions WHERE user_id = ? LIMIT 1',
[userId]
);
return rows.length > 0;
}
};

View File

@@ -11,4 +11,9 @@ router.get('/vapid-public-key', pushController.getVapidPublicKey);
router.post('/subscribe', requireAuth, pushController.subscribe);
router.delete('/unsubscribe', requireAuth, pushController.unsubscribe);
// ntfy 구독 관리
router.post('/ntfy/subscribe', requireAuth, pushController.ntfySubscribe);
router.delete('/ntfy/unsubscribe', requireAuth, pushController.ntfyUnsubscribe);
router.get('/ntfy/status', requireAuth, pushController.ntfyStatus);
module.exports = router;

View File

@@ -0,0 +1,31 @@
// utils/ntfySender.js — ntfy HTTP POST 래퍼
const NTFY_BASE_URL = process.env.NTFY_BASE_URL || 'http://ntfy:80';
const NTFY_PUBLISH_TOKEN = process.env.NTFY_PUBLISH_TOKEN;
const TKFB_BASE_URL = process.env.TKFB_BASE_URL || 'https://tkfb.technicalkorea.net';
async function sendNtfy(userId, { title, body, url }) {
if (!NTFY_PUBLISH_TOKEN) return;
const topic = `tkfactory-user-${userId}`;
const headers = {
'Authorization': `Bearer ${NTFY_PUBLISH_TOKEN}`,
'Title': title || '',
'Tags': 'bell',
};
if (url) {
headers['Click'] = url.startsWith('http') ? url : `${TKFB_BASE_URL}${url}`;
}
try {
const resp = await fetch(`${NTFY_BASE_URL}/${topic}`, {
method: 'POST',
headers,
body: body || '',
});
if (!resp.ok) console.warn(`[ntfy] ${userId} 발송 실패: ${resp.status}`);
} catch (e) {
console.error(`[ntfy] ${userId} 발송 오류:`, e.message);
}
}
module.exports = { sendNtfy };