tractatus/public/js/admin/comment-analysis.js
TheFlow ef6cfb4a2a feat(content): add framework-guided blog pre-publication and comment analysis
Blog Pre-Publication Workflow:
- New admin interface (blog-pre-publication.html) for framework-guided content review
- Analysis provides: sensitivity check, compliance validation, audience analysis
- Publication guidance: timing, monitoring, action recommendations
- Response templates for anticipated reader feedback
- Overall recommendation: APPROVE/REVIEW/REJECT decision
- CSP-compliant implementation (no inline scripts/styles)

Comment & Feedback Analysis Workflow:
- New admin interface (comment-analysis.html) for social media/article feedback
- Sentiment analysis (positive/negative/neutral/mixed with confidence)
- Values alignment check (aligned values, concerns, misunderstandings)
- Risk assessment (low/medium/high with factors)
- Recommended responses (prioritized with rationale)
- Framework guidance on whether/how to respond

Backend Implementation:
- New controller: framework-content-analysis.controller.js
- Services invoked: PluralisticDeliberationOrchestrator, BoundaryEnforcer
- API routes: /api/admin/blog/analyze, /api/admin/feedback/analyze
- Integration with existing auth and validation middleware

Framework Validation:
During implementation, framework caught and blocked TWO CSP violations:
1. Inline onclick attribute - forced addEventListener pattern
2. Inline style attribute - forced data attributes + JavaScript
This demonstrates framework is actively preventing violations in real-time.

Transforms blog curation from passive reporter to active agency manager.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-27 19:45:43 +13:00

421 lines
14 KiB
JavaScript

