Skip to content

Commit

Permalink
short-links: Add support in backlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
clbn committed Jul 25, 2023
1 parent 4292ff8 commit e497690
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 8 deletions.
6 changes: 2 additions & 4 deletions app/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function addModel(dbAdapter) {
class Post {
id;
intId;
shortId;
attachments;
userId;
timelineIds;
Expand All @@ -46,6 +47,7 @@ export function addModel(dbAdapter) {
constructor(params) {
this.id = params.id;
this.intId = params.intId;
this.shortId = params.shortId;
this.body = params.body;
this.attachments = params.attachments || [];
this.userId = params.userId;
Expand All @@ -60,10 +62,6 @@ export function addModel(dbAdapter) {
this.isProtected = params.isProtected || '0';
this.isPropagable = params.isPropagable || '0';

if (params.shortId) {
this.shortId = params.shortId;
}

if (params.friendfeedUrl) {
this.friendfeedUrl = params.friendfeedUrl;
}
Expand Down
9 changes: 5 additions & 4 deletions app/support/DbAdapter/backlinks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Comment } from '../../models';
import { extractUUIDs } from '../backlinks';
import { extractShortIds, extractUUIDs } from '../backlinks';

///////////////////////////////////////////////////
// Backlinks
Expand Down Expand Up @@ -53,9 +53,10 @@ const backlinksTrait = (superClass) =>
}

async updateBacklinks(text, refPostUID, refCommentUID = null, db = this.database) {
const uuids = await db.getCol(`select uid from posts where uid = any(?)`, [
extractUUIDs(text),
]);
const uuids = await db.getCol(
`select long_id from post_short_ids where long_id = any(?) or short_id = any(?)`,
[extractUUIDs(text), extractShortIds(text)],
);

// Remove the old backlinks
if (refCommentUID) {
Expand Down
5 changes: 5 additions & 0 deletions app/support/backlinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export function extractUUIDs(text: string | null): UUID[] {
return text ? [...text.matchAll(uuidRe)].map((m) => m[0]).filter(onlyUnique) : [];
}

export const shortLinkRe = /\/[A-Za-z0-9]{3,25}\/([0-9a-f]{6,10})/gi;
export function extractShortIds(text: string | null): string[] {
return text ? [...text.matchAll(shortLinkRe)].map((m) => m[1]).filter(onlyUnique) : [];
}

function onlyUnique<T>(value: T, index: number, arr: T[]) {
return arr.indexOf(value) === index;
}
Expand Down
93 changes: 93 additions & 0 deletions test/integration/support/DbAdapter/backlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,97 @@ describe('Backlinks DB trait', () => {
expect(result, 'to equal', new Map([[lunaPost.id, 2]]));
});
});

describe('Short links', () => {
let jupiterFeed, jupiterPost, jupiterCommentNo2;
let lunaPostShortId;

before(async () => {
jupiterFeed = await jupiter.getPostsTimeline();
lunaPostShortId = await lunaPost.getShortId();
});

it(`should count short links on post update`, async () => {
jupiterPost = new Post({
body: 'just a post',
userId: jupiter.id,
timelineIds: [jupiterFeed.id],
});
await jupiterPost.create();

let result = await dbAdapter.getBacklinksCounts([lunaPost.id]);
expect(result, 'to equal', new Map([[lunaPost.id, 2]]));

await jupiterPost.update({ body: `luna post: /luna/${lunaPostShortId}` });

result = await dbAdapter.getBacklinksCounts([lunaPost.id]);
expect(result, 'to equal', new Map([[lunaPost.id, 3]]));

await jupiterPost.update({ body: `just a post` });

result = await dbAdapter.getBacklinksCounts([lunaPost.id]);
expect(result, 'to equal', new Map([[lunaPost.id, 2]]));
});

it(`should count short links on comment update`, async () => {
jupiterCommentNo2 = jupiter.newComment({
postId: jupiterPost.id,
body: `just a comment`,
});
await jupiterCommentNo2.create();

let result = await dbAdapter.getBacklinksCounts([lunaPost.id]);
expect(result, 'to equal', new Map([[lunaPost.id, 2]]));

await jupiterCommentNo2.update({ body: `luna post: /luna/${lunaPostShortId}` });

result = await dbAdapter.getBacklinksCounts([lunaPost.id]);
expect(result, 'to equal', new Map([[lunaPost.id, 3]]));

await jupiterCommentNo2.update({ body: `just a comment` });

result = await dbAdapter.getBacklinksCounts([lunaPost.id]);
expect(result, 'to equal', new Map([[lunaPost.id, 2]]));
});

it(`should count short links on post create`, async () => {
await jupiterCommentNo2.destroy();
await jupiterPost.destroy();

jupiterPost = new Post({
body: `luna post: /luna/${lunaPostShortId}`,
userId: jupiter.id,
timelineIds: [jupiterFeed.id],
});
await jupiterPost.create();

const result = await dbAdapter.getBacklinksCounts([lunaPost.id]);
expect(result, 'to equal', new Map([[lunaPost.id, 3]]));
});

it(`should count short links on comment create`, async () => {
jupiterCommentNo2 = jupiter.newComment({
postId: jupiterPost.id,
body: `luna post: /luna/${lunaPostShortId}`,
});
await jupiterCommentNo2.create();

const result = await dbAdapter.getBacklinksCounts([lunaPost.id]);
expect(result, 'to equal', new Map([[lunaPost.id, 4]]));
});

it(`should count short links on comment destroy`, async () => {
await jupiterCommentNo2.destroy();

const result = await dbAdapter.getBacklinksCounts([lunaPost.id]);
expect(result, 'to equal', new Map([[lunaPost.id, 3]]));
});

it(`should count short links on post destroy`, async () => {
await jupiterPost.destroy();

const result = await dbAdapter.getBacklinksCounts([lunaPost.id]);
expect(result, 'to equal', new Map([[lunaPost.id, 2]]));
});
});
});

0 comments on commit e497690

Please sign in to comment.