hippofish/src/services/register-hashtag.ts

29 lines
605 B
TypeScript
Raw Normal View History

2018-07-18 00:19:24 +02:00
import { IUser } from '../models/user';
import Hashtag from '../models/hashtag';
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
});
}
}