Skip to content

Commit 3974047

Browse files
committed
chore: add author and source_id to reference tweet
1 parent c6d9a7c commit 3974047

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

__tests__/workers/postUpdated.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,15 +659,15 @@ it('should create referenced social post for quote and set sharedPostId', async
659659
expect(post.type).toEqual(PostType.SocialTwitter);
660660
expect(post.subType).toEqual('quote');
661661
expect(post.sharedPostId).toBeTruthy();
662-
expect(post.showOnFeed).toEqual(false);
663-
expect(post.private).toEqual(true);
664662

665663
const referencedPost = await con.getRepository(Post).findOneByOrFail({
666664
id: post.sharedPostId,
667665
});
668666
expect(referencedPost.type).toEqual(PostType.SocialTwitter);
669667
expect(referencedPost.subType).toEqual('tweet');
670668
expect(referencedPost.title).toEqual('Referenced quoted tweet');
669+
expect(referencedPost.showOnFeed).toEqual(false);
670+
expect(referencedPost.private).toEqual(true);
671671
});
672672

673673
it('should reuse existing referenced social post for quote when status id already exists', async () => {
@@ -755,15 +755,15 @@ it('should create referenced social post for repost and set sharedPostId', async
755755
expect(post.type).toEqual(PostType.SocialTwitter);
756756
expect(post.subType).toEqual('repost');
757757
expect(post.sharedPostId).toBeTruthy();
758-
expect(post.showOnFeed).toEqual(false);
759-
expect(post.private).toEqual(true);
760758

761759
const referencedPost = await con.getRepository(Post).findOneByOrFail({
762760
id: post.sharedPostId,
763761
});
764762
expect(referencedPost.type).toEqual(PostType.SocialTwitter);
765763
expect(referencedPost.subType).toEqual('tweet');
766764
expect(referencedPost.title).toEqual('Referenced repost tweet');
765+
expect(referencedPost.showOnFeed).toEqual(false);
766+
expect(referencedPost.private).toEqual(true);
767767
});
768768

769769
it('should map social twitter tweet subtype as plain tweet', async () => {

src/common/schema/socialTwitter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export const twitterSocialReferenceSchema = z
4444
content_html: z.string().nullish(),
4545
sub_type: z.string().nullish(),
4646
media: z.array(twitterSocialMediaSchema).nullish(),
47+
source_id: z.string().nullish(),
48+
author_username: z.string().nullish(),
4749
})
4850
.passthrough();
4951

src/common/twitterSocial.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ValidationError } from 'apollo-server-errors';
22
import type { EntityManager } from 'typeorm';
33
import { generateShortId } from '../ids';
4-
import { UNKNOWN_SOURCE } from '../entity/Source';
4+
import { Source, SourceType, UNKNOWN_SOURCE } from '../entity/Source';
55
import { Post, PostOrigin, PostType } from '../entity/posts/Post';
66
import { SocialTwitterPost } from '../entity/posts/SocialTwitterPost';
77
import { markdown } from './markdown';
@@ -30,6 +30,7 @@ export interface TwitterReferencePost {
3030
contentHtml?: string | null;
3131
image?: string | null;
3232
videoId?: string | null;
33+
authorUsername?: string | null;
3334
}
3435

3536
export interface TwitterMappingResult {
@@ -41,9 +42,7 @@ export interface TwitterMappingResult {
4142
export interface TwitterReferenceUpsertParams {
4243
entityManager: EntityManager;
4344
reference: TwitterReferencePost;
44-
sourceId?: string | null;
4545
language?: string | null;
46-
isPrivate?: boolean;
4746
}
4847

4948
const getStringOrUndefined = (value?: string | null): string | undefined => {
@@ -270,6 +269,7 @@ const extractTwitterReference = (
270269
contentHtml: getStringOrUndefined(reference.content_html),
271270
image: pickPrimaryImage(reference.media || []),
272271
videoId: pickPrimaryVideoId(reference.media || []),
272+
authorUsername: getStringOrUndefined(reference.author_username),
273273
};
274274
};
275275

@@ -374,12 +374,38 @@ export const mapTwitterSocialPayload = ({
374374
};
375375
};
376376

377+
const resolveSourceByTwitterUsername = async ({
378+
entityManager,
379+
authorUsername,
380+
}: {
381+
entityManager: EntityManager;
382+
authorUsername?: string | null;
383+
}): Promise<{ id: string; isPrivate: boolean } | undefined> => {
384+
if (!authorUsername) {
385+
return undefined;
386+
}
387+
388+
const matchedSource = await entityManager
389+
.getRepository(Source)
390+
.createQueryBuilder('source')
391+
.select(['source.id', 'source.private'])
392+
.where('source.type = :type', { type: SourceType.Machine })
393+
.andWhere('LOWER(source.twitter) = :twitter', {
394+
twitter: authorUsername.toLowerCase(),
395+
})
396+
.getOne();
397+
398+
if (!matchedSource) {
399+
return undefined;
400+
}
401+
402+
return { id: matchedSource.id, isPrivate: matchedSource.private };
403+
};
404+
377405
export const upsertTwitterReferencedPost = async ({
378406
entityManager,
379407
reference,
380-
sourceId,
381408
language,
382-
isPrivate = false,
383409
}: TwitterReferenceUpsertParams): Promise<string | undefined> => {
384410
const referenceUrl = getStringOrUndefined(reference.url);
385411
if (!referenceUrl) {
@@ -425,7 +451,12 @@ export const upsertTwitterReferencedPost = async ({
425451
getStringOrUndefined(reference.contentHtml) ||
426452
(content ? markdown.render(content) : undefined);
427453
const visible = !!(title || content);
428-
const referenceSourceId = sourceId || UNKNOWN_SOURCE;
454+
const resolvedSource = await resolveSourceByTwitterUsername({
455+
entityManager,
456+
authorUsername: reference.authorUsername,
457+
});
458+
const referenceSourceId = resolvedSource?.id || UNKNOWN_SOURCE;
459+
const isPrivate = resolvedSource?.isPrivate ?? true;
429460

430461
const repository = entityManager.getRepository(SocialTwitterPost);
431462

src/workers/postUpdated.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -831,20 +831,8 @@ const worker: Worker = {
831831
fixedData.sharedPostId = await upsertTwitterReferencedPost({
832832
entityManager,
833833
reference: twitterReference,
834-
sourceId: fixedData.sourceId,
835834
language: fixedData.language,
836-
isPrivate: fixedData.private,
837835
});
838-
839-
if (fixedData.sharedPostId) {
840-
fixedData.showOnFeed = false;
841-
fixedData.private = true;
842-
fixedData.flags = {
843-
...fixedData.flags,
844-
showOnFeed: false,
845-
private: true,
846-
};
847-
}
848836
}
849837

850838
// See if post id is not available

0 commit comments

Comments
 (0)