Parse Mentions as Immutable.Record

next-old
Alex Gleason 2022-03-10 17:57:12 -06:00
rodzic 9afd43a42d
commit 8decaa2d9f
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
5 zmienionych plików z 31 dodań i 43 usunięć

Wyświetl plik

@ -32,30 +32,34 @@ describe('normalizeStatus', () => {
it('adds mention to self in self-reply on Mastodon', () => {
const status = fromJS(require('soapbox/__fixtures__/mastodon-reply-to-self.json'));
const expected = fromJS([{
const expected = {
id: '106801667066418367',
username: 'benis911',
acct: 'benis911',
url: 'https://mastodon.social/@benis911',
}]);
};
const result = normalizeStatus(status).get('mentions');
const result = normalizeStatus(status).mentions;
expect(result).toEqual(expected);
expect(result.size).toBe(1);
expect(result.get(0).toJS()).toMatchObject(expected);
expect(result.get(0).id).toEqual('106801667066418367');
expect(ImmutableRecord.isRecord(result.get(0))).toBe(true);
});
it('normalizes mentions with only acct', () => {
const status = fromJS({ mentions: [{ acct: 'alex@gleasonator.com' }] });
const expected = fromJS([{
const expected = [{
id: '',
acct: 'alex@gleasonator.com',
username: 'alex',
url: '',
}]);
}];
const result = normalizeStatus(status).get('mentions');
expect(result).toEqual(expected);
expect(result.toJS()).toEqual(expected);
});
it('normalizes Mitra attachments', () => {

Wyświetl plik

@ -97,6 +97,13 @@ const normalizeLocation = (account: ImmutableMap<string, any>) => {
});
};
// Set username from acct, if applicable
const fixUsername = (account: ImmutableMap<string, any>) => {
return account.update('username', username => (
username || (account.get('acct') || '').split('@')[0]
));
};
export const normalizeAccount = (account: ImmutableMap<string, any>): IAccount => {
return AccountRecord(
account.withMutations(account => {
@ -104,6 +111,7 @@ export const normalizeAccount = (account: ImmutableMap<string, any>): IAccount =
normalizeVerified(account);
normalizeBirthday(account);
normalizeLocation(account);
fixUsername(account);
}),
);
};

Wyświetl plik

@ -4,8 +4,8 @@ import {
Record as ImmutableRecord,
} from 'immutable';
import { normalizeAccount } from 'soapbox/normalizers/account';
import { IStatus } from 'soapbox/types';
import { accountToMention } from 'soapbox/utils/accounts';
import { mergeDefined } from 'soapbox/utils/normalizers';
const StatusRecord = ImmutableRecord({
@ -79,6 +79,14 @@ const AttachmentRecord = ImmutableRecord({
url: '',
});
// https://docs.joinmastodon.org/entities/mention/
const MentionRecord = ImmutableRecord({
id: '',
acct: '',
username: '',
url: '',
});
// Ensure attachments have required fields
// https://docs.joinmastodon.org/entities/attachment/
const normalizeAttachment = (attachment: ImmutableMap<string, any>) => {
@ -104,13 +112,7 @@ const normalizeAttachments = (status: ImmutableMap<string, any>) => {
// Normalize mentions
const normalizeMention = (mention: ImmutableMap<string, any>) => {
const base = ImmutableMap({
acct: '',
username: (mention.get('acct') || '').split('@')[0],
url: '',
});
return mention.mergeWith(mergeDefined, base);
return MentionRecord(normalizeAccount(mention));
};
const normalizeMentions = (status: ImmutableMap<string, any>) => {
@ -184,8 +186,8 @@ const addSelfMention = (status: ImmutableMap<string, any>) => {
const isSelfReply = accountId === status.get('in_reply_to_account_id');
const hasSelfMention = accountId === status.getIn(['mentions', 0, 'id']);
if (isSelfReply && !hasSelfMention) {
const mention = accountToMention(status.get('account'));
if (isSelfReply && !hasSelfMention && accountId) {
const mention = normalizeMention(status.get('account'));
return status.update('mentions', ImmutableList(), mentions => (
ImmutableList([mention]).concat(mentions)
));

Wyświetl plik

@ -6,7 +6,6 @@ import {
isStaff,
isAdmin,
isModerator,
accountToMention,
} from '../accounts';
describe('getDomain', () => {
@ -116,19 +115,3 @@ describe('isModerator', () => {
});
});
});
describe('accountToMention', () => {
it('converts the account to a mention', () => {
const account = fromJS(require('soapbox/__fixtures__/pleroma-account.json'));
const expected = fromJS({
id: '9v5bmRalQvjOy0ECcC',
username: 'alex',
acct: 'alex',
url: 'https://gleasonator.com/users/alex',
});
const result = accountToMention(account);
expect(result).toEqual(expected);
});
});

Wyświetl plik

@ -62,12 +62,3 @@ export const isLocal = (account: ImmutableMap<string, any>): boolean => {
};
export const isRemote = (account: ImmutableMap<string, any>): boolean => !isLocal(account);
export const accountToMention = (account: ImmutableMap<string, any>): ImmutableMap<string, any> => {
return ImmutableMap({
id: account.get('id'),
username: account.get('username'),
acct: account.get('acct'),
url: account.get('url'),
});
};