2019-07-04 12:31:29 +10:00
|
|
|
function stripUserId(user_id) {
|
|
|
|
// Remove leading @
|
|
|
|
return user_id.substring(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function stripRoomName(name) {
|
|
|
|
// Remove leading # (aliases)
|
|
|
|
return name[0] == "#" ? name.substring(1) : name
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function hueFrom(string) {
|
|
|
|
// Calculate and return a unique hue between 0 and 1 for the string
|
|
|
|
var hue = 0
|
|
|
|
for (var i = 0; i < string.length; i++) {
|
|
|
|
hue += string.charCodeAt(i) * 99
|
|
|
|
}
|
|
|
|
return hue % 360 / 360
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-04 14:24:21 +10:00
|
|
|
function avatarHue(name) { // TODO: bad name
|
2019-07-04 12:31:29 +10:00
|
|
|
return Qt.hsla(
|
|
|
|
hueFrom(name),
|
|
|
|
HStyle.avatar.background.saturation,
|
|
|
|
HStyle.avatar.background.lightness,
|
|
|
|
HStyle.avatar.background.alpha
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-04 14:24:21 +10:00
|
|
|
function nameHue(name) { // TODO: bad name
|
2019-07-04 12:31:29 +10:00
|
|
|
return Qt.hsla(
|
|
|
|
hueFrom(name),
|
|
|
|
HStyle.displayName.saturation,
|
|
|
|
HStyle.displayName.lightness,
|
|
|
|
1
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-04 14:24:21 +10:00
|
|
|
function coloredNameHtml(name, alt_id) {
|
|
|
|
return "<font color='" + nameHue(name || stripUserId(alt_id)) + "'>" +
|
|
|
|
escapeHtml(name || alt_id) +
|
|
|
|
"</font>"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-04 12:31:29 +10:00
|
|
|
function escapeHtml(string) {
|
|
|
|
// Replace special HTML characters by encoded alternatives
|
|
|
|
return string.replace("&", "&")
|
|
|
|
.replace("<", "<")
|
|
|
|
.replace(">", ">")
|
|
|
|
.replace('"', """)
|
|
|
|
.replace("'", "'")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-04 14:24:21 +10:00
|
|
|
function eventIsMessage(ev) {
|
|
|
|
return /^RoomMessage($|[A-Z])/.test(ev.eventType)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-04 12:31:29 +10:00
|
|
|
function translatedEventContent(ev) {
|
2019-07-05 06:01:44 +10:00
|
|
|
// ev: timelines item
|
2019-07-04 12:31:29 +10:00
|
|
|
if (ev.translatable == false) { return ev.content }
|
|
|
|
|
|
|
|
// %S → sender display name
|
2019-07-05 06:01:44 +10:00
|
|
|
var name = users.getUser(ev.senderId).displayName
|
2019-07-04 14:24:21 +10:00
|
|
|
var text = ev.content.replace("%S", coloredNameHtml(name, ev.senderId))
|
2019-07-04 12:31:29 +10:00
|
|
|
|
|
|
|
// %T → target (event state_key) display name
|
|
|
|
if (ev.targetUserId) {
|
2019-07-05 06:01:44 +10:00
|
|
|
var tname = users.getUser(ev.targetUserId).displayName
|
2019-07-04 14:24:21 +10:00
|
|
|
text = text.replace("%T", coloredNameHtml(tname, ev.targetUserId))
|
2019-07-04 12:31:29 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
text = qsTr(text)
|
2019-07-04 14:24:21 +10:00
|
|
|
if (ev.translatable == true) { return text }
|
2019-07-04 12:31:29 +10:00
|
|
|
|
|
|
|
// Else, model.translatable should be an array of args
|
2019-07-04 14:24:21 +10:00
|
|
|
for (var i = 0; ev.translatable.length; i++) {
|
|
|
|
text = text.arg(ev.translatable[i])
|
2019-07-04 12:31:29 +10:00
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|