import "../components/message"; import dayjs from 'dayjs'; import tpl_new_day from "../templates//new_day.js"; import { CustomElement } from './element.js'; import { __ } from '@converse/headless/i18n'; import { api } from "@converse/headless/converse-core"; import { html } from 'lit-element'; import { repeat } from 'lit-html/directives/repeat.js'; const i18n_no_history = __('No message history available.'); const tpl_message = (o) => html` `; // Return a TemplateResult indicating a new day if the passed in message is // more than a day later than its predecessor. function getDayIndicator (model) { const models = model.collection.models; const idx = models.indexOf(model); const prev_model = models[idx-1]; if (!prev_model || dayjs(model.get('time')).isAfter(dayjs(prev_model.get('time')), 'day')) { const day_date = dayjs(model.get('time')).startOf('day'); return tpl_new_day({ 'type': 'date', 'time': day_date.toISOString(), 'datestring': day_date.format("dddd MMM Do YYYY") }); } } function getHats (model) { if (model.get('type') === 'groupchat') { if (api.settings.get('muc_hats_from_vcard')) { const role = model.vcard ? model.vcard.get('role') : null; return role ? role.split(',') : []; } else { return model.occupant?.get('hats') || []; } } return []; } class MessageHistory extends CustomElement { static get properties () { return { chatview: { type: Object}, messages: { type: Array} } } render () { const msgs = this.messages; return msgs.length ? html`${repeat(msgs, m => m.get('id'), m => this.renderMessage(m)) }` : html`
${i18n_no_history}
`; } renderMessage (model) { // XXX: leaky abstraction "is_only_key" from converse-omemo if (model.get('dangling_retraction') || model.get('is_only_key')) { return ''; } const day = getDayIndicator(model); const templates = day ? [day] : []; const is_groupchat = model.get('type') === 'groupchat'; const chatbox = this.chatview.model; const message = tpl_message( Object.assign(model.toJSON(), { 'chatview': this.chatview, 'has_mentions': is_groupchat && model.get('sender') === 'them' && chatbox.isUserMentioned(model), 'hats': getHats(model), 'is_first_unread': chatbox.get('first_unread_id') === model.get('id'), 'is_me_message': model.isMeCommand(), 'is_retracted': model.get('retracted') || model.get('moderated') === 'retracted', 'occupant': model.occupant, 'username': model.getDisplayName(), model, })); return [...templates, message]; } } customElements.define('converse-message-history', MessageHistory);