mirror of
https://github.com/paricafe/misskey.git
synced 2024-11-30 09:26:44 -06:00
34 lines
976 B
TypeScript
34 lines
976 B
TypeScript
import rndstr from 'rndstr';
|
|
import { Note } from '../../../models/entities/note';
|
|
import { User } from '../../../models/entities/user';
|
|
import { PromoReads, PromoNotes, Notes, Users } from '../../../models';
|
|
|
|
export async function injectPromo(timeline: Note[], user?: User | null) {
|
|
if (timeline.length < 5) return;
|
|
|
|
// TODO: readやexpireフィルタはクエリ側でやる
|
|
|
|
const reads = user ? await PromoReads.find({
|
|
userId: user.id
|
|
}) : [];
|
|
|
|
let promos = await PromoNotes.find();
|
|
|
|
promos = promos.filter(n => n.expiresAt.getTime() > Date.now());
|
|
promos = promos.filter(n => !reads.map(r => r.noteId).includes(n.noteId));
|
|
|
|
if (promos.length === 0) return;
|
|
|
|
// Pick random promo
|
|
const promo = promos[Math.floor(Math.random() * promos.length)];
|
|
|
|
const note = await Notes.findOneOrFail(promo.noteId);
|
|
|
|
// Join
|
|
note.user = await Users.findOneOrFail(note.userId);
|
|
|
|
(note as any)._prId_ = rndstr('a-z0-9', 8);
|
|
|
|
// Inject promo
|
|
timeline.splice(3, 0, note);
|
|
}
|