yumechi-no-kuni/src/services/register-hashtag.ts

32 lines
683 B
TypeScript
Raw Normal View History

2018-07-17 17:19:24 -05:00
import { IUser } from '../models/user';
import Hashtag from '../models/hashtag';
2018-10-21 02:54:07 -05:00
import { hashtagStats } from './stats';
2018-07-17 17:19:24 -05:00
export default async function(user: IUser, tag: string) {
tag = tag.toLowerCase();
const index = await Hashtag.findOne({ tag });
if (index != null) {
// 自分が初めてこのタグを使ったなら
if (!index.mentionedUserIds.some(id => id.equals(user._id))) {
Hashtag.update({ tag }, {
$push: {
mentionedUserIds: user._id
},
$inc: {
mentionedUserIdsCount: 1
}
});
}
} else {
Hashtag.insert({
tag,
mentionedUserIds: [user._id],
mentionedUserIdsCount: 1
});
}
2018-10-21 02:54:07 -05:00
hashtagStats.update(tag, user._id);
2018-07-17 17:19:24 -05:00
}