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