diff options
| author | ha-dai <contact@haradai.net> | 2018-05-04 02:49:46 +0900 |
|---|---|---|
| committer | ha-dai <contact@haradai.net> | 2018-05-04 02:49:46 +0900 |
| commit | f850283147072c681df1b39c57f8bd0b14f18016 (patch) | |
| tree | 63ff533c91097da2d8ca2070fc67a28f67ee33da /src/server/api/common | |
| parent | Merge branch 'master' of github.com:syuilo/misskey (diff) | |
| parent | 1.7.0 (diff) | |
| download | misskey-f850283147072c681df1b39c57f8bd0b14f18016.tar.gz misskey-f850283147072c681df1b39c57f8bd0b14f18016.tar.bz2 misskey-f850283147072c681df1b39c57f8bd0b14f18016.zip | |
Merge branch 'master' of github.com:syuilo/misskey
Diffstat (limited to 'src/server/api/common')
| -rw-r--r-- | src/server/api/common/generate-native-user-token.ts | 3 | ||||
| -rw-r--r-- | src/server/api/common/get-friends.ts | 47 | ||||
| -rw-r--r-- | src/server/api/common/get-host-lower.ts | 5 | ||||
| -rw-r--r-- | src/server/api/common/is-native-token.ts | 1 | ||||
| -rw-r--r-- | src/server/api/common/read-messaging-message.ts | 66 | ||||
| -rw-r--r-- | src/server/api/common/read-notification.ts | 52 | ||||
| -rw-r--r-- | src/server/api/common/signin.ts | 22 |
7 files changed, 196 insertions, 0 deletions
diff --git a/src/server/api/common/generate-native-user-token.ts b/src/server/api/common/generate-native-user-token.ts new file mode 100644 index 0000000000..2082b89a5a --- /dev/null +++ b/src/server/api/common/generate-native-user-token.ts @@ -0,0 +1,3 @@ +import rndstr from 'rndstr'; + +export default () => `!${rndstr('a-zA-Z0-9', 32)}`; diff --git a/src/server/api/common/get-friends.ts b/src/server/api/common/get-friends.ts new file mode 100644 index 0000000000..50ba71ea96 --- /dev/null +++ b/src/server/api/common/get-friends.ts @@ -0,0 +1,47 @@ +import * as mongodb from 'mongodb'; +import Following from '../../../models/following'; + +export const getFriendIds = async (me: mongodb.ObjectID, includeMe = true) => { + // Fetch relation to other users who the I follows + // SELECT followee + const followings = await Following + .find({ + followerId: me + }, { + fields: { + followeeId: true + } + }); + + // ID list of other users who the I follows + const myfollowingIds = followings.map(following => following.followeeId); + + if (includeMe) { + myfollowingIds.push(me); + } + + return myfollowingIds; +}; + +export const getFriends = async (me: mongodb.ObjectID, includeMe = true) => { + // Fetch relation to other users who the I follows + const followings = await Following + .find({ + followerId: me + }); + + // ID list of other users who the I follows + const myfollowings = followings.map(following => ({ + id: following.followeeId, + stalk: following.stalk + })); + + if (includeMe) { + myfollowings.push({ + id: me, + stalk: true + }); + } + + return myfollowings; +}; diff --git a/src/server/api/common/get-host-lower.ts b/src/server/api/common/get-host-lower.ts new file mode 100644 index 0000000000..550c233001 --- /dev/null +++ b/src/server/api/common/get-host-lower.ts @@ -0,0 +1,5 @@ +import { toUnicode } from 'punycode'; + +export default host => { + return toUnicode(host).toLowerCase(); +}; diff --git a/src/server/api/common/is-native-token.ts b/src/server/api/common/is-native-token.ts new file mode 100644 index 0000000000..0769a4812e --- /dev/null +++ b/src/server/api/common/is-native-token.ts @@ -0,0 +1 @@ +export default (token: string) => token[0] == '!'; diff --git a/src/server/api/common/read-messaging-message.ts b/src/server/api/common/read-messaging-message.ts new file mode 100644 index 0000000000..c52f9363b5 --- /dev/null +++ b/src/server/api/common/read-messaging-message.ts @@ -0,0 +1,66 @@ +import * as mongo from 'mongodb'; +import Message from '../../../models/messaging-message'; +import { IMessagingMessage as IMessage } from '../../../models/messaging-message'; +import publishUserStream from '../../../publishers/stream'; +import { publishMessagingStream } from '../../../publishers/stream'; +import { publishMessagingIndexStream } from '../../../publishers/stream'; + +/** + * Mark as read message(s) + */ +export default ( + user: string | mongo.ObjectID, + otherparty: string | mongo.ObjectID, + message: string | string[] | IMessage | IMessage[] | mongo.ObjectID | mongo.ObjectID[] +) => new Promise<any>(async (resolve, reject) => { + + const userId = mongo.ObjectID.prototype.isPrototypeOf(user) + ? user + : new mongo.ObjectID(user); + + const otherpartyId = mongo.ObjectID.prototype.isPrototypeOf(otherparty) + ? otherparty + : new mongo.ObjectID(otherparty); + + const ids: mongo.ObjectID[] = Array.isArray(message) + ? mongo.ObjectID.prototype.isPrototypeOf(message[0]) + ? (message as mongo.ObjectID[]) + : typeof message[0] === 'string' + ? (message as string[]).map(m => new mongo.ObjectID(m)) + : (message as IMessage[]).map(m => m._id) + : mongo.ObjectID.prototype.isPrototypeOf(message) + ? [(message as mongo.ObjectID)] + : typeof message === 'string' + ? [new mongo.ObjectID(message)] + : [(message as IMessage)._id]; + + // Update documents + await Message.update({ + _id: { $in: ids }, + userId: otherpartyId, + recipientId: userId, + isRead: false + }, { + $set: { + isRead: true + } + }, { + multi: true + }); + + // Publish event + publishMessagingStream(otherpartyId, userId, 'read', ids.map(id => id.toString())); + publishMessagingIndexStream(userId, 'read', ids.map(id => id.toString())); + + // Calc count of my unread messages + const count = await Message + .count({ + recipientId: userId, + isRead: false + }); + + if (count == 0) { + // 全ての(いままで未読だった)自分宛てのメッセージを(これで)読みましたよというイベントを発行 + publishUserStream(userId, 'read_all_messaging_messages'); + } +}); diff --git a/src/server/api/common/read-notification.ts b/src/server/api/common/read-notification.ts new file mode 100644 index 0000000000..9bd41519fb --- /dev/null +++ b/src/server/api/common/read-notification.ts @@ -0,0 +1,52 @@ +import * as mongo from 'mongodb'; +import { default as Notification, INotification } from '../../../models/notification'; +import publishUserStream from '../../../publishers/stream'; + +/** + * Mark as read notification(s) + */ +export default ( + user: string | mongo.ObjectID, + message: string | string[] | INotification | INotification[] | mongo.ObjectID | mongo.ObjectID[] +) => new Promise<any>(async (resolve, reject) => { + + const userId = mongo.ObjectID.prototype.isPrototypeOf(user) + ? user + : new mongo.ObjectID(user); + + const ids: mongo.ObjectID[] = Array.isArray(message) + ? mongo.ObjectID.prototype.isPrototypeOf(message[0]) + ? (message as mongo.ObjectID[]) + : typeof message[0] === 'string' + ? (message as string[]).map(m => new mongo.ObjectID(m)) + : (message as INotification[]).map(m => m._id) + : mongo.ObjectID.prototype.isPrototypeOf(message) + ? [(message as mongo.ObjectID)] + : typeof message === 'string' + ? [new mongo.ObjectID(message)] + : [(message as INotification)._id]; + + // Update documents + await Notification.update({ + _id: { $in: ids }, + isRead: false + }, { + $set: { + isRead: true + } + }, { + multi: true + }); + + // Calc count of my unread notifications + const count = await Notification + .count({ + notifieeId: userId, + isRead: false + }); + + if (count == 0) { + // 全ての(いままで未読だった)通知を(これで)読みましたよというイベントを発行 + publishUserStream(userId, 'read_all_notifications'); + } +}); diff --git a/src/server/api/common/signin.ts b/src/server/api/common/signin.ts new file mode 100644 index 0000000000..44e1336f27 --- /dev/null +++ b/src/server/api/common/signin.ts @@ -0,0 +1,22 @@ +import * as Koa from 'koa'; + +import config from '../../../config'; +import { ILocalUser } from '../../../models/user'; + +export default function(ctx: Koa.Context, user: ILocalUser, redirect = false) { + const expires = 1000 * 60 * 60 * 24 * 365; // One Year + ctx.cookies.set('i', user.token, { + path: '/', + domain: config.hostname, + secure: config.url.startsWith('https'), + httpOnly: false, + expires: new Date(Date.now() + expires), + maxAge: expires + }); + + if (redirect) { + ctx.redirect(config.url); + } else { + ctx.status = 204; + } +} |