From 3cfb3383369de5f9135c83209044b8a3dfebaaf9 Mon Sep 17 00:00:00 2001 From: Robin van Boven <497556+Beanow@users.noreply.github.com> Date: Tue, 7 Jan 2020 22:25:09 +0100 Subject: [PATCH] Discourse: add mirror findUsername (#1528) Gets the username of a user, if it exists. Helpful for fixing capitalization issues such as #1479, and verifying the user exists for reference detection. --- src/plugins/discourse/createGraph.test.js | 3 +++ src/plugins/discourse/mirrorRepository.js | 20 +++++++++++++++++++ .../discourse/mirrorRepository.test.js | 17 ++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/plugins/discourse/createGraph.test.js b/src/plugins/discourse/createGraph.test.js index 7d616c6..8e6052d 100644 --- a/src/plugins/discourse/createGraph.test.js +++ b/src/plugins/discourse/createGraph.test.js @@ -80,6 +80,9 @@ describe("plugins/discourse/createGraph", () => { "Method topicsInCategories should be unused for createGraph" ); } + findUsername() { + throw new Error("Method findUsername should be unused by createGraph"); + } } function example() { diff --git a/src/plugins/discourse/mirrorRepository.js b/src/plugins/discourse/mirrorRepository.js index 475ae10..63c1ad8 100644 --- a/src/plugins/discourse/mirrorRepository.js +++ b/src/plugins/discourse/mirrorRepository.js @@ -60,6 +60,13 @@ export interface ReadRepository { topicsInCategories( categoryIds: $ReadOnlyArray ): $ReadOnlyArray; + + /** + * Gets the username of a user, if it exists. + * + * Note: input username is case-insensitive. + */ + findUsername(username: string): ?string; } export type SyncHeads = {| @@ -304,6 +311,19 @@ export class SqliteMirrorRepository .all(); } + findUsername(username: string): ?string { + return this._db + .prepare( + dedent`\ + SELECT username + FROM users + WHERE username = :username COLLATE NOCASE + ` + ) + .pluck() + .get({username}); + } + likes(): $ReadOnlyArray { return this._db .prepare("SELECT post_id, username, timestamp_ms FROM likes") diff --git a/src/plugins/discourse/mirrorRepository.test.js b/src/plugins/discourse/mirrorRepository.test.js index cbbcbe4..89a4d52 100644 --- a/src/plugins/discourse/mirrorRepository.test.js +++ b/src/plugins/discourse/mirrorRepository.test.js @@ -26,6 +26,23 @@ describe("plugins/discourse/mirrorRepository", () => { expect(fs.readFileSync(filename).toJSON()).toEqual(data); }); + it("findUsername does a case-insensitive query", () => { + // Given + const db = new Database(":memory:"); + const url = "http://example.com"; + const username = "PascalFan1988"; + const repository = new SqliteMirrorRepository(db, url); + + // When + repository.addUser(username); + const result1 = repository.findUsername("pascalfan1988"); + const result2 = repository.findUsername(username); + + // Then + expect(result1).toEqual(username); + expect(result2).toEqual(username); + }); + it("bumpedMsForTopic finds an existing topic's bumpedMs", () => { // Given const db = new Database(":memory:");