diff options
Diffstat (limited to 'src/server/api/service/github.ts')
| -rw-r--r-- | src/server/api/service/github.ts | 55 |
1 files changed, 32 insertions, 23 deletions
diff --git a/src/server/api/service/github.ts b/src/server/api/service/github.ts index cf3589a4b7..e59b149d19 100644 --- a/src/server/api/service/github.ts +++ b/src/server/api/service/github.ts @@ -2,13 +2,14 @@ import * as Koa from 'koa'; import * as Router from 'koa-router'; import * as request from 'request'; import { OAuth2 } from 'oauth'; -import User, { pack, ILocalUser } from '../../../models/user'; import config from '../../../config'; import { publishMainStream } from '../../../services/stream'; import redis from '../../../db/redis'; import * as uuid from 'uuid'; import signin from '../common/signin'; import fetchMeta from '../../../misc/fetch-meta'; +import { Users, UserServiceLinkings } from '../../../models'; +import { ILocalUser } from '../../../models/entities/user'; function getUserToken(ctx: Koa.BaseContext) { return ((ctx.headers['cookie'] || '').match(/i=(!\w+)/) || [null, null])[1]; @@ -39,19 +40,24 @@ router.get('/disconnect/github', async ctx => { return; } - const user = await User.findOneAndUpdate({ + const user = await Users.findOne({ host: null, - 'token': userToken + token: userToken + }); + + await UserServiceLinkings.update({ + userId: user.id }, { - $set: { - 'github': null - } + github: false, + githubAccessToken: null, + githubId: null, + githubLogin: null, }); ctx.body = `GitHubの連携を解除しました :v:`; // Publish i updated event - publishMainStream(user._id, 'meUpdated', await pack(user, user, { + publishMainStream(user.id, 'meUpdated', await Users.pack(user, user, { detail: true, includeSecrets: true })); @@ -185,17 +191,21 @@ router.get('/gh/cb', async ctx => { return; } - const user = await User.findOne({ - host: null, - 'github.id': id - }) as ILocalUser; + const link = await UserServiceLinkings.createQueryBuilder() + .where('github @> :github', { + github: { + id: id, + }, + }) + .andWhere('userHost IS NULL') + .getOne(); - if (!user) { + if (link == null) { ctx.throw(404, `@${login}と連携しているMisskeyアカウントはありませんでした...`); return; } - signin(ctx, user, true); + signin(ctx, await Users.findOne(link.userId) as ILocalUser, true); } else { const code = ctx.query.code; @@ -248,23 +258,22 @@ router.get('/gh/cb', async ctx => { return; } - const user = await User.findOneAndUpdate({ + const user = await Users.findOne({ host: null, token: userToken - }, { - $set: { - github: { - accessToken, - id, - login - } - } + }); + + await UserServiceLinkings.update({ userId: user.id }, { + github: true, + githubAccessToken: accessToken, + githubId: id, + githubLogin: login, }); ctx.body = `GitHub: @${login} を、Misskey: @${user.username} に接続しました!`; // Publish i updated event - publishMainStream(user._id, 'meUpdated', await pack(user, user, { + publishMainStream(user.id, 'meUpdated', await Users.pack(user, user, { detail: true, includeSecrets: true })); |