2019-04-07 07:50:36 -05:00
|
|
|
import { User } from '../models/entities/user';
|
|
|
|
import { Hashtags, Users } from '../models';
|
|
|
|
import { hashtagChart } from './chart';
|
|
|
|
import { genId } from '../misc/gen-id';
|
|
|
|
import { Hashtag } from '../models/entities/hashtag';
|
2019-02-17 08:41:47 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
export async function updateHashtag(user: User, tag: string, isUserAttached = false, inc = true) {
|
2019-02-17 08:41:47 -06:00
|
|
|
tag = tag.toLowerCase();
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
const index = await Hashtags.findOne({ name: tag });
|
2019-02-17 08:41:47 -06:00
|
|
|
|
|
|
|
if (index == null && !inc) return;
|
|
|
|
|
|
|
|
if (index != null) {
|
2019-04-07 07:50:36 -05:00
|
|
|
const q = Hashtags.createQueryBuilder('tag').update()
|
|
|
|
.where('tag.name = :name', { name: tag });
|
|
|
|
|
|
|
|
const set = {} as any;
|
2019-02-17 08:41:47 -06:00
|
|
|
|
|
|
|
if (isUserAttached) {
|
|
|
|
if (inc) {
|
|
|
|
// 自分が初めてこのタグを使ったなら
|
2019-04-07 07:50:36 -05:00
|
|
|
if (!index.attachedUserIds.some(id => id === user.id)) {
|
|
|
|
set.attachedUserIds = () => `array_append(tag.attachedUserIds, '${user.id}')`;
|
|
|
|
set.attachedUsersCount = () => `tag.attachedUsersCount + 1`;
|
2019-02-17 08:41:47 -06:00
|
|
|
}
|
|
|
|
// 自分が(ローカル内で)初めてこのタグを使ったなら
|
2019-04-07 07:50:36 -05:00
|
|
|
if (Users.isLocalUser(user) && !index.attachedLocalUserIds.some(id => id === user.id)) {
|
|
|
|
set.attachedLocalUserIds = () => `array_append(tag.attachedLocalUserIds, '${user.id}')`;
|
|
|
|
set.attachedLocalUsersCount = () => `tag.attachedLocalUsersCount + 1`;
|
2019-02-17 08:41:47 -06:00
|
|
|
}
|
2019-02-17 20:47:25 -06:00
|
|
|
// 自分が(リモートで)初めてこのタグを使ったなら
|
2019-04-07 07:50:36 -05:00
|
|
|
if (Users.isRemoteUser(user) && !index.attachedRemoteUserIds.some(id => id === user.id)) {
|
|
|
|
set.attachedRemoteUserIds = () => `array_append(tag.attachedRemoteUserIds, '${user.id}')`;
|
|
|
|
set.attachedRemoteUsersCount = () => `tag.attachedRemoteUsersCount + 1`;
|
2019-02-17 20:47:25 -06:00
|
|
|
}
|
2019-02-17 08:41:47 -06:00
|
|
|
} else {
|
2019-04-07 07:50:36 -05:00
|
|
|
set.attachedUserIds = () => `array_remove(tag.attachedUserIds, '${user.id}')`;
|
|
|
|
set.attachedUsersCount = () => `tag.attachedUsersCount - 1`;
|
|
|
|
if (Users.isLocalUser(user)) {
|
|
|
|
set.attachedLocalUserIds = () => `array_remove(tag.attachedLocalUserIds, '${user.id}')`;
|
|
|
|
set.attachedLocalUsersCount = () => `tag.attachedLocalUsersCount - 1`;
|
2019-02-17 20:47:25 -06:00
|
|
|
} else {
|
2019-04-07 07:50:36 -05:00
|
|
|
set.attachedRemoteUserIds = () => `array_remove(tag.attachedRemoteUserIds, '${user.id}')`;
|
|
|
|
set.attachedRemoteUsersCount = () => `tag.attachedRemoteUsersCount - 1`;
|
2019-02-17 08:41:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 自分が初めてこのタグを使ったなら
|
2019-04-07 07:50:36 -05:00
|
|
|
if (!index.mentionedUserIds.some(id => id === user.id)) {
|
|
|
|
set.mentionedUserIds = () => `array_append(tag.mentionedUserIds, '${user.id}')`;
|
|
|
|
set.mentionedUsersCount = () => `tag.mentionedUsersCount + 1`;
|
2019-02-17 08:41:47 -06:00
|
|
|
}
|
|
|
|
// 自分が(ローカル内で)初めてこのタグを使ったなら
|
2019-04-07 07:50:36 -05:00
|
|
|
if (Users.isLocalUser(user) && !index.mentionedLocalUserIds.some(id => id === user.id)) {
|
|
|
|
set.mentionedLocalUserIds = () => `array_append(tag.mentionedLocalUserIds, '${user.id}')`;
|
|
|
|
set.mentionedLocalUsersCount = () => `tag.mentionedLocalUsersCount + 1`;
|
2019-02-17 08:41:47 -06:00
|
|
|
}
|
2019-02-17 20:47:25 -06:00
|
|
|
// 自分が(リモートで)初めてこのタグを使ったなら
|
2019-04-07 07:50:36 -05:00
|
|
|
if (Users.isRemoteUser(user) && !index.mentionedRemoteUserIds.some(id => id === user.id)) {
|
|
|
|
set.mentionedRemoteUserIds = () => `array_append(tag.mentionedRemoteUserIds, '${user.id}')`;
|
|
|
|
set.mentionedRemoteUsersCount = () => `tag.mentionedRemoteUsersCount + 1`;
|
2019-02-17 20:47:25 -06:00
|
|
|
}
|
2019-02-17 08:41:47 -06:00
|
|
|
}
|
|
|
|
|
2019-04-13 04:23:32 -05:00
|
|
|
if (Object.keys(set).length > 0) {
|
2019-05-17 10:38:33 -05:00
|
|
|
q.set(set);
|
2019-04-13 04:23:32 -05:00
|
|
|
q.execute();
|
|
|
|
}
|
2019-02-17 08:41:47 -06:00
|
|
|
} else {
|
|
|
|
if (isUserAttached) {
|
2019-04-07 07:50:36 -05:00
|
|
|
Hashtags.save({
|
|
|
|
id: genId(),
|
|
|
|
name: tag,
|
2019-02-17 08:41:47 -06:00
|
|
|
mentionedUserIds: [],
|
|
|
|
mentionedUsersCount: 0,
|
|
|
|
mentionedLocalUserIds: [],
|
|
|
|
mentionedLocalUsersCount: 0,
|
2019-02-17 20:47:25 -06:00
|
|
|
mentionedRemoteUserIds: [],
|
|
|
|
mentionedRemoteUsersCount: 0,
|
2019-04-07 07:50:36 -05:00
|
|
|
attachedUserIds: [user.id],
|
2019-02-17 08:41:47 -06:00
|
|
|
attachedUsersCount: 1,
|
2019-04-07 07:50:36 -05:00
|
|
|
attachedLocalUserIds: Users.isLocalUser(user) ? [user.id] : [],
|
|
|
|
attachedLocalUsersCount: Users.isLocalUser(user) ? 1 : 0,
|
|
|
|
attachedRemoteUserIds: Users.isRemoteUser(user) ? [user.id] : [],
|
|
|
|
attachedRemoteUsersCount: Users.isRemoteUser(user) ? 1 : 0,
|
|
|
|
} as Hashtag);
|
2019-02-17 08:41:47 -06:00
|
|
|
} else {
|
2019-04-07 07:50:36 -05:00
|
|
|
Hashtags.save({
|
|
|
|
id: genId(),
|
|
|
|
name: tag,
|
|
|
|
mentionedUserIds: [user.id],
|
2019-02-17 08:41:47 -06:00
|
|
|
mentionedUsersCount: 1,
|
2019-04-07 07:50:36 -05:00
|
|
|
mentionedLocalUserIds: Users.isLocalUser(user) ? [user.id] : [],
|
|
|
|
mentionedLocalUsersCount: Users.isLocalUser(user) ? 1 : 0,
|
|
|
|
mentionedRemoteUserIds: Users.isRemoteUser(user) ? [user.id] : [],
|
|
|
|
mentionedRemoteUsersCount: Users.isRemoteUser(user) ? 1 : 0,
|
2019-02-17 08:41:47 -06:00
|
|
|
attachedUserIds: [],
|
|
|
|
attachedUsersCount: 0,
|
|
|
|
attachedLocalUserIds: [],
|
2019-02-17 20:47:25 -06:00
|
|
|
attachedLocalUsersCount: 0,
|
|
|
|
attachedRemoteUserIds: [],
|
|
|
|
attachedRemoteUsersCount: 0,
|
2019-04-07 07:50:36 -05:00
|
|
|
} as Hashtag);
|
2019-02-17 08:41:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isUserAttached) {
|
|
|
|
hashtagChart.update(tag, user);
|
|
|
|
}
|
|
|
|
}
|