mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-24 02:43:09 +02:00
111 lines
4.4 KiB
TypeScript
111 lines
4.4 KiB
TypeScript
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';
|
|
|
|
export async function updateHashtag(user: User, tag: string, isUserAttached = false, inc = true) {
|
|
tag = tag.toLowerCase();
|
|
|
|
const index = await Hashtags.findOne({ name: tag });
|
|
|
|
if (index == null && !inc) return;
|
|
|
|
if (index != null) {
|
|
const q = Hashtags.createQueryBuilder('tag').update()
|
|
.where('tag.name = :name', { name: tag });
|
|
|
|
const set = {} as any;
|
|
|
|
if (isUserAttached) {
|
|
if (inc) {
|
|
// 自分が初めてこのタグを使ったなら
|
|
if (!index.attachedUserIds.some(id => id === user.id)) {
|
|
set.attachedUserIds = () => `array_append(tag.attachedUserIds, '${user.id}')`;
|
|
set.attachedUsersCount = () => `tag.attachedUsersCount + 1`;
|
|
}
|
|
// 自分が(ローカル内で)初めてこのタグを使ったなら
|
|
if (Users.isLocalUser(user) && !index.attachedLocalUserIds.some(id => id === user.id)) {
|
|
set.attachedLocalUserIds = () => `array_append(tag.attachedLocalUserIds, '${user.id}')`;
|
|
set.attachedLocalUsersCount = () => `tag.attachedLocalUsersCount + 1`;
|
|
}
|
|
// 自分が(リモートで)初めてこのタグを使ったなら
|
|
if (Users.isRemoteUser(user) && !index.attachedRemoteUserIds.some(id => id === user.id)) {
|
|
set.attachedRemoteUserIds = () => `array_append(tag.attachedRemoteUserIds, '${user.id}')`;
|
|
set.attachedRemoteUsersCount = () => `tag.attachedRemoteUsersCount + 1`;
|
|
}
|
|
} else {
|
|
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`;
|
|
} else {
|
|
set.attachedRemoteUserIds = () => `array_remove(tag.attachedRemoteUserIds, '${user.id}')`;
|
|
set.attachedRemoteUsersCount = () => `tag.attachedRemoteUsersCount - 1`;
|
|
}
|
|
}
|
|
} else {
|
|
// 自分が初めてこのタグを使ったなら
|
|
if (!index.mentionedUserIds.some(id => id === user.id)) {
|
|
set.mentionedUserIds = () => `array_append(tag.mentionedUserIds, '${user.id}')`;
|
|
set.mentionedUsersCount = () => `tag.mentionedUsersCount + 1`;
|
|
}
|
|
// 自分が(ローカル内で)初めてこのタグを使ったなら
|
|
if (Users.isLocalUser(user) && !index.mentionedLocalUserIds.some(id => id === user.id)) {
|
|
set.mentionedLocalUserIds = () => `array_append(tag.mentionedLocalUserIds, '${user.id}')`;
|
|
set.mentionedLocalUsersCount = () => `tag.mentionedLocalUsersCount + 1`;
|
|
}
|
|
// 自分が(リモートで)初めてこのタグを使ったなら
|
|
if (Users.isRemoteUser(user) && !index.mentionedRemoteUserIds.some(id => id === user.id)) {
|
|
set.mentionedRemoteUserIds = () => `array_append(tag.mentionedRemoteUserIds, '${user.id}')`;
|
|
set.mentionedRemoteUsersCount = () => `tag.mentionedRemoteUsersCount + 1`;
|
|
}
|
|
}
|
|
|
|
if (Object.keys(set).length > 0) {
|
|
q.set(set);
|
|
q.execute();
|
|
}
|
|
} else {
|
|
if (isUserAttached) {
|
|
Hashtags.save({
|
|
id: genId(),
|
|
name: tag,
|
|
mentionedUserIds: [],
|
|
mentionedUsersCount: 0,
|
|
mentionedLocalUserIds: [],
|
|
mentionedLocalUsersCount: 0,
|
|
mentionedRemoteUserIds: [],
|
|
mentionedRemoteUsersCount: 0,
|
|
attachedUserIds: [user.id],
|
|
attachedUsersCount: 1,
|
|
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);
|
|
} else {
|
|
Hashtags.save({
|
|
id: genId(),
|
|
name: tag,
|
|
mentionedUserIds: [user.id],
|
|
mentionedUsersCount: 1,
|
|
mentionedLocalUserIds: Users.isLocalUser(user) ? [user.id] : [],
|
|
mentionedLocalUsersCount: Users.isLocalUser(user) ? 1 : 0,
|
|
mentionedRemoteUserIds: Users.isRemoteUser(user) ? [user.id] : [],
|
|
mentionedRemoteUsersCount: Users.isRemoteUser(user) ? 1 : 0,
|
|
attachedUserIds: [],
|
|
attachedUsersCount: 0,
|
|
attachedLocalUserIds: [],
|
|
attachedLocalUsersCount: 0,
|
|
attachedRemoteUserIds: [],
|
|
attachedRemoteUsersCount: 0,
|
|
} as Hashtag);
|
|
}
|
|
}
|
|
|
|
if (!isUserAttached) {
|
|
hashtagChart.update(tag, user);
|
|
}
|
|
}
|