142 lines
3.7 KiB
JavaScript
Raw Normal View History

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-05 19:54:16 -04:00
function typingTextFor(members, our_user_id) {
2019-07-07 03:53:32 -04:00
var profiles = []
var names = []
2019-07-05 19:54:16 -04:00
for (var i = 0; i < members.length; i++) {
if (members[i] != our_user_id) {
2019-07-07 22:41:32 -04:00
profiles.push(users.find(members[i]))
2019-07-05 19:54:16 -04:00
}
}
2019-07-07 03:53:32 -04:00
profiles.sort(function(left, right) {
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(
user_id, category, room_id, display_name, avatar_url, topic,
2019-07-07 05:08:58 -04:00
members, typing_members, inviter_id
2019-07-05 19:54:16 -04:00
) {
2019-07-04 16:01:44 -04:00
roomCategories.upsert({"userId": user_id, "name": category}, {
"userId": user_id,
"name": category
})
function find(for_category) {
var found = rooms.getIndices(
{"userId": user_id, "roomId": room_id, "category": for_category}, 1
)
return found.length > 0 ? found[0] : null
}
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")}
var item = {
2019-07-05 19:54:16 -04:00
"userId": user_id,
"category": category,
"roomId": room_id,
"displayName": display_name,
"avatarUrl": avatar_url,
"topic": topic,
2019-07-07 05:08:58 -04:00
"members": members,
2019-07-05 19:54:16 -04:00
"typingText": typingTextFor(typing_members, user_id),
"inviterId": inviter_id
}
if (replace === null) {
rooms.upsert(
{"userId": user_id, "roomId": room_id, "category": category},
item
)
} else {
rooms.set(replace, item)
}
}
2019-07-07 22:19:17 -04:00
function onRoomForgotten(user_id, room_id) {
rooms.popWhere({"userId": user_id, "roomId": room_id})
}
function onRoomMemberUpdated(room_id, user_id, typing) {
}
function onRoomMemberDeleted(room_id, user_id) {
}
function onTimelineEventReceived(
event_type, room_id, event_id, sender_id, date, content,
content_type, is_local_echo, show_name_line, translatable, target_user_id
) {
2019-07-03 21:20:49 -04:00
var item = {
"eventType": py.getattr(event_type, "__name__"),
"roomId": room_id,
"eventId": event_id,
"senderId": sender_id,
"date": date,
"content": content,
2019-07-03 21:20:49 -04:00
"contentType": content_type,
"isLocalEcho": is_local_echo,
"showNameLine": show_name_line,
"translatable": translatable,
2019-07-08 13:24:05 -04:00
"targetUserId": target_user_id,
2019-07-03 21:20:49 -04:00
}
if (is_local_echo) {
timelines.append(item)
return
}
// Replace first matching local echo
2019-07-04 16:01:44 -04:00
var found = timelines.getIndices({
2019-07-03 21:20:49 -04:00
"roomId": room_id,
"senderId": sender_id,
"content": content,
"isLocalEcho": true
2019-07-05 02:45:30 -04:00
}, 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-04 18:53:10 -04:00
} else {
// Multiple clients will emit duplicate events with the same eventId
2019-07-05 02:45:30 -04:00
timelines.upsert({"eventId": event_id}, item, true, 250)
2019-07-03 21:20:49 -04:00
}
}
2019-07-03 21:20:49 -04:00
var onTimelineMessageReceived = onTimelineEventReceived
2019-07-05 19:54:16 -04:00
function onTypingNoticeEvent(room_id, members) {
}