summaryrefslogtreecommitdiff
path: root/src/server/activitypub
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/activitypub')
-rw-r--r--src/server/activitypub/featured.ts21
-rw-r--r--src/server/activitypub/followers.ts38
-rw-r--r--src/server/activitypub/following.ts41
-rw-r--r--src/server/activitypub/outbox.ts70
4 files changed, 62 insertions, 108 deletions
diff --git a/src/server/activitypub/featured.ts b/src/server/activitypub/featured.ts
index fc6150902b..f43312d79a 100644
--- a/src/server/activitypub/featured.ts
+++ b/src/server/activitypub/featured.ts
@@ -1,35 +1,28 @@
-import { ObjectID } from 'mongodb';
import * as Router from 'koa-router';
import config from '../../config';
-import User from '../../models/user';
import { renderActivity } from '../../remote/activitypub/renderer';
import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection';
import { setResponseType } from '../activitypub';
-import Note from '../../models/note';
import renderNote from '../../remote/activitypub/renderer/note';
+import { Users, Notes, UserNotePinings } from '../../models';
export default async (ctx: Router.IRouterContext) => {
- if (!ObjectID.isValid(ctx.params.user)) {
- ctx.status = 404;
- return;
- }
-
- const userId = new ObjectID(ctx.params.user);
+ const userId = ctx.params.user;
// Verify user
- const user = await User.findOne({
- _id: userId,
+ const user = await Users.findOne({
+ id: userId,
host: null
});
- if (user === null) {
+ if (user == null) {
ctx.status = 404;
return;
}
- const pinnedNoteIds = user.pinnedNoteIds || [];
+ const pinings = await UserNotePinings.find({ userId: user.id });
- const pinnedNotes = await Promise.all(pinnedNoteIds.filter(ObjectID.isValid).map(id => Note.findOne({ _id: id })));
+ const pinnedNotes = await Promise.all(pinings.map(pining => Notes.findOne(pining.noteId)));
const renderedNotes = await Promise.all(pinnedNotes.map(note => renderNote(note)));
diff --git a/src/server/activitypub/followers.ts b/src/server/activitypub/followers.ts
index 002576b2e7..62c54399ed 100644
--- a/src/server/activitypub/followers.ts
+++ b/src/server/activitypub/followers.ts
@@ -1,24 +1,18 @@
-import { ObjectID } from 'mongodb';
import * as Router from 'koa-router';
import config from '../../config';
import $ from 'cafy';
-import ID, { transform } from '../../misc/cafy-id';
-import User from '../../models/user';
-import Following from '../../models/following';
+import { ID } from '../../misc/cafy-id';
import * as url from '../../prelude/url';
import { renderActivity } from '../../remote/activitypub/renderer';
import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection';
import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page';
import renderFollowUser from '../../remote/activitypub/renderer/follow-user';
import { setResponseType } from '../activitypub';
+import { Users, Followings } from '../../models';
+import { LessThan } from 'typeorm';
export default async (ctx: Router.IRouterContext) => {
- if (!ObjectID.isValid(ctx.params.user)) {
- ctx.status = 404;
- return;
- }
-
- const userId = new ObjectID(ctx.params.user);
+ const userId = ctx.params.user;
// Get 'cursor' parameter
const [cursor, cursorErr] = $.optional.type(ID).get(ctx.request.query.cursor);
@@ -34,12 +28,12 @@ export default async (ctx: Router.IRouterContext) => {
}
// Verify user
- const user = await User.findOne({
- _id: userId,
+ const user = await Users.findOne({
+ id: userId,
host: null
});
- if (user === null) {
+ if (user == null) {
ctx.status = 404;
return;
}
@@ -49,22 +43,20 @@ export default async (ctx: Router.IRouterContext) => {
if (page) {
const query = {
- followeeId: user._id
+ followeeId: user.id
} as any;
// カーソルが指定されている場合
if (cursor) {
- query._id = {
- $lt: transform(cursor)
- };
+ query.id = LessThan(cursor);
}
// Get followers
- const followings = await Following
- .find(query, {
- limit: limit + 1,
- sort: { _id: -1 }
- });
+ const followings = await Followings.find({
+ where: query,
+ take: limit + 1,
+ order: { id: -1 }
+ });
// 「次のページ」があるかどうか
const inStock = followings.length === limit + 1;
@@ -80,7 +72,7 @@ export default async (ctx: Router.IRouterContext) => {
null,
inStock ? `${partOf}?${url.query({
page: 'true',
- cursor: followings[followings.length - 1]._id.toHexString()
+ cursor: followings[followings.length - 1].id
})}` : null
);
diff --git a/src/server/activitypub/following.ts b/src/server/activitypub/following.ts
index 0d7486f68a..4894aac1f8 100644
--- a/src/server/activitypub/following.ts
+++ b/src/server/activitypub/following.ts
@@ -1,24 +1,19 @@
-import { ObjectID } from 'mongodb';
import * as Router from 'koa-router';
import config from '../../config';
import $ from 'cafy';
-import ID, { transform } from '../../misc/cafy-id';
-import User from '../../models/user';
-import Following from '../../models/following';
+import { ID } from '../../misc/cafy-id';
import * as url from '../../prelude/url';
import { renderActivity } from '../../remote/activitypub/renderer';
import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection';
import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page';
import renderFollowUser from '../../remote/activitypub/renderer/follow-user';
import { setResponseType } from '../activitypub';
+import { Users, Followings } from '../../models';
+import { LessThan, FindConditions } from 'typeorm';
+import { Following } from '../../models/entities/following';
export default async (ctx: Router.IRouterContext) => {
- if (!ObjectID.isValid(ctx.params.user)) {
- ctx.status = 404;
- return;
- }
-
- const userId = new ObjectID(ctx.params.user);
+ const userId = ctx.params.user;
// Get 'cursor' parameter
const [cursor, cursorErr] = $.optional.type(ID).get(ctx.request.query.cursor);
@@ -34,12 +29,12 @@ export default async (ctx: Router.IRouterContext) => {
}
// Verify user
- const user = await User.findOne({
- _id: userId,
+ const user = await Users.findOne({
+ id: userId,
host: null
});
- if (user === null) {
+ if (user == null) {
ctx.status = 404;
return;
}
@@ -49,22 +44,20 @@ export default async (ctx: Router.IRouterContext) => {
if (page) {
const query = {
- followerId: user._id
- } as any;
+ followerId: user.id
+ } as FindConditions<Following>;
// カーソルが指定されている場合
if (cursor) {
- query._id = {
- $lt: transform(cursor)
- };
+ query.id = LessThan(cursor);
}
// Get followings
- const followings = await Following
- .find(query, {
- limit: limit + 1,
- sort: { _id: -1 }
- });
+ const followings = await Followings.find({
+ where: query,
+ take: limit + 1,
+ order: { id: -1 }
+ });
// 「次のページ」があるかどうか
const inStock = followings.length === limit + 1;
@@ -80,7 +73,7 @@ export default async (ctx: Router.IRouterContext) => {
null,
inStock ? `${partOf}?${url.query({
page: 'true',
- cursor: followings[followings.length - 1]._id.toHexString()
+ cursor: followings[followings.length - 1].id
})}` : null
);
diff --git a/src/server/activitypub/outbox.ts b/src/server/activitypub/outbox.ts
index ff8f884b19..377f43c986 100644
--- a/src/server/activitypub/outbox.ts
+++ b/src/server/activitypub/outbox.ts
@@ -1,28 +1,23 @@
-import { ObjectID } from 'mongodb';
import * as Router from 'koa-router';
import config from '../../config';
import $ from 'cafy';
-import ID, { transform } from '../../misc/cafy-id';
-import User from '../../models/user';
+import { ID } from '../../misc/cafy-id';
import { renderActivity } from '../../remote/activitypub/renderer';
import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection';
import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page';
import { setResponseType } from '../activitypub';
-
-import Note, { INote } from '../../models/note';
import renderNote from '../../remote/activitypub/renderer/note';
import renderCreate from '../../remote/activitypub/renderer/create';
import renderAnnounce from '../../remote/activitypub/renderer/announce';
import { countIf } from '../../prelude/array';
import * as url from '../../prelude/url';
+import { Users, Notes } from '../../models';
+import { makePaginationQuery } from '../api/common/make-pagination-query';
+import { Brackets } from 'typeorm';
+import { Note } from '../../models/entities/note';
export default async (ctx: Router.IRouterContext) => {
- if (!ObjectID.isValid(ctx.params.user)) {
- ctx.status = 404;
- return;
- }
-
- const userId = new ObjectID(ctx.params.user);
+ const userId = ctx.params.user;
// Get 'sinceId' parameter
const [sinceId, sinceIdErr] = $.optional.type(ID).get(ctx.request.query.since_id);
@@ -41,12 +36,12 @@ export default async (ctx: Router.IRouterContext) => {
}
// Verify user
- const user = await User.findOne({
- _id: userId,
+ const user = await Users.findOne({
+ id: userId,
host: null
});
- if (user === null) {
+ if (user == null) {
ctx.status = 404;
return;
}
@@ -55,34 +50,15 @@ export default async (ctx: Router.IRouterContext) => {
const partOf = `${config.url}/users/${userId}/outbox`;
if (page) {
- //#region Construct query
- const sort = {
- _id: -1
- };
-
- const query = {
- userId: user._id,
- visibility: { $in: ['public', 'home'] },
- localOnly: { $ne: true }
- } as any;
-
- if (sinceId) {
- sort._id = 1;
- query._id = {
- $gt: transform(sinceId)
- };
- } else if (untilId) {
- query._id = {
- $lt: transform(untilId)
- };
- }
- //#endregion
+ const query = makePaginationQuery(Notes.createQueryBuilder('note'), sinceId, untilId)
+ .andWhere('note.userId = :userId', { userId: user.id })
+ .andWhere(new Brackets(qb => { qb
+ .where(`note.visibility = 'public'`)
+ .orWhere(`note.visibility = 'home'`);
+ }))
+ .andWhere('note.localOnly = FALSE');
- const notes = await Note
- .find(query, {
- limit: limit,
- sort: sort
- });
+ const notes = await query.take(limit).getMany();
if (sinceId) notes.reverse();
@@ -96,11 +72,11 @@ export default async (ctx: Router.IRouterContext) => {
user.notesCount, activities, partOf,
notes.length ? `${partOf}?${url.query({
page: 'true',
- since_id: notes[0]._id.toHexString()
+ since_id: notes[0].id
})}` : null,
notes.length ? `${partOf}?${url.query({
page: 'true',
- until_id: notes[notes.length - 1]._id.toHexString()
+ until_id: notes[notes.length - 1].id
})}` : null
);
@@ -123,10 +99,10 @@ export default async (ctx: Router.IRouterContext) => {
* Pack Create<Note> or Announce Activity
* @param note Note
*/
-export async function packActivity(note: INote): Promise<object> {
- if (note.renoteId && note.text == null && note.poll == null && (note.fileIds == null || note.fileIds.length == 0)) {
- const renote = await Note.findOne(note.renoteId);
- return renderAnnounce(renote.uri ? renote.uri : `${config.url}/notes/${renote._id}`, note);
+export async function packActivity(note: Note): Promise<object> {
+ if (note.renoteId && note.text == null && !note.hasPoll && (note.fileIds == null || note.fileIds.length == 0)) {
+ const renote = await Notes.findOne(note.renoteId);
+ return renderAnnounce(renote.uri ? renote.uri : `${config.url}/notes/${renote.id}`, note);
}
return renderCreate(await renderNote(note, false), note);