2020-08-12 17:57:39 +02:00
|
|
|
import "./message";
|
2020-05-15 14:33:31 +02:00
|
|
|
import dayjs from 'dayjs';
|
2021-03-26 10:34:51 +01:00
|
|
|
import tpl_new_day from "./templates/new-day.js";
|
2021-03-24 11:59:09 +01:00
|
|
|
import { CustomElement } from 'shared/components/element.js';
|
2020-12-03 13:40:30 +01:00
|
|
|
import { _converse, api } from "@converse/headless/core";
|
2021-04-14 22:56:59 +02:00
|
|
|
import { html } from 'lit';
|
|
|
|
import { repeat } from 'lit/directives/repeat.js';
|
2020-05-15 14:33:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 14:09:30 +02:00
|
|
|
// This is set to _converse so that it can be overriden. An attempt was made to use
|
|
|
|
// a hook instead, but hook returns a promise and it forces the asynchronicity up
|
|
|
|
// to the render method.
|
|
|
|
_converse.getHats = function (model) {
|
2020-06-01 16:05:00 +02:00
|
|
|
if (model.get('type') === 'groupchat') {
|
2020-10-15 09:51:19 +02:00
|
|
|
const allowed_hats = api.settings.get('muc_hats').filter(hat => hat).map((hat) => (hat.toLowerCase()));
|
|
|
|
let vcard_roles = []
|
|
|
|
if (allowed_hats.includes('vcard_roles')) {
|
|
|
|
vcard_roles = model.vcard ? model.vcard.get('role') : null;
|
|
|
|
vcard_roles = vcard_roles ? vcard_roles.split(',').filter(hat => hat).map((hat) => ({title: hat})) : [];
|
2020-06-01 16:05:00 +02:00
|
|
|
}
|
2020-10-15 09:51:19 +02:00
|
|
|
const muc_role = model.occupant ? [model.occupant.get('role')] : [];
|
|
|
|
const muc_affiliation = model.occupant ? [model.occupant.get('affiliation')] : [];
|
|
|
|
|
|
|
|
const affiliation_role_hats = [...muc_role, ...muc_affiliation]
|
|
|
|
.filter(hat => hat).filter((hat) => (allowed_hats.includes(hat.toLowerCase())))
|
|
|
|
.map((hat) => ({title: hat}));
|
|
|
|
const hats = allowed_hats.includes('xep317') ? model.occupant?.get('hats') || [] : [];
|
|
|
|
return [...hats, ...vcard_roles, ...affiliation_role_hats];
|
2020-06-01 16:05:00 +02:00
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-05-15 14:33:31 +02:00
|
|
|
|
2020-07-01 21:45:18 +02:00
|
|
|
export default class MessageHistory extends CustomElement {
|
2020-05-15 14:33:31 +02:00
|
|
|
|
|
|
|
static get properties () {
|
|
|
|
return {
|
2021-06-03 16:29:31 +02:00
|
|
|
model: { type: Object },
|
|
|
|
messages: { type: Array }
|
2020-05-15 14:33:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const msgs = this.messages;
|
2021-03-17 12:36:56 +01:00
|
|
|
return msgs.length ? html`${repeat(msgs, m => m.get('id'), m => this.renderMessage(m)) }` : '';
|
2020-05-15 14:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
renderMessage (model) {
|
|
|
|
if (model.get('dangling_retraction') || model.get('is_only_key')) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
const day = getDayIndicator(model);
|
|
|
|
const templates = day ? [day] : [];
|
2021-04-15 17:23:53 +02:00
|
|
|
const message = html`<converse-chat-message
|
|
|
|
jid="${this.model.get('jid')}"
|
|
|
|
mid="${model.get('id')}"></converse-chat-message>`
|
|
|
|
|
2020-05-15 14:33:31 +02:00
|
|
|
return [...templates, message];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 21:45:18 +02:00
|
|
|
api.elements.define('converse-message-history', MessageHistory);
|