summaryrefslogtreecommitdiff
path: root/src/chart/notes.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-10-23 05:36:35 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-10-23 05:36:35 +0900
commit7c7f32d9a6597fdc7bea02da0cfd4a843fd32d22 (patch)
treea67396d881993b4240b49f288594b827ef58531c /src/chart/notes.ts
parentfix(package): update file-type to version 10.1.0 (#2984) (diff)
downloadsharkey-7c7f32d9a6597fdc7bea02da0cfd4a843fd32d22.tar.gz
sharkey-7c7f32d9a6597fdc7bea02da0cfd4a843fd32d22.tar.bz2
sharkey-7c7f32d9a6597fdc7bea02da0cfd4a843fd32d22.zip
Refactoring
Diffstat (limited to 'src/chart/notes.ts')
-rw-r--r--src/chart/notes.ts114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/chart/notes.ts b/src/chart/notes.ts
new file mode 100644
index 0000000000..738778e72a
--- /dev/null
+++ b/src/chart/notes.ts
@@ -0,0 +1,114 @@
+import autobind from 'autobind-decorator';
+import Chart, { Obj } from '.';
+import Note, { INote } from '../models/note';
+import { isLocalUser } from '../models/user';
+
+/**
+ * 投稿に関するチャート
+ */
+type NotesLog = {
+ local: {
+ /**
+ * 集計期間時点での、全投稿数
+ */
+ total: number;
+
+ /**
+ * 増加した投稿数
+ */
+ inc: number;
+
+ /**
+ * 減少した投稿数
+ */
+ dec: number;
+
+ diffs: {
+ /**
+ * 通常の投稿数の差分
+ */
+ normal: number;
+
+ /**
+ * リプライの投稿数の差分
+ */
+ reply: number;
+
+ /**
+ * Renoteの投稿数の差分
+ */
+ renote: number;
+ };
+ };
+
+ remote: NotesLog['local'];
+};
+
+class NotesChart extends Chart<NotesLog> {
+ constructor() {
+ super('notes');
+ }
+
+ @autobind
+ protected async getTemplate(init: boolean, latest?: NotesLog): Promise<NotesLog> {
+ const [localCount, remoteCount] = init ? await Promise.all([
+ Note.count({ '_user.host': null }),
+ Note.count({ '_user.host': { $ne: null } })
+ ]) : [
+ latest ? latest.local.total : 0,
+ latest ? latest.remote.total : 0
+ ];
+
+ return {
+ local: {
+ total: localCount,
+ inc: 0,
+ dec: 0,
+ diffs: {
+ normal: 0,
+ reply: 0,
+ renote: 0
+ }
+ },
+ remote: {
+ total: remoteCount,
+ inc: 0,
+ dec: 0,
+ diffs: {
+ normal: 0,
+ reply: 0,
+ renote: 0
+ }
+ }
+ };
+ }
+
+ @autobind
+ public async update(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({
+ [isLocalUser(note._user) ? 'local' : 'remote']: update
+ });
+ }
+}
+
+export default new NotesChart();