2021-10-30 17:26:41 +00:00
|
|
|
import * as moment from 'moment';
|
|
|
|
|
|
|
|
import ElementsUtil from './require/elements-util';
|
|
|
|
|
2021-11-07 16:50:30 +00:00
|
|
|
import { Message, Member, IDummyTextMessage } from '../data-types';
|
|
|
|
import Q from '../q-module';
|
2021-11-22 02:47:29 +00:00
|
|
|
import CombinedGuild from '../guild-combined';
|
2021-10-30 17:26:41 +00:00
|
|
|
|
2021-11-21 18:29:42 +00:00
|
|
|
export default function createTextMessage(q: Q, guild: CombinedGuild, message: Message | IDummyTextMessage): HTMLElement {
|
2021-10-30 17:26:41 +00:00
|
|
|
let memberInfo: {
|
|
|
|
roleColor: string | null,
|
|
|
|
displayName: string,
|
|
|
|
avatarResourceId: string | null
|
|
|
|
};
|
|
|
|
if (message instanceof Message) {
|
|
|
|
if (message.member instanceof Member) {
|
|
|
|
memberInfo = {
|
|
|
|
roleColor: message.member.roleColor,
|
|
|
|
displayName: message.member.displayName,
|
|
|
|
avatarResourceId: message.member.avatarResourceId
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
memberInfo = {
|
|
|
|
roleColor: null,
|
|
|
|
displayName: 'Unknown Member',
|
|
|
|
avatarResourceId: null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
memberInfo = {
|
|
|
|
roleColor: null,
|
|
|
|
displayName: message.member.displayName,
|
|
|
|
avatarResourceId: message.member.avatarResourceId
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let nameStyle = memberInfo.roleColor ? 'color: ' + memberInfo.roleColor : '';
|
2021-11-22 02:47:29 +00:00
|
|
|
let element = q.create({ class: 'message', 'meta-id': message.id, 'meta-member-id': message.member.id, 'meta-guild-id': guild.id, content: [
|
2021-10-30 17:26:41 +00:00
|
|
|
{ class: 'member-avatar', content: { tag: 'img', src: './img/loading.svg', alt: memberInfo.displayName } },
|
|
|
|
{ class: 'right', content: [
|
|
|
|
{ class: 'header', content: [
|
|
|
|
{ class: 'member-name', style: nameStyle, content: memberInfo.displayName },
|
|
|
|
{ class: 'timestamp', content: moment(message.sent).calendar(ElementsUtil.calendarFormats) }
|
|
|
|
] },
|
|
|
|
{ class: 'content text', content: ElementsUtil.parseMessageText(message.text || '') }
|
|
|
|
] }
|
|
|
|
] }) as HTMLElement;
|
|
|
|
(async () => {
|
2021-11-07 16:50:30 +00:00
|
|
|
(q.$$$(element, '.member-avatar img') as HTMLImageElement).src =
|
2021-11-22 02:47:29 +00:00
|
|
|
await ElementsUtil.getImageBufferFromResourceFailSoftly(guild, memberInfo.avatarResourceId);
|
2021-10-30 17:26:41 +00:00
|
|
|
})();
|
|
|
|
return element;
|
|
|
|
}
|