[LINE] 通知の表示に対応
This commit is contained in:
		
							parent
							
								
									a702b27c3d
								
							
						
					
					
						commit
						3f0f307104
					
				|  | @ -5,6 +5,7 @@ import User, { IUser, init as initUser } from '../models/user'; | |||
| 
 | ||||
| import getPostSummary from '../../common/get-post-summary'; | ||||
| import getUserSummary from '../../common/get-user-summary'; | ||||
| import getNotificationSummary from '../../common/get-notification-summary'; | ||||
| 
 | ||||
| import Othello, { ai as othelloAi } from '../../common/othello'; | ||||
| 
 | ||||
|  | @ -84,6 +85,7 @@ export default class BotCore extends EventEmitter { | |||
| 					'logout, signout: サインアウトします\n' + | ||||
| 					'post: 投稿します\n' + | ||||
| 					'tl: タイムラインを見ます\n' + | ||||
| 					'no: 通知を見ます\n' + | ||||
| 					'@<ユーザー名>: ユーザーを表示します'; | ||||
| 
 | ||||
| 			case 'me': | ||||
|  | @ -115,6 +117,11 @@ export default class BotCore extends EventEmitter { | |||
| 			case 'タイムライン': | ||||
| 				return await this.tlCommand(); | ||||
| 
 | ||||
| 			case 'no': | ||||
| 			case 'notifications': | ||||
| 			case '通知': | ||||
| 				return await this.notificationsCommand(); | ||||
| 
 | ||||
| 			case 'guessing-game': | ||||
| 			case '数当てゲーム': | ||||
| 				this.setContext(new GuessingGameContext(this)); | ||||
|  | @ -155,6 +162,7 @@ export default class BotCore extends EventEmitter { | |||
| 		this.emit('updated'); | ||||
| 	} | ||||
| 
 | ||||
| 	// TODO: if (this.user == null) return 'まずサインインしてください。'; を @signinRequired みたいなデコレータでいい感じにする
 | ||||
| 	public async tlCommand(): Promise<string | void> { | ||||
| 		if (this.user == null) return 'まずサインインしてください。'; | ||||
| 
 | ||||
|  | @ -163,7 +171,21 @@ export default class BotCore extends EventEmitter { | |||
| 		}, this.user); | ||||
| 
 | ||||
| 		const text = tl | ||||
| 			.map(post => getPostSummary(post)) | ||||
| 			.map(post => post.user.name + ': ' + getPostSummary(post)) | ||||
| 			.join('\n-----\n'); | ||||
| 
 | ||||
| 		return text; | ||||
| 	} | ||||
| 
 | ||||
| 	public async notificationsCommand(): Promise<string | void> { | ||||
| 		if (this.user == null) return 'まずサインインしてください。'; | ||||
| 
 | ||||
| 		const notifications = await require('../endpoints/i/notifications')({ | ||||
| 			limit: 5 | ||||
| 		}, this.user); | ||||
| 
 | ||||
| 		const text = notifications | ||||
| 			.map(notification => getNotificationSummary(notification)) | ||||
| 			.join('\n-----\n'); | ||||
| 
 | ||||
| 		return text; | ||||
|  |  | |||
|  | @ -1,8 +1,47 @@ | |||
| import * as mongo from 'mongodb'; | ||||
| import db from '../../db/mongodb'; | ||||
| import { IUser } from './user'; | ||||
| 
 | ||||
| export default db.get('notifications') as any; // fuck type definition
 | ||||
| 
 | ||||
| export interface INotification { | ||||
| 	_id: mongo.ObjectID; | ||||
| 	created_at: Date; | ||||
| 
 | ||||
| 	/** | ||||
| 	 * 通知の受信者 | ||||
| 	 */ | ||||
| 	notifiee?: IUser; | ||||
| 
 | ||||
| 	/** | ||||
| 	 * 通知の受信者 | ||||
| 	 */ | ||||
| 	notifiee_id: mongo.ObjectID; | ||||
| 
 | ||||
| 	/** | ||||
| 	 * イニシエータ(initiator)。通知を行う原因となったユーザー | ||||
| 	 */ | ||||
| 	notifier?: IUser; | ||||
| 
 | ||||
| 	/** | ||||
| 	 * イニシエータ(initiator)。通知を行う原因となったユーザー | ||||
| 	 */ | ||||
| 	notifier_id: mongo.ObjectID; | ||||
| 
 | ||||
| 	/** | ||||
| 	 * 通知の種類。 | ||||
| 	 * follow - フォローされた | ||||
| 	 * mention - 投稿で自分が言及された | ||||
| 	 * reply - (自分または自分がWatchしている)投稿が返信された | ||||
| 	 * repost - (自分または自分がWatchしている)投稿がRepostされた | ||||
| 	 * quote - (自分または自分がWatchしている)投稿が引用Repostされた | ||||
| 	 * reaction - (自分または自分がWatchしている)投稿にリアクションされた | ||||
| 	 * poll_vote - (自分または自分がWatchしている)投稿の投票に投票された | ||||
| 	 */ | ||||
| 	type: 'follow' | 'mention' | 'reply' | 'repost' | 'quote' | 'reaction' | 'poll_vote'; | ||||
| 
 | ||||
| 	/** | ||||
| 	 * 通知が読まれたかどうか | ||||
| 	 */ | ||||
| 	is_read: Boolean; | ||||
| } | ||||
|  |  | |||
|  | @ -0,0 +1,28 @@ | |||
| import { INotification } from '../api/models/notification'; | ||||
| import getPostSummary from './get-post-summary'; | ||||
| import getReactionEmoji from './get-reaction-emoji'; | ||||
| 
 | ||||
| /** | ||||
|  * 通知を表す文字列を取得します。 | ||||
|  * @param notification 通知 | ||||
|  */ | ||||
| export default function(notification: INotification & any): string { | ||||
| 	switch (notification.type) { | ||||
| 		case 'follow': | ||||
| 			return `${notification.notifier.name}にフォローされました`; | ||||
| 		case 'mention': | ||||
| 			return `言及されました: ${notification.notifier.name}「${getPostSummary(notification.post)}」`; | ||||
| 		case 'reply': | ||||
| 			return `返信されました: ${notification.notifier.name}「${getPostSummary(notification.post)}」`; | ||||
| 		case 'repost': | ||||
| 			return `Repostされました: ${notification.notifier.name}「${getPostSummary(notification.post)}」`; | ||||
| 		case 'quote': | ||||
| 			return `引用されました: ${notification.notifier.name}「${getPostSummary(notification.post)}」`; | ||||
| 		case 'reaction': | ||||
| 			return `リアクションされました: ${notification.notifier.name} <${getReactionEmoji(notification.reaction)}>「${getPostSummary(notification.post)}」`; | ||||
| 		case 'poll_vote': | ||||
| 			return `投票されました: ${notification.notifier.name}「${getPostSummary(notification.post)}」`; | ||||
| 		default: | ||||
| 			return `<不明な通知タイプ: ${notification.type}>`; | ||||
| 	} | ||||
| } | ||||
|  | @ -0,0 +1,14 @@ | |||
| export default function(reaction: string): string { | ||||
| 	switch (reaction) { | ||||
| 		case 'like': return '👍'; | ||||
| 		case 'love': return '❤️'; | ||||
| 		case 'laugh': return '😆'; | ||||
| 		case 'hmm': return '🤔'; | ||||
| 		case 'surprise': return '😮'; | ||||
| 		case 'congrats': return '🎉'; | ||||
| 		case 'angry': return '💢'; | ||||
| 		case 'confused': return '😥'; | ||||
| 		case 'pudding': return '🍮'; | ||||
| 		default: return ''; | ||||
| 	} | ||||
| } | ||||
		Loading…
	
		Reference in New Issue