normalizeAccount(): normalize Pleroma legacy fields
This commit is contained in:
parent
2eefdbe235
commit
19ac4a54c2
|
@ -0,0 +1,46 @@
|
||||||
|
{
|
||||||
|
"acct": "alex",
|
||||||
|
"avatar": "https://freespeechextremist.com/images/avi.png",
|
||||||
|
"avatar_static": "https://freespeechextremist.com/images/avi.png",
|
||||||
|
"bot": false,
|
||||||
|
"created_at": "2022-02-28T01:55:05.000Z",
|
||||||
|
"display_name": "Alex Gleason",
|
||||||
|
"emojis": [],
|
||||||
|
"fields": [],
|
||||||
|
"followers_count": 0,
|
||||||
|
"following_count": 0,
|
||||||
|
"header": "https://freespeechextremist.com/images/banner.png",
|
||||||
|
"header_static": "https://freespeechextremist.com/images/banner.png",
|
||||||
|
"id": "AGv8wCadU7DqWgMqNk",
|
||||||
|
"locked": false,
|
||||||
|
"note": "I'm testing out compatibility with an older Pleroma version",
|
||||||
|
"pleroma": {
|
||||||
|
"accepts_chat_messages": true,
|
||||||
|
"ap_id": "https://freespeechextremist.com/users/alex",
|
||||||
|
"background_image": null,
|
||||||
|
"confirmation_pending": false,
|
||||||
|
"favicon": null,
|
||||||
|
"hide_favorites": true,
|
||||||
|
"hide_followers": false,
|
||||||
|
"hide_followers_count": false,
|
||||||
|
"hide_follows": false,
|
||||||
|
"hide_follows_count": false,
|
||||||
|
"is_admin": false,
|
||||||
|
"is_moderator": false,
|
||||||
|
"relationship": {},
|
||||||
|
"skip_thread_containment": false,
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"source": {
|
||||||
|
"fields": [],
|
||||||
|
"note": "I'm testing out compatibility with an older Pleroma version",
|
||||||
|
"pleroma": {
|
||||||
|
"actor_type": "Person",
|
||||||
|
"discoverable": true
|
||||||
|
},
|
||||||
|
"sensitive": false
|
||||||
|
},
|
||||||
|
"statuses_count": 0,
|
||||||
|
"url": "https://freespeechextremist.com/users/alex",
|
||||||
|
"username": "alex"
|
||||||
|
}
|
|
@ -16,4 +16,24 @@ describe('normalizeAccount()', () => {
|
||||||
|
|
||||||
expect(result.get('birthday')).toEqual('1993-07-03');
|
expect(result.get('birthday')).toEqual('1993-07-03');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('normalizes Pleroma legacy fields', () => {
|
||||||
|
const account = fromJS(require('soapbox/__fixtures__/pleroma-2.2.2-account.json'));
|
||||||
|
const result = normalizeAccount(account);
|
||||||
|
|
||||||
|
expect(result.getIn(['pleroma', 'is_active'])).toBe(true);
|
||||||
|
expect(result.getIn(['pleroma', 'is_confirmed'])).toBe(true);
|
||||||
|
expect(result.getIn(['pleroma', 'is_approved'])).toBe(true);
|
||||||
|
|
||||||
|
expect(result.hasIn(['pleroma', 'confirmation_pending'])).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('prefers new Pleroma fields', () => {
|
||||||
|
const account = fromJS(require('soapbox/__fixtures__/pleroma-account.json'));
|
||||||
|
const result = normalizeAccount(account);
|
||||||
|
|
||||||
|
expect(result.getIn(['pleroma', 'is_active'])).toBe(true);
|
||||||
|
expect(result.getIn(['pleroma', 'is_confirmed'])).toBe(true);
|
||||||
|
expect(result.getIn(['pleroma', 'is_approved'])).toBe(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,4 +1,25 @@
|
||||||
export const normalizeAccount = account => {
|
import { Map as ImmutableMap } from 'immutable';
|
||||||
|
|
||||||
|
import { mergeDefined } from 'soapbox/utils/normalizers';
|
||||||
|
|
||||||
|
// https://gitlab.com/soapbox-pub/soapbox-fe/-/issues/549
|
||||||
|
const normalizePleromaLegacyFields = account => {
|
||||||
|
return account.update('pleroma', ImmutableMap(), pleroma => {
|
||||||
|
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
|
||||||
|
const normalizeBirthday = account => {
|
||||||
const birthday = [
|
const birthday = [
|
||||||
account.getIn(['pleroma', 'birthday']),
|
account.getIn(['pleroma', 'birthday']),
|
||||||
account.getIn(['other_settings', 'birthday']),
|
account.getIn(['other_settings', 'birthday']),
|
||||||
|
@ -6,3 +27,10 @@ export const normalizeAccount = account => {
|
||||||
|
|
||||||
return account.set('birthday', birthday);
|
return account.set('birthday', birthday);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const normalizeAccount = account => {
|
||||||
|
return account.withMutations(account => {
|
||||||
|
normalizePleromaLegacyFields(account);
|
||||||
|
normalizeBirthday(account);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { Map as ImmutableMap } from 'immutable';
|
import { Map as ImmutableMap } from 'immutable';
|
||||||
|
|
||||||
import { parseVersion, PLEROMA } from 'soapbox/utils/features';
|
import { parseVersion, PLEROMA } from 'soapbox/utils/features';
|
||||||
|
import { mergeDefined } from 'soapbox/utils/normalizers';
|
||||||
import { isNumber } from 'soapbox/utils/numbers';
|
import { isNumber } from 'soapbox/utils/numbers';
|
||||||
|
|
||||||
// Use Mastodon defaults
|
// Use Mastodon defaults
|
||||||
|
@ -36,9 +37,6 @@ const pleromaToMastodonConfig = instance => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Use new value only if old value is undefined
|
|
||||||
const mergeDefined = (oldVal, newVal) => oldVal === undefined ? newVal : oldVal;
|
|
||||||
|
|
||||||
// Get the software's default attachment limit
|
// Get the software's default attachment limit
|
||||||
const getAttachmentLimit = software => software === PLEROMA ? Infinity : 4;
|
const getAttachmentLimit = software => software === PLEROMA ? Infinity : 4;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
||||||
|
|
||||||
import { accountToMention } from 'soapbox/utils/accounts';
|
import { accountToMention } from 'soapbox/utils/accounts';
|
||||||
|
import { mergeDefined } from 'soapbox/utils/normalizers';
|
||||||
|
|
||||||
// Some backends can return null, or omit these required fields
|
// Some backends can return null, or omit these required fields
|
||||||
const baseStatus = ImmutableMap({
|
const baseStatus = ImmutableMap({
|
||||||
|
@ -40,9 +41,6 @@ const basePoll = ImmutableMap({
|
||||||
votes_count: 0,
|
votes_count: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Merger function for only overriding undefined values
|
|
||||||
const mergeDefined = (oldVal, newVal) => oldVal === undefined ? newVal : oldVal;
|
|
||||||
|
|
||||||
// Merge base status
|
// Merge base status
|
||||||
const mergeBase = status => {
|
const mergeBase = status => {
|
||||||
return status.mergeDeepWith(mergeDefined, baseStatus);
|
return status.mergeDeepWith(mergeDefined, baseStatus);
|
||||||
|
|
|
@ -31,7 +31,6 @@ import { CHATS_FETCH_SUCCESS, CHATS_EXPAND_SUCCESS, CHAT_FETCH_SUCCESS } from 's
|
||||||
import { normalizeAccount as normalizeAccount2 } from 'soapbox/actions/importer/normalizer';
|
import { normalizeAccount as normalizeAccount2 } from 'soapbox/actions/importer/normalizer';
|
||||||
import { STREAMING_CHAT_UPDATE } from 'soapbox/actions/streaming';
|
import { STREAMING_CHAT_UPDATE } from 'soapbox/actions/streaming';
|
||||||
import { normalizeAccount } from 'soapbox/normalizers/account';
|
import { normalizeAccount } from 'soapbox/normalizers/account';
|
||||||
import { normalizePleromaUserFields } from 'soapbox/utils/pleroma';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ACCOUNT_IMPORT,
|
ACCOUNT_IMPORT,
|
||||||
|
@ -41,24 +40,18 @@ import {
|
||||||
|
|
||||||
const initialState = ImmutableMap();
|
const initialState = ImmutableMap();
|
||||||
|
|
||||||
const normalizePleroma = account => {
|
const minifyAccount = account => {
|
||||||
if (!account.pleroma) return account;
|
return account.deleteAll([
|
||||||
account.pleroma = normalizePleromaUserFields(account.pleroma);
|
'followers_count',
|
||||||
delete account.pleroma.chat_token;
|
'following_count',
|
||||||
return account;
|
'statuses_count',
|
||||||
|
'source',
|
||||||
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
const fixAccount = (state, account) => {
|
const fixAccount = (state, account) => {
|
||||||
const normalized = fromJS(normalizePleroma(account)).withMutations(account => {
|
const normalized = minifyAccount(normalizeAccount(fromJS(account)));
|
||||||
account.deleteAll([
|
return state.set(account.id, normalized);
|
||||||
'followers_count',
|
|
||||||
'following_count',
|
|
||||||
'statuses_count',
|
|
||||||
'source',
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
|
|
||||||
return state.set(account.id, normalizeAccount(normalized));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const normalizeAccounts = (state, accounts) => {
|
const normalizeAccounts = (state, accounts) => {
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
// Use new value only if old value is undefined
|
||||||
|
export const mergeDefined = (oldVal, newVal) => oldVal === undefined ? newVal : oldVal;
|
|
@ -1,10 +0,0 @@
|
||||||
// https://gitlab.com/soapbox-pub/soapbox-fe/-/issues/549
|
|
||||||
export const normalizePleromaUserFields = obj => {
|
|
||||||
obj.is_active = obj.is_active === undefined ? !obj.deactivated : obj.is_active;
|
|
||||||
obj.is_confirmed = obj.is_confirmed === undefined ? !obj.confirmation_pending : obj.is_confirmed;
|
|
||||||
obj.is_approved = obj.is_approved === undefined ? !obj.approval_pending : obj.is_approved;
|
|
||||||
delete obj.deactivated;
|
|
||||||
delete obj.confirmation_pending;
|
|
||||||
delete obj.approval_pending;
|
|
||||||
return obj;
|
|
||||||
};
|
|
Loading…
Reference in New Issue