2020-10-23 14:15:14 +00:00
|
|
|
const {store} = require("../store/store.js")
|
|
|
|
const lsm = require("../lsm.js")
|
|
|
|
const {resolveMxc} = require("../functions.js")
|
2020-10-15 09:06:41 +00:00
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
let lastBatch = null
|
2020-10-15 09:06:41 +00:00
|
|
|
|
|
|
|
function sync() {
|
2020-10-19 11:43:33 +00:00
|
|
|
const url = new URL(`${lsm.get("domain")}/_matrix/client/r0/sync`)
|
|
|
|
url.searchParams.append("access_token", lsm.get("access_token"))
|
|
|
|
const filter = {
|
|
|
|
room: {
|
|
|
|
// pulling more from the timeline massively increases download size
|
|
|
|
timeline: {
|
2020-11-28 04:14:47 +00:00
|
|
|
limit: 1
|
2020-10-19 11:43:33 +00:00
|
|
|
},
|
|
|
|
// members are not currently needed
|
|
|
|
state: {
|
|
|
|
lazy_load_members: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
presence: {
|
|
|
|
// presence is not implemented, ignore it
|
|
|
|
types: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
url.searchParams.append("filter", JSON.stringify(filter))
|
|
|
|
url.searchParams.append("timeout", 20000)
|
|
|
|
if (lastBatch) {
|
|
|
|
url.searchParams.append("since", lastBatch)
|
|
|
|
}
|
|
|
|
return fetch(url.toString()).then(res => res.json()).then(root => {
|
|
|
|
lastBatch = root.next_batch
|
|
|
|
return root
|
|
|
|
})
|
2020-10-15 09:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function manageSync(root) {
|
2020-10-19 11:43:33 +00:00
|
|
|
try {
|
|
|
|
let newEvents = false
|
2020-11-24 12:27:41 +00:00
|
|
|
let notificationsChange = false
|
2020-10-19 07:20:08 +00:00
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
// set up directs
|
2020-11-12 04:29:46 +00:00
|
|
|
if (root.account_data) {
|
|
|
|
const directs = root.account_data.events.find(e => e.type === "m.direct")
|
|
|
|
if (directs) {
|
|
|
|
Object.values(directs.content).forEach(ids => {
|
|
|
|
ids.forEach(id => store.directs.add(id))
|
|
|
|
})
|
|
|
|
}
|
2020-10-19 11:43:33 +00:00
|
|
|
}
|
2020-10-15 09:06:41 +00:00
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
// set up rooms
|
2020-11-12 04:29:46 +00:00
|
|
|
if (root.rooms) {
|
|
|
|
if (root.rooms.join) {
|
|
|
|
Object.entries(root.rooms.join).forEach(([id, data]) => {
|
|
|
|
if (!store.rooms.has(id)) {
|
|
|
|
store.rooms.askAdd(id, data)
|
|
|
|
}
|
|
|
|
const room = store.rooms.get(id).value()
|
|
|
|
const timeline = room.timeline
|
2020-12-14 12:05:00 +00:00
|
|
|
if (data.state && data.state.events) timeline.updateStateEvents(data.state.events)
|
|
|
|
if (data.timeline && data.timeline.events) {
|
2020-11-12 04:29:46 +00:00
|
|
|
if (!timeline.from) timeline.from = data.timeline.prev_batch
|
|
|
|
if (data.timeline.events.length) {
|
|
|
|
newEvents = true
|
|
|
|
timeline.updateEvents(data.timeline.events)
|
|
|
|
}
|
|
|
|
}
|
2020-12-14 12:05:00 +00:00
|
|
|
if (data.ephemeral && data.ephemeral.events) timeline.updateEphemeral(data.ephemeral.events)
|
2020-11-24 12:27:41 +00:00
|
|
|
if (data.unread_notifications) {
|
|
|
|
timeline.updateNotificationCount(data.unread_notifications.notification_count)
|
|
|
|
notificationsChange = true
|
|
|
|
}
|
|
|
|
if (data["org.matrix.msc2654.unread_count"] != undefined) {
|
|
|
|
timeline.updateUnreadCount(data["org.matrix.msc2654.unread_count"])
|
|
|
|
notificationsChange = true
|
|
|
|
}
|
2020-11-12 04:29:46 +00:00
|
|
|
})
|
2020-10-19 11:43:33 +00:00
|
|
|
}
|
2020-11-12 04:29:46 +00:00
|
|
|
}
|
2020-10-19 07:20:08 +00:00
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
// set up groups
|
2020-11-12 04:29:46 +00:00
|
|
|
if (root.groups) {
|
|
|
|
Promise.all(
|
|
|
|
Object.keys(root.groups.join).map(id => {
|
|
|
|
if (!store.groups.has(id)) {
|
|
|
|
return Promise.all(["profile", "rooms"].map(path => {
|
|
|
|
const url = new URL(`${lsm.get("domain")}/_matrix/client/r0/groups/${id}/${path}`)
|
|
|
|
url.searchParams.append("access_token", lsm.get("access_token"))
|
|
|
|
return fetch(url.toString()).then(res => res.json())
|
|
|
|
})).then(([profile, rooms]) => {
|
|
|
|
rooms = rooms.chunk
|
|
|
|
let order = 999
|
|
|
|
let orderEvent = root.account_data.events.find(e => e.type === "im.vector.web.tag_ordering")
|
|
|
|
if (orderEvent) {
|
|
|
|
if (orderEvent.content.tags.includes(id)) {
|
|
|
|
order = orderEvent.content.tags.indexOf(id)
|
|
|
|
}
|
2020-10-19 11:43:33 +00:00
|
|
|
}
|
2020-11-12 04:29:46 +00:00
|
|
|
const data = {
|
|
|
|
name: profile.name,
|
|
|
|
icon: resolveMxc(profile.avatar_url, 96, "crop"),
|
|
|
|
order
|
2020-10-19 11:43:33 +00:00
|
|
|
}
|
2020-11-12 04:29:46 +00:00
|
|
|
store.groups.askAdd(id, data)
|
|
|
|
rooms.forEach(groupRoom => {
|
|
|
|
if (store.rooms.has(groupRoom.room_id)) {
|
|
|
|
store.rooms.get(groupRoom.room_id).value().setGroup(id)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
store.newEvents.broadcast("changeSelf") // trigger a room list update
|
2020-10-19 11:43:33 +00:00
|
|
|
})
|
2020-11-12 04:29:46 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
).then(() => {
|
|
|
|
store.rooms.sort()
|
2020-10-19 11:43:33 +00:00
|
|
|
})
|
2020-11-12 04:29:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
if (newEvents) store.newEvents.broadcast("changeSelf")
|
2020-11-24 12:27:41 +00:00
|
|
|
if (notificationsChange) store.notificationsChange.broadcast("changeSelf")
|
2020-10-19 11:43:33 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(root)
|
|
|
|
throw e
|
|
|
|
}
|
2020-10-15 09:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function syncLoop() {
|
2020-10-19 11:43:33 +00:00
|
|
|
return sync().then(manageSync).then(syncLoop)
|
2020-10-15 09:06:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
;[
|
|
|
|
{
|
|
|
|
id: "directs",
|
|
|
|
name: "Directs",
|
2020-10-20 10:25:04 +00:00
|
|
|
icon: staticFiles.get("/assets/icons/directs.svg"),
|
2020-10-19 11:43:33 +00:00
|
|
|
order: -2
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "channels",
|
|
|
|
name: "Channels",
|
2020-10-20 10:25:04 +00:00
|
|
|
icon: staticFiles.get("/assets/icons/channels.svg"),
|
2020-10-19 11:43:33 +00:00
|
|
|
order: -1
|
|
|
|
}
|
|
|
|
].forEach(data => store.groups.askAdd(data.id, data))
|
2020-10-19 07:20:08 +00:00
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
store.activeGroup.set(store.groups.get("directs").value())
|
2020-10-19 07:20:08 +00:00
|
|
|
|
2020-10-21 07:56:36 +00:00
|
|
|
if (lsm.get("access_token")) {
|
|
|
|
syncLoop()
|
|
|
|
}
|