Merge pull request #445 from BeaudanBrown/auth-db

[Public Authentication] Add database tables and helper functions for public server auth
pull/447/head
Beaudan Campbell-Brown 6 years ago committed by GitHub
commit 1cabc8d02b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -120,6 +120,9 @@ module.exports = {
getAllPrivateConversations, getAllPrivateConversations,
getAllRssFeedConversations, getAllRssFeedConversations,
getAllPublicConversations, getAllPublicConversations,
getPublicConversationsByServer,
savePublicServerToken,
getPublicServerTokenByServerName,
getAllGroupsInvolvingId, getAllGroupsInvolvingId,
searchConversations, searchConversations,
@ -766,6 +769,26 @@ async function getAllPrivateConversations({ ConversationCollection }) {
return collection; 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 }) { async function getAllGroupsInvolvingId(id, { ConversationCollection }) {
const conversations = await channels.getAllGroupsInvolvingId(id); const conversations = await channels.getAllGroupsInvolvingId(id);

Loading…
Cancel
Save