<commit_before>import { List, Match } from 'powerful';
const isEmpty = List.isEmpty;
import {TalkGroupBase, TalkUserMessage} from '../../../models';
import {ITalkMessage, IUser} from '../../../interfaces';
import serialize from '../../../core/serialize-talk-message';
import readTalkMessage from '../../../core/read-talk-message';

/**
 * Talkのストリームを取得します
 * @param me API利用ユーザー
 * @param limit 取得する投稿の最大数
 * @param sinceCursor 取得するメッセージを、設定されたカーソルよりも大きなカーソルを持つもののみに制限します
 * @param maxCursor 取得するメッセージを、設定されたカーソルよりも小さなカーソルを持つもののみに制限します
 * @param userId 相手のユーザーのID
 * @param groupId
 */
export default function(
	me: IUser,
	limit: number = 10,
	sinceCursor: number = null,
	maxCursor: number = null,
	userId: string = null,
	groupId: string = null
): Promise<Object[]> {
	'use strict';

	return new Promise<Object[]>((resolve, reject) => {
		if (userId !== null) {
			getUserStream();
		} else if (groupId !== null) {
			getGroupStream();
		} else {
			reject('empty-destination-query');
		}

		function getUserStream(): void {
			'use strict';
			const query = Object.assign({
				$or: [{
					user: me.id,
					recipient: userId
				}, {
					user: userId,
					recipient: me.id
				}]
			}, new Match<void, any>(null)
				.when(() => sinceCursor !== null, () => {
					return { cursor: { $gt: sinceCursor } };
				})
				.when(() => maxCursor !== null, () => {
					return { cursor: { $lt: maxCursor } };
				})
				.getValue({})
			);

			TalkUserMessage
			.find(query)
			.sort('-createdAt')
			.limit(limit)
			.exec((err: any, messages: ITalkMessage[]) => {
				if (err !== null) {
					return reject(err);
				} else if (isEmpty(messages)) {
					return resolve([]);
				}

				Promise.all(messages.map(message =>
					serialize(message, me)
				)).then(resolve, reject);

				// 既読にする
				messages.forEach(message => {
					readTalkMessage(me, message);
				});
			});
		}

		function getGroupStream(): void {
			'use strict';
			const query = Object.assign({
				group: groupId,
			}, new Match<void, any>(null)
				.when(() => sinceCursor !== null, () => {
					return { cursor: { $gt: sinceCursor } };
				})
				.when(() => maxCursor !== null, () => {
					return { cursor: { $lt: maxCursor } };
				})
				.getValue({})
			);

			TalkGroupBase
			.find(query)
			.sort('-createdAt')
			.limit(limit)
			.exec((err: any, messages: ITalkMessage[]) => {
				if (err !== null) {
					return reject(err);
				} else if (isEmpty(messages)) {
					return resolve([]);
				}

				Promise.all(messages.map(message =>
					serialize(message, me)
				)).then(resolve, reject);

				// 既読にする
				messages.forEach(message => {
					readTalkMessage(me, message);
				});
			});
		}
	});
}<commit_msg>Fix bug<commit_after>import { List, Match } from 'powerful';
const isEmpty = List.isEmpty;
import {TalkGroup, TalkGroupBase, TalkUserMessage} from '../../../models';
import {ITalkGroup, ITalkMessage, IUser} from '../../../interfaces';
import serialize from '../../../core/serialize-talk-message';
import readTalkMessage from '../../../core/read-talk-message';

/**
 * Talkのストリームを取得します
 * @param me API利用ユーザー
 * @param limit 取得する投稿の最大数
 * @param sinceCursor 取得するメッセージを、設定されたカーソルよりも大きなカーソルを持つもののみに制限します
 * @param maxCursor 取得するメッセージを、設定されたカーソルよりも小さなカーソルを持つもののみに制限します
 * @param userId 相手のユーザーのID
 * @param groupId
 */
export default function(
	me: IUser,
	limit: number = 10,
	sinceCursor: number = null,
	maxCursor: number = null,
	userId: string = null,
	groupId: string = null
): Promise<Object[]> {
	'use strict';

	return new Promise<Object[]>((resolve, reject) => {
		if (userId !== null) {
			getUserStream();
		} else if (groupId !== null) {
			getGroupStream();
		} else {
			reject('empty-destination-query');
		}

		function getUserStream(): void {
			'use strict';
			const query = Object.assign({
				$or: [{
					user: me.id,
					recipient: userId
				}, {
					user: userId,
					recipient: me.id
				}]
			}, new Match<void, any>(null)
				.when(() => sinceCursor !== null, () => {
					return { cursor: { $gt: sinceCursor } };
				})
				.when(() => maxCursor !== null, () => {
					return { cursor: { $lt: maxCursor } };
				})
				.getValue({})
			);

			TalkUserMessage
			.find(query)
			.sort('-createdAt')
			.limit(limit)
			.exec((err: any, messages: ITalkMessage[]) => {
				if (err !== null) {
					return reject(err);
				} else if (isEmpty(messages)) {
					return resolve([]);
				}

				Promise.all(messages.map(message =>
					serialize(message, me)
				)).then(resolve, reject);

				// 既読にする
				messages.forEach(message => {
					readTalkMessage(me, message);
				});
			});
		}

		function getGroupStream(): void {
			'use strict';
			TalkGroup
			.findById(groupId)
			.exec((groupFindErr: any, group: ITalkGroup) => {
				if (groupFindErr !== null) {
					return reject(groupFindErr);
				} else if (group === null) {
					return reject('group-not-found');
				} else if (
					(<string[]>group.members)
					.map(member => member.toString())
					.indexOf(me.id.toString()) === -1
				) {
					return reject('access-denied');
				}

				const query = Object.assign({
					group: group.id,
				}, new Match<void, any>(null)
					.when(() => sinceCursor !== null, () => {
						return { cursor: { $gt: sinceCursor } };
					})
					.when(() => maxCursor !== null, () => {
						return { cursor: { $lt: maxCursor } };
					})
					.getValue({})
				);

				TalkGroupBase
				.find(query)
				.sort('-createdAt')
				.limit(limit)
				.exec((err: any, messages: ITalkMessage[]) => {
					if (err !== null) {
						return reject(err);
					} else if (isEmpty(messages)) {
						return resolve([]);
					}

					Promise.all(messages.map(message =>
						serialize(message, me)
					)).then(resolve, reject);

					// 既読にする
					messages.forEach(message => {
						readTalkMessage(me, message);
					});
				});
			});
		}
	});
}
{'fraction_non_alphanumeric': 0.18071672354948806, 'fraction_numerical': 0.0008532423208191126, 'mean_word_length': 2.781290322580645, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6873382', 'n_tokens_mistral': 2590, 'n_tokens_neox': 2060, 'n_words': 427}