2019-07-07 23:52:41 -04:00
|
|
|
// Copyright 2019 miruka
|
|
|
|
// This file is part of harmonyqml, licensed under LGPLv3.
|
|
|
|
|
2019-07-06 17:53:51 -04:00
|
|
|
Qt.include("../utils.js")
|
2019-07-06 17:29:32 -04:00
|
|
|
|
2019-07-18 04:17:35 -04:00
|
|
|
function typingTextFor(members, ourUserId) {
|
2019-07-07 03:53:32 -04:00
|
|
|
var profiles = []
|
|
|
|
var names = []
|
2019-06-28 18:12:45 -04:00
|
|
|
|
2019-07-05 19:54:16 -04:00
|
|
|
for (var i = 0; i < members.length; i++) {
|
2019-07-18 04:17:35 -04:00
|
|
|
if (members[i] != ourUserId) {
|
2019-07-07 22:41:32 -04:00
|
|
|
profiles.push(users.find(members[i]))
|
2019-07-05 19:54:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 03:13:34 -04:00
|
|
|
profiles.sort((left, right) => {
|
2019-07-07 03:53:32 -04:00
|
|
|
if (left.displayName < right.displayName) { return -1 }
|
|
|
|
if (left.displayName > right.displayName) { return +1 }
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
|
|
|
|
for (var i = 0; i < profiles.length; i++) {
|
|
|
|
var profile = profiles[i]
|
|
|
|
names.push(coloredNameHtml(profile.displayName, profile.userId))
|
|
|
|
}
|
|
|
|
|
2019-07-05 19:54:16 -04:00
|
|
|
if (names.length == 0) { return "" }
|
|
|
|
if (names.length == 1) { return qsTr("%1 is typing...").arg(names[0]) }
|
|
|
|
|
|
|
|
var text = qsTr("%1 and %2 are typing...")
|
|
|
|
|
|
|
|
if (names.length == 2) { return text.arg(names[0]).arg(names[1]) }
|
|
|
|
|
|
|
|
return text.arg(names.slice(0, -1).join(", ")).arg(names.slice(-1)[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onRoomUpdated(
|
2019-07-18 04:17:35 -04:00
|
|
|
userId, category, roomId, displayName, avatarUrl, topic,
|
|
|
|
members, typingMembers, inviterId
|
2019-07-05 19:54:16 -04:00
|
|
|
) {
|
2019-07-18 04:17:35 -04:00
|
|
|
roomCategories.upsert({userId, name: category}, {userId, name: category})
|
2019-07-02 13:59:52 -04:00
|
|
|
|
2019-07-18 04:17:35 -04:00
|
|
|
function find(category) {
|
|
|
|
var found = rooms.getIndices({userId, roomId, category}, 1)
|
2019-07-07 02:35:22 -04:00
|
|
|
return found.length > 0 ? found[0] : null
|
2019-07-02 13:59:52 -04:00
|
|
|
}
|
2019-06-28 18:12:45 -04:00
|
|
|
|
2019-07-07 02:35:22 -04:00
|
|
|
var replace = null
|
|
|
|
if (category == "Invites") { replace = find("Rooms") || find("Left") }
|
|
|
|
else if (category == "Rooms") { replace = find("Invites") || find("Left") }
|
|
|
|
else if (category == "Left") { replace = find("Invites") || find("Rooms")}
|
2019-06-28 18:12:45 -04:00
|
|
|
|
2019-07-07 02:35:22 -04:00
|
|
|
var item = {
|
2019-07-18 04:17:35 -04:00
|
|
|
typingText: typingTextFor(typingMembers, userId),
|
|
|
|
|
|
|
|
userId, category, roomId, displayName, avatarUrl, topic, members,
|
|
|
|
inviterId
|
2019-07-07 02:35:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (replace === null) {
|
2019-07-18 04:17:35 -04:00
|
|
|
rooms.upsert({userId, roomId, category}, item)
|
2019-07-07 02:35:22 -04:00
|
|
|
} else {
|
|
|
|
rooms.set(replace, item)
|
|
|
|
}
|
2019-06-28 18:12:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-18 04:17:35 -04:00
|
|
|
function onRoomForgotten(userId, roomId) {
|
|
|
|
rooms.popWhere({userId, roomId})
|
2019-06-28 18:12:45 -04:00
|
|
|
}
|
2019-07-02 22:22:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
function onTimelineEventReceived(
|
2019-07-18 04:17:35 -04:00
|
|
|
eventType, roomId, eventId, senderId, date, content,
|
|
|
|
contentType, isLocalEcho, showNameLine, translatable, targetUserId
|
2019-07-02 22:22:29 -04:00
|
|
|
) {
|
2019-07-03 21:20:49 -04:00
|
|
|
var item = {
|
2019-07-18 04:17:35 -04:00
|
|
|
eventType: py.getattr(eventType, "__name__"),
|
|
|
|
|
|
|
|
roomId, eventId, senderId, date, content, contentType, isLocalEcho,
|
|
|
|
showNameLine, translatable, targetUserId
|
2019-07-03 21:20:49 -04:00
|
|
|
}
|
|
|
|
|
2019-07-18 04:17:35 -04:00
|
|
|
if (isLocalEcho) {
|
2019-07-04 23:12:21 -04:00
|
|
|
timelines.append(item)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace first matching local echo
|
2019-07-18 04:17:35 -04:00
|
|
|
var found = timelines.getIndices(
|
|
|
|
{roomId, senderId, content, "isLocalEcho": true}, 1, 250
|
|
|
|
)
|
2019-07-04 18:53:10 -04:00
|
|
|
|
2019-07-03 21:20:49 -04:00
|
|
|
if (found.length > 0) {
|
2019-07-04 16:01:44 -04:00
|
|
|
timelines.set(found[0], item)
|
2019-07-08 13:29:05 -04:00
|
|
|
}
|
|
|
|
// Multiple clients will emit duplicate events with the same eventId
|
|
|
|
else if (item.eventType == "OlmEvent" || item.eventType == "MegolmEvent") {
|
|
|
|
// Don't replace if an item with the same eventId is found in these
|
|
|
|
// cases, because it would be the ecrypted version of the event.
|
2019-07-18 04:17:35 -04:00
|
|
|
timelines.upsert({eventId}, item, false, 250)
|
2019-07-08 13:29:05 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-07-18 04:17:35 -04:00
|
|
|
timelines.upsert({eventId}, item, true, 250)
|
2019-07-03 21:20:49 -04:00
|
|
|
}
|
2019-07-02 22:22:29 -04:00
|
|
|
}
|
|
|
|
|
2019-07-03 21:20:49 -04:00
|
|
|
|
2019-07-02 22:22:29 -04:00
|
|
|
var onTimelineMessageReceived = onTimelineEventReceived
|