Move feed logs to SQLite 1

This commit is contained in:
Natsumi
2021-06-28 17:43:20 +12:00
parent 2af8ba89c0
commit b2cd4985cc
2 changed files with 81 additions and 94 deletions
+64 -81
View File
@@ -41,7 +41,6 @@ speechSynthesis.getVoices();
); );
await configRepository.init(); await configRepository.init();
await database.init();
if (configRepository.getBool('migrate_config_20201101') === null) { if (configRepository.getBool('migrate_config_20201101') === null) {
var legacyConfigKeys = [ var legacyConfigKeys = [
@@ -5665,39 +5664,34 @@ speechSynthesis.getVoices();
location = ''; location = '';
} }
var worldName = await this.getWorldName(location); var worldName = await this.getWorldName(location);
this.addFeed('Offline', ctx.ref, { var feed = {
location,
worldName,
time
});
database.addOnlineOfflineToDatabase({
created_at: new Date().toJSON(), created_at: new Date().toJSON(),
type: 'Offline',
userId: ctx.ref.id, userId: ctx.ref.id,
displayName: ctx.ref.displayName, displayName: ctx.ref.displayName,
type: 'Offline',
location, location,
worldName, worldName,
time time
}); };
this.addFeed(feed);
database.addOnlineOfflineToDatabase(feed);
} }
} else if (newState === 'online') { } else if (newState === 'online') {
ctx.ref.$location_at = Date.now(); ctx.ref.$location_at = Date.now();
ctx.ref.$online_for = Date.now(); ctx.ref.$online_for = Date.now();
ctx.ref.$offline_for = ''; ctx.ref.$offline_for = '';
var worldName = await this.getWorldName(ctx.ref.location); var worldName = await this.getWorldName(ctx.ref.location);
this.addFeed('Online', ctx.ref, { var feed = {
location: ctx.ref.location,
worldName
});
database.addOnlineOfflineToDatabase({
created_at: new Date().toJSON(), created_at: new Date().toJSON(),
type: 'Online',
userId: ctx.ref.id, userId: ctx.ref.id,
displayName: ctx.ref.displayName, displayName: ctx.ref.displayName,
type: 'Online',
location, location,
worldName, worldName,
time: '' time: ''
}); };
this.addFeed(feed);
database.addOnlineOfflineToDatabase(feed);
} }
} }
if (ctx.state === 'online') { if (ctx.state === 'online') {
@@ -6118,9 +6112,11 @@ speechSynthesis.getVoices();
} }
API.$on('LOGIN', async function (args) { API.$on('LOGIN', async function (args) {
await database.init(args.json.id);
$app.feedTable.data = await database.getFeedDatabase(); $app.feedTable.data = await database.getFeedDatabase();
//$app.feedTable.data = VRCXStorage.GetArray(`${args.ref.id}_feedTable`); $app.sweepFeed();
//$app.sweepFeed(); //remove old table
VRCXStorage.Remove(`${args.ref.id}_feedTable`);
}); });
API.$on('USER:UPDATE', async function (args) { API.$on('USER:UPDATE', async function (args) {
@@ -6134,39 +6130,40 @@ speechSynthesis.getVoices();
(props.location[1] !== 'offline') && (props.location[1] !== 'offline') &&
(props.location[1] !== '')) { (props.location[1] !== '')) {
var worldName = await $app.getWorldName(props.location[0]); var worldName = await $app.getWorldName(props.location[0]);
$app.addFeed('GPS', ref, { var feed = {
location: props.location[0],
worldName,
previousLocation: props.location[1],
time: props.location[2]
});
database.addGPSToDatabase({
created_at: new Date().toJSON(), created_at: new Date().toJSON(),
type: 'GPS',
userId: ref.id, userId: ref.id,
displayName: ref.displayName, displayName: ref.displayName,
location: props.location[0], location: props.location[0],
worldName, worldName,
previousLocation: props.location[1], previousLocation: props.location[1],
time: props.location[2] time: props.location[2]
}); };
$app.addFeed(feed);
database.addGPSToDatabase(feed);
$app.updateFriendGPS(ref.id); $app.updateFriendGPS(ref.id);
$app.feedDownloadWorldCache(ref.id, props.location[0]); $app.feedDownloadWorldCache(ref.id, props.location[0]);
} }
if (props.currentAvatarImageUrl || if (props.currentAvatarImageUrl ||
props.currentAvatarThumbnailImageUrl) { props.currentAvatarThumbnailImageUrl) {
var currentAvatarImageUrl = '';
var previousCurrentAvatarImageUrl = '';
var currentAvatarThumbnailImageUrl = '';
var previousCurrentAvatarThumbnailImageUrl = '';
if (props.currentAvatarImageUrl) { if (props.currentAvatarImageUrl) {
var currentAvatarImageUrl = props.currentAvatarImageUrl[0]; currentAvatarImageUrl = props.currentAvatarImageUrl[0];
var previousCurrentAvatarImageUrl = props.currentAvatarImageUrl[1]; previousCurrentAvatarImageUrl = props.currentAvatarImageUrl[1];
} else { } else {
var currentAvatarImageUrl = ref.currentAvatarImageUrl; currentAvatarImageUrl = ref.currentAvatarImageUrl;
var previousCurrentAvatarImageUrl = ref.currentAvatarImageUrl; previousCurrentAvatarImageUrl = ref.currentAvatarImageUrl;
} }
if (props.currentAvatarThumbnailImageUrl) { if (props.currentAvatarThumbnailImageUrl) {
var currentAvatarThumbnailImageUrl = props.currentAvatarThumbnailImageUrl[0]; currentAvatarThumbnailImageUrl = props.currentAvatarThumbnailImageUrl[0];
var previousCurrentAvatarThumbnailImageUrl = props.currentAvatarThumbnailImageUrl[1]; previousCurrentAvatarThumbnailImageUrl = props.currentAvatarThumbnailImageUrl[1];
} else { } else {
var currentAvatarThumbnailImageUrl = ref.currentAvatarThumbnailImageUrl; currentAvatarThumbnailImageUrl = ref.currentAvatarThumbnailImageUrl;
var previousCurrentAvatarThumbnailImageUrl = ref.currentAvatarThumbnailImageUrl; previousCurrentAvatarThumbnailImageUrl = ref.currentAvatarThumbnailImageUrl;
} }
var avatarInfo = { var avatarInfo = {
ownerId: '', ownerId: '',
@@ -6176,16 +6173,9 @@ speechSynthesis.getVoices();
avatarInfo = await $app.getAvatarName(currentAvatarImageUrl); avatarInfo = await $app.getAvatarName(currentAvatarImageUrl);
} catch (err) { } catch (err) {
} }
$app.addFeed('Avatar', ref, { var feed = {
ownerId: avatarInfo.ownerId,
avatarName: avatarInfo.avatarName,
currentAvatarImageUrl,
currentAvatarThumbnailImageUrl,
previousCurrentAvatarImageUrl,
previousCurrentAvatarThumbnailImageUrl
});
database.addAvatarToDatabase({
created_at: new Date().toJSON(), created_at: new Date().toJSON(),
type: 'Avatar',
userId: ref.id, userId: ref.id,
displayName: ref.displayName, displayName: ref.displayName,
ownerId: avatarInfo.ownerId, ownerId: avatarInfo.ownerId,
@@ -6194,63 +6184,56 @@ speechSynthesis.getVoices();
currentAvatarThumbnailImageUrl, currentAvatarThumbnailImageUrl,
previousCurrentAvatarImageUrl, previousCurrentAvatarImageUrl,
previousCurrentAvatarThumbnailImageUrl previousCurrentAvatarThumbnailImageUrl
}); };
$app.addFeed(feed);
database.addAvatarToDatabase(feed);
} }
if (props.status || if (props.status ||
props.statusDescription) { props.statusDescription) {
var status = '';
var previousStatus = '';
var statusDescription = '';
var previousStatusDescription = '';
if (props.status) { if (props.status) {
var status = props.status[0]; if (props.status[0]) {
var previousStatus = props.status[1]; status = props.status[0];
}
if (props.status[1]) {
previousStatus = props.status[1];
}
} else { } else {
var status = ref.status; status = ref.status;
var previousStatus = ref.status; previousStatus = ref.status;
} }
if (props.statusDescription) { if (props.statusDescription) {
var statusDescription = props.statusDescription[0]; if (props.statusDescription[0]) {
var previousStatusDescription = props.statusDescription[1]; statusDescription = props.statusDescription[0];
}
if (props.statusDescription[1]) {
previousStatusDescription = props.statusDescription[1];
}
} else { } else {
var statusDescription = ref.statusDescription; statusDescription = ref.statusDescription;
var previousStatusDescription = ref.statusDescription; previousStatusDescription = ref.statusDescription;
} }
$app.addFeed('Status', ref, { var feed = {
status,
statusDescription,
previousStatus,
previousStatusDescription
});
database.addStatusToDatabase({
created_at: new Date().toJSON(), created_at: new Date().toJSON(),
type: 'Status',
userId: ref.id, userId: ref.id,
displayName: ref.displayName, displayName: ref.displayName,
status, status,
statusDescription, statusDescription,
previousStatus, previousStatus,
previousStatusDescription previousStatusDescription
}); };
$app.addFeed(feed);
database.addStatusToDatabase(feed);
} }
}); });
var saveFeedTimer = null; $app.methods.addFeed = function (feed) {
$app.methods.saveFeed = function () { this.feedTable.data.push(feed);
if (saveFeedTimer !== null) { this.sweepFeed();
return;
}
saveFeedTimer = setTimeout(() => {
saveFeedTimer = null;
VRCXStorage.SetArray(`${API.currentUser.id}_feedTable`, this.feedTable.data);
}, 1);
};
$app.methods.addFeed = function (type, ref, extra) {
this.feedTable.data.push({
created_at: new Date().toJSON(),
type,
userId: ref.id,
displayName: ref.displayName,
...extra
});
//this.sweepFeed();
//this.saveFeed();
this.updateSharedFeed(false); this.updateSharedFeed(false);
this.notifyMenu('feed'); this.notifyMenu('feed');
}; };
+17 -13
View File
@@ -1,23 +1,27 @@
import sqliteService from '../service/sqlite.js'; import sqliteService from '../service/sqlite.js';
class Database { class Database {
async init() { async init(userId) {
Database.userId = userId.replaceAll('-', '').replaceAll('_', '');
await sqliteService.executeNonQuery( await sqliteService.executeNonQuery(
'CREATE TABLE IF NOT EXISTS feed_gps (id INTEGER PRIMARY KEY, created_at TEXT, user_id TEXT, display_name TEXT, location TEXT, world_name TEXT, previous_location TEXT, time INTEGER)' `CREATE TABLE IF NOT EXISTS ${Database.userId}_feed_gps (id INTEGER PRIMARY KEY, created_at TEXT, user_id TEXT, display_name TEXT, location TEXT, world_name TEXT, previous_location TEXT, time INTEGER)`
); );
await sqliteService.executeNonQuery( await sqliteService.executeNonQuery(
'CREATE TABLE IF NOT EXISTS feed_status (id INTEGER PRIMARY KEY, created_at TEXT, user_id TEXT, display_name TEXT, status TEXT, status_description TEXT, previous_status TEXT, previous_status_description TEXT)' `CREATE TABLE IF NOT EXISTS ${Database.userId}_feed_status (id INTEGER PRIMARY KEY, created_at TEXT, user_id TEXT, display_name TEXT, status TEXT, status_description TEXT, previous_status TEXT, previous_status_description TEXT)`
); );
await sqliteService.executeNonQuery( await sqliteService.executeNonQuery(
'CREATE TABLE IF NOT EXISTS feed_avatar (id INTEGER PRIMARY KEY, created_at TEXT, user_id TEXT, display_name TEXT, owner_id TEXT, avatar_name TEXT, current_avatar_image_url TEXT, current_avatar_thumbnail_image_url TEXT, previous_current_avatar_image_url TEXT, previous_current_avatar_thumbnail_image_url TEXT)' `CREATE TABLE IF NOT EXISTS ${Database.userId}_feed_avatar (id INTEGER PRIMARY KEY, created_at TEXT, user_id TEXT, display_name TEXT, owner_id TEXT, avatar_name TEXT, current_avatar_image_url TEXT, current_avatar_thumbnail_image_url TEXT, previous_current_avatar_image_url TEXT, previous_current_avatar_thumbnail_image_url TEXT)`
); );
await sqliteService.executeNonQuery( await sqliteService.executeNonQuery(
'CREATE TABLE IF NOT EXISTS feed_online_offline (id INTEGER PRIMARY KEY, created_at TEXT, user_id TEXT, display_name TEXT, type TEXT, location TEXT, world_name TEXT, time INTEGER)' `CREATE TABLE IF NOT EXISTS ${Database.userId}_feed_online_offline (id INTEGER PRIMARY KEY, created_at TEXT, user_id TEXT, display_name TEXT, type TEXT, location TEXT, world_name TEXT, time INTEGER)`
); );
} }
async getFeedDatabase() { async getFeedDatabase() {
var feedDatabase = []; var feedDatabase = [];
var date = new Date();
date.setDate(date.getDate() - 3); // 3 day limit
var dateOffset = date.toJSON();
await sqliteService.execute((dbRow) => { await sqliteService.execute((dbRow) => {
var row = { var row = {
created_at: dbRow[1], created_at: dbRow[1],
@@ -30,7 +34,7 @@ class Database {
time: dbRow[7] time: dbRow[7]
}; };
feedDatabase.unshift(row); feedDatabase.unshift(row);
}, 'SELECT * FROM feed_gps'); }, `SELECT * FROM ${Database.userId}_feed_gps WHERE created_at >= date('${dateOffset}')`);
await sqliteService.execute((dbRow) => { await sqliteService.execute((dbRow) => {
var row = { var row = {
created_at: dbRow[1], created_at: dbRow[1],
@@ -43,7 +47,7 @@ class Database {
previousStatusDescription: dbRow[7] previousStatusDescription: dbRow[7]
}; };
feedDatabase.unshift(row); feedDatabase.unshift(row);
}, 'SELECT * FROM feed_status'); }, `SELECT * FROM ${Database.userId}_feed_status WHERE created_at >= date('${dateOffset}')`);
await sqliteService.execute((dbRow) => { await sqliteService.execute((dbRow) => {
var row = { var row = {
created_at: dbRow[1], created_at: dbRow[1],
@@ -58,7 +62,7 @@ class Database {
previousCurrentAvatarThumbnailImageUrl: dbRow[9] previousCurrentAvatarThumbnailImageUrl: dbRow[9]
}; };
feedDatabase.unshift(row); feedDatabase.unshift(row);
}, 'SELECT * FROM feed_avatar'); }, `SELECT * FROM ${Database.userId}_feed_avatar WHERE created_at >= date('${dateOffset}')`);
await sqliteService.execute((dbRow) => { await sqliteService.execute((dbRow) => {
var row = { var row = {
created_at: dbRow[1], created_at: dbRow[1],
@@ -70,7 +74,7 @@ class Database {
time: dbRow[7] time: dbRow[7]
}; };
feedDatabase.unshift(row); feedDatabase.unshift(row);
}, 'SELECT * FROM feed_online_offline'); }, `SELECT * FROM ${Database.userId}_feed_online_offline WHERE created_at >= date('${dateOffset}')`);
var compareByCreatedAt = function (a, b) { var compareByCreatedAt = function (a, b) {
var A = a.created_at; var A = a.created_at;
var B = b.created_at; var B = b.created_at;
@@ -88,7 +92,7 @@ class Database {
addGPSToDatabase(entry) { addGPSToDatabase(entry) {
sqliteService.executeNonQuery( sqliteService.executeNonQuery(
'INSERT OR IGNORE INTO feed_gps (created_at, user_id, display_name, location, world_name, previous_location, time) VALUES (@created_at, @user_id, @display_name, @location, @world_name, @previous_location, @time)', `INSERT OR IGNORE INTO ${Database.userId}_feed_gps (created_at, user_id, display_name, location, world_name, previous_location, time) VALUES (@created_at, @user_id, @display_name, @location, @world_name, @previous_location, @time)`,
{ {
'@created_at': entry.created_at, '@created_at': entry.created_at,
'@user_id': entry.userId, '@user_id': entry.userId,
@@ -103,7 +107,7 @@ class Database {
addStatusToDatabase(entry) { addStatusToDatabase(entry) {
sqliteService.executeNonQuery( sqliteService.executeNonQuery(
'INSERT OR IGNORE INTO feed_status (created_at, user_id, display_name, status, status_description, previous_status, previous_status_description) VALUES (@created_at, @user_id, @display_name, @status, @status_description, @previous_status, @previous_status_description)', `INSERT OR IGNORE INTO ${Database.userId}_feed_status (created_at, user_id, display_name, status, status_description, previous_status, previous_status_description) VALUES (@created_at, @user_id, @display_name, @status, @status_description, @previous_status, @previous_status_description)`,
{ {
'@created_at': entry.created_at, '@created_at': entry.created_at,
'@user_id': entry.userId, '@user_id': entry.userId,
@@ -118,7 +122,7 @@ class Database {
addAvatarToDatabase(entry) { addAvatarToDatabase(entry) {
sqliteService.executeNonQuery( sqliteService.executeNonQuery(
'INSERT OR IGNORE INTO feed_avatar (created_at, user_id, display_name, owner_id, avatar_name, current_avatar_image_url, current_avatar_thumbnail_image_url, previous_current_avatar_image_url, previous_current_avatar_thumbnail_image_url) VALUES (@created_at, @user_id, @display_name, @owner_id, @avatar_name, @current_avatar_image_url, @current_avatar_thumbnail_image_url, @previous_current_avatar_image_url, @previous_current_avatar_thumbnail_image_url)', `INSERT OR IGNORE INTO ${Database.userId}_feed_avatar (created_at, user_id, display_name, owner_id, avatar_name, current_avatar_image_url, current_avatar_thumbnail_image_url, previous_current_avatar_image_url, previous_current_avatar_thumbnail_image_url) VALUES (@created_at, @user_id, @display_name, @owner_id, @avatar_name, @current_avatar_image_url, @current_avatar_thumbnail_image_url, @previous_current_avatar_image_url, @previous_current_avatar_thumbnail_image_url)`,
{ {
'@created_at': entry.created_at, '@created_at': entry.created_at,
'@user_id': entry.userId, '@user_id': entry.userId,
@@ -135,7 +139,7 @@ class Database {
addOnlineOfflineToDatabase(entry) { addOnlineOfflineToDatabase(entry) {
sqliteService.executeNonQuery( sqliteService.executeNonQuery(
'INSERT OR IGNORE INTO feed_online_offline (created_at, user_id, display_name, type, location, world_name, time) VALUES (@created_at, @user_id, @display_name, @type, @location, @world_name, @time)', `INSERT OR IGNORE INTO ${Database.userId}_feed_online_offline (created_at, user_id, display_name, type, location, world_name, time) VALUES (@created_at, @user_id, @display_name, @type, @location, @world_name, @time)`,
{ {
'@created_at': entry.created_at, '@created_at': entry.created_at,
'@user_id': entry.userId, '@user_id': entry.userId,