-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
223 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,33 +1,22 @@ | ||
# Logs | ||
logs | ||
lib-cov | ||
*.seed | ||
*.log | ||
npm-debug.log* | ||
|
||
# Runtime data | ||
pids | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.seed | ||
*.gz | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
pids | ||
logs | ||
results | ||
|
||
# Dependency directory | ||
npm-debug.log | ||
node_modules | ||
|
||
# Optional npm cache directory | ||
.npm | ||
config/* | ||
!config/default.coffee | ||
!config/test.coffee | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
db/* | ||
!db/.gitkeep |
This file contains 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,4 @@ | ||
.git* | ||
test/ | ||
src/ | ||
.DS_Store |
This file contains 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,7 @@ | ||
language: node_js | ||
node_js: | ||
- "0.10" | ||
- "0.11" | ||
- "0.12" | ||
- "4" | ||
- "5" |
This file contains 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,16 @@ | ||
express = require 'express' | ||
logger = require 'graceful-logger' | ||
config = require 'config' | ||
bodyParser = require 'body-parser' | ||
app = express() | ||
|
||
bot = require './src/bot' | ||
|
||
app.use bodyParser.json(limit: '10mb') | ||
app.use bodyParser.urlencoded(extended: true, limit: '10mb') | ||
|
||
app.bot = bot app | ||
|
||
app.listen config.port, -> logger.info "Bot listen on #{config.port}" | ||
|
||
module.exports = app |
This file contains 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,3 @@ | ||
module.exports = | ||
port: 8201 | ||
dbPath: 'db' |
This file contains 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 @@ | ||
module.exports = {} |
Empty file.
This file contains 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,38 @@ | ||
{ | ||
"name": "talk-checkin-bot", | ||
"version": "0.0.0", | ||
"description": "talk-checkin-bot", | ||
"main": "./lib/index.js", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"scripts": { | ||
"test": "NODE_ENV=test ./node_modules/.bin/mocha --require coffee-script/register --require should --reporter spec test/main.coffee" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "[email protected]:jianliaoim/talk-checkin-bot.git" | ||
}, | ||
"author": { | ||
"name": "Xu Jingxin", | ||
"email": "[email protected]" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"mocha": "^2.4.5", | ||
"should": "^8.2.1", | ||
"supertest": "^1.1.0" | ||
}, | ||
"dependencies": { | ||
"bluebird": "^3.1.5", | ||
"body-parser": "^1.14.2", | ||
"coffee-script": "^1.10.0", | ||
"config": "^1.19.0", | ||
"cron": "^1.1.0", | ||
"express": "^4.13.4", | ||
"graceful-logger": "^0.4.3", | ||
"lodash": "^4.0.1", | ||
"moment": "^2.11.1", | ||
"nedb": "^1.7.2" | ||
} | ||
} |
This file contains 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,3 @@ | ||
replies = [ | ||
|
||
] |
This file contains 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,25 @@ | ||
config = require 'config' | ||
model = require './model' | ||
|
||
class Bot | ||
|
||
module.exports = (app) -> | ||
|
||
bot = new Bot | ||
|
||
app.post '/messages', (req, res) -> | ||
|
||
return res.status(400).send({error: "Missing creator"}) unless req.body.creator | ||
|
||
checkinData = | ||
_creatorId: req.body.creator._id | ||
name: req.body.creator.name | ||
time: Date.now() | ||
|
||
model.addCheckin checkinData | ||
|
||
.then -> res.status(200).send ok: 1 | ||
|
||
.catch (err) -> res.status(400).send error: err.message | ||
|
||
bot |
This file contains 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,57 @@ | ||
path = require 'path' | ||
Datastore = require 'nedb' | ||
config = require 'config' | ||
moment = require 'moment' | ||
Promise = require 'bluebird' | ||
logger = require 'graceful-logger' | ||
_ = require 'lodash' | ||
fs = require 'fs' | ||
|
||
dbs = {} | ||
|
||
Promise.promisifyAll fs | ||
Promise.promisifyAll Datastore.prototype | ||
|
||
model = | ||
|
||
getDb: -> | ||
today = moment().format('YYYYMMDD') | ||
dbName = "checkin_#{today}.json" | ||
unless dbs[dbName] | ||
dbs[dbName] = new Datastore | ||
filename: path.join(config.dbPath, dbName) | ||
autoload: true | ||
dbs[dbName] | ||
|
||
destroyDb: -> | ||
today = moment().format('YYYYMMDD') | ||
dbName = "checkin_#{today}.json" | ||
delete dbs[dbName] | ||
fs.unlinkAsync path.join(config.dbPath, dbName) | ||
.catch (err) -> | ||
|
||
addCheckin: (checkinData) -> | ||
db = model.getDb() | ||
|
||
$firstCheckinData = db.findOneAsync _creatorId: checkinData._creatorId, type: 'first' | ||
|
||
.then (firstCheckinData) -> | ||
return firstCheckinData if firstCheckinData | ||
db.insertAsync _.assign {}, checkinData, type: 'first' | ||
|
||
$lastCheckinData = db.updateAsync | ||
_creatorId: checkinData._creatorId | ||
type: 'last' | ||
, | ||
_.assign {}, checkinData, type: 'last' | ||
, | ||
upsert: true | ||
returnUpdatedDocs: true | ||
|
||
Promise.all [$firstCheckinData, $lastCheckinData] | ||
|
||
getCheckins: -> | ||
db = model.getDb() | ||
db.findAsync({}) | ||
|
||
module.exports = model |
Empty file.
This file contains 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,54 @@ | ||
should = require 'should' | ||
fs = require 'fs' | ||
moment = require 'moment' | ||
app = require '../app' | ||
model = require '../src/model' | ||
request = require 'supertest' | ||
|
||
cleanup = (done) -> model.destroyDb().nodeify done | ||
|
||
describe 'Bot', -> | ||
|
||
before cleanup | ||
|
||
it 'should record the first checkin time of user', (done) -> | ||
|
||
request(app).post '/messages' | ||
.set 'Content-Type': 'application/json' | ||
.send | ||
creator: | ||
_id: 1 | ||
name: 'xxx' | ||
body: 'Hello' | ||
.end (err, res) -> | ||
res.body.should.eql ok: 1 | ||
done err | ||
|
||
it 'should update the last visit time of user', (done) -> | ||
|
||
# Checkin again | ||
request(app).post '/messages' | ||
.set 'Content-Type': 'application/json' | ||
.send | ||
creator: | ||
_id: 1 | ||
name: 'xxx' | ||
body: 'Hello Again' | ||
.end (err, res) -> | ||
res.body.should.eql ok: 1 | ||
done err | ||
|
||
it 'should get all the checkin data', (done) -> | ||
|
||
model.getCheckins() | ||
|
||
.then (datas) -> | ||
datas.length.should.eql 2 | ||
datas.forEach (data) -> data._creatorId.should.eql 1 | ||
datas.sort (x, y) -> if x.time > y.time then -1 else 1 | ||
datas[0].type.should.eql 'last' | ||
datas[1].type.should.eql 'first' | ||
|
||
.nodeify done | ||
|
||
after cleanup |