Add database tables and helper functions for public server auth

This commit is contained in:
Beaudan Brown 2019-08-28 17:00:48 +10:00
parent 6c8a35d8e0
commit 37366198fc
2 changed files with 100 additions and 1 deletions

View File

@ -94,11 +94,14 @@ module.exports = {
saveConversation,
saveConversations,
getConversationById,
savePublicServerToken,
getPublicServerTokenByServerName,
updateConversation,
removeConversation,
getAllConversations,
getAllRssFeedConversations,
getAllPublicConversations,
getPublicConversationsByServer,
getPubKeysWithFriendStatus,
getAllConversationIds,
getAllPrivateConversations,
@ -790,6 +793,13 @@ async function updateToLokiSchemaVersion1(currentVersion, instance) {
ADD COLUMN serverId STRING;`
);
await instance.run(
`CREATE TABLE server_tokens(
server STRING PRIMARY KEY ASC,
token TEXT
);`
);
const initConversation = async data => {
const { id, type, name, friendRequestStatus } = data;
await instance.run(
@ -822,6 +832,11 @@ async function updateToLokiSchemaVersion1(currentVersion, instance) {
);
};
const lokiPublicServerData = {
server: 'chat.lokinet.org',
token: null,
};
const baseData = {
friendRequestStatus: 4, // Friends
sealedSender: 0,
@ -837,11 +852,27 @@ async function updateToLokiSchemaVersion1(currentVersion, instance) {
const publicChatData = {
...baseData,
id: 'publicChat:1@chat.lokinet.org',
server: 'https://chat.lokinet.org',
server: 'chat.lokinet.org',
name: 'Loki Public Chat',
channelId: '1',
};
const { server, token } = lokiPublicServerData;
await instance.run(
`INSERT INTO server_tokens (
server,
token
) values (
$server,
$token
);`,
{
$server: server,
$token: token,
}
);
const newsRssFeedData = {
...baseData,
id: 'rss://loki.network/feed/',
@ -1590,6 +1621,38 @@ async function removeConversation(id) {
);
}
async function savePublicServerToken(data) {
const { server, token } = data;
await db.run(
`INSERT OR REPLACE INTO server_tokens (
server,
token
) values (
$server,
$token
)`,
{
$server: server,
$token: token,
}
);
}
async function getPublicServerTokenByServerName(server) {
const row = await db.get(
'SELECT * FROM server_tokens WHERE server = $server;',
{
$server: server,
}
);
if (!row) {
return null;
}
return row.token;
}
async function getConversationById(id) {
const row = await db.get('SELECT * FROM conversations WHERE id = $id;', {
$id: id,
@ -1656,6 +1719,19 @@ async function getAllPublicConversations() {
return map(rows, row => jsonToObject(row.json));
}
async function getPublicConversationsByServer(server) {
const rows = await db.all(
`SELECT * FROM conversations WHERE
server = $server
ORDER BY id ASC;`,
{
$server: server,
}
);
return map(rows, row => jsonToObject(row.json));
}
async function getAllGroupsInvolvingId(id) {
const rows = await db.all(
`SELECT json FROM conversations WHERE

View File

@ -120,6 +120,9 @@ module.exports = {
getAllPrivateConversations,
getAllRssFeedConversations,
getAllPublicConversations,
getPublicConversationsByServer,
savePublicServerToken,
getPublicServerTokenByServerName,
getAllGroupsInvolvingId,
searchConversations,
@ -766,6 +769,26 @@ async function getAllPrivateConversations({ ConversationCollection }) {
return collection;
}
async function savePublicServerToken(data) {
await channels.savePublicServerToken(data);
}
async function getPublicServerTokenByServerName(server) {
const token = await channels.getPublicServerTokenByServerName(server);
return token;
}
async function getPublicConversationsByServer(
server,
{ ConversationCollection }
) {
const conversations = await channels.getPublicConversationsByServer(server);
const collection = new ConversationCollection();
collection.add(conversations);
return collection;
}
async function getAllGroupsInvolvingId(id, { ConversationCollection }) {
const conversations = await channels.getAllGroupsInvolvingId(id);