|
| 1 | +/* @flow strict-local */ |
| 2 | +import template from './template'; |
| 3 | +import type { ThemeName, MessageSnapshot, UserOrBot, Auth } from '../../types'; |
| 4 | +import { shortTime, humanDate } from '../../utils/date'; |
| 5 | +import css from '../css/css'; |
| 6 | +import script from '../js/script'; |
| 7 | + |
| 8 | +const TOPIC_EDITED = 'TOPIC_EDITED'; |
| 9 | +const CONTENT_EDITED = 'CONTENT_EDITED'; |
| 10 | +const BOTH_EDITED = 'BOTH_EDITED'; |
| 11 | +const NOT_EDITED = 'NOT_EDITED'; |
| 12 | + |
| 13 | +const renderSnapshot = (snapshot: MessageSnapshot, usersById: Map<number, UserOrBot>): string => { |
| 14 | + const user = usersById.get(snapshot.user_id); |
| 15 | + const date = new Date(snapshot.timestamp * 1000); |
| 16 | + const editedTime = `${humanDate(date)} ${shortTime(date)}`; |
| 17 | + let displayedContent: string = ''; |
| 18 | + let tag: string = ''; |
| 19 | + let edit_flag; |
| 20 | + |
| 21 | + if (snapshot.prev_topic === undefined && snapshot.prev_content === undefined) { |
| 22 | + edit_flag = NOT_EDITED; |
| 23 | + } else if (snapshot.prev_content !== undefined && snapshot.prev_topic === undefined) { |
| 24 | + edit_flag = CONTENT_EDITED; |
| 25 | + } else if (snapshot.prev_content === undefined && snapshot.prev_topic !== undefined) { |
| 26 | + edit_flag = TOPIC_EDITED; |
| 27 | + } else { |
| 28 | + edit_flag = BOTH_EDITED; |
| 29 | + } |
| 30 | + |
| 31 | + switch (edit_flag) { |
| 32 | + case NOT_EDITED: { |
| 33 | + tag += template`<span class="message-tag">Original message</span>`; |
| 34 | + displayedContent = snapshot.content; |
| 35 | + break; |
| 36 | + } |
| 37 | + |
| 38 | + case TOPIC_EDITED: { |
| 39 | + tag += template`<span class="message-tag">Topic edited by ${ |
| 40 | + user ? user.full_name : 'unknown user' |
| 41 | + }</span>`; |
| 42 | + |
| 43 | + // prev_topic is always present for this case. $FlowFixMe. |
| 44 | + displayedContent = template`Topic: |
| 45 | + <span class="highlight_text_inserted">${snapshot.topic}</span> |
| 46 | + <span class="highlight_text_deleted">${snapshot.prev_topic}</span>`; |
| 47 | + break; |
| 48 | + } |
| 49 | + |
| 50 | + case CONTENT_EDITED: { |
| 51 | + tag += template`<span class="message-tag">Edited by ${ |
| 52 | + user ? user.full_name : 'unknown user' |
| 53 | + }</span>`; |
| 54 | + |
| 55 | + // If prev_content is present, then so is content_html_diff. $FlowFixMe. |
| 56 | + displayedContent += snapshot.content_html_diff; |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + case BOTH_EDITED: { |
| 61 | + tag += template`<span class="message-tag">Edited by ${ |
| 62 | + user ? user.full_name : 'unknown user' |
| 63 | + }</span>`; |
| 64 | + |
| 65 | + // If prev_content is present, then so is content_html_diff. $FlowFixMe. |
| 66 | + displayedContent += `Topic: ${snapshot.topic}<br/>${snapshot.content_html_diff}`; |
| 67 | + break; |
| 68 | + } |
| 69 | + |
| 70 | + default: |
| 71 | + // eslint-disable-next-line no-console |
| 72 | + console.error('Invalid edit_flag'); |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + return template` |
| 77 | + <div class="edit-history-block"> |
| 78 | + <div class="static-timestamp">${editedTime}</div> |
| 79 | + <span class="edit-history-content">$!${displayedContent}</span> |
| 80 | + <div class="message-tags">$!${tag}</div> |
| 81 | + </div>`; |
| 82 | +}; |
| 83 | + |
| 84 | +export default ( |
| 85 | + editHistory: MessageSnapshot[], |
| 86 | + theme: ThemeName, |
| 87 | + usersById: Map<number, UserOrBot>, |
| 88 | + auth: Auth, |
| 89 | +): string => { |
| 90 | + let html: string = ''; |
| 91 | + for (const snapshot of editHistory) { |
| 92 | + html += renderSnapshot(snapshot, usersById); |
| 93 | + } |
| 94 | + |
| 95 | + return template` |
| 96 | +$!${script(null, auth)} |
| 97 | +$!${css(theme)} |
| 98 | +<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> |
| 99 | +<body style="overflow-x: hidden;"> |
| 100 | +$!${html} |
| 101 | +</body>`; |
| 102 | +}; |
0 commit comments