From 410b9ad6bcde756e5cdf13f55711199027dfc7a6 Mon Sep 17 00:00:00 2001
From: MeiMei <30769358+mei23@users.noreply.github.com>
Date: Sun, 12 May 2019 09:37:00 +0900
Subject: [PATCH] =?UTF-8?q?Fix:=20=E3=83=94=E3=83=B3=E7=95=99=E3=82=81?=
 =?UTF-8?q?=E6=8A=95=E7=A8=BF=E3=81=AE=E8=A1=A8=E7=A4=BA=E9=A0=86=E3=81=8C?=
 =?UTF-8?q?=E3=81=8A=E3=81=8B=E3=81=97=E3=81=84=20(#4906)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Fix: syuilo#4904

* fix comment
---
 src/models/repositories/user.ts         | 5 ++++-
 src/remote/activitypub/models/person.ts | 8 +++++++-
 src/server/activitypub/featured.ts      | 5 ++++-
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/src/models/repositories/user.ts b/src/models/repositories/user.ts
index 33eb53612a..330220fb72 100644
--- a/src/models/repositories/user.ts
+++ b/src/models/repositories/user.ts
@@ -72,7 +72,10 @@ export class UserRepository extends Repository<User> {
 		const meId = me ? typeof me === 'string' ? me : me.id : null;
 
 		const relation = meId && (meId !== user.id) && opts.detail ? await this.getRelation(meId, user.id) : null;
-		const pins = opts.detail ? await UserNotePinings.find({ userId: user.id }) : [];
+		const pins = opts.detail ? await UserNotePinings.find({
+			where: { userId: user.id },
+			order: { id: 'DESC' }
+		}) : [];
 		const profile = opts.detail ? await UserProfiles.findOne(user.id).then(ensure) : null;
 
 		const falsy = opts.detail ? false : undefined;
diff --git a/src/remote/activitypub/models/person.ts b/src/remote/activitypub/models/person.ts
index a40677dfc3..654d36403e 100644
--- a/src/remote/activitypub/models/person.ts
+++ b/src/remote/activitypub/models/person.ts
@@ -474,9 +474,15 @@ export async function updateFeatured(userId: User['id']) {
 		.slice(0, 5)
 		.map(item => limit(() => resolveNote(item, resolver))));
 
+	// delete
+	await UserNotePinings.delete({ userId: user.id });
+
+	// とりあえずidを別の時間で生成して順番を維持
+	let td = 0;
 	for (const note of featuredNotes.filter(note => note != null)) {
+		td -= 1000;
 		UserNotePinings.save({
-			id: genId(),
+			id: genId(new Date(Date.now() + td)),
 			createdAt: new Date(),
 			userId: user.id,
 			noteId: note!.id
diff --git a/src/server/activitypub/featured.ts b/src/server/activitypub/featured.ts
index 86ec1000c7..dae72f16b6 100644
--- a/src/server/activitypub/featured.ts
+++ b/src/server/activitypub/featured.ts
@@ -21,7 +21,10 @@ export default async (ctx: Router.IRouterContext) => {
 		return;
 	}
 
-	const pinings = await UserNotePinings.find({ userId: user.id });
+	const pinings = await UserNotePinings.find({
+		where: { userId: user.id },
+		order: { id: 'DESC' }
+	});
 
 	const pinnedNotes = await Promise.all(pinings.map(pining =>
 		Notes.findOne(pining.noteId).then(ensure)));