-
Notifications
You must be signed in to change notification settings - Fork 36
Send a notice when an account connects/disconnects #265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Half-Shot
wants to merge
10
commits into
develop
Choose a base branch
from
hs/add-account-notices
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ea13a70
Send a notice when an account changes state
Half-Shot b2cf712
Quit on SIGINT, and properly close purple properly
Half-Shot d1c6b29
Remove stale comment
Half-Shot c9b6d82
Sort imports
Half-Shot 8599281
changelog
Half-Shot 7543409
Merge remote-tracking branch 'origin/develop' into hs/add-account-not…
Half-Shot b279269
Words
Half-Shot dcd86f8
Update schema
Half-Shot 438f7d1
Merge remote-tracking branch 'origin/develop' into hs/add-account-not…
Half-Shot ae7f060
oops
Half-Shot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Send a notice when the bridge connects or disconnects a user from their account. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { IAccountErrorEvent, IAccountEvent } from "./bifrost/Events"; | ||
| import { IBifrostInstance } from "./bifrost/Instance"; | ||
| import { Bridge, Logging } from "matrix-appservice-bridge"; | ||
| import { IStore } from "./store/Store"; | ||
| import { Config } from "./Config"; | ||
|
|
||
| const log = Logging.get("AccountHandler"); | ||
| /** | ||
| * Class to manage account settings, including commands sent from Matrix users | ||
| */ | ||
| export class AccountHandler { | ||
|
|
||
| constructor(private instance: IBifrostInstance, | ||
| private store: IStore, | ||
| private bridge: Bridge, | ||
| private config: Config) { | ||
| instance.on("account-signed-on", (ev: IAccountEvent) => { | ||
| this.onAccountSignedOn(ev); | ||
| }); | ||
| instance.on("account-connection-error", (ev: IAccountErrorEvent) => { | ||
| this.onAccountConnectionError(ev); | ||
| }); | ||
| instance.on("account-signed-off", (ev: IAccountEvent) => { | ||
| this.onAccountSignedOff(ev); | ||
| }); | ||
| } | ||
|
|
||
| private async getInstanceEventContext(ev: IAccountEvent) { | ||
| const account = this.instance.getAccount(ev.account.username, ev.account.protocol_id); | ||
| const user = await this.store.getMatrixUserForAccount(ev.account); | ||
| const protocol = this.instance.getProtocol(ev.account.protocol_id); | ||
| if (!account) { | ||
| log.warn("Account not registered with Bifrost, ignoring"); | ||
| return; | ||
| } | ||
| if (!user) { | ||
| log.warn(`Account registered with Bifrost, but not assigned to a user!!`); | ||
Half-Shot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
| const adminRoom = await this.store.getAdminRoom(user.getId()); | ||
| return { | ||
| account, | ||
| user, | ||
| protocol, | ||
| adminRoom, | ||
| } | ||
| } | ||
|
|
||
| private async onAccountSignedOn(ev: IAccountEvent) { | ||
| log.info(`${ev.account.protocol_id}://${ev.account.username} signed on`); | ||
| const {account, adminRoom, protocol} = await this.getInstanceEventContext(ev); | ||
| account.setStatus('available', true); | ||
| if (!this.config.purple.sendConnectionNotices) { | ||
| return; | ||
| } | ||
| await this.bridge.getIntent().sendMessage(adminRoom.roomId, { | ||
| msgtype: "m.notice", | ||
| body: `🟢 ${ev.account.username} (${protocol.name}) has signed on` | ||
| }); | ||
| } | ||
|
|
||
| private async onAccountConnectionError(ev: IAccountErrorEvent) { | ||
| log.warn(`${ev.account.protocol_id}://${ev.account.username} had a connection error`, ev); | ||
| if (!this.config.purple.sendConnectionNotices) { | ||
| return; | ||
| } | ||
| const {protocol, adminRoom} = await this.getInstanceEventContext(ev); | ||
| await this.bridge.getIntent().sendMessage(adminRoom.roomId, { | ||
| msgtype: "m.notice", | ||
| body: `⚠️ ${ev.account.username} (${protocol.name}) had a connection error: ${ev.description}` | ||
| }); | ||
| } | ||
|
|
||
| private async onAccountSignedOff(ev: IAccountEvent) { | ||
| log.info(`${ev.account.protocol_id}://${ev.account.username} signed off.`); | ||
| const {protocol, adminRoom} = await this.getInstanceEventContext(ev); | ||
| if (!this.config.purple.sendConnectionNotices) { | ||
| return; | ||
| } | ||
| await this.bridge.getIntent().sendMessage(adminRoom.roomId, { | ||
| msgtype: "m.notice", | ||
| body: `🔴 ${ev.account.username} (${protocol.name}) has signed off ` | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.