/**
* Comment & Feedback Analysis
* Framework-guided analysis of social media comments and article feedback
*/
// Get auth token
function getAuthToken() {
return localStorage.getItem('admin_token');
}
// Analyze feedback
async function analyzeFeedback() {
const source = document.getElementById('feedback-source').value;
const relatedPost = document.getElementById('related-post').value.trim();
const content = document.getElementById('feedback-content').value.trim();
const notes = document.getElementById('your-notes').value.trim();
if (!content) {
alert('Please enter feedback content');
return;
}
if (!source) {
alert('Please select a source platform');
return;
}
// Show loading state
const analyzeBtn = document.getElementById('analyze-feedback-btn');
analyzeBtn.disabled = true;
analyzeBtn.innerHTML = '<svg class="animate-spin w-5 h-5 inline-block mr-2" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg> Analyzing...';
try {
const response = await fetch('/api/admin/feedback/analyze', {
method: 'POST',
headers: {
'Authorization': `Bearer ${getAuthToken()}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ source, relatedPost, content, notes })
});
const data = await response.json();
if (data.success) {
displayResults(data.analysis);
} else {
alert('Analysis failed: ' + (data.error || 'Unknown error'));
}
} catch (error) {
console.error('Analysis error:', error);
alert('Failed to analyze feedback. Please try again.');
} finally {
analyzeBtn.disabled = false;
analyzeBtn.innerHTML = '<svg class="w-5 h-5 inline-block mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"/></svg>Analyze with Framework';
}
}
// Display analysis results
function displayResults(analysis) {
const resultsSection = document.getElementById('results-section');
resultsSection.classList.remove('hidden');
// Sentiment analysis
const sentimentEl = document.getElementById('sentiment-result');
sentimentEl.innerHTML = renderSentiment(analysis.sentiment);
// Values & concerns
const valuesEl = document.getElementById('values-result');
valuesEl.innerHTML = renderValues(analysis.values);
// Risk assessment
const riskEl = document.getElementById('risk-result');
riskEl.innerHTML = renderRisk(analysis.risk);
// Recommended responses
const responseEl = document.getElementById('response-options');
responseEl.innerHTML = renderResponses(analysis.responses);
// Framework guidance
const guidanceEl = document.getElementById('framework-guidance');
guidanceEl.innerHTML = renderGuidance(analysis.guidance);
// Setup copy handlers for responses
setupResponseCopyHandlers();
// Scroll to results
resultsSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
// Render sentiment analysis
function renderSentiment(sentiment) {
const sentimentColors = {
positive: { bg: 'bg-green-100', text: 'text-green-700', icon: '😊' },
neutral: { bg: 'bg-gray-100', text: 'text-gray-700', icon: '😐' },
negative: { bg: 'bg-red-100', text: 'text-red-700', icon: '😟' },
mixed: { bg: 'bg-yellow-100', text: 'text-yellow-700', icon: '🤔' }
};
const colors = sentimentColors[sentiment.overall] || sentimentColors.neutral;
let html = `
<div class="flex items-center mb-4">
<div class="text-4xl mr-3">${colors.icon}</div>
<div>
<div class="text-lg font-semibold ${colors.text}">${sentiment.overall.charAt(0).toUpperCase() + sentiment.overall.slice(1)} Sentiment</div>
<div class="text-sm text-gray-600">${sentiment.summary}</div>
</div>
</div>
`;
if (sentiment.confidence) {
html += `
<div class="mb-3">
<div class="text-xs font-medium text-gray-500 mb-1">Confidence Level</div>
<div class="flex items-center">
<div class="flex-1 h-2 bg-gray-200 rounded-full">
<div class="h-2 ${colors.bg} rounded-full" data-width="${sentiment.confidence}"></div>
</div>
<span class="ml-3 text-sm font-semibold">${sentiment.confidence}%</span>
</div>
</div>
`;
}
if (sentiment.keyPhrases && sentiment.keyPhrases.length > 0) {
html += `
<div>
<div class="text-xs font-medium text-gray-500 mb-2">Key Phrases</div>
<div class="flex flex-wrap gap-2">
${sentiment.keyPhrases.map(phrase =>
`<span class="px-2 py-1 ${colors.bg} ${colors.text} rounded-full text-xs">${phrase}</span>`
).join('')}
</div>
</div>
`;
}
return html;
}
// Render values & concerns
function renderValues(values) {
let html = '<div class="space-y-3">';
if (values.alignedWith && values.alignedWith.length > 0) {
html += `
<div>
<div class="text-xs font-medium text-green-600 mb-1 flex items-center">
<svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
</svg>
Aligned With Our Values
</div>
<ul class="list-disc list-inside text-sm text-gray-700 space-y-1">
${values.alignedWith.map(v => `<li>${v}</li>`).join('')}
</ul>
</div>
`;
}
if (values.concernsRaised && values.concernsRaised.length > 0) {
html += `
<div>
<div class="text-xs font-medium text-orange-600 mb-1 flex items-center">
<svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
</svg>
Concerns Raised
</div>
<ul class="list-disc list-inside text-sm text-gray-700 space-y-1">
${values.concernsRaised.map(c => `<li>${c}</li>`).join('')}
</ul>
</div>
`;
}
if (values.misunderstandings && values.misunderstandings.length > 0) {
html += `
<div>
<div class="text-xs font-medium text-blue-600 mb-1 flex items-center">
<svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
Potential Misunderstandings
</div>
<ul class="list-disc list-inside text-sm text-gray-700 space-y-1">
${values.misunderstandings.map(m => `<li>${m}</li>`).join('')}
</ul>
</div>
`;
}
html += '</div>';
return html;
}
// Render risk assessment
function renderRisk(risk) {
const riskLevels = {
low: { color: 'text-green-600', bg: 'bg-green-50', icon: '✅' },
medium: { color: 'text-yellow-600', bg: 'bg-yellow-50', icon: '⚠️' },
high: { color: 'text-red-600', bg: 'bg-red-50', icon: '🚨' }
};
const level = riskLevels[risk.level] || riskLevels.low;
let html = `
<div class="mb-3">
<div class="flex items-center">
<span class="text-2xl mr-2">${level.icon}</span>
<span class="${level.color} font-semibold text-lg">${risk.level.toUpperCase()} Risk</span>
</div>
<p class="text-sm text-gray-700 mt-2">${risk.summary}</p>
</div>
`;
if (risk.factors && risk.factors.length > 0) {
html += `
<div class="mb-3">
<div class="text-xs font-medium text-gray-500 mb-2">Risk Factors</div>
<ul class="list-disc list-inside text-sm text-gray-700 space-y-1">
${risk.factors.map(f => `<li>${f}</li>`).join('')}
</ul>
</div>
`;
}
if (risk.recommendations && risk.recommendations.length > 0) {
html += `
<div class="p-3 ${level.bg} rounded border border-gray-200">
<div class="text-xs font-medium text-gray-700 mb-2">Recommended Actions</div>
<ul class="list-disc list-inside text-xs text-gray-700 space-y-1">
${risk.recommendations.map(r => `<li>${r}</li>`).join('')}
</ul>
</div>
`;
}
return html;
}
// Render recommended responses
function renderResponses(responses) {
if (!responses || responses.length === 0) {
return '<p class="text-sm text-gray-500 italic">No response recommendations generated</p>';
}
return responses.map((response, index) => {
const priorityColors = {
high: 'border-red-300 bg-red-50',
medium: 'border-yellow-300 bg-yellow-50',
low: 'border-gray-300 bg-gray-50'
};
const borderColor = priorityColors[response.priority] || priorityColors.low;
return `
<div class="border-2 ${borderColor} rounded-lg p-4 response-card" data-response-index="${index}">
<div class="flex items-start justify-between mb-2">
<div>
<div class="font-semibold text-gray-900">${response.approach}</div>
${response.priority ? `<div class="text-xs text-gray-600 mt-1">Priority: ${response.priority}</div>` : ''}
</div>
<button class="copy-response-btn text-xs text-blue-600 hover:text-blue-800 font-medium">Copy</button>
</div>
<div class="text-sm text-gray-700 mb-3 response-text">${response.text}</div>
${response.rationale ? `<div class="text-xs text-gray-600 italic">${response.rationale}</div>` : ''}
</div>
`;
}).join('');
}
// Render framework guidance
function renderGuidance(guidance) {
let html = '';
if (guidance.shouldRespond !== undefined) {
const respondIcon = guidance.shouldRespond ? '✅' : '❌';
const respondText = guidance.shouldRespond ? 'Yes - Response Recommended' : 'No - Consider Not Responding';
const respondColor = guidance.shouldRespond ? 'text-green-700' : 'text-red-700';
html += `
<div class="mb-3">
<span class="${respondColor} font-semibold">${respondIcon} ${respondText}</span>
</div>
`;
}
if (guidance.keyConsiderations && guidance.keyConsiderations.length > 0) {
html += `
<div class="mb-3">
<div class="text-sm font-medium text-gray-700 mb-2">Key Considerations</div>
<ul class="list-disc list-inside text-sm text-gray-700 space-y-1">
${guidance.keyConsiderations.map(k => `<li>${k}</li>`).join('')}
</ul>
</div>
`;
}
if (guidance.tone) {
html += `
<div class="p-3 bg-blue-50 rounded border border-blue-200">
<div class="text-xs font-medium text-gray-700 mb-1">Recommended Tone</div>
<div class="text-sm text-gray-700">${guidance.tone}</div>
</div>
`;
}
return html || '<p class="text-sm text-gray-500 italic">No framework guidance available</p>';
}
// Setup copy handlers for responses (CSP-compliant)
function setupResponseCopyHandlers() {
document.querySelectorAll('.copy-response-btn').forEach(btn => {
btn.addEventListener('click', function(e) {
e.stopPropagation();
const card = this.closest('.response-card');
const text = card.querySelector('.response-text').textContent;
navigator.clipboard.writeText(text).then(() => {
const originalText = this.textContent;
this.textContent = 'Copied!';
this.classList.add('text-green-600');
setTimeout(() => {
this.textContent = originalText;
this.classList.remove('text-green-600');
}, 2000);
});
});
});
}
// Apply dynamic styles using data attributes (CSP-compliant)
function applyDynamicStyles() {
document.querySelectorAll('[data-width]').forEach(el => {
const width = el.getAttribute('data-width');
el.style.width = width + '%';
});
}
// Save analysis
async function saveAnalysis() {
const source = document.getElementById('feedback-source').value;
const relatedPost = document.getElementById('related-post').value.trim();
const content = document.getElementById('feedback-content').value.trim();
const notes = document.getElementById('your-notes').value.trim();
if (!content) {
alert('Please enter feedback content before saving');
return;
}
try {
const response = await fetch('/api/admin/feedback/save', {
method: 'POST',
headers: {
'Authorization': `Bearer ${getAuthToken()}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ source, relatedPost, content, notes })
});
const data = await response.json();
if (data.success) {
alert('Analysis saved successfully!');
} else {
alert('Failed to save analysis: ' + (data.error || 'Unknown error'));
}
} catch (error) {
console.error('Save error:', error);
alert('Failed to save analysis. Please try again.');
}
}
// Export report
async function exportReport() {
const source = document.getElementById('feedback-source').value;
const relatedPost = document.getElementById('related-post').value.trim();
const content = document.getElementById('feedback-content').value.trim();
if (!content) {
alert('Please analyze feedback before exporting');
return;
}
try {
const response = await fetch('/api/admin/feedback/export', {
method: 'POST',
headers: {
'Authorization': `Bearer ${getAuthToken()}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ source, relatedPost, content })
});
if (response.ok) {
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `feedback-analysis-${Date.now()}.pdf`;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
} else {
alert('Failed to export report');
}
} catch (error) {
console.error('Export error:', error);
alert('Failed to export report. Please try again.');
}
}
// Event listeners
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('analyze-feedback-btn').addEventListener('click', analyzeFeedback);
document.getElementById('save-analysis-btn')?.addEventListener('click', saveAnalysis);
document.getElementById('export-report-btn')?.addEventListener('click', exportReport);
});