Skip to content

Commit

Permalink
Feat!: hubot 11 support and rewrite to ESM
Browse files Browse the repository at this point in the history
hubot 11 requires adapters to be written in ESM (hubotio/hubot#1699)
  • Loading branch information
sapphi-red committed May 19, 2024
1 parent 225d750 commit ca70a7f
Show file tree
Hide file tree
Showing 20 changed files with 543 additions and 336 deletions.
714 changes: 480 additions & 234 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "hubot-traq",
"type": "module",
"version": "1.8.3",
"description": "Hubot adapter for traQ.",
"main": "./traq",
"main": "./traq.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -12,7 +13,7 @@
"ws": "^8.17.0"
},
"peerDependencies": {
"hubot": ">=3.0"
"hubot": ">=11.0"
},
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions src/WebSocketWithHeader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const WebSocket = require('ws')
import WebSocket from 'ws'

exports.createWebSocketWithHeader = headers => {
export const createWebSocketWithHeader = headers => {
class WebSocketWithAuth extends WebSocket {
constructor(url, protocols) {
super(url, protocols, { headers })
Expand Down
14 changes: 6 additions & 8 deletions src/adapter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { Adapter } = require("hubot/es2015")
const ReconnectingWebSocket = require('reconnecting-websocket')
const { createWebSocketWithHeader } = require('./WebSocketWithHeader')
const Request = require("./request")
const Handler = require("./handler")
import { Adapter } from "hubot"
import ReconnectingWebSocket from 'reconnecting-websocket'
import { createWebSocketWithHeader } from './WebSocketWithHeader'
import Request from "./request.js"
import Handler from "./handler.js"

class TraQAdapter extends Adapter {
export default class TraQAdapter extends Adapter {
constructor(robot, { mode, verifyToken, accessToken, path, embed = false, domain }) {
super(robot)
this.robot = robot
Expand Down Expand Up @@ -105,5 +105,3 @@ class TraQAdapter extends Adapter {
this.robot.logger.debug("Topic")
}
}

module.exports = TraQAdapter
8 changes: 3 additions & 5 deletions src/events/BotMessageStampsUpdated.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Message } = require("hubot/es2015")
const { createNullUser } = require("../nullUser")
import { Message } from "hubot"
import { createNullUser } from "../nullUser.js"

class BotMessageStampsUpdated extends Message {
export default class BotMessageStampsUpdated extends Message {
constructor({ eventTime, messageId, stamps }, done) {
const user = createNullUser()
super(user, done)
Expand All @@ -12,5 +12,3 @@ class BotMessageStampsUpdated extends Message {
this.stamps = stamps
}
}

module.exports = BotMessageStampsUpdated
6 changes: 2 additions & 4 deletions src/events/ChannelCreated.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Message } = require("hubot/es2015")
import { Message } from "hubot"

class ChannelCreated extends Message {
export default class ChannelCreated extends Message {
constructor({ eventTime, channel }, done) {
const u = {
...channel.creator,
Expand All @@ -17,5 +17,3 @@ class ChannelCreated extends Message {
this.channel = channel
}
}

module.exports = ChannelCreated
6 changes: 2 additions & 4 deletions src/events/ChannelTopicChanged.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Message } = require("hubot/es2015")
import { Message } from "hubot"

class ChannelTopicChanged extends Message {
export default class ChannelTopicChanged extends Message {
constructor({ eventTime, channel, topic, updater }, done) {
const u = {
...updater,
Expand All @@ -18,5 +18,3 @@ class ChannelTopicChanged extends Message {
this.updater = updater
}
}

module.exports = ChannelTopicChanged
6 changes: 2 additions & 4 deletions src/events/DirectMessageCreated.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { TextMessage } = require("hubot/es2015")
import { TextMessage } from "hubot"

class DirectMessageCreated extends TextMessage {
export default class DirectMessageCreated extends TextMessage {
constructor({ eventTime, message }) {
const u = {
...message.user,
Expand All @@ -19,5 +19,3 @@ class DirectMessageCreated extends TextMessage {
this.updatedAt = message.updatedAt
}
}

module.exports = DirectMessageCreated
8 changes: 3 additions & 5 deletions src/events/Joined.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Message } = require("hubot/es2015")
const { createNullUser } = require("../nullUser")
import { Message } from "hubot"
import { createNullUser } from "../nullUser.js"

class Joined extends Message {
export default class Joined extends Message {
constructor({ eventTime, channel }, done) {
const user = createNullUser()
user.room = {
Expand All @@ -16,5 +16,3 @@ class Joined extends Message {
this.channel = channel
}
}

module.exports = Joined
8 changes: 3 additions & 5 deletions src/events/Left.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Message } = require("hubot/es2015")
const { createNullUser } = require("../nullUser")
import { Message } from "hubot"
import { createNullUser } from "../nullUser.js"

class Left extends Message {
export default class Left extends Message {
constructor({ eventTime, channel }, done) {
const user = createNullUser()
user.room = {
Expand All @@ -16,5 +16,3 @@ class Left extends Message {
this.channel = channel
}
}

module.exports = Left
6 changes: 2 additions & 4 deletions src/events/MessageCreated.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { TextMessage } = require("hubot/es2015")
import { TextMessage } from "hubot"

class MessageCreated extends TextMessage {
export default class MessageCreated extends TextMessage {
constructor({ eventTime, message }) {
const u = {
...message.user,
Expand All @@ -19,5 +19,3 @@ class MessageCreated extends TextMessage {
this.updatedAt = message.updatedAt
}
}

module.exports = MessageCreated
6 changes: 2 additions & 4 deletions src/events/StampCreated.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Message } = require("hubot/es2015")
import { Message } from "hubot"

class StampCreated extends Message {
export default class StampCreated extends Message {
constructor({ eventTime, id, name, fileId, creator }, done) {
const user = {
...creator,
Expand All @@ -18,5 +18,3 @@ class StampCreated extends Message {
this.creator = creator
}
}

module.exports = StampCreated
8 changes: 3 additions & 5 deletions src/events/TagAdded.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Message } = require("hubot/es2015")
const { createNullUser } = require("../nullUser")
import { Message } from "hubot"
import { createNullUser } from "../nullUser.js"

class TagAdded extends Message {
export default class TagAdded extends Message {
constructor({ eventTime, tagId, tag }, done) {
const user = createNullUser()
super(user, done)
Expand All @@ -12,5 +12,3 @@ class TagAdded extends Message {
this.tag = tag
}
}

module.exports = TagAdded
8 changes: 3 additions & 5 deletions src/events/TagRemoved.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Message } = require("hubot/es2015")
const { createNullUser } = require("../nullUser")
import { Message } from "hubot"
import { createNullUser } from "../nullUser.js"

class TagRemoved extends Message {
export default class TagRemoved extends Message {
constructor({ eventTime, tagId, tag }, done) {
const user = createNullUser()
super(user, done)
Expand All @@ -12,5 +12,3 @@ class TagRemoved extends Message {
this.tag = tag
}
}

module.exports = TagRemoved
6 changes: 2 additions & 4 deletions src/events/UserCreated.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Message } = require("hubot/es2015")
import { Message } from "hubot"

class UserCreated extends Message {
export default class UserCreated extends Message {
constructor({ eventTime, user }, done) {
const u = {
...user,
Expand All @@ -15,5 +15,3 @@ class UserCreated extends Message {
this.userData = user
}
}

module.exports = UserCreated
36 changes: 11 additions & 25 deletions src/events/events.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
const Joined = require("./Joined")
const Left = require("./Left")
const MessageCreated = require("./MessageCreated")
const DirectMessageCreated = require("./DirectMessageCreated")
const ChannelCreated = require("./ChannelCreated")
const ChannelTopicChanged = require("./ChannelTopicChanged")
const UserCreated = require("./UserCreated")
const StampCreated = require("./StampCreated")
const TagAdded = require("./TagAdded")
const TagRemoved = require("./TagRemoved")
const BotMessageStampsUpdated = require("./BotMessageStampsUpdated")

module.exports = {
Joined,
Left,
MessageCreated,
DirectMessageCreated,
ChannelCreated,
ChannelTopicChanged,
UserCreated,
StampCreated,
TagAdded,
TagRemoved,
BotMessageStampsUpdated
}
export { default as Joined } from "./Joined.js"
export { default as Left } from "./Left.js"
export { default as MessageCreated } from "./MessageCreated.js"
export { default as DirectMessageCreated } from "./DirectMessageCreated.js"
export { default as ChannelCreated } from "./ChannelCreated.js"
export { default as ChannelTopicChanged } from "./ChannelTopicChanged.js"
export { default as UserCreated } from "./UserCreated.js"
export { default as StampCreated } from "./StampCreated.js"
export { default as TagAdded } from "./TagAdded.js"
export { default as TagRemoved } from "./TagRemoved.js"
export { default as BotMessageStampsUpdated } from "./BotMessageStampsUpdated.js"
8 changes: 3 additions & 5 deletions src/handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {
import {
Joined,
Left,
MessageCreated,
Expand All @@ -10,12 +10,12 @@ const {
TagAdded,
TagRemoved,
BotMessageStampsUpdated
} = require("./events/events")
} from "./events/events.js"

const TOKEN_HEADER = "X-TRAQ-BOT-TOKEN"
const EVENT_HEADER = "X-TRAQ-BOT-EVENT"

class TraQEventHandler {
export default class TraQEventHandler {
constructor(token, request) {
this.token = token
this.request = request
Expand Down Expand Up @@ -141,5 +141,3 @@ class TraQEventHandler {
}
}
}

module.exports = TraQEventHandler
2 changes: 1 addition & 1 deletion src/nullUser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports.createNullUser = () => ({
export const createNullUser = () => ({
id: "00000000-0000-0000-0000-000000000000",
name: "",
displayName: "",
Expand Down
6 changes: 2 additions & 4 deletions src/request.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { Apis } = require("@traptitech/traq")
import { Apis } from "@traptitech/traq"

const createArgError = (path, obj) => {
return new Error(`無効な引数が渡されました: ${path}: ${JSON.stringify(obj)}`)
}

class Request {
export default class Request {
constructor(token, robot, domain, embed) {
this.token = token
this.robot = robot
Expand Down Expand Up @@ -165,5 +165,3 @@ class Request {
*/
// emote(envelope, strings...) {}
}

module.exports = Request
14 changes: 8 additions & 6 deletions traq.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const TraQAdapter = require("./src/adapter.js")
import TraQAdapter from "./src/adapter.js"

const NAME_ENV_NAME = "HUBOT_TRAQ_NAME"
const MODE_ENV_NAME = "HUBOT_TRAQ_MODE"
Expand Down Expand Up @@ -62,10 +62,12 @@ const getEnvs = () => {
return { mode, accessToken, embed, verifyToken, path, domain }
}

exports.use = robot => {
try {
return new TraQAdapter(robot, getEnvs())
} catch (e) {
console.error(e)
export default {
use(robot) {
try {
return new TraQAdapter(robot, getEnvs())
} catch (e) {
console.error(e)
}
}
}

0 comments on commit ca70a7f

Please sign in to comment.