Implement #2993
This commit is contained in:
		
							parent
							
								
									daa22d68fa
								
							
						
					
					
						commit
						a136715111
					
				
					 5 changed files with 153 additions and 0 deletions
				
			
		
							
								
								
									
										65
									
								
								src/chart/federation.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								src/chart/federation.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,65 @@
 | 
			
		|||
import autobind from 'autobind-decorator';
 | 
			
		||||
import Chart, { Obj } from '.';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * フェデレーションに関するチャート
 | 
			
		||||
 */
 | 
			
		||||
type FederationLog = {
 | 
			
		||||
	instance: {
 | 
			
		||||
		/**
 | 
			
		||||
		 * インスタンス数の合計
 | 
			
		||||
		 */
 | 
			
		||||
		total: number;
 | 
			
		||||
 | 
			
		||||
		/**
 | 
			
		||||
		 * 増加インスタンス数
 | 
			
		||||
		 */
 | 
			
		||||
		inc: number;
 | 
			
		||||
 | 
			
		||||
		/**
 | 
			
		||||
		 * 減少インスタンス数
 | 
			
		||||
		 */
 | 
			
		||||
		dec: number;
 | 
			
		||||
	};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class FederationChart extends Chart<FederationLog> {
 | 
			
		||||
	constructor() {
 | 
			
		||||
		super('federation');
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async getTemplate(init: boolean, latest?: FederationLog): Promise<FederationLog> {
 | 
			
		||||
		const [total] = init ? await Promise.all([
 | 
			
		||||
			Instance.count({})
 | 
			
		||||
		]) : [
 | 
			
		||||
			latest ? latest.instance.total : 0
 | 
			
		||||
		];
 | 
			
		||||
 | 
			
		||||
		return {
 | 
			
		||||
			instance: {
 | 
			
		||||
				total: total,
 | 
			
		||||
				inc: 0,
 | 
			
		||||
				dec: 0
 | 
			
		||||
			}
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	public async update(isAdditional: boolean) {
 | 
			
		||||
		const update: Obj = {};
 | 
			
		||||
 | 
			
		||||
		update.total = isAdditional ? 1 : -1;
 | 
			
		||||
		if (isAdditional) {
 | 
			
		||||
			update.inc = 1;
 | 
			
		||||
		} else {
 | 
			
		||||
			update.dec = 1;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		await this.inc({
 | 
			
		||||
			instance: update
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default new FederationChart();
 | 
			
		||||
							
								
								
									
										35
									
								
								src/models/instance.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/models/instance.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,35 @@
 | 
			
		|||
import * as mongo from 'mongodb';
 | 
			
		||||
import db from '../db/mongodb';
 | 
			
		||||
 | 
			
		||||
const Instance = db.get<IInstance>('instances');
 | 
			
		||||
Instance.createIndex('host', { unique: true });
 | 
			
		||||
export default Instance;
 | 
			
		||||
 | 
			
		||||
export interface IInstance {
 | 
			
		||||
	_id: mongo.ObjectID;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * ホスト
 | 
			
		||||
	 */
 | 
			
		||||
	host: string;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * このインスタンスを捕捉した日時
 | 
			
		||||
	 */
 | 
			
		||||
	caughtAt: Date;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * このインスタンスのシステム (MastodonとかMisskeyとかPleromaとか)
 | 
			
		||||
	 */
 | 
			
		||||
	system: string;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * このインスタンスのユーザー数
 | 
			
		||||
	 */
 | 
			
		||||
	usersCount: number;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * このインスタンスから受け取った投稿数
 | 
			
		||||
	 */
 | 
			
		||||
	notesCount: number;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -13,6 +13,8 @@ import htmlToMFM from '../../../mfm/html-to-mfm';
 | 
			
		|||
import usersChart from '../../../chart/users';
 | 
			
		||||
import { URL } from 'url';
 | 
			
		||||
import { resolveNote } from './note';
 | 
			
		||||
import registerInstance from '../../../services/register-instance';
 | 
			
		||||
import Instance from '../../../models/instance';
 | 
			
		||||
 | 
			
		||||
const log = debug('misskey:activitypub');
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -173,6 +175,18 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise<IU
 | 
			
		|||
		throw e;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Register host
 | 
			
		||||
	registerInstance(host).then(i => {
 | 
			
		||||
		Instance.update({ _id: i._id }, {
 | 
			
		||||
			$inc: {
 | 
			
		||||
				usersCount: 1
 | 
			
		||||
			}
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		// TODO
 | 
			
		||||
		//perInstanceChart.newUser();
 | 
			
		||||
	});
 | 
			
		||||
 | 
			
		||||
	//#region Increment users count
 | 
			
		||||
	Meta.update({}, {
 | 
			
		||||
		$inc: {
 | 
			
		||||
| 
						 | 
				
			
			@ -214,6 +228,7 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise<IU
 | 
			
		|||
	//#endregion
 | 
			
		||||
 | 
			
		||||
	await updateFeatured(user._id).catch(err => console.log(err));
 | 
			
		||||
 | 
			
		||||
	return user;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -28,6 +28,8 @@ import perUserNotesChart from '../../chart/per-user-notes';
 | 
			
		|||
 | 
			
		||||
import { erase, unique } from '../../prelude/array';
 | 
			
		||||
import insertNoteUnread from './unread';
 | 
			
		||||
import registerInstance from '../register-instance';
 | 
			
		||||
import Instance from '../../models/instance';
 | 
			
		||||
 | 
			
		||||
type NotificationType = 'reply' | 'renote' | 'quote' | 'mention';
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -170,6 +172,20 @@ export default async (user: IUser, data: Option, silent = false) => new Promise<
 | 
			
		|||
	notesChart.update(note, true);
 | 
			
		||||
	perUserNotesChart.update(user, note, true);
 | 
			
		||||
 | 
			
		||||
	// Register host
 | 
			
		||||
	if (isRemoteUser(user)) {
 | 
			
		||||
		registerInstance(user.host).then(i => {
 | 
			
		||||
			Instance.update({ _id: i._id }, {
 | 
			
		||||
				$inc: {
 | 
			
		||||
					notesCount: 1
 | 
			
		||||
				}
 | 
			
		||||
			});
 | 
			
		||||
 | 
			
		||||
			// TODO
 | 
			
		||||
			//perInstanceChart.newNote();
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// ハッシュタグ登録
 | 
			
		||||
	tags.map(tag => registerHashtag(user, tag));
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										22
									
								
								src/services/register-instance.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/services/register-instance.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,22 @@
 | 
			
		|||
import Instance, { IInstance } from '../models/instance';
 | 
			
		||||
import federationChart from '../chart/federation';
 | 
			
		||||
 | 
			
		||||
export default async function(host: string): Promise<IInstance> {
 | 
			
		||||
	if (host == null) return null;
 | 
			
		||||
 | 
			
		||||
	const index = await Instance.findOne({ host });
 | 
			
		||||
 | 
			
		||||
	if (index == null) {
 | 
			
		||||
		const i = await Instance.insert({
 | 
			
		||||
			host,
 | 
			
		||||
			caughtAt: new Date(),
 | 
			
		||||
			system: null // TODO
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		federationChart.update(true);
 | 
			
		||||
 | 
			
		||||
		return i;
 | 
			
		||||
	} else {
 | 
			
		||||
		return index;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue