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.
This commit is contained in:
Robin van Boven 2020-01-07 22:25:09 +01:00 committed by GitHub
parent 6c15372d9f
commit 3cfb338336
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 0 deletions

View File

@ -80,6 +80,9 @@ describe("plugins/discourse/createGraph", () => {
"Method topicsInCategories should be unused for createGraph" "Method topicsInCategories should be unused for createGraph"
); );
} }
findUsername() {
throw new Error("Method findUsername should be unused by createGraph");
}
} }
function example() { function example() {

View File

@ -60,6 +60,13 @@ export interface ReadRepository {
topicsInCategories( topicsInCategories(
categoryIds: $ReadOnlyArray<CategoryId> categoryIds: $ReadOnlyArray<CategoryId>
): $ReadOnlyArray<TopicId>; ): $ReadOnlyArray<TopicId>;
/**
* Gets the username of a user, if it exists.
*
* Note: input username is case-insensitive.
*/
findUsername(username: string): ?string;
} }
export type SyncHeads = {| export type SyncHeads = {|
@ -304,6 +311,19 @@ export class SqliteMirrorRepository
.all(); .all();
} }
findUsername(username: string): ?string {
return this._db
.prepare(
dedent`\
SELECT username
FROM users
WHERE username = :username COLLATE NOCASE
`
)
.pluck()
.get({username});
}
likes(): $ReadOnlyArray<LikeAction> { likes(): $ReadOnlyArray<LikeAction> {
return this._db return this._db
.prepare("SELECT post_id, username, timestamp_ms FROM likes") .prepare("SELECT post_id, username, timestamp_ms FROM likes")

View File

@ -26,6 +26,23 @@ describe("plugins/discourse/mirrorRepository", () => {
expect(fs.readFileSync(filename).toJSON()).toEqual(data); 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", () => { it("bumpedMsForTopic finds an existing topic's bumpedMs", () => {
// Given // Given
const db = new Database(":memory:"); const db = new Database(":memory:");