Browse Source

adding/ loading of bookmarks implemented (only for fixed User)

master
Senta Mandutz 5 years ago
parent
commit
a6dd34cf81
5 changed files with 302 additions and 219 deletions
  1. 3
    1
      mong_usr.js
  2. 2
    0
      public/routes/auth.js
  3. 49
    101
      public/routes/bookmark.js
  4. 29
    3
      public/routes/msgCard.js
  5. 219
    114
      server/dbs.js

+ 3
- 1
mong_usr.js View File

db.user.insert([
db.users.insert([
// author,OHMNEWS2019 // author,OHMNEWS2019
{"_id":"author","name":"Test Author","type":"author","roles":["user","author"],"hash":"sha256","salt":"cy5CkPrplcEze6o4psURhw==","pwd":"Gc8ozlxjEGITkS+fW9tz2xLROzws44s04EOCWuP04qE=","tags":["wichtig","th"],"bookmarks":["th"],}, {"_id":"author","name":"Test Author","type":"author","roles":["user","author"],"hash":"sha256","salt":"cy5CkPrplcEze6o4psURhw==","pwd":"Gc8ozlxjEGITkS+fW9tz2xLROzws44s04EOCWuP04qE=","tags":["wichtig","th"],"bookmarks":["th"],},
// admin,oZuse1 // admin,oZuse1
{"_id":"admin","name":"Test Admin","type":"admin","roles":["user","author","admin"],"hash":"sha256","salt":"PoIUHbDp7tP34ji31iQ0zw==","pwd":"mJpIfIksYs6LJJwYRBemFKqR6TacsSl2E0ZtpO1GMuk=","tags":["wichtig","th"],"bookmarks":["wichtig"],}, {"_id":"admin","name":"Test Admin","type":"admin","roles":["user","author","admin"],"hash":"sha256","salt":"PoIUHbDp7tP34ji31iQ0zw==","pwd":"mJpIfIksYs6LJJwYRBemFKqR6TacsSl2E0ZtpO1GMuk=","tags":["wichtig","th"],"bookmarks":["wichtig"],},
]) ])


db.users.insert({"name":"mustermannmax", "roles":["user"],"abos":[],"bookmarks":[]})

//var c = require('./server/crypto.js'), //var c = require('./server/crypto.js'),
// e = {"_id":"writer","hash":"sha256"}; // e = {"_id":"writer","hash":"sha256"};
//c.fillLocalAuth(e,"pwd"); //c.fillLocalAuth(e,"pwd");

+ 2
- 0
public/routes/auth.js View File

auth.mail = ''; // Full mail address auth.mail = ''; // Full mail address
auth.roles = {}; // Role authorizations e.g. {user: true} auth.roles = {}; // Role authorizations e.g. {user: true}
auth.gender = ''; // Gender (e.g. Frau) auth.gender = ''; // Gender (e.g. Frau)
auth.bookmarks = []; // Ids of bookmarks
auth.abos = []; // Abos
} }
clearAuthState(); clearAuthState();



+ 49
- 101
public/routes/bookmark.js View File

const BookmarkRouter = { const BookmarkRouter = {
template: ` template: `
<div class="content"> <div class="content">
<div class="is-hidden-desktop">
<div class="bookmark-headline"><h4>Gepeicherte Beiträge</h4></div>
<div class="om-card card">
<h6 class="msg-head">
<b>Betreff</b>
<img src="favicon.ico" width=20px height=20px>
</h6>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. <br><br>
<a href="#">#beispiel #tag</a>
<div class="om-card-footer">
<div class="om-user-line">
<i class="material-icons">account_circle</i>
Erstellt von User 1
</div>
<i class="material-icons" @click="myFilter" v-if="!isActive">bookmark_border</i>
<i class="material-icons" @click="myFilter" v-else="isActive">bookmark</i>
</div>
</div>
<div class="om-card card">
<h6 class="msg-head">
<b>Betreff</b>
<img src="favicon.ico" width=20px height=20px>
</h6>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. <br><br>
<a href="#">#beispiel #tag</a>
<div class="om-card-footer">
<div class="om-user-line">
<i class="material-icons">account_circle</i>
Erstellt von User 2
</div>
<i class="material-icons" @click="myFilter" v-if="!isActive">bookmark_border</i>
<i class="material-icons" @click="myFilter" v-else="isActive">bookmark</i>
</div>
</div>
<div class="om-card card">
<h6 class="msg-head">
<b>Betreff</b>
<img src="favicon.ico" width=20px height=20px>
</h6>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. <br><br>
<a href="#">#beispiel #tag</a>
<div class="om-card-footer">
<div class="om-user-line">
<i class="material-icons">account_circle</i>
Erstellt von User 3
</div>
<i class="material-icons" @click="myFilter" v-if="!isActive">bookmark_border</i>
<i class="material-icons" @click="myFilter" v-else="isActive">bookmark</i>
</div>
</div>
<div id="om-msg-cards" class="is-hidden-desktop">
<MsgCard
v-for="id in msgDisplay"
:key="id"
:msg="messages[id] || {}"
></MsgCard>
</div> </div>



<div class="column is-three-quarters is-pulled-right is-hidden-touch content-desktop"> <div class="column is-three-quarters is-pulled-right is-hidden-touch content-desktop">
<div class="bookmark-headline"><h4>Gepeicherte Beiträge</h4></div>
<div class="om-card card">
<h6 class="msg-head">
<b>Betreff</b>
<img src="favicon.ico" width=20px height=20px>
</h6>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. <br><br>
<a href="#">#beispiel #tag</a>
<div class="om-card-footer">
<div class="om-user-line">
<i class="material-icons">account_circle</i>
Erstellt von User 1
</div>
<i class="material-icons" @click="myFilter" v-if="!isActive">bookmark_border</i>
<i class="material-icons" @click="myFilter" v-else="isActive">bookmark</i>
</div>
</div>
<div class="om-card card">
<h6 class="msg-head">
<b>Betreff</b>
<img src="favicon.ico" width=20px height=20px>
</h6>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. <br><br>
<a href="#">#beispiel #tag</a>
<div class="om-card-footer">
<div class="om-user-line">
<i class="material-icons">account_circle</i>
Erstellt von User 2
</div>
<i class="material-icons" @click="myFilter" v-if="!isActive">bookmark_border</i>
<i class="material-icons" @click="myFilter" v-else="isActive">bookmark</i>
</div>
</div>
<div class="om-card card">
<h6 class="msg-head">
<b>Betreff</b>
<img src="favicon.ico" width=20px height=20px>
</h6>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. <br><br>
<a href="#">#beispiel #tag</a>
<div class="om-card-footer">
<div class="om-user-line">
<i class="material-icons">account_circle</i>
Erstellt von User 3
</div>
<i class="material-icons" @click="myFilter" v-if="!isActive">bookmark_border</i>
<i class="material-icons" @click="myFilter" v-else="isActive">bookmark</i>
</div>
</div>

</div> </div>
</div>`, </div>`,


data: function () { data: function () {
return { return {
messagelist: _messagelist,
messages: _messages, messages: _messages,
isActive: false,
isActive: true,
messageId: '',
}
},
computed:{
msgDisplay: function(){
return (this.messagelist.slice().reverse());
} }
}, },
methods: { methods: {
myFilter () {
myFilter: function() {
this.isActive = !this.isActive; this.isActive = !this.isActive;
},
getMessageId: function(){

},
getMessages: function(){
var _usrid = "5d360f06b8f3ce67d12ccc92";
$.ajax({
url: "api/usr/id/" + _usrid,
method: "GET"
})
.done(have_result).fail(have_error);

function have_result(res) {
var str = JSON.stringify(res[0].bookmarks).split(",");
_messagelist.splice(0, _messagelist.length);
for (var e in str) {
if (!_messages[str[e]]) {
str[e] =str[e].replace(/\[/g, '').replace(/\"/g, '').replace(/\]/g, '')
get_insert_message(str[e]);
}
}
_messagelist.push.apply(_messagelist, str);
//console.log("bookmark str: "+str);
}

function have_error(err) {
console.log("error: " + err.responseText);
console.log(err);
}
} }
}, },
mounted: function (){
this.getMessages();
}
}; };

+ 29
- 3
public/routes/msgCard.js View File

template: `<div class="om-card card"> template: `<div class="om-card card">
<h6 class="msg-head"> <h6 class="msg-head">
<b>{{ msg.subject }}</b> <b>{{ msg.subject }}</b>
<div id="msgid">{{msg._id}}</div>
<img src="favicon.ico" width=20px height=20px> <img src="favicon.ico" width=20px height=20px>
</h6> </h6>
{{ msg.message }}<br> {{ msg.message }}<br>
<div class="om-card-footer"> <div class="om-user-line"> <div class="om-card-footer"> <div class="om-user-line">
<i class="material-icons">account_circle</i> <i class="material-icons">account_circle</i>
Erstellt von {{ msg.user }}</div> Erstellt von {{ msg.user }}</div>
<i class="material-icons" @click="myFilter()" v-if="!isActive">bookmark_border</i>
<i class="material-icons" @click="myFilter()" v-else="isActive">bookmark</i>
<i class="material-icons" @click="myFilter(msg._id)" v-if="!isActive">bookmark_border</i>
<i class="material-icons" @click="myFilter(msg._id)" v-else="isActive">bookmark</i>
</div></div>`, </div></div>`,
props: ['msg'], props: ['msg'],


}, },


methods: { methods: {
myFilter: function() {
myFilter: function(_messageid) {
//var _messageid = $("#msgid").text();

var _bookmark = auth.bookmarks;
_bookmark.push(_messageid);
console.log("msgid"+_bookmark);
var _userid = "5d360f06b8f3ce67d12ccc92";
this.isActive = !this.isActive; this.isActive = !this.isActive;
if(this.isActive){
$.ajax({
url: "api/usr",
data: {
userid: _userid,
bookmark: _bookmark
},
method: "PUT"
}).done(have_result).fail(have_error);

function have_result(res) {
console.log(res);
}

function have_error(err) {
console.log("error: " + err.responseText);
console.log(err);
}
}
}, },
filterForTag: function(tag) { filterForTag: function(tag) {
console.log("link: " +tag); console.log("link: " +tag);

+ 219
- 114
server/dbs.js View File

* -> Array of message schema object ids * -> Array of message schema object ids
* Get ALL known message ids * Get ALL known message ids
*/ */
get: function(req, res) {
model.Messages.find({},{_id: true}).exec()
get: function (req, res) {
model.Messages.find({}, {
_id: true
}).exec()
.then(results => { .then(results => {
//selects id from message:
var parsed = [];
for (var i in results) {
parsed.push (results[i]._id);
}
res.send(parsed);
} )
.catch(err => {
console.log (err);
res.status(500).json(err);
});
//selects id from message:
var parsed = [];
for (var i in results) {
parsed.push(results[i]._id);
}
res.send(parsed);
})
.catch(err => {
console.log(err);
res.status(500).json(err);
});
}, },
}, },
"msg/id": { "msg/id": {
* -> Message schema * -> Message schema
* Get a particular message * Get a particular message
*/ */
get: function(req, res) {
model.Messages.findById(req.params.id) .exec(function(err, result) {
get: function (req, res) {
model.Messages.findById(req.params.id).exec(function (err, result) {
if (err) { if (err) {
console.log (err);
console.log(err);
res.status(404).json(err); res.status(404).json(err);
} else { } else {
//console.log(JSON.stringify(result)); //console.log(JSON.stringify(result));
* -> Message schema * -> Message schema
* Get a particular message * Get a particular message
*/ */
get: function(req, res) {
model.Messages.find(
{$text: {$search: req.params.phrase}})
.sort({tag:-1})
.exec().then(results => {
//selects id from message:
var parsed = [];
for (var i in results) {
parsed.push (results[i]._id);
}
res.send(parsed);
} )
.catch(err => {
console.log (err);
res.status(500).json(err);
});
get: function (req, res) {
model.Messages.find({
$text: {
$search: req.params.phrase
}
})
.sort({
tag: -1
})
.exec().then(results => {
//selects id from message:
var parsed = [];
for (var i in results) {
parsed.push(results[i]._id);
}
res.send(parsed);
})
.catch(err => {
console.log(err);
res.status(500).json(err);
});
},
},

"usr/bookmark/ids": {
/* GET /api/usr/bookmark/ids [no args]
* -> Array of message schema object ids
* Get bookmark message ids
*/

get: function (req, res) {

model.Users.find({

},
function (err, result) {
if (err) {
return res.status(401).json(err.message);
} else {
var parsed = [];
for (var i in result) {
parsed.push(result[i].bookmarks);
}
console.log("bookmarks:" + JSON.stringify(parsed));
res.send(parsed);
}
if (result == null) {
return res.status(500).json("Can save bookmark.")
}
});
}, },
}, },
"msg": { "msg": {
* -> Message schema * -> Message schema
* Create a new message * Create a new message
*/ */
post: function(req, res) {
post: function (req, res) {
/* /*
if ( !(req.body.tags instanceof Array) ) { if ( !(req.body.tags instanceof Array) ) {
return res.status(400).json({ error: "bad request" }); return res.status(400).json({ error: "bad request" });
message: req.body.message, message: req.body.message,
user: req.body.user, user: req.body.user,
tag: req.body.tag tag: req.body.tag
}, function(err, result) {
}, function (err, result) {
if (err) { if (err) {
return res.status(401).json(err.message); return res.status(401).json(err.message);
} else { } else {
res.json({message: "Message created!!"});
res.json({
message: "Message created!!"
});
} }
if (result == null) { if (result == null) {
return res.status(500).json("Can not create message.") return res.status(500).json("Can not create message.")
* -> Array of tag schema object ids * -> Array of tag schema object ids
* Get ALL known tag ids * Get ALL known tag ids
*/ */
get: function(req, res) {
model.Tags.find({},{_id: true}).exec()
get: function (req, res) {
model.Tags.find({}, {
_id: true
}).exec()
.then(results => { .then(results => {
//selects id from tag:
var parsed = [];
for (var i in results) {
parsed.push (results[i]._id);
}
res.send(parsed);
} )
.catch(err => {
console.log (err);
res.status(500).json(err);
});
//selects id from tag:
var parsed = [];
for (var i in results) {
parsed.push(results[i]._id);
}
res.send(parsed);
})
.catch(err => {
console.log(err);
res.status(500).json(err);
});
}, },
}, },
"tag/id": { "tag/id": {
* -> Tag schema * -> Tag schema
* Get a particular tag * Get a particular tag
*/ */
get: function(req, res) {
get: function (req, res) {
model.Tags.findById(req.params.id) model.Tags.findById(req.params.id)
.exec(function(err, result) {
if (err) {
console.log (err);
res.status(404).json(err);
} else {
//console.log(JSON.stringify(result));
res.json(result);
}
});
.exec(function (err, result) {
if (err) {
console.log(err);
res.status(404).json(err);
} else {
//console.log(req.params.id);
res.json(result);
}
});
}, },
}, },
"usr/id": { "usr/id": {
* -> User Schema * -> User Schema
* Get a particular user * Get a particular user
*/ */
get: function(req, res) {
model.Users.findById(req.params.id)
.exec(function(err, result) {
if (err) {
console.log(err);
res.status(404).json(err);
} else {
//console.log(JSON.stringify(result));
res.json(result);
}
});
get: function (req, res) {
model.Users.find({
_id: req.params.id
})
.exec(function (err, result) {
if (err) {
console.log(err);
res.status(404).json(err);
} else {
console.log(result);
res.json(result);
}
});
}, },
}, },
"usr": { "usr": {
* -> User schema * -> User schema
* Create a new user * Create a new user
*/ */
post: function(req, res) {
model.User.create({
post: function (req, res) {
model.Users.create({
_id: req.body.id, _id: req.body.id,
name: req.body.name, name: req.body.name,
type: req.body.type, type: req.body.type,
pwd: req.body.pwd, pwd: req.body.pwd,
abos: req.body.abos, abos: req.body.abos,
bookmarks: req.body.bookmarks, bookmarks: req.body.bookmarks,
}, function(err, result) {
}, function (err, result) {
if (err) { if (err) {
console.log (err);
console.log(err);
return res.status(401).json(err.message); return res.status(401).json(err.message);
} else { } else {
res.json({message: "User created!!"});
res.json({
message: "User created!!"
});
} }
if (result == null) { if (result == null) {
return res.status(500).json("Can not create user.") return res.status(500).json("Can not create user.")
* -> User schema * -> User schema
* Change a user * Change a user
*/ */
put: function(req, res) {
model.Users.findById(reg.body._id)
.exec(function(err, entry) {
if (err)
console.log (err);
if (entry == null)
return res.status(404).json(err.message);
// TODO Catch edge cases
entry.save(function(err, data) {
put: function (req, res) {

model.Users.find({
_id: req.body.userid
})
.exec(function (err, entry) {
if (err) {
console.log(err);
} else {

model.Users.update({
_id: req.body.userid
}, {
bookmarks: req.body.bookmark
}, function (err, data) {

if (err) {
console.log(err);
} else {
res.json({
id: "Bookmark saved"
});
}
if (data == null) {
return res.status(404).json(err.message);
}

});

}

if (entry == null)
return res.status(404).json(err.message);
// TODO Catch edge cases

// TODO Save/Update user // TODO Save/Update user
/* /*
_id: req.body.id, _id: req.body.id,
abos: req.body.abos, abos: req.body.abos,
bookmarks: req.body.bookmarks, bookmarks: req.body.bookmarks,
*/ */
res.json(data);
// res.json(data);
}); });
});
}, },
}, },
}, },
* Local db: common.config.dbLocalConn * Local db: common.config.dbLocalConn
* TH db: common.config.dbConn * TH db: common.config.dbConn
*/ */
common.mongoose.connect (common.config.dbLocalConn, {
common.mongoose.connect(common.config.dbLocalConn, {
useNewUrlParser: true useNewUrlParser: true
}).then(() => { }).then(() => {
console.log("Database connected successfully."); console.log("Database connected successfully.");
* - per entry: _comment: Comment for Admin UI - TODO: not working yet * - per entry: _comment: Comment for Admin UI - TODO: not working yet
*/ */
var messageSchema = common.mongoose.Schema({ var messageSchema = common.mongoose.Schema({
subject: { type: String, required: true,
_comment: "" },
message: { type: String, required: true,
_comment: "" },
user: { type: String, required: true,
_comment: "" },
tag: { type: [String],
_comment: "" },
subject: {
type: String,
required: true,
_comment: ""
},
message: {
type: String,
required: true,
_comment: ""
},
user: {
type: String,
required: true,
_comment: ""
},
tag: {
type: [String],
_comment: ""
},
//createtime: { type: Date, default: Date.now }, //createtime: { type: Date, default: Date.now },
}); });
messageSchema.index({ "$**":'text' });
messageSchema.index({
"$**": 'text'
});
model.Messages = common.mongoose.model('messages', messageSchema); model.Messages = common.mongoose.model('messages', messageSchema);
model.Messages._list = [ "" ];
model.Messages._list = [""];


var tagSchema = common.mongoose.Schema({ var tagSchema = common.mongoose.Schema({
name: { type: String, required: true,
_comment: "" }, //unique
name: {
type: String,
required: true,
_comment: ""
}, //unique
}); });
model.Tags = common.mongoose.model('tags', messageSchema); model.Tags = common.mongoose.model('tags', messageSchema);
model.Tags._list = [ "" ];
model.Tags._list = [""];


var userSchema = common.mongoose.Schema({ var userSchema = common.mongoose.Schema({
_id: { type: String },
name: { type: String, required: true,
_comment: "" },
pwd: { type: String,
_comment: "" },
hash: { type: String },
salt: { type: String },
type: { type: String },
roles: { type: [String], required: true,
_comment: "" },
abos: { type: [String],
_comment: "" },
/*_id: {
type: String
},*/
name: {
type: String,
required: true,
_comment: ""
},
pwd: {
type: String,
_comment: ""
},
hash: {
type: String
},
salt: {
type: String
},
type: {
type: String
},
roles: {
type: [String],
required: true,
_comment: ""
},
abos: {
type: [String],
_comment: ""
},
//deactivated: { type: Boolean }, //deactivated: { type: Boolean },
//host: { type: Boolean }, //host: { type: Boolean },
bookmarks: { type: [String],
_comment: "" },
bookmarks: {
type: [String],
_comment: ""
},
}); });
model.Users = common.mongoose.model('users', userSchema); model.Users = common.mongoose.model('users', userSchema);
model.Users._list = [ "" ];
model.Users._list = [""];
}, },
models: model, models: model,
}; };

Loading…
Cancel
Save