Rewrite the pronoun field matching

Closes #39
This commit is contained in:
nachtjasmin 2023-07-10 22:11:11 +02:00
parent c25274b007
commit 71d6da4f66
No known key found for this signature in database
3 changed files with 64 additions and 35 deletions

View file

@ -1,10 +1,10 @@
import { debug, error, info, warn } from "./logging";
import { cachePronouns, getPronouns } from "./caching";
import { normaliseAccountName } from "./protootshelpers";
import { extractFromStatus } 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.
@ -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 = extractFromStatus(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;
}
/**
@ -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.

32
src/libs/pronouns.js Normal file
View file

@ -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;
}

View file

@ -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();