Skip to content
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

Add High Available mode support #89

Merged
merged 17 commits into from
Mar 31, 2020
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ script:
- node --version
- npm --version
- npm test
- ./node_modules/.bin/ts-node tests/fixtures/smoke-testing.ts
- ./node_modules/.bin/ts-node --files tests/fixtures/smoke-testing.ts

notifications:
email:
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ We are current DevOps the master branch from the repo to Heroku under the protec
You can visit the staging system at <http://OSSChat.kaiyuanshe.cn/>

## How to use

use osschat is so easy, just need 4 steps, please refer [How to use](https://github.com/kaiyuanshe/osschat/blob/master/docs/pages/how-to-use.md)

## Meeting Notes
Expand Down Expand Up @@ -98,6 +99,11 @@ To be added...
- Wechaty Puppet Padplus sponsored by: [JuziBot](https://www.juzi.bot)
- Heroku Getting Started Template from [Wechaty](https://github.com/wechaty/)

## Links

- [Scaling your Redux App with ducks](https://www.freecodecamp.org/news/scaling-your-redux-app-with-ducks-6115955638be/)
- [Redux Style Guide](https://redux.js.org/style-guide/style-guide#do-not-put-non-serializable-values-in-state-or-actions)

## Copyright & License

- Code & Docs © 2019-now 开源社
Expand Down
8 changes: 3 additions & 5 deletions bin/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
log,
VERSION,
} from '../src/config'
import { getWechaty } from '../src/get-wechaty'
import { getHAWechaty } from '../src/get-wechaty'
import { startBot } from '../src/start-bot'
import { startFinis } from '../src/start-finis'

Expand Down Expand Up @@ -40,15 +40,13 @@ export = async (app: Application) => {

log.verbose('main', 'main()')

const bot = getWechaty()
const bot = getHAWechaty()

await Promise.all([
bot.start(),
startBot(bot),
startFinis(bot),
])

while (bot.state.on()) {
await new Promise(resolve => setTimeout(resolve, 1000))
}
await bot.state.ready('off')
}
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"name": "osschat",
"version": "0.2.53",
"version": "0.3.6",
"description": "Apache OSSChat",
"main": "index.js",
"engines": {
"node": "10"
},
"scripts": {
"clean": "shx rm -fr dist/*",
"dist": "npm run build",
"build": "tsc",
"lint": "npm run lint:es && npm run lint:ts",
"lint:ts": "tsc --noEmit",
Expand All @@ -26,14 +28,18 @@
},
"homepage": "https://github.com/kaiyuanshe/OSSChat#readme",
"dependencies": {
"@reduxjs/toolkit": "^1.3.2",
"array-flatten": "^3.0.0",
"brolog": "^1.6.5",
"commander": "^4.0.1",
"finis": "^0.4.3",
"flatten-array": "^1.0.0",
"micromatch": "^4.0.2",
"moment": "^2.24.0",
"node-cache": "^5.1.0",
"probot": "^9.8.1",
"qrcode-terminal": "^0.12.0",
"redux": "^4.0.5",
"smee-client": "^1.1.0",
"wechaty": "^0.37.8",
"wechaty-puppet-padplus": "^0.5.27"
Expand All @@ -48,6 +54,7 @@
"@types/read-pkg-up": "^6.0.0",
"eslint": "^6.8.0",
"express": "^4.17.1",
"shx": "^0.3.2",
"tstest": "^0.4.2"
},
"git": {
Expand Down
18 changes: 11 additions & 7 deletions src/chatops.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
Wechaty,
UrlLink,
Message,
} from 'wechaty'
Expand All @@ -12,19 +11,20 @@ import {
DEV_ROOM_ID,
HEARTBEAT_ROOM_ID,
} from './config'
import { HAWechaty } from './ha-wechaty'

export class Chatops {

private static singleton: Chatops

public static instance (
bot?: Wechaty,
haBot?: HAWechaty,
) {
if (!this.singleton) {
if (!bot) {
if (!haBot) {
throw new Error('instance need a Wechaty instance to initialize')
}
this.singleton = new Chatops(bot)
this.singleton = new Chatops(haBot)
}
return this.singleton
}
Expand All @@ -38,7 +38,7 @@ export class Chatops {
private delayQueueExecutor: DelayQueueExecutor

private constructor (
private bot: Wechaty,
private haBot: HAWechaty,
) {
this.delayQueueExecutor = new DelayQueueExecutor(5 * 1000) // set delay period time to 5 seconds
}
Expand All @@ -61,13 +61,17 @@ export class Chatops {
): Promise<void> {
log.info('Chatops', 'roomMessage(%s, %s)', roomId, info)

const online = this.bot.logonoff()
const online = this.haBot.logonoff()
if (!online) {
log.error('Chatops', 'roomMessage() this.bot is offline')
return
}

const room = this.bot.Room.load(roomId)
const room = await this.haBot.Room.load(roomId)
if (!room) {
log.error('Chatops', 'roomMessage() no bot found in room %s', roomId)
return
}

if (typeof info === 'string') {
await room.say(info)
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference path="./typings.d.ts" />
/**
* VERSION
*/
Expand Down
6 changes: 6 additions & 0 deletions src/ducks/counter/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createAction } from '@reduxjs/toolkit'

import * as types from './types'

export const mo = createAction(types.MO)
export const mt = createAction(types.MT)
16 changes: 16 additions & 0 deletions src/ducks/counter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import reducer from './reducers'

// export { default as counterSelectors } from './selectors'
// export { default as counterOperations } from './operations'

import * as counterActions from './actions'
import * as counterTypes from './types'
import * as counterSelectors from './selectors'

export {
counterActions,
counterSelectors,
counterTypes,
}

export default reducer
14 changes: 14 additions & 0 deletions src/ducks/counter/operations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// import {

// } from './actions'

// // This is a link to an action defined in actions.js.
// export const simpleQuack = actions.quack

// // This is a thunk which dispatches multiple actions from actions.js
// export const complexQuack = ( distance ) => ( dispatch ) => {
// dispatch( actions.quack( ) ).then( ( ) => {
// dispatch( actions.swim( distance ) );
// dispatch( /* any action */ );
// } );
// }
31 changes: 31 additions & 0 deletions src/ducks/counter/reducers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
createReducer,
} from '@reduxjs/toolkit'

import * as types from './types'
// import * as actions from './actions'

const initialState = {
mo: 0,
mt: 0,
} as types.State

const moReducer = (state: types.State) => ({
...state,
mo: state.mo + 1,
})

const mtReducer = (state: types.State) => ({
...state,
mt: state.mt + 1,
})

const counterReducer = createReducer(
initialState,
{
[types.MO]: moReducer,
[types.MT]: mtReducer,
},
)

export default counterReducer
13 changes: 13 additions & 0 deletions src/ducks/counter/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as types from './types'

export function mo (
state: types.State,
) {
return state.mo
}

export function mt (
state: types.State,
) {
return state.mt
}
16 changes: 16 additions & 0 deletions src/ducks/counter/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// import expect from 'expect.js'
// import reducer from './reducers'
// import actions from './actions'

// describe('duck reducer', function( ) {
// describe('quack', function( ) {
// const quack = actions.quack( )
// const initialState = false

// const result = reducer( initialState, quack )

// it( 'should quack', function( ) {
// expect( result ).to.be( true )
// } )
// } )
// } )
7 changes: 7 additions & 0 deletions src/ducks/counter/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const MO = 'osschat/wechaty/message/mo'
export const MT = 'osschat/wechaty/message/mt'

export interface State {
mo: number,
mt: number,
}
45 changes: 45 additions & 0 deletions src/ducks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Huan(202003): Redux with Ducks
*
* Scaling your Redux App with ducks:
* https://www.freecodecamp.org/news/scaling-your-redux-app-with-ducks-6115955638be/
*
* Redux Toolkit - Usage With TypeScript:
* https://redux-toolkit.js.org/usage/usage-with-typescript
*
*/
import { combineReducers } from 'redux'
import { configureStore } from '@reduxjs/toolkit'

import logonoff, {
logonoffActions,
logonoffSelectors,
} from './logonoff'
import counter, {
counterActions,
counterSelectors,
} from './counter'

export {
logonoffActions,
logonoffSelectors,

counterActions,
counterSelectors,
}

const reducer = combineReducers({
counter,
logonoff,
})

export const store = configureStore({
reducer,
})

store.subscribe(() => {
console.info('state:', store.getState())
})

export type RootState = ReturnType<typeof reducer>
export type AppDispatch = typeof store.dispatch
40 changes: 40 additions & 0 deletions src/ducks/logonoff/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
createAction,
} from '@reduxjs/toolkit'

import * as types from './types'

const prepareScan = (
id: string,
qrcode: string,
) => {
const payload = {
id,
qrcode,
}
return { payload }
}

const prepareLogin = (
id: string,
userName: string,
) => {
const payload = {
id,
userName,
}
return { payload }
}

const prepareLogout = (
id: string,
) => {
const payload = {
id,
}
return { payload }
}

export const scan = createAction(types.SCAN, prepareScan)
export const login = createAction(types.LOGIN, prepareLogin)
export const logout = createAction(types.LOGOUT, prepareLogout)
14 changes: 14 additions & 0 deletions src/ducks/logonoff/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import reducer from './reducers'

import * as logonoffSelectors from './selectors'
// export { default as duckOperations } from './operations'
import * as logonoffTypes from './types'
import * as logonoffActions from './actions'

export {
logonoffActions,
logonoffSelectors,
logonoffTypes,
}

export default reducer
14 changes: 14 additions & 0 deletions src/ducks/logonoff/operations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// import {

// } from './actions'

// // This is a link to an action defined in actions.js.
// export const simpleQuack = actions.quack

// // This is a thunk which dispatches multiple actions from actions.js
// export const complexQuack = ( distance ) => ( dispatch ) => {
// dispatch( actions.quack( ) ).then( ( ) => {
// dispatch( actions.swim( distance ) );
// dispatch( /* any action */ );
// } );
// }
Loading