summaryrefslogtreecommitdiff
path: root/src/server/api/common
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2020-02-18 19:05:11 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2020-02-18 19:05:11 +0900
commitd9986b7a2fabffff50068f4114a16d315941591f (patch)
tree7627c97a74a4e175cf8e0357cd380d21ac15b898 /src/server/api/common
parentImprove paging (diff)
downloadsharkey-d9986b7a2fabffff50068f4114a16d315941591f.tar.gz
sharkey-d9986b7a2fabffff50068f4114a16d315941591f.tar.bz2
sharkey-d9986b7a2fabffff50068f4114a16d315941591f.zip
Implement featured note injection
Diffstat (limited to 'src/server/api/common')
-rw-r--r--src/server/api/common/inject-featured.ts45
-rw-r--r--src/server/api/common/inject-promo.ts10
-rw-r--r--src/server/api/common/signup.ts1
3 files changed, 50 insertions, 6 deletions
diff --git a/src/server/api/common/inject-featured.ts b/src/server/api/common/inject-featured.ts
new file mode 100644
index 0000000000..ce6d421bb1
--- /dev/null
+++ b/src/server/api/common/inject-featured.ts
@@ -0,0 +1,45 @@
+import rndstr from 'rndstr';
+import { Note } from '../../../models/entities/note';
+import { User } from '../../../models/entities/user';
+import { Notes, UserProfiles } from '../../../models';
+import { generateMuteQuery } from './generate-mute-query';
+import { ensure } from '../../../prelude/ensure';
+
+// TODO: リアクション、Renote、返信などをしたノートは除外する
+
+export async function injectFeatured(timeline: Note[], user?: User | null) {
+ if (timeline.length < 5) return;
+
+ if (user) {
+ const profile = await UserProfiles.findOne(user.id).then(ensure);
+ if (!profile.injectFeaturedNote) return;
+ }
+
+ const max = 30;
+ const day = 1000 * 60 * 60 * 24 * 3; // 3日前まで
+
+ const query = Notes.createQueryBuilder('note')
+ .addSelect('note.score')
+ .where('note.userHost IS NULL')
+ .andWhere(`note.score > 0`)
+ .andWhere(`note.createdAt > :date`, { date: new Date(Date.now() - day) })
+ .andWhere(`note.visibility = 'public'`)
+ .leftJoinAndSelect('note.user', 'user');
+
+ if (user) generateMuteQuery(query, user);
+
+ const notes = await query
+ .orderBy('note.score', 'DESC')
+ .take(max)
+ .getMany();
+
+ if (notes.length === 0) return;
+
+ // Pick random one
+ const featured = notes[Math.floor(Math.random() * notes.length)];
+
+ (featured as any)._featuredId_ = rndstr('a-z0-9', 8);
+
+ // Inject featured
+ timeline.splice(3, 0, featured);
+}
diff --git a/src/server/api/common/inject-promo.ts b/src/server/api/common/inject-promo.ts
index 60817329c3..f694ce6ea0 100644
--- a/src/server/api/common/inject-promo.ts
+++ b/src/server/api/common/inject-promo.ts
@@ -4,14 +4,14 @@ import { User } from '../../../models/entities/user';
import { PromoReads, PromoNotes, Notes, Users } from '../../../models';
import { ensure } from '../../../prelude/ensure';
-export async function injectPromo(user: User, timeline: Note[]) {
+export async function injectPromo(timeline: Note[], user?: User | null) {
if (timeline.length < 5) return;
// TODO: readやexpireフィルタはクエリ側でやる
- const reads = await PromoReads.find({
+ const reads = user ? await PromoReads.find({
userId: user.id
- });
+ }) : [];
let promos = await PromoNotes.find();
@@ -20,15 +20,15 @@ export async function injectPromo(user: User, timeline: Note[]) {
if (promos.length === 0) return;
+ // Pick random promo
const promo = promos[Math.floor(Math.random() * promos.length)];
- // Pick random promo
const note = await Notes.findOne(promo.noteId).then(ensure);
// Join
note.user = await Users.findOne(note.userId).then(ensure);
- (note as any)._prInjectionId_ = rndstr('a-z0-9', 8);
+ (note as any)._prId_ = rndstr('a-z0-9', 8);
// Inject promo
timeline.splice(3, 0, note);
diff --git a/src/server/api/common/signup.ts b/src/server/api/common/signup.ts
index f0eb27e5e4..b6e13b36f1 100644
--- a/src/server/api/common/signup.ts
+++ b/src/server/api/common/signup.ts
@@ -88,7 +88,6 @@ export async function signup(username: User['username'], password: UserProfile['
await transactionalEntityManager.save(new UserProfile({
userId: account.id,
autoAcceptFollowed: true,
- autoWatch: false,
password: hash,
}));