normalizeAccount(): normalize Pleroma legacy fields

merge-requests/1061/head
Alex Gleason 2022-02-27 20:21:39 -06:00
rodzic 2eefdbe235
commit 19ac4a54c2
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
8 zmienionych plików z 108 dodań i 33 usunięć

Wyświetl plik

@ -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"
}

Wyświetl plik

@ -16,4 +16,24 @@ describe('normalizeAccount()', () => {
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);
});
});

Wyświetl plik

@ -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 = [
account.getIn(['pleroma', 'birthday']),
account.getIn(['other_settings', 'birthday']),
@ -6,3 +27,10 @@ export const normalizeAccount = account => {
return account.set('birthday', birthday);
};
export const normalizeAccount = account => {
return account.withMutations(account => {
normalizePleromaLegacyFields(account);
normalizeBirthday(account);
});
};

Wyświetl plik

@ -1,6 +1,7 @@
import { Map as ImmutableMap } from 'immutable';
import { parseVersion, PLEROMA } from 'soapbox/utils/features';
import { mergeDefined } from 'soapbox/utils/normalizers';
import { isNumber } from 'soapbox/utils/numbers';
// 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
const getAttachmentLimit = software => software === PLEROMA ? Infinity : 4;

Wyświetl plik

@ -1,6 +1,7 @@
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
import { accountToMention } from 'soapbox/utils/accounts';
import { mergeDefined } from 'soapbox/utils/normalizers';
// Some backends can return null, or omit these required fields
const baseStatus = ImmutableMap({
@ -40,9 +41,6 @@ const basePoll = ImmutableMap({
votes_count: 0,
});
// Merger function for only overriding undefined values
const mergeDefined = (oldVal, newVal) => oldVal === undefined ? newVal : oldVal;
// Merge base status
const mergeBase = status => {
return status.mergeDeepWith(mergeDefined, baseStatus);

Wyświetl plik

@ -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 { STREAMING_CHAT_UPDATE } from 'soapbox/actions/streaming';
import { normalizeAccount } from 'soapbox/normalizers/account';
import { normalizePleromaUserFields } from 'soapbox/utils/pleroma';
import {
ACCOUNT_IMPORT,
@ -41,24 +40,18 @@ import {
const initialState = ImmutableMap();
const normalizePleroma = account => {
if (!account.pleroma) return account;
account.pleroma = normalizePleromaUserFields(account.pleroma);
delete account.pleroma.chat_token;
return account;
const minifyAccount = account => {
return account.deleteAll([
'followers_count',
'following_count',
'statuses_count',
'source',
]);
};
const fixAccount = (state, account) => {
const normalized = fromJS(normalizePleroma(account)).withMutations(account => {
account.deleteAll([
'followers_count',
'following_count',
'statuses_count',
'source',
]);
});
return state.set(account.id, normalizeAccount(normalized));
const normalized = minifyAccount(normalizeAccount(fromJS(account)));
return state.set(account.id, normalized);
};
const normalizeAccounts = (state, accounts) => {

Wyświetl plik

@ -0,0 +1,2 @@
// Use new value only if old value is undefined
export const mergeDefined = (oldVal, newVal) => oldVal === undefined ? newVal : oldVal;

Wyświetl plik

@ -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;
};