fchat-rising/chat/common.ts

133 lines
4.2 KiB
TypeScript
Raw Normal View History

2018-04-16 23:14:13 +00:00
import {isToday} from 'date-fns';
import {Keys} from '../keys';
2017-09-02 01:50:31 +00:00
import {Character, Conversation, Settings as ISettings} from './interfaces';
2020-03-15 14:02:31 +00:00
export function profileLink(this: any | never, character: string): string {
2017-09-02 01:50:31 +00:00
return `https://www.f-list.net/c/${character}`;
}
2020-03-15 14:02:31 +00:00
export function characterImage(this: any | never, character: string): string {
2017-09-02 01:50:31 +00:00
return `https://static.f-list.net/images/avatar/${character.toLowerCase()}.png`;
}
2020-03-15 14:02:31 +00:00
export function getByteLength(this: any | never, str: string): number {
2017-09-02 01:50:31 +00:00
let byteLen = 0;
for(let i = 0; i < str.length; i++) {
2018-04-16 23:14:13 +00:00
let c = str.charCodeAt(i);
if(c > 0xD800 && c < 0xD8FF) //surrogate pairs
c = (c - 0xD800) * 0x400 + str.charCodeAt(++i) - 0xDC00 + 0x10000;
byteLen += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : c < 0x200000 ? 4 : c < 0x4000000 ? 5 : 6;
2017-09-02 01:50:31 +00:00
}
return byteLen;
}
export class Settings implements ISettings {
playSound = true;
clickOpensMessage = false;
disallowedTags: string[] = [];
notifications = true;
highlight = true;
highlightWords: string[] = [];
showAvatars = true;
animatedEicons = true;
idleTimer = 0;
messageSeparators = false;
eventMessages = true;
joinMessages = false;
alwaysNotify = false;
logMessages = true;
logAds = false;
fontSize = 14;
showNeedsReply = false;
2018-03-28 13:51:05 +00:00
enterSend = true;
colorBookmarks = false;
2018-07-20 01:12:26 +00:00
bbCodeBar = true;
risingAdScore = true;
risingLinkPreview = true;
risingAutoCompareKinks = true;
2020-10-25 16:55:21 +00:00
risingAutoExpandCustomKinks = true;
risingCharacterPreview = true;
risingComparisonInUserMenu = true;
risingComparisonInSearch = true;
2020-11-27 20:58:14 +00:00
risingShowUnreadOfflineCount = true;
2020-12-30 04:54:16 +00:00
risingColorblindMode = false;
2017-09-02 01:50:31 +00:00
}
2019-06-02 23:57:32 +00:00
export class AdSettings implements Conversation.AdSettings {
ads: string[] = [];
2020-12-28 23:07:10 +00:00
randomOrder = false;
2019-06-02 23:57:32 +00:00
}
2017-09-02 01:50:31 +00:00
export class ConversationSettings implements Conversation.Settings {
notify = Conversation.Setting.Default;
highlight = Conversation.Setting.Default;
highlightWords: string[] = [];
joinMessages = Conversation.Setting.Default;
2017-10-18 23:29:28 +00:00
defaultHighlights = true;
2020-12-28 23:07:10 +00:00
adSettings: Conversation.AdSettings = { ads: [], randomOrder: false };
2017-09-02 01:50:31 +00:00
}
2018-04-16 23:14:13 +00:00
function pad(num: number): string | number {
return num < 10 ? `0${num}` : num;
}
2020-03-15 14:02:31 +00:00
export function formatTime(this: any | never, date: Date, noDate: boolean = false): string {
2018-04-16 23:14:13 +00:00
if(!noDate && isToday(date)) return `${pad(date.getHours())}:${pad(date.getMinutes())}`;
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
2017-09-02 01:50:31 +00:00
}
export function messageToString(
this: any | never,
msg: Conversation.Message,
timeFormatter: (date: Date) => string = formatTime,
characterTransform: (str: string) => string = (x) => x,
textTransform: (str: string) => string = (x) => x
): string {
2017-09-02 01:50:31 +00:00
let text = `[${timeFormatter(msg.time)}] `;
if(msg.type !== Conversation.Message.Type.Event)
2021-03-17 23:29:56 +00:00
text += (msg.type === Conversation.Message.Type.Action ? '*' : '') + characterTransform(msg.sender.name) +
2017-09-02 01:50:31 +00:00
(msg.type === Conversation.Message.Type.Message ? ':' : '');
2021-03-17 23:29:56 +00:00
return `${text} ${textTransform(msg.text)}\r\n`;
2017-09-02 01:50:31 +00:00
}
export function getKey(e: KeyboardEvent): Keys {
2019-07-06 16:49:19 +00:00
// tslint:disable-next-line deprecation
return e.keyCode;
2017-09-02 01:50:31 +00:00
}
/*tslint:disable:no-any no-unsafe-any*///because errors can be any
export function errorToString(e: any): string {
return e instanceof Error ? e.message : e !== undefined ? e.toString() : '';
}
2018-04-16 23:14:13 +00:00
2017-09-02 01:50:31 +00:00
//tslint:enable
let messageId = 0;
export class Message implements Conversation.ChatMessage {
readonly id = ++messageId;
isHighlight = false;
2019-07-07 21:44:32 +00:00
score = 0;
2019-07-07 01:37:15 +00:00
2017-09-02 01:50:31 +00:00
constructor(readonly type: Conversation.Message.Type, readonly sender: Character, readonly text: string,
readonly time: Date = new Date()) {
2018-03-28 13:51:05 +00:00
if(Conversation.Message.Type[type] === undefined) throw new Error('Unknown type'); //tslint:disable-line
2017-09-02 01:50:31 +00:00
}
}
export class EventMessage implements Conversation.EventMessage {
readonly id = ++messageId;
readonly type = Conversation.Message.Type.Event;
2019-07-07 21:44:32 +00:00
readonly score = 0;
2017-09-02 01:50:31 +00:00
constructor(readonly text: string, readonly time: Date = new Date()) {
}
2020-03-15 14:02:31 +00:00
}