Skip to content

Commit

Permalink
Merge pull request #9 from djmaze/add-admin-room
Browse files Browse the repository at this point in the history
Setup admin room and log diagnostic and error output there
  • Loading branch information
djmaze authored Sep 12, 2021
2 parents a052397 + 039cc9d commit ca68c25
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
HOMESERVER_URL=https://matrix.org
ACCESS_TOKEN=foobar123
INTERVAL_MINUTES=10
INTERVAL_MINUTES=10
#FEEDBACK_ROOM=
#ADMIN_ROOM_ID=
21 changes: 21 additions & 0 deletions src/admin_logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import AdminRoom from "./admin_room"

export default class AdminLogger {
adminRoom?: AdminRoom

constructor(adminRoom?: AdminRoom) {
this.adminRoom = adminRoom
}

info(message: string) : void {
if (this.adminRoom)
this.adminRoom.logInfo(message)
console.info(message)
}

error(message: string) : void {
if (this.adminRoom)
this.adminRoom.logError(message)
console.error(message)
}
}
42 changes: 42 additions & 0 deletions src/admin_room.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { MatrixClient } from "matrix-bot-sdk"

export default class AdminRoom {
client: MatrixClient
roomId: string

constructor(client: MatrixClient, roomId: string) {
this.client = client
this.roomId = roomId
}

async join() : Promise<void> {
await this.client.joinRoom(this.roomId)
}

async listen() : Promise<void> {
const userId = await this.client.getUserId()

this.client.on("room.event", async (roomId, event) => {
if (roomId !== this.roomId) return

if (event.type === "m.room.member" && event.content.membership === "join" && event.sender === userId) {
console.debug("admin room just entered, sending welcome message", roomId)
await this.showHelp()
}
})
}

async showHelp() : Promise<void> {
const text = "<p>Willkommen im Admin-Raum des MINA-Bots!"

await this.client.sendHtmlText(this.roomId, text)
}

async logError(message: string) : Promise<void> {
await this.client.sendHtmlText(this.roomId, `⚠️ ${message}`)
}

async logInfo(message: string) : Promise<void> {
await this.client.sendText(this.roomId, `🏁 ${message}`)
}
}
28 changes: 26 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { AutojoinRoomsMixin, MatrixClient, MessageEvent, RichReply, SimpleFsStorageProvider, TextualMessageEventContent } from "matrix-bot-sdk"
import AdminLogger from "./admin_logger"
import AdminRoom from "./admin_room"
import AGSSearch from "./ags"
import NinaWarnings, { LastSent, MINAWarnItem } from "./nina_api"
import WarnLists from "./warn_lists"
Expand All @@ -7,6 +9,7 @@ const homeserverUrl = process.env.HOMESERVER_URL // make sure to update this wit
const accessToken = process.env.ACCESS_TOKEN
const INTERVAL = parseInt(process.env.INTERVAL_MINUTES || "10") * 60 * 1000
const FEEDBACK_ROOM = process.env.FEEDBACK_ROOM
const ADMIN_ROOM_ID = process.env.ADMIN_ROOM_ID

const LOCATION_EVENT_TYPE = "de.nina-bot.location"
const LAST_SENT_TYPE = "de.nina-bot.last-sent"
Expand All @@ -33,17 +36,36 @@ type RoomLocation = {
const rooms: RoomLocation[] = []
const agsSearch = new AGSSearch()
const warnLists = new WarnLists()
const warnings = new NinaWarnings(warnLists, INTERVAL)
const logger = new AdminLogger()
const warnings = new NinaWarnings(warnLists, INTERVAL, logger)

client.start().then(() => console.log("Client started!"))

client.getJoinedRooms().then(async (matrixRooms) => {
console.debug("joined rooms", matrixRooms)
console.debug("got joined rooms", matrixRooms)

if (ADMIN_ROOM_ID) {
const adminRoom = new AdminRoom(client, ADMIN_ROOM_ID)
logger.adminRoom = adminRoom

console.debug(`Listening in admin room ${ADMIN_ROOM_ID}`)
adminRoom.listen()

if (!matrixRooms.includes(ADMIN_ROOM_ID)) {
console.debug(`Joining admin room ${ADMIN_ROOM_ID}`)
await adminRoom.join()
}
}

logger.info("Started")

await setupRooms(matrixRooms)
logger.info("Set up all rooms")
})

client.on("room.event", async (roomId, event) => {
if (roomId === ADMIN_ROOM_ID) return

if (event.type === "m.room.create") {
console.debug("room just created, sending welcome message", roomId)
await showHelp(roomId)
Expand All @@ -58,6 +80,8 @@ client.on("room.event", async (roomId, event) => {
})

client.on("room.message", async (roomId, ev: MessageEvent<any>) => {
if (roomId === ADMIN_ROOM_ID) return

const event = new MessageEvent<TextualMessageEventContent>(ev)
if (event.isRedacted) return
if (!event.textBody) return
Expand Down
9 changes: 6 additions & 3 deletions src/nina_api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fetch from "node-fetch"
import hash from "object-hash"
import AdminLogger from "./admin_logger"

import WarnLists, { WarnItem } from "./warn_lists"

Expand Down Expand Up @@ -77,10 +78,12 @@ export default class NinaWarnings {
warnLists: WarnLists
interval: number
locations: Record<string, Location> = {}
logger: AdminLogger

constructor(warnLists: WarnLists, interval: number) {
constructor(warnLists: WarnLists, interval: number, logger: AdminLogger) {
this.warnLists = warnLists
this.interval = interval
this.logger = logger

this.start()
}
Expand Down Expand Up @@ -190,7 +193,7 @@ export default class NinaWarnings {
.map((item) => {
const provider = item.payload.data.provider
if (!Object.prototype.hasOwnProperty.call(ninaProviders, provider))
console.error(`Unknown provider ${provider}`)
this.logger.error(`Unknown provider ${provider}`)
return this.mapProviderData(provider, item)
})
.filter((item) => item) as MINAWarnItem[]
Expand Down Expand Up @@ -239,7 +242,7 @@ export default class NinaWarnings {
onset: info.onset ? new Date(info.onset) : undefined,
expires: info.expires ? new Date(info.expires) : undefined
}
} else console.error(`Provider-Item for ${provider} ${item.id} not found`)
} else this.logger.error(`Provider-Item for ${provider} ${item.id} not found`)
}

private areaDesc(areas: NinaArea[]) : string {
Expand Down

0 comments on commit ca68c25

Please sign in to comment.