kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
Create normalizers/status module
rodzic
9753067ccc
commit
d16b764de7
|
@ -0,0 +1,59 @@
|
|||
import { fromJS } from 'immutable';
|
||||
|
||||
import { normalizeStatus } from '../status';
|
||||
|
||||
describe('normalizeStatus', () => {
|
||||
it('fixes the order of mentions', () => {
|
||||
const status = fromJS(require('soapbox/__fixtures__/status-unordered-mentions.json'));
|
||||
|
||||
const expected = ['NEETzsche', 'alex', 'Lumeinshin', 'sneeden'];
|
||||
|
||||
const result = normalizeStatus(status)
|
||||
.get('mentions')
|
||||
.map(mention => mention.get('username'))
|
||||
.toJS();
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
|
||||
it('normalizes Mitra attachments', () => {
|
||||
const status = fromJS(require('soapbox/__fixtures__/mitra-status-with-attachments.json'));
|
||||
|
||||
const expected = fromJS([{
|
||||
id: '017eeb0e-e5df-30a4-77a7-a929145cb836',
|
||||
type: 'image',
|
||||
url: 'https://mitra.social/media/8e04e6091bbbac79641b5812508683ce72c38693661c18d16040553f2371e18d.png',
|
||||
preview_url: 'https://mitra.social/media/8e04e6091bbbac79641b5812508683ce72c38693661c18d16040553f2371e18d.png',
|
||||
remote_url: 'https://mitra.social/media/8e04e6091bbbac79641b5812508683ce72c38693661c18d16040553f2371e18d.png',
|
||||
}, {
|
||||
id: '017eeb0e-e5e4-2a48-2889-afdebf368a54',
|
||||
type: 'unknown',
|
||||
url: 'https://mitra.social/media/8f72dc2e98572eb4ba7c3a902bca5f69c448fc4391837e5f8f0d4556280440ac',
|
||||
preview_url: 'https://mitra.social/media/8f72dc2e98572eb4ba7c3a902bca5f69c448fc4391837e5f8f0d4556280440ac',
|
||||
remote_url: 'https://mitra.social/media/8f72dc2e98572eb4ba7c3a902bca5f69c448fc4391837e5f8f0d4556280440ac',
|
||||
}, {
|
||||
id: '017eeb0e-e5e5-79fd-6054-8b6869b1db49',
|
||||
type: 'unknown',
|
||||
url: 'https://mitra.social/media/55a81a090247cc4fc127e5716bcf7964f6e0df9b584f85f4696c0b994747a4d0.oga',
|
||||
preview_url: 'https://mitra.social/media/55a81a090247cc4fc127e5716bcf7964f6e0df9b584f85f4696c0b994747a4d0.oga',
|
||||
remote_url: 'https://mitra.social/media/55a81a090247cc4fc127e5716bcf7964f6e0df9b584f85f4696c0b994747a4d0.oga',
|
||||
}, {
|
||||
id: '017eeb0e-e5e6-c416-a444-21e560c47839',
|
||||
type: 'unknown',
|
||||
url: 'https://mitra.social/media/0d96a4ff68ad6d4b6f1f30f713b18d5184912ba8dd389f86aa7710db079abcb0',
|
||||
preview_url: 'https://mitra.social/media/0d96a4ff68ad6d4b6f1f30f713b18d5184912ba8dd389f86aa7710db079abcb0',
|
||||
remote_url: 'https://mitra.social/media/0d96a4ff68ad6d4b6f1f30f713b18d5184912ba8dd389f86aa7710db079abcb0',
|
||||
}]);
|
||||
|
||||
const result = normalizeStatus(status);
|
||||
|
||||
expect(result.get('media_attachments')).toEqual(expected);
|
||||
});
|
||||
|
||||
it('leaves Pleroma attachments alone', () => {
|
||||
const status = fromJS(require('soapbox/__fixtures__/pleroma-status-with-attachments.json'));
|
||||
const result = normalizeStatus(status);
|
||||
|
||||
expect(status.get('media_attachments')).toEqual(result.get('media_attachments'));
|
||||
});
|
||||
});
|
|
@ -0,0 +1,49 @@
|
|||
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
||||
|
||||
// Ensure attachments have required fields
|
||||
// https://docs.joinmastodon.org/entities/attachment/
|
||||
const normalizeAttachment = attachment => {
|
||||
const url = [
|
||||
attachment.get('url'),
|
||||
attachment.get('preview_url'),
|
||||
attachment.get('remote_url'),
|
||||
].find(url => url) || '';
|
||||
|
||||
const base = ImmutableMap({
|
||||
url,
|
||||
preview_url: url,
|
||||
remote_url: url,
|
||||
});
|
||||
|
||||
return attachment.mergeWith((o, n) => o || n, base);
|
||||
};
|
||||
|
||||
const normalizeAttachments = status => {
|
||||
return status.update('media_attachments', ImmutableList(), attachments => {
|
||||
return attachments.map(normalizeAttachment);
|
||||
});
|
||||
};
|
||||
|
||||
// Fix order of mentions
|
||||
const fixMentions = status => {
|
||||
const mentions = status.get('mentions');
|
||||
const inReplyToAccountId = status.get('in_reply_to_account_id');
|
||||
|
||||
// Sort the replied-to mention to the top
|
||||
const sorted = mentions.sort((a, b) => {
|
||||
if (a.get('id') === inReplyToAccountId) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
|
||||
return status.set('mentions', sorted);
|
||||
};
|
||||
|
||||
export const normalizeStatus = status => {
|
||||
return status.withMutations(status => {
|
||||
fixMentions(status);
|
||||
normalizeAttachments(status);
|
||||
});
|
||||
};
|
|
@ -1,5 +1,6 @@
|
|||
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
|
||||
import { Map as ImmutableMap, fromJS } from 'immutable';
|
||||
|
||||
import { normalizeStatus } from 'soapbox/normalizers/status';
|
||||
import { simulateEmojiReact, simulateUnEmojiReact } from 'soapbox/utils/emoji_reacts';
|
||||
|
||||
import {
|
||||
|
@ -26,47 +27,6 @@ import {
|
|||
} from '../actions/statuses';
|
||||
import { TIMELINE_DELETE } from '../actions/timelines';
|
||||
|
||||
// Ensure attachments have required fields
|
||||
// https://docs.joinmastodon.org/entities/attachment/
|
||||
const normalizeAttachment = attachment => {
|
||||
const url = [
|
||||
attachment.get('url'),
|
||||
attachment.get('preview_url'),
|
||||
attachment.get('remote_url'),
|
||||
].find(url => url) || '';
|
||||
|
||||
const base = ImmutableMap({
|
||||
url,
|
||||
preview_url: url,
|
||||
remote_url: url,
|
||||
});
|
||||
|
||||
return attachment.mergeWith((o, n) => o || n, base);
|
||||
};
|
||||
|
||||
const normalizeAttachments = status => {
|
||||
return status.update('media_attachments', ImmutableList(), attachments => {
|
||||
return attachments.map(normalizeAttachment);
|
||||
});
|
||||
};
|
||||
|
||||
// Fix order of mentions
|
||||
const fixMentions = status => {
|
||||
const mentions = status.get('mentions');
|
||||
const inReplyToAccountId = status.get('in_reply_to_account_id');
|
||||
|
||||
// Sort the replied-to mention to the top
|
||||
const sorted = mentions.sort((a, b) => {
|
||||
if (a.get('id') === inReplyToAccountId) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
|
||||
return status.set('mentions', sorted);
|
||||
};
|
||||
|
||||
const isQuote = status => {
|
||||
return Boolean(status.get('quote_id') || status.getIn(['pleroma', 'quote_url']));
|
||||
};
|
||||
|
@ -84,15 +44,14 @@ const fixQuote = (state, status) => {
|
|||
}
|
||||
};
|
||||
|
||||
const normalizeStatus = (state, status) => {
|
||||
const fixStatus = (state, status) => {
|
||||
return status.withMutations(status => {
|
||||
fixMentions(status);
|
||||
normalizeStatus(status);
|
||||
fixQuote(state, status);
|
||||
normalizeAttachments(status);
|
||||
});
|
||||
};
|
||||
|
||||
const importStatus = (state, status) => state.set(status.id, normalizeStatus(state, fromJS(status)));
|
||||
const importStatus = (state, status) => state.set(status.id, fixStatus(state, fromJS(status)));
|
||||
|
||||
const importStatuses = (state, statuses) =>
|
||||
state.withMutations(mutable => statuses.forEach(status => importStatus(mutable, status)));
|
||||
|
|
Ładowanie…
Reference in New Issue