Implement featured note injection
This commit is contained in:
@@ -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);
|
||||
|
Reference in New Issue
Block a user