chatcontrol-live-demo/chatapp.svg

453 lines
14 KiB
XML
Raw Normal View History

2022-08-16 09:56:32 +00:00
<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="300"
2022-08-21 15:53:31 +00:00
height="600"
viewBox="0 0 300 600"
2022-08-16 09:56:32 +00:00
onload="start_chat('Ven')"
overflow="auto"
>
<defs>
2022-08-21 12:07:57 +00:00
<symbol id="kaereste_foto" width="60" height="200">
<g style="fill: pink; stroke: pink;">
<circle cx="20" cy="20" r="20" />
<line x1="20" y1="40" x2="20" y2="60" />
<ellipse cx="20" cy="110" rx="10" ry="50" />
<!-- left arm -->
<polyline fill="none"
points="15,70 5,50 0,40" />
<!-- right arm -->
<polyline fill="none"
points="25,70 35,50 40,40" />
<!-- left leg -->
<polyline fill="none"
points="15,155 5,135 0,125" />
<!-- right leg -->
<polyline fill="none"
points="25,155 35,135 40,125" />
<!-- the thing -->
<text x="14" y="160" style="font: bold 20px sans-serif" fill="black">?</text>
</g>
</symbol>
2022-08-16 09:56:32 +00:00
</defs>
<script type="application/ecmascript">
<![CDATA[
'use strict';
2022-08-16 09:56:32 +00:00
const svgns = "http://www.w3.org/2000/svg";
// must be the same as the SVG dimensions
const width = 300;
2022-08-21 15:53:31 +00:00
const height = 550;
2022-08-16 09:56:32 +00:00
const line_height = 14;
const bubble_padding = 6;
const bubble_spacing = 5;
2022-08-21 15:53:31 +00:00
const visible_chat_area = {'top': 60,
'bottom': height - 100 // minus status & input areas
};
const typing_speed = 50;
2022-08-16 09:56:32 +00:00
var conversation_count = 0;
/// SVG 1.1 doesn't do proper text splitting into several lines.
/// we need to do it ourselves.
function split_text_into_lines(text, upper_line_length) {
let result = [];
while(text.length) {
if(text.length < upper_line_length) {
result.push(text);
break; // we are done
}
let found_split_point = false;
for(let i = upper_line_length; i; i--) {
if(text[i] == ' ') {
result.push(text.slice(0, i));
text = text.slice(i+1);
found_split_point = true;
break;
}
}
if(!found_split_point) {
// no <space> found. Split at character boundary instead
result.push(text.slice(0, upper_line_length));
text = text.slice(upper_line_length);
}
}
return result;
}
/// A class holding a text chat message.
2022-08-16 09:56:32 +00:00
///
/// *ChatMessages are owned by a Dialog.
function TextChatMessage(message_text, is_myself) {
2022-08-16 19:35:51 +00:00
let lines = split_text_into_lines(message_text, 28);
2022-08-21 12:07:57 +00:00
let bubble_color = (is_myself)? 'white': '#DDFF66';
2022-08-16 09:56:32 +00:00
let container = document.getElementById('messages');
// attributes
this.height = 0; // height on screen when fully visible
this.group = null; // a <g> with a transform=translate(0,y_shift) attribute
2022-08-16 09:56:32 +00:00
/// Render the chat message on the screen
this.draw = function(y_offset, y_shift) {
let group = create_svg_node('g', {'transform': `translate(0, ${y_shift})`});
2022-08-16 19:35:51 +00:00
let x = (is_myself ? 190 : 110);
let text = create_svg_node('text', {
'x': `${x}%`,
'font-size': `${line_height}px`,
'text-anchor': is_myself ? 'end' : 'start'
});
let height_so_far = y_offset;
2022-08-16 09:56:32 +00:00
lines.forEach(function(line) {
let y = height_so_far;
let tspan = create_svg_node('tspan', {
'x': `${x}%`,
'y': `${y + line_height}`, // important: y is lower text baseline
'fill': 'green',
'width': '5%'
});
2022-08-16 09:56:32 +00:00
tspan.appendChild(document.createTextNode(line));
2022-08-16 09:56:32 +00:00
text.appendChild(tspan);
height_so_far += line_height;
2022-08-16 09:56:32 +00:00
});
group.appendChild(text); // needs to be part of the DOM *now*
2022-08-21 12:07:57 +00:00
container.appendChild(group);
let bubble = create_bubble(text, bubble_color);
group.appendChild(bubble);
redraw_on_top(text);
this.height = bubble.getBBox().height + bubble_spacing;
this.group = group;
2022-08-16 09:56:32 +00:00
}
/// Move a chat message on the screen (to simulate scrolling)
this.shift_y_pos = function(by) {
redraw_on_top(document.getElementById('contact_name_box'));
this.group.setAttribute('transform', `translate(0, ${by})`);
}
}
/// A class holding a image-based chat message.
///
/// *ChatMessages are owned by a Dialog.
function ImageChatMessage(symbol_id, is_myself) {
2022-08-21 12:07:57 +00:00
let container = document.getElementById('messages');
let bubble_color = (is_myself)? 'white': '#DDFF66';
// attributes
this.height = 0; // height on screen when fully visible
this.group = null; // a <g> with a transform=translate(0,y_shift) attribute
this.draw = function(y_offset, y_shift) {
2022-08-21 12:07:57 +00:00
let group = create_svg_node('g', {'transform': `translate(0, ${y_shift})`});
let x = (is_myself ? 190 : 110);
let img = create_svg_node('use', {
'href': symbol_id,
x: `${x}%`,
y: y_offset,
});
group.appendChild(img); // needs to be part of the DOM *now*
container.appendChild(group);
let bubble = create_bubble(img, bubble_color);
group.appendChild(bubble);
redraw_on_top(img);
this.height = bubble.getBBox().height + bubble_spacing;
this.group = group;
}
this.shift_y_pos = function(by) {
2022-08-21 12:07:57 +00:00
redraw_on_top(document.getElementById('contact_name_box'));
this.group.setAttribute('transform', `translate(0, ${by})`);
2022-08-16 09:56:32 +00:00
}
}
function create_chat_message(content, is_myself) {
let constr = (content.startsWith('#'))? ImageChatMessage: TextChatMessage;
return new constr(content, is_myself);
2022-08-16 09:56:32 +00:00
}
/// Promise-based version of setTimeout
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
/// toggle viewport between message list and contact list
/// That means: show either the left or right side of the SVG
function swipe_viewport() {
let svg = document.getElementsByTagName('svg')[0];
let coords = svg.getAttribute('viewBox').split(' ');
let x = parseInt(coords.shift());
if(x == 0) {
// moving from messages to contacts
2022-08-16 09:56:32 +00:00
var step = 5;
var end = width;
} else {
// moving from contacts to messages
2022-08-16 09:56:32 +00:00
var step = -5;
var end = 0;
}
let viewBox_suffix = coords.join(' '); // only 3 elements
async function animate(resolve, reject) {
while(x!=end) {
await wait(1);
x += step;
svg.setAttribute('viewBox', `${x} ${viewBox_suffix}`);
}
resolve();
};
new Promise(animate);
}
// Create a chat bubble around an element.
//
// The element must already be inside the DOM for this to work.
function create_bubble(inner_element, color) {
const bbox = inner_element.getBBox();
let bubble = create_svg_node('rect', {
'x': bbox.x - bubble_padding,
2022-08-21 15:53:31 +00:00
'y': bbox.y - bubble_padding,
'width': bbox.width + 2 * bubble_padding,
'height': bbox.height + 2 * bubble_padding,
'rx': 8,
'fill': color,
});
return bubble;
}
function create_svg_node(tag_name, attrs) {
let node = document.createElementNS(svgns, tag_name);
for(let attr in attrs) {
node.setAttribute(attr, attrs[attr]);
}
return node;
}
/// Ensure that an element is redrawn above all other elements
///
/// The element's children will also be redrawn on top
function redraw_on_top(element) {
let parent = element.parentNode;
parent.appendChild(parent.removeChild(element));
}
2022-08-16 09:56:32 +00:00
function removeAllChildren(parentNode) {
while(parentNode.firstChild) {
parentNode.removeChild(parentNode.lastChild);
}
}
function start_chat(who) {
let indicator = document.getElementById('contact_indicator');
indicator.childNodes[0].data = `Kontakt: ${who}`;
removeAllChildren(document.getElementById('messages'));
swipe_viewport();
switch(who) {
case 'Ven':
dialog_ven();
break;
case 'Mor':
dialog_mor();
break;
case 'Kæreste':
dialog_kaereste();
break;
default:
2022-08-21 15:53:31 +00:00
alert(`kontakten ${who} er ikke implementeret endnu`);
2022-08-16 09:56:32 +00:00
}
}
function Dialog(chat_partner) {
let conversation_id = ++conversation_count;
this.messages = [];
this.y_shift = visible_chat_area.top; // for scrolling
this.all_elements_height = 0; // also for scrolling
2022-08-16 09:56:32 +00:00
async function post_message(dialog, message, is_myself) {
2022-08-21 15:53:31 +00:00
if(!is_myself && dialog.messages.length) {
await wait(message.length * typing_speed);
} // first message should be instant
2022-08-16 09:56:32 +00:00
if(conversation_id != conversation_count) {
return; // Do not add messages to old conversation
}
let chat_message = create_chat_message(message, is_myself);
chat_message.draw(dialog.all_elements_height, // offset
dialog.y_shift);
dialog.all_elements_height += chat_message.height;
2022-08-16 09:56:32 +00:00
dialog.messages.push(chat_message);
let bottom = dialog.all_elements_height + dialog.y_shift;
let scroll = Math.max(0, bottom - visible_chat_area.bottom);
while(scroll > line_height) {
dialog.y_shift -= line_height;
scroll -= line_height;
dialog.messages.forEach(msg => msg.shift_y_pos(dialog.y_shift));
2022-08-16 09:56:32 +00:00
await wait(50);
}
}
2022-08-21 15:53:31 +00:00
this.me = async function(message, will_be_flagged) {
await wait(1500); // give me some time to read
await type_message(message);
hit_send_button();
await local_message_processing(will_be_flagged);
clear_input_field();
release_send_button();
2022-08-16 09:56:32 +00:00
await post_message(this, message, true);
}
this.you = async function(message) {
await post_message(this, message, false);
}
}
2022-08-21 15:53:31 +00:00
async function type_message(message) {
clear_input_field(); // just to be sure
let text = document.getElementById('message_input_data').firstChild;
let lines = split_text_into_lines(message, 22);
for(let line of lines) {
text.data = '';
for(let c of line) {
await wait(typing_speed);
text.data += c;
}
await wait(20); // otherwise it's too fast
}
}
function hit_send_button() {
document.getElementById('message_submit_button').setAttribute('fill', 'white');
document.getElementById('message_submit_box').setAttribute('fill', '#888888');
}
function release_send_button() {
document.getElementById('message_submit_button').setAttribute('fill', 'black');
document.getElementById('message_submit_box').setAttribute('fill', '#CCCCCC');
}
function clear_input_field() {
let element = document.getElementById('message_input_data')
removeAllChildren(element);
element.appendChild(document.createTextNode(''));
}
async function local_message_processing(will_be_flagged) {
let text_element = document.getElementById('message_status');
let indicator = text_element.childNodes[0];
indicator.data = 'scanning for illegal content';
await wait(1500);
if(will_be_flagged) {
let box = document.getElementById('message_status_box');
text_element.setAttribute('fill', 'red');
box.setAttribute('fill', 'yellow');
indicator.data = 'Reporting suspicious content';
await wait(1500);
text_element.setAttribute('fill', 'white');
box.setAttribute('fill', 'blue');
}
indicator.data = 'encrypting message';
await wait(200);
indicator.data = 'sending message';
await wait(200);
indicator.data = 'message was sent';
}
2022-08-16 09:56:32 +00:00
async function dialog_ven() {
let d = new Dialog('Ven');
await d.me("hej");
await d.you("tak for sidst!");
await d.you('Har du hørt om den nye EU lov "ChatControl"?');
await d.me("nej, det har jeg ikke");
await d.me("hvad handler det om?");
2022-08-16 10:20:42 +00:00
await d.you('EU kommissionen planlægger at læse alle chatbeskeder i EU');
2022-08-16 09:56:32 +00:00
await d.me('Ja, men vi krypterer jo vores beskeder? Tough luck!');
2022-08-16 10:20:42 +00:00
await d.you('Det tager de højde for. Aflytningen vil ske før krypteringen på din telefon!');
await d.me('Det lyder overhovedet ikke rart. Hvorfor vil de gøre det?');
2022-08-16 09:56:32 +00:00
await d.you('De siger at det er for at beskytte børn på nettet.');
await d.you('Men det giver ikke meget mening');
2022-08-21 15:53:31 +00:00
await d.me('Alle vores beskeder skal scannes på grund af børn på nettet? Det lyder dumt!', true);
2022-08-16 10:20:42 +00:00
await d.you('Her kan du læse mere om det:');
2022-08-16 09:56:32 +00:00
await d.you('https://chatcontrol.eu');
}
2022-08-16 09:56:32 +00:00
async function dialog_mor() {
let d = new Dialog('Mor');
await d.you("Jeg har fundet nogle gamle familiebilleder fra vores ferie 10 år siden");
2022-08-16 10:20:42 +00:00
await d.you("Her leger du på stranden");
2022-08-16 09:56:32 +00:00
await d.you("(naked stick figure image here)");
}
async function dialog_kaereste() {
let d = new Dialog('Kæreste');
await d.me('hej smukkeste');
await d.you('hej, jeg har lige tænkt på dig!');
2022-08-16 10:20:42 +00:00
await d.you('og derfor har jeg taget et sexet billede');
2022-08-21 12:07:57 +00:00
await d.you('#kaereste_foto');
2022-08-21 15:53:31 +00:00
await d.me('tak. Det har jeg savnet!', true);
2022-08-21 12:07:57 +00:00
await d.me('du ser stadig så ung ud.');
await d.me('Jeg tror at vi skal være forsigtige, fordi der er en overvågning af vores beskeder');
await d.you('What the fuck??? Hvem overvågner os?????');
2022-08-21 15:53:31 +00:00
await d.me('Det kan være at chattilbyderen gør det, fordi de har lov til det');
await d.me('Men i fremtiden vil det faktisk blive forpligtende at scanne beskeder i EU\'en');
2022-08-21 12:07:57 +00:00
await d.me('Og hvis der er beskeder som "ligner" børneborno får politiet en kopi af dem');
await d.you('Jeg vil hellere ikke at politiet får mine nøgenbilleder. Det er min krop og jeg vil selv bestemme over det!');
2022-08-16 09:56:32 +00:00
}
]]>
</script>
<!-- [right] contact name view -->
<g id="contact_name_box">
<rect x="300" y="0" width="300" height="50" fill="yellow" />
<polyline points="300,20 320,0 320,40" fill="blue" onclick="swipe_viewport()" /> <!-- XXX: these polyline points depend on the svg width -->
<text x="375" y="30" id="contact_indicator">loading...</text>
</g>
2022-08-16 09:56:32 +00:00
<!-- [right] messages view -->
<rect x="300" y="50" width="300" height="450" fill="aqua" />
2022-08-16 09:56:32 +00:00
<g id="messages"></g>
2022-08-21 15:53:31 +00:00
<!-- [right] message status -->
<rect id="message_status_box" x="300" y="500" fill="blue" width="300" height="50" />
<text id="message_status" x="310" y="530" fill="white">status</text>
<!-- [right] text input -->
<rect id="message_input_box" x="300" y="550" stroke="black" fill="white" width="250" height="50" />
<rect id="message_submit_box" x="550" y="550" stroke="black" fill="#CCCCCC" width="50" height="50" />
<text id="message_input_data" x="310" y="575" fill="black"></text>
<text id="message_submit_button" x="555" y="580" fill="black">SEND</text>
<!-- [left] contact list -->
<rect x="0" y="0" width="300" height="50" fill="yellow" />
<text x="60" y="30">Dine kontakter</text>
2022-08-21 15:53:31 +00:00
<rect x="0" y="50" width="300" height="550" style="stroke: green; stroke-width: 10px" fill="aqua" />
2022-08-16 09:56:32 +00:00
<text x="15" y="75" onclick="start_chat('Ven')">Ven</text>
<text x="15" y="100" onclick="start_chat('Mor')">Mor</text>
<text x="15" y="125" onclick="start_chat('Kæreste')">Kæreste&#10084;</text>
2022-08-21 15:53:31 +00:00
<text x="15" y="150" onclick="start_chat('Politi')">Politi</text>
<text x="15" y="175" onclick="start_chat('Tekniker')">Chat Support</text>
<text x="15" y="200" onclick="start_chat('Nabopige')">nabopige</text>
2022-08-16 09:56:32 +00:00
</svg>