create a channel for every translated language

This commit is contained in:
HF 2021-01-31 19:06:44 +01:00
parent 88d231867e
commit acf353619f
3 changed files with 37 additions and 3 deletions

View File

@ -89,14 +89,12 @@ const ChannelDropDown = ({
}
// latest lastTs first
sortChansNew.sort((c1, c2) => {
if (c1[3] === 0) return 0;
if (c1[4] > c2[4]) return -1;
if (c2[4] > c1[4]) return 1;
return 0;
});
// unread first
sortChansNew.sort((c1, c2) => {
if (c1[3] === 0) return 0;
if (c1[1] && !c2[1]) return -1;
if (c2[1] && !c1[1]) return 1;
return 0;

View File

@ -7,12 +7,14 @@ import RateLimiter from '../utils/RateLimiter';
import { Channel, RegUser, UserChannel } from '../data/models';
import ChatMessageBuffer from './ChatMessageBuffer';
import { cheapDetector } from './isProxy';
import ttags from './ttag';
import { CHAT_CHANNELS, EVENT_USER_NAME, INFO_USER_NAME } from './constants';
export class ChatProvider {
constructor() {
this.defaultChannels = {};
this.langChannels = {};
this.enChannelId = 0;
this.intChannelId = 0;
this.infoUserId = 1;
@ -71,6 +73,28 @@ export class ChatProvider {
lastTs,
];
}
// find or create non-english lang channels
const langs = Object.key(ttags);
for (let i = 0; i < langs; i += 1) {
const name = langs[i];
if (name === 'default') {
continue;
}
// eslint-disable-next-line no-await-in-loop
const channel = await Channel.findOrCreate({
where: { name },
defaults: {
name,
},
});
const { id, type, lastTs } = channel[0];
this.langChannels[name] = {
id,
name,
type,
lastTs,
};
}
// find or create default users
let name = INFO_USER_NAME;
const infoUser = await RegUser.findOrCreate({
@ -102,6 +126,18 @@ export class ChatProvider {
this.eventUserId = eventUser[0].id;
}
getDefaultChannels(lang) {
const { defaultChannels, langChannels } = this;
const channels = { ...defaultChannels };
if (lang && lang !== 'default' && langChannels[lang]) {
const {
id, name, type, lastTs,
} = langChannels[lang];
channels[id] = [name, type, lastTs];
}
return channels;
}
static async addUserToChannel(
userId,
channelId,

View File

@ -32,7 +32,7 @@ export default async function getMe(user, lang = 'default') {
userdata.canvases = getLocalicedCanvases(lang);
userdata.channels = {
...chatProvider.defaultChannels,
...chatProvider.getDefaultChannels(lang),
...userdata.channels,
};