Skip to content

Commit

Permalink
Rewrite the pronoun field matching
Browse files Browse the repository at this point in the history
Closes #39
  • Loading branch information
nachtjasmin committed Jul 10, 2023
1 parent 66f8039 commit f449fa2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 35 deletions.
41 changes: 6 additions & 35 deletions src/libs/fetchPronouns.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { debug, error, info, warn } from "./logging";
import { cachePronouns, getPronouns } from "./caching";
import { normaliseAccountName } from "./protootshelpers";
import { extractPronouns } from "./pronouns";

const cacheMaxAge = 24 * 60 * 60 * 1000; // time after which cached pronouns should be checked again: 24h
let conversationsCache;
const fieldNames = ["pronouns", "pronoun", "professional nouns", "pronomen"];

/**
* Fetches pronouns associated with account name.
Expand Down Expand Up @@ -60,12 +60,14 @@ export async function fetchPronouns(dataID, accountName, type) {
status = await fetchStatus(dataID);
}

const PronounField = getPronounField(status, accountName);
if (PronounField == "null") {
let pronouns = extractPronouns(status);
if (!pronouns) {
pronouns = "null";
//TODO: if no field check bio
info(`no pronouns found for ${accountName}, cached null`);
}
return PronounField;
await cachePronouns(accountName, pronouns);
return pronouns;
}

/**
Expand Down Expand Up @@ -159,37 +161,6 @@ async function fetchConversations() {
return conversations;
}

/**
* Searches for fields labelled "pronouns" in the statuses' author.
* If found returns the value of said field.
*
* @param {any} status
* @param {string} accountName
* @returns {string} Author pronouns if found. Otherwise returns "null"
*/
function getPronounField(status, accountName) {
// get account from status and pull out fields
const account = status.account;
const fields = account.fields;

for (const field of fields) {
//match fields against fieldNames
for (const searchTerm of fieldNames) {
if (field.name.toLowerCase().includes(searchTerm)) {
debug(`${account.acct}: ${field.value}`);

cachePronouns(accountName, field.value);
return field.value;
}
}
}

//if not returned by this point no field with pronouns was found

cachePronouns(accountName, "null");
return "null";
}

/**
* Fetches the current access token for the user.
* @returns {Promise<string>} The accessToken for the current user if we are logged in.
Expand Down
32 changes: 32 additions & 0 deletions src/libs/pronouns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const fieldMatchers = [/pro.*nouns?/i, "pronomen"];

/**
* Tries to extract the pronouns for the given status.
* This is done by searching for pronoun fields that match the {@see fieldMatchers}.
*
* If found, it sanitizes and returns the value of said field.
*
* @param {any} status
* @returns {string|null} Author pronouns if found. Otherwise returns null.
*/
export function extractFromStatus(status) {
// get account from status and pull out fields
const account = status.account;
const fields = account.fields;

let pronouns;
for (const field of fields) {
// TODO: add ranking of fields
if (pronouns) break;

for (const matcher of fieldMatchers) {
if (typeof matcher === "string" && field.name.toLowerCase().includes(matcher)) {
pronouns = field.value;
} else if (field.name.match(matcher)) {
pronouns = field.value;
}
}
}
if (!pronouns) return null;
return pronouns;
}
26 changes: 26 additions & 0 deletions tests/extractPronouns.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { suite } from "uvu";
import * as assert from "uvu/assert";
import * as pronouns from "../src/libs/pronouns.js";

const extract = suite("field extraction");
const validFields = [
"pronoun",
"pronouns",
"PRONOUNS",
"professional nouns",
"pronomen",
"Pronouns / Pronomen",
];

for (const field of validFields) {
extract(`${field} is extracted`, () => {
const result = pronouns.extractFromStatus({
account: {
fields: [{ name: field, value: "pro/nouns" }],
},
});
assert.equal("pro/nouns", result);
});
}

extract.run();

0 comments on commit f449fa2

Please sign in to comment.