2023-09-28 20:08:17 +02:00
|
|
|
import { User } from "@/models/entities/user.js";
|
2023-09-13 23:16:31 +02:00
|
|
|
import config from "@/config/index.js";
|
2023-09-15 17:01:15 +02:00
|
|
|
import { DriveFiles, UserProfiles, Users } from "@/models/index.js";
|
2023-09-13 23:16:31 +02:00
|
|
|
import { EmojiConverter } from "@/server/api/mastodon/converters/emoji.js";
|
|
|
|
import { populateEmojis } from "@/misc/populate-emojis.js";
|
|
|
|
import { escapeMFM } from "@/server/api/mastodon/converters/mfm.js";
|
|
|
|
import mfm from "mfm-js";
|
2023-09-15 15:50:24 +02:00
|
|
|
import { awaitAll } from "@/prelude/await-all.js";
|
2023-09-18 22:19:33 +02:00
|
|
|
import { AccountCache, UserHelpers } from "@/server/api/mastodon/helpers/user.js";
|
2023-10-01 17:27:00 +02:00
|
|
|
import { MfmHelpers } from "@/server/api/mastodon/helpers/mfm.js";
|
2023-09-13 23:16:31 +02:00
|
|
|
|
|
|
|
type Field = {
|
2023-10-01 17:52:59 +02:00
|
|
|
name: string;
|
|
|
|
value: string;
|
|
|
|
verified?: boolean;
|
2023-09-13 23:16:31 +02:00
|
|
|
};
|
|
|
|
|
2023-09-15 12:14:36 +02:00
|
|
|
export class UserConverter {
|
2023-10-01 17:52:59 +02:00
|
|
|
public static async encode(u: User, cache: AccountCache = UserHelpers.getFreshAccountCache()): Promise<MastodonEntity.Account> {
|
|
|
|
return cache.locks.acquire(u.id, async () => {
|
|
|
|
const cacheHit = cache.accounts.find(p => p.id == u.id);
|
|
|
|
if (cacheHit) return cacheHit;
|
2023-09-18 22:19:33 +02:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
let acct = u.username;
|
|
|
|
let acctUrl = `https://${u.host || config.host}/@${u.username}`;
|
|
|
|
if (u.host) {
|
|
|
|
acct = `${u.username}@${u.host}`;
|
|
|
|
acctUrl = `https://${u.host}/@${u.username}`;
|
|
|
|
}
|
2023-10-06 03:12:52 +02:00
|
|
|
const profile = UserProfiles.findOneBy({ userId: u.id });
|
2023-10-01 17:52:59 +02:00
|
|
|
const bio = profile.then(profile => MfmHelpers.toHtml(mfm.parse(profile?.description ?? "")) ?? escapeMFM(profile?.description ?? ""));
|
|
|
|
const avatar = u.avatarId
|
2023-10-06 03:12:52 +02:00
|
|
|
? (DriveFiles.findOneBy({ id: u.avatarId }))
|
2023-10-01 17:52:59 +02:00
|
|
|
.then(p => p?.url ?? Users.getIdenticonUrl(u.id))
|
|
|
|
: Users.getIdenticonUrl(u.id);
|
|
|
|
const banner = u.bannerId
|
2023-10-06 03:12:52 +02:00
|
|
|
? (DriveFiles.findOneBy({ id: u.bannerId }))
|
2023-10-01 17:52:59 +02:00
|
|
|
.then(p => p?.url ?? `${config.url}/static-assets/transparent.png`)
|
|
|
|
: `${config.url}/static-assets/transparent.png`;
|
2023-10-04 23:06:17 +02:00
|
|
|
const followersCount = profile.then(profile => {
|
|
|
|
if (profile === null) return u.followersCount;
|
|
|
|
switch (profile.ffVisibility) {
|
|
|
|
case "public":
|
|
|
|
return u.followersCount;
|
|
|
|
case "followers":
|
|
|
|
// FIXME: check following relationship
|
|
|
|
return 0;
|
|
|
|
case "private":
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const followingCount = profile.then(profile => {
|
|
|
|
if (profile === null) return u.followingCount;
|
|
|
|
switch (profile.ffVisibility) {
|
|
|
|
case "public":
|
|
|
|
return u.followingCount;
|
|
|
|
case "followers":
|
|
|
|
// FIXME: check following relationship
|
|
|
|
return 0;
|
|
|
|
case "private":
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
2023-09-13 23:16:31 +02:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
return awaitAll({
|
|
|
|
id: u.id,
|
|
|
|
username: u.username,
|
|
|
|
acct: acct,
|
|
|
|
display_name: u.name || u.username,
|
|
|
|
locked: u.isLocked,
|
|
|
|
created_at: u.createdAt.toISOString(),
|
2023-10-04 23:06:17 +02:00
|
|
|
followers_count: followersCount,
|
|
|
|
following_count: followingCount,
|
2023-10-01 17:52:59 +02:00
|
|
|
statuses_count: u.notesCount,
|
|
|
|
note: bio,
|
|
|
|
url: u.uri ?? acctUrl,
|
|
|
|
avatar: avatar,
|
|
|
|
avatar_static: avatar,
|
|
|
|
header: banner,
|
|
|
|
header_static: banner,
|
|
|
|
emojis: populateEmojis(u.emojis, u.host).then(emoji => emoji.map((e) => EmojiConverter.encode(e))),
|
|
|
|
moved: null, //FIXME
|
|
|
|
fields: profile.then(profile => profile?.fields.map(p => this.encodeField(p)) ?? []),
|
|
|
|
bot: u.isBot,
|
|
|
|
discoverable: u.isExplorable
|
|
|
|
}).then(p => {
|
|
|
|
cache.accounts.push(p);
|
|
|
|
return p;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2023-09-13 23:16:31 +02:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
public static async encodeMany(users: User[], cache: AccountCache = UserHelpers.getFreshAccountCache()): Promise<MastodonEntity.Account[]> {
|
|
|
|
const encoded = users.map(u => this.encode(u, cache));
|
|
|
|
return Promise.all(encoded);
|
|
|
|
}
|
2023-09-24 17:50:28 +02:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
private static encodeField(f: Field): MastodonEntity.Field {
|
|
|
|
return {
|
|
|
|
name: f.name,
|
|
|
|
value: MfmHelpers.toHtml(mfm.parse(f.value)) ?? escapeMFM(f.value),
|
|
|
|
verified_at: f.verified ? (new Date()).toISOString() : null,
|
|
|
|
}
|
|
|
|
}
|
2023-09-13 23:16:31 +02:00
|
|
|
}
|