|
| 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 getHtml from './html'; |
| 6 | + |
| 7 | +type HtmlContent = { |
| 8 | + tagHtml: string, |
| 9 | + contentHtml: string, |
| 10 | +}; |
| 11 | + |
| 12 | +const geHtmlFromSnapshot = (snapshot: MessageSnapshot, user: UserOrBot | void): HtmlContent => { |
| 13 | + const { prev_content, prev_topic, content_html_diff, topic, rendered_content } = snapshot; |
| 14 | + |
| 15 | + if (prev_content !== undefined && prev_topic === undefined && content_html_diff !== undefined) { |
| 16 | + // Only content edited. |
| 17 | + return { |
| 18 | + tagHtml: template`<span class="message-tag">Edited by ${ |
| 19 | + user ? user.full_name : 'unknown user' |
| 20 | + }</span>`, |
| 21 | + contentHtml: content_html_diff, |
| 22 | + }; |
| 23 | + } else if (prev_content === undefined && prev_topic !== undefined) { |
| 24 | + // Only topic edited. |
| 25 | + return { |
| 26 | + tagHtml: template`<span class="message-tag">Topic edited by ${ |
| 27 | + user ? user.full_name : 'unknown user' |
| 28 | + }</span>`, |
| 29 | + contentHtml: template`Topic: |
| 30 | + <span class="highlight_text_inserted">${topic}</span> |
| 31 | + <span class="highlight_text_deleted">${prev_topic}</span>`, |
| 32 | + }; |
| 33 | + } else if (prev_topic !== undefined && content_html_diff !== undefined) { |
| 34 | + return { |
| 35 | + // Both topic and content edited. |
| 36 | + tagHtml: template`<span class="message-tag">Edited by ${ |
| 37 | + user ? user.full_name : 'unknown user' |
| 38 | + }</span>`, |
| 39 | + contentHtml: template`Topic: |
| 40 | + <span class="highlight_text_inserted">${topic}</span> |
| 41 | + <span class="highlight_text_deleted">${prev_topic}</span> |
| 42 | + <br/>$!${content_html_diff}`, |
| 43 | + }; |
| 44 | + } else { |
| 45 | + // Original message. |
| 46 | + return { |
| 47 | + tagHtml: template`<span class="message-tag">Original message</span>`, |
| 48 | + contentHtml: rendered_content, |
| 49 | + }; |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +const renderSnapshot = (snapshot: MessageSnapshot, usersById: Map<number, UserOrBot>): string => { |
| 54 | + const user = usersById.get(snapshot.user_id); |
| 55 | + const date = new Date(snapshot.timestamp * 1000); |
| 56 | + const editedTime = template`${humanDate(date)} ${shortTime(date)}`; |
| 57 | + const { tagHtml, contentHtml } = geHtmlFromSnapshot(snapshot, user); |
| 58 | + |
| 59 | + return template` |
| 60 | + <div class="edit-history-block"> |
| 61 | + <div class="static-timestamp">${editedTime}</div> |
| 62 | + <span class="edit-history-content">$!${contentHtml}</span> |
| 63 | + <div class="message-tags">$!${tagHtml}</div> |
| 64 | + </div>`; |
| 65 | +}; |
| 66 | + |
| 67 | +export default ( |
| 68 | + editHistory: MessageSnapshot[], |
| 69 | + themeName: ThemeName, |
| 70 | + usersById: Map<number, UserOrBot>, |
| 71 | + auth: Auth, |
| 72 | +): string => { |
| 73 | + let html: string = ''; |
| 74 | + for (const snapshot of editHistory) { |
| 75 | + html += renderSnapshot(snapshot, usersById); |
| 76 | + } |
| 77 | + return getHtml(themeName, { scrollMessageId: null, auth }, html); |
| 78 | +}; |
0 commit comments