summaryrefslogtreecommitdiff
path: root/src/server/api/common
diff options
context:
space:
mode:
authortamaina <tamaina@hotmail.co.jp>2018-04-11 20:27:09 +0900
committerGitHub <noreply@github.com>2018-04-11 20:27:09 +0900
commitd43fe853c3605696e2e57e240845d0fc9c284f61 (patch)
tree838914e262c0fca5737588a7bba64e2b9f3d8e5f /src/server/api/common
parentUpdate README.md (diff)
parentwip #1443 (diff)
downloadmisskey-d43fe853c3605696e2e57e240845d0fc9c284f61.tar.gz
misskey-d43fe853c3605696e2e57e240845d0fc9c284f61.tar.bz2
misskey-d43fe853c3605696e2e57e240845d0fc9c284f61.zip
Merge pull request #1 from syuilo/master
追従
Diffstat (limited to 'src/server/api/common')
-rw-r--r--src/server/api/common/generate-native-user-token.ts3
-rw-r--r--src/server/api/common/get-friends.ts24
-rw-r--r--src/server/api/common/get-host-lower.ts5
-rw-r--r--src/server/api/common/is-native-token.ts1
-rw-r--r--src/server/api/common/read-messaging-message.ts66
-rw-r--r--src/server/api/common/read-notification.ts52
-rw-r--r--src/server/api/common/signin.ts19
7 files changed, 170 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..c1cc3957d8
--- /dev/null
+++ b/src/server/api/common/get-friends.ts
@@ -0,0 +1,24 @@
+import * as mongodb from 'mongodb';
+import Following from '../../../models/following';
+
+export default async (me: mongodb.ObjectID, includeMe: boolean = true) => {
+ // Fetch relation to other users who the I follows
+ // SELECT followee
+ const myfollowing = await Following
+ .find({
+ followerId: me
+ }, {
+ fields: {
+ followeeId: true
+ }
+ });
+
+ // ID list of other users who the I follows
+ const myfollowingIds = myfollowing.map(follow => follow.followeeId);
+
+ if (includeMe) {
+ myfollowingIds.push(me);
+ }
+
+ return myfollowingIds;
+};
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..fc4b30439e
--- /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).replace(/[A-Z]+/, match => match.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..8bb327694d
--- /dev/null
+++ b/src/server/api/common/signin.ts
@@ -0,0 +1,19 @@
+import config from '../../../config';
+
+export default function(res, user, redirect: boolean) {
+ const expires = 1000 * 60 * 60 * 24 * 365; // One Year
+ res.cookie('i', user.token, {
+ path: '/',
+ domain: `.${config.hostname}`,
+ secure: config.url.substr(0, 5) === 'https',
+ httpOnly: false,
+ expires: new Date(Date.now() + expires),
+ maxAge: expires
+ });
+
+ if (redirect) {
+ res.redirect(config.url);
+ } else {
+ res.sendStatus(204);
+ }
+}