Skip to content

Commit 580a780

Browse files
committed
Back off on presence updates
Previously was sending presence for every timeline event, which means getting 429-d if you are currently receiving a timeline sync.
1 parent e8413ee commit 580a780

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/matrix.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ export default {
2121
constructor () {
2222
super(...arguments)
2323
this.robot.logger.info('Constructor')
24+
this.lastPresenceUpdate = 0
25+
this.PRESENCE_UPDATE_INTERVAL = 60000 // Minimum 1 minute between updates
26+
}
27+
28+
updatePresence () {
29+
const now = Date.now()
30+
if (now - this.lastPresenceUpdate >= this.PRESENCE_UPDATE_INTERVAL) {
31+
this.robot.matrixClient.setPresence({ presence: 'online' })
32+
.catch(err => {
33+
if (err.errcode === 'M_LIMIT_EXCEEDED') {
34+
this.robot.logger.warn(`Rate limited when setting presence. Retry after: ${err.retry_after_ms}ms`)
35+
}
36+
})
37+
this.lastPresenceUpdate = now
38+
}
2439
}
2540

2641
handleUnknownDevices (err) {
@@ -43,6 +58,7 @@ export default {
4358
}
4459

4560
send (envelope, ...strings) {
61+
this.updatePresence()
4662
return (() => {
4763
const result = []
4864
for (const str of Array.from(strings)) {
@@ -138,6 +154,7 @@ export default {
138154
switch (state) {
139155
case 'PREPARED':
140156
that.robot.logger.info(`Synced ${that.robot.matrixClient.getRooms().length} rooms`)
157+
that.updatePresence()
141158
// We really don't want to let people set the display name to something other than the bot
142159
// name because the bot only reacts to it's own name.
143160
const userId = that.robot.matrixClient.getUserId()
@@ -160,6 +177,9 @@ export default {
160177
if (name !== userId) {
161178
that.robot.logger.info(`Received message: ${JSON.stringify(message)} in room: ${user.room}, from: ${user.name} (${user.id}).`)
162179
if (message.msgtype === 'm.text') {
180+
if (message.body.indexOf(that.robot.name) !== -1) {
181+
that.updatePresence()
182+
}
163183
that.receive(new TextMessage(user, message.body))
164184
}
165185
if ((message.msgtype !== 'm.text') || (message.body.indexOf(that.robot.name) !== -1)) {

0 commit comments

Comments
 (0)