2022-03-08 21:25:30 -08:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList, Record } from 'immutable';
|
2022-02-27 18:21:39 -08:00
|
|
|
|
|
|
|
import { mergeDefined } from 'soapbox/utils/normalizers';
|
|
|
|
|
2022-03-09 10:16:24 -08:00
|
|
|
interface Account {
|
|
|
|
acct: string;
|
|
|
|
avatar: string;
|
|
|
|
avatar_static: string;
|
|
|
|
birthday: Date | undefined;
|
|
|
|
bot: boolean;
|
|
|
|
created_at: Date;
|
|
|
|
display_name: string;
|
|
|
|
emojis: ImmutableList<any>;
|
|
|
|
fields: ImmutableList<any>;
|
|
|
|
followers_count: number;
|
|
|
|
following_count: number;
|
|
|
|
fqn: string;
|
|
|
|
header: string;
|
|
|
|
header_static: string;
|
|
|
|
id: string;
|
|
|
|
last_status_at: Date;
|
|
|
|
location: string;
|
|
|
|
locked: boolean;
|
|
|
|
moved: null;
|
|
|
|
note: string;
|
|
|
|
pleroma: ImmutableMap<any, any>;
|
|
|
|
source: ImmutableMap<any, any>;
|
|
|
|
statuses_count: number;
|
|
|
|
uri: string;
|
|
|
|
url: string;
|
|
|
|
username: string;
|
|
|
|
verified: boolean;
|
|
|
|
|
|
|
|
// Internal fields
|
|
|
|
display_name_html: string;
|
|
|
|
note_emojified: string;
|
|
|
|
note_plain: string;
|
|
|
|
patron: ImmutableMap<any, any>;
|
|
|
|
relationship: ImmutableList<any>;
|
|
|
|
should_refetch: boolean;
|
|
|
|
}
|
|
|
|
|
2022-03-08 21:25:30 -08:00
|
|
|
const AccountRecord = Record({
|
|
|
|
acct: '',
|
|
|
|
avatar: '',
|
|
|
|
avatar_static: '',
|
|
|
|
birthday: undefined,
|
|
|
|
bot: false,
|
|
|
|
created_at: new Date(),
|
|
|
|
display_name: '',
|
|
|
|
emojis: ImmutableList(),
|
|
|
|
fields: ImmutableList(),
|
|
|
|
followers_count: 0,
|
|
|
|
following_count: 0,
|
|
|
|
fqn: '',
|
|
|
|
header: '',
|
|
|
|
header_static: '',
|
|
|
|
id: '',
|
|
|
|
last_status_at: new Date(),
|
|
|
|
location: '',
|
|
|
|
locked: false,
|
|
|
|
moved: null,
|
|
|
|
note: '',
|
|
|
|
pleroma: ImmutableMap(),
|
|
|
|
source: ImmutableMap(),
|
|
|
|
statuses_count: 0,
|
|
|
|
uri: '',
|
|
|
|
url: '',
|
|
|
|
username: '',
|
|
|
|
verified: false,
|
|
|
|
|
|
|
|
// Internal fields
|
|
|
|
display_name_html: '',
|
|
|
|
note_emojified: '',
|
|
|
|
note_plain: '',
|
2022-03-08 22:38:28 -08:00
|
|
|
patron: ImmutableMap(),
|
|
|
|
relationship: ImmutableList(),
|
2022-03-08 21:25:30 -08:00
|
|
|
should_refetch: false,
|
|
|
|
});
|
|
|
|
|
2022-02-27 18:21:39 -08:00
|
|
|
// https://gitlab.com/soapbox-pub/soapbox-fe/-/issues/549
|
2022-03-09 10:16:24 -08:00
|
|
|
const normalizePleromaLegacyFields = (account: ImmutableMap<string, any>) => {
|
|
|
|
return account.update('pleroma', ImmutableMap(), (pleroma: ImmutableMap<string, any>) => {
|
2022-02-27 18:21:39 -08:00
|
|
|
return pleroma.withMutations(pleroma => {
|
|
|
|
const legacy = ImmutableMap({
|
|
|
|
is_active: !pleroma.get('deactivated'),
|
|
|
|
is_confirmed: !pleroma.get('confirmation_pending'),
|
|
|
|
is_approved: !pleroma.get('approval_pending'),
|
|
|
|
});
|
|
|
|
|
|
|
|
pleroma.mergeWith(mergeDefined, legacy);
|
|
|
|
pleroma.deleteAll(['deactivated', 'confirmation_pending', 'approval_pending']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Normalize Pleroma/Fedibird birthday
|
2022-03-09 10:16:24 -08:00
|
|
|
const normalizeBirthday = (account: ImmutableMap<string, any>) => {
|
2022-02-27 12:42:42 -08:00
|
|
|
const birthday = [
|
|
|
|
account.getIn(['pleroma', 'birthday']),
|
|
|
|
account.getIn(['other_settings', 'birthday']),
|
|
|
|
].find(Boolean);
|
|
|
|
|
|
|
|
return account.set('birthday', birthday);
|
|
|
|
};
|
2022-02-27 18:21:39 -08:00
|
|
|
|
2022-03-09 10:16:24 -08:00
|
|
|
// Get Pleroma tags
|
|
|
|
const getTags = (account: ImmutableMap<string, any>): ImmutableList<any> => {
|
|
|
|
const tags = account.getIn(['pleroma', 'tags']);
|
|
|
|
return ImmutableList(ImmutableList.isList(tags) ? tags : []);
|
|
|
|
};
|
|
|
|
|
2022-02-27 20:22:08 -08:00
|
|
|
// Normalize Truth Social/Pleroma verified
|
2022-03-09 10:16:24 -08:00
|
|
|
const normalizeVerified = (account: ImmutableMap<string, any>) => {
|
2022-02-27 20:22:08 -08:00
|
|
|
return account.update('verified', verified => {
|
|
|
|
return [
|
|
|
|
verified === true,
|
2022-03-09 10:16:24 -08:00
|
|
|
getTags(account).includes('verified'),
|
2022-02-27 20:22:08 -08:00
|
|
|
].some(Boolean);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-02-28 15:25:20 -08:00
|
|
|
// Normalize Fedibird/Truth Social location
|
2022-03-09 10:16:24 -08:00
|
|
|
const normalizeLocation = (account: ImmutableMap<string, any>) => {
|
2022-02-28 15:25:20 -08:00
|
|
|
return account.update('location', location => {
|
|
|
|
return [
|
|
|
|
location,
|
|
|
|
account.getIn(['other_settings', 'location']),
|
|
|
|
].find(Boolean);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-03-09 10:16:24 -08:00
|
|
|
export const normalizeAccount = (account: ImmutableMap<string, any>): Account => {
|
2022-03-08 21:25:30 -08:00
|
|
|
return AccountRecord(
|
|
|
|
account.withMutations(account => {
|
|
|
|
normalizePleromaLegacyFields(account);
|
|
|
|
normalizeVerified(account);
|
|
|
|
normalizeBirthday(account);
|
|
|
|
normalizeLocation(account);
|
|
|
|
}),
|
|
|
|
);
|
2022-02-27 18:21:39 -08:00
|
|
|
};
|