diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-01-21 15:31:16 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-01-21 15:31:16 +0900 |
| commit | 9c424a35e0cf4d2ab5369698a3dd81c38aa0a6a9 (patch) | |
| tree | 377eacc67577816171d5de8203f3869e56f23c7a /src/api | |
| parent | Fix: Add missing semicolon (diff) | |
| download | sharkey-9c424a35e0cf4d2ab5369698a3dd81c38aa0a6a9.tar.gz sharkey-9c424a35e0cf4d2ab5369698a3dd81c38aa0a6a9.tar.bz2 sharkey-9c424a35e0cf4d2ab5369698a3dd81c38aa0a6a9.zip | |
Implement Twitter Connect
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/serializers/user.ts | 5 | ||||
| -rw-r--r-- | src/api/service/twitter.ts | 30 |
2 files changed, 30 insertions, 5 deletions
diff --git a/src/api/serializers/user.ts b/src/api/serializers/user.ts index 9002285da4..d9dd4af7ca 100644 --- a/src/api/serializers/user.ts +++ b/src/api/serializers/user.ts @@ -64,7 +64,10 @@ export default ( delete _user.password; delete _user.token; delete _user.username_lower; - delete _user.twitter; + if (_user.twitter) { + delete _user.twitter.accessToken; + delete _user.twitter.accessTokenSecret; + } // Visible via only the official client if (!opts.includeSecrets) { diff --git a/src/api/service/twitter.ts b/src/api/service/twitter.ts index 4796d32124..9891ddd64c 100644 --- a/src/api/service/twitter.ts +++ b/src/api/service/twitter.ts @@ -3,10 +3,18 @@ import * as express from 'express'; //const Twitter = require('twitter'); import autwh from 'autwh'; import redis from '../../db/redis'; +import User from '../models/user'; +import serialize from '../serializers/user'; +import event from '../event'; import config from '../../conf'; module.exports = (app: express.Application) => { - if (config.twitter == null) return; + if (config.twitter == null) { + app.get('/connect/twitter', (req, res) => { + res.send('現在Twitterへ接続できません'); + }); + return; + } const twAuth = autwh({ consumerKey: config.twitter.consumer_key, @@ -24,9 +32,23 @@ module.exports = (app: express.Application) => { app.get('/tw/cb', (req, res): any => { if (res.locals.user == null) return res.send('plz signin'); redis.get(res.locals.user, async (_, ctx) => { - const tokens = await twAuth.done(JSON.parse(ctx), req.query.oauth_verifier); - console.log(tokens); - res.send('Authorized!'); + const result = await twAuth.done(JSON.parse(ctx), req.query.oauth_verifier); + + const user = await User.findOneAndUpdate({ + token: res.locals.user + }, { + $set: { + twitter: result + } + }); + + res.send(`Twitter: @${result.screenName} を、Misskey: @${user.username} に接続しました!`); + + // Publish i updated event + event(user._id, 'i_updated', await serialize(user, user, { + detail: true, + includeSecrets: true + })); }) }); }; |