2022-07-07 18:23:44 +02:00
|
|
|
var express = require('express');
|
|
|
|
var router = express.Router();
|
2022-07-07 19:03:04 +02:00
|
|
|
var sqlite3 = require('sqlite3');
|
2022-07-07 18:23:44 +02:00
|
|
|
|
2022-07-07 19:03:04 +02:00
|
|
|
const database_masto = "mastodon_multi_accounts.db"
|
2022-07-07 18:23:44 +02:00
|
|
|
/* GET home page. */
|
2022-07-07 19:03:04 +02:00
|
|
|
router.get('/', function (req, res, next) {
|
|
|
|
res.render('index', {title: 'Express'});
|
2022-07-07 18:23:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// publier un message avec un certain compte
|
2022-07-07 19:03:04 +02:00
|
|
|
router.get('/publish', function (req, res, next) {
|
|
|
|
res.render('index', {title: 'Express'});
|
2022-07-07 18:23:44 +02:00
|
|
|
});
|
|
|
|
|
2022-07-07 19:03:04 +02:00
|
|
|
|
|
|
|
const connect = require('@databases/sqlite');
|
|
|
|
const {sql} = require('@databases/sqlite');
|
|
|
|
|
|
|
|
const db = connect(database_masto);
|
|
|
|
|
|
|
|
async function prepare() {
|
|
|
|
await db.query(sql`
|
|
|
|
CREATE TABLE IF NOT EXISTS app_data (
|
|
|
|
id VARCHAR NOT NULL PRIMARY KEY,
|
|
|
|
value VARCHAR NOT NULL
|
|
|
|
);
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
const prepared = prepare();
|
2022-07-07 18:23:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
function createDatabase() {
|
2022-07-07 19:03:04 +02:00
|
|
|
var newdb = new sqlite3.Database(database_masto, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
|
|
|
|
|
2022-07-07 18:23:44 +02:00
|
|
|
createTables(newdb);
|
2022-07-07 19:03:04 +02:00
|
|
|
})
|
2022-07-07 18:23:44 +02:00
|
|
|
}
|
2022-07-07 19:03:04 +02:00
|
|
|
|
2022-07-07 18:23:44 +02:00
|
|
|
function createTables(newdb) {
|
|
|
|
newdb.exec(`
|
2022-07-07 19:03:04 +02:00
|
|
|
create table posts_scheduled
|
|
|
|
(
|
|
|
|
action_id int primary key not null,
|
|
|
|
post_username text not null,
|
|
|
|
content text not null,
|
|
|
|
medias text,
|
|
|
|
date_schedule datetime
|
|
|
|
);
|
|
|
|
insert into posts_scheduled
|
|
|
|
values (NULL, "modominem", "un message d'example", "un jour")
|
|
|
|
;
|
|
|
|
`, () => {
|
2022-07-07 18:23:44 +02:00
|
|
|
getAllPosts(newdb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAllPosts(db) {
|
|
|
|
db.all(`
|
2022-07-07 19:03:04 +02:00
|
|
|
select *
|
|
|
|
from posts_scheduled p orderyby p.action_id desc`, "les posts", (err, rows) => {
|
|
|
|
if (rows) {
|
2022-07-07 18:23:44 +02:00
|
|
|
rows.forEach(row => {
|
|
|
|
console.log(row.post_username + "\t" +
|
|
|
|
row.content + "\t" +
|
|
|
|
row.medias);
|
|
|
|
});
|
2022-07-07 19:03:04 +02:00
|
|
|
} else {
|
2022-07-07 18:23:44 +02:00
|
|
|
console.log('no rows')
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-07 19:03:04 +02:00
|
|
|
router.get('/init-db', function (req, res, next) {
|
|
|
|
var db = new sqlite3.Database(database_masto, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
|
|
|
|
console.log('got to create db')
|
|
|
|
createDatabase()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
res.render('created_db', {});
|
2022-07-07 18:23:44 +02:00
|
|
|
});
|
|
|
|
|
2022-07-07 19:03:04 +02:00
|
|
|
router.get('/list', function (req, res, next) {
|
|
|
|
|
2022-07-07 18:23:44 +02:00
|
|
|
|
2022-07-07 19:03:04 +02:00
|
|
|
var db = new sqlite3.Database('../masto_posts.db', sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
|
2022-07-07 18:23:44 +02:00
|
|
|
if (err && err.code == "SQLITE_CANTOPEN") {
|
|
|
|
createDatabase();
|
|
|
|
return;
|
|
|
|
} else if (err) {
|
|
|
|
console.log("Getting error " + err);
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-07-07 19:03:04 +02:00
|
|
|
getAllPosts(db);
|
2022-07-07 18:23:44 +02:00
|
|
|
});
|
|
|
|
|
2022-07-07 19:03:04 +02:00
|
|
|
let posts_list = [{
|
|
|
|
action_id: 1,
|
|
|
|
post_username: "modominem",
|
|
|
|
content: "demo post list",
|
|
|
|
medias: "media file name from assets folder",
|
|
|
|
date_schedule: "2022-07-07 "
|
|
|
|
}]
|
2022-07-07 18:23:44 +02:00
|
|
|
|
|
|
|
getAllPosts(db)
|
|
|
|
res.render('database', {posts_list: posts_list});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2022-07-07 19:03:04 +02:00
|
|
|
router.get('/add-to-db-example', function (req, res, next) {
|
|
|
|
|
|
|
|
var db = new sqlite3.Database('../masto_posts.db',
|
|
|
|
sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE,
|
|
|
|
(err) => {
|
|
|
|
|
|
|
|
if (err && err.code == "SQLITE_CANTOPEN") {
|
|
|
|
createDatabase();
|
|
|
|
return;
|
|
|
|
} else if (err) {
|
|
|
|
console.log("Getting error " + err);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
db.exec(`
|
|
|
|
insert into posts_scheduled
|
|
|
|
values (NULL, "modominem", "un message d'example", "un jour")
|
|
|
|
;
|
|
|
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
let posts_list = []
|
|
|
|
res.render('database', {posts_list: posts_list});
|
|
|
|
});
|
|
|
|
|
2022-07-07 18:23:44 +02:00
|
|
|
|
|
|
|
// publier le message en db dans la file
|
2022-07-07 19:03:04 +02:00
|
|
|
router.get('/publish-last-entry', function (req, res, next) {
|
|
|
|
res.render('index', {title: 'Express'});
|
2022-07-07 18:23:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|