Implemet per user notes stats

This commit is contained in:
syuilo 2018-10-22 04:30:27 +09:00
parent 59266b3190
commit b8c56c4dda
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69
3 changed files with 96 additions and 2 deletions

View File

@ -23,7 +23,7 @@ import registerHashtag from '../register-hashtag';
import isQuote from '../../misc/is-quote';
import { TextElementMention } from '../../mfm/parse/elements/mention';
import { TextElementHashtag } from '../../mfm/parse/elements/hashtag';
import { notesStats } from '../stats';
import { notesStats, perUserNotesStats } from '../stats';
import { erase, unique } from '../../prelude/array';
import insertNoteUnread from './unread';
@ -166,6 +166,7 @@ export default async (user: IUser, data: Option, silent = false) => new Promise<
// 統計を更新
notesStats.update(note, true);
perUserNotesStats.update(user, note, true);
// ハッシュタグ登録
tags.map(tag => registerHashtag(user, tag));

View File

@ -6,7 +6,7 @@ import pack from '../../remote/activitypub/renderer';
import { deliver } from '../../queue';
import Following from '../../models/following';
import renderTombstone from '../../remote/activitypub/renderer/tombstone';
import { notesStats } from '../stats';
import { notesStats, perUserNotesStats } from '../stats';
import config from '../../config';
import NoteUnread from '../../models/note-unread';
import read from './read';
@ -64,4 +64,5 @@ export default async function(user: IUser, note: INote) {
// 統計を更新
notesStats.update(note, false);
perUserNotesStats.update(user, note, false);
}

View File

@ -819,3 +819,95 @@ class FollowingStats extends Stats<FollowingLog> {
export const followingStats = new FollowingStats();
//#endregion
//#region Per user notes stats
/**
* 稿
*/
type PerUserNotesLog = {
/**
* 稿
*/
total: number;
/**
* 稿
*/
inc: number;
/**
* 稿
*/
dec: number;
diffs: {
/**
* 稿
*/
normal: number;
/**
* 稿
*/
reply: number;
/**
* Renoteの投稿数の差分
*/
renote: number;
};
};
class PerUserNotesStats extends Stats<PerUserNotesLog> {
constructor() {
super('notes');
}
@autobind
protected async getTemplate(init: boolean, latest?: PerUserNotesLog, group?: any): Promise<PerUserNotesLog> {
const [count] = init ? await Promise.all([
Note.count({ userId: group, deletedAt: null }),
]) : [
latest ? latest.total : 0
];
return {
total: count,
inc: 0,
dec: 0,
diffs: {
normal: 0,
reply: 0,
renote: 0
}
};
}
@autobind
public async update(user: IUser, note: INote, isAdditional: boolean) {
const update: Obj = {
diffs: {}
};
update.total = isAdditional ? 1 : -1;
if (isAdditional) {
update.inc = 1;
} else {
update.dec = 1;
}
if (note.replyId != null) {
update.diffs.reply = isAdditional ? 1 : -1;
} else if (note.renoteId != null) {
update.diffs.renote = isAdditional ? 1 : -1;
} else {
update.diffs.normal = isAdditional ? 1 : -1;
}
await this.inc(update, user._id);
}
}
export const perUserNotesStats = new PerUserNotesStats();
//#endregion