Skip to content

Commit

Permalink
Associate TM chat messages with the correct MW message, instead of th…
Browse files Browse the repository at this point in the history
…e latest

Partly addresses #9.
  • Loading branch information
amire80 committed Jul 9, 2017
1 parent fe4e487 commit b7c9b0c
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions MediaWikiTelegramBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ function userKnown(userID) {

function initUser(userID) {
userStatus[userID] = {
languageCode: "",
currentMwMessageIndex: 0,
loadedMwMessages: [],
publishingTgMessages: {}
loadedTranslationMemory: {},
publishingTgMessages: {},
languageCode: ""
};
}

Expand Down Expand Up @@ -93,6 +94,21 @@ function normalizeLanguageCode(code) {
return REDIRECT_LANGUAGES[lower] || lower;
}

function cacheTranslationMemory(userID, targetMwMessage, i, text) {
const user = getUser(userID);
const title = targetMwMessage.title;

if (user.loadedTranslationMemory[title] === undefined) {
user.loadedTranslationMemory[title] = [];
}

user.loadedTranslationMemory[title][i] = text;
}

function getCachedTranslationMemory(userID, targetMwMessage) {
return getUser(userID).loadedTranslationMemory[targetMwMessage.title];
}

function addUserToDbByTgMsg(tgMsg, cb) {
const userID = tgMsg.from.id;
const insertStmtStr = `INSERT INTO user (user_telegram_id) VALUES (${userID})`;
Expand Down Expand Up @@ -297,6 +313,7 @@ function showTranslationMemory(userID) {
const title = targetMwMessage.title;
debug(userID, `Getting translation memory for "${title}"`, 1);

// TODO Check if it's already cached
mwApi.getTranslationMemory(title, (translationMemory) => {
let i;

Expand All @@ -309,9 +326,14 @@ function showTranslationMemory(userID) {
}

for (i = 0; i < translationMemory.length; i++) {
cacheTranslationMemory(userID, targetMwMessage, i, translationMemory[i].target);
}

const ttmCache = getCachedTranslationMemory(userID, targetMwMessage);
for (i = 0; i < ttmCache.length; i++) {
tgBot.sendMessage(
userID,
translationMemory[i].target
ttmCache[i]
);
}
});
Expand Down Expand Up @@ -380,6 +402,7 @@ function showCurrentMwMessage(userID) {
);
tgBot.sendMessage(userID, targetMwMessage.translation);
}

const user = getUser(userID);
user.mode = TRANSLATING_MODE;
});
Expand Down

0 comments on commit b7c9b0c

Please sign in to comment.