Skip to content
Snippets Groups Projects

Bf3 feed anzeigen

Merged Amelie Brucker requested to merge BF3-feed-anzeigen into develop
6 files
+ 151
76
Compare changes
  • Side-by-side
  • Inline
Files
6
+ 41
22
const userService = require('../User/Services/userService');
//TODO: tagLikeDislikeArray in req contains tagName, tlr, neutralValue, totalMemes, likes and dislikes as value
//tagLikeDislikeArray in req contains tagName, totalMemes, likes and dislikes as value
//sets an array called tlrArray with {tagId, tagName, neutralValue, tlr}
function setTLRForAllTags (req, res, next) {
let tags = [];
function setTLRForAllTags(req, res, next) {
if (!req.body.tagLikeDislikeArray){
return res.status(400).json({ error: 'No tagLikeDislikeArray'});
}
let tagLikeDislikeArray = req.body.tagLikeDislikeArray;
let likeRatio;
let newTlr;
for (let tag of req.body.tagLikeDislikeArray){
likeRatio = tag.neutralValue + (tag.likes/tag.totalMemes)*0.5 - (tag.dislikes/tag.totalMemes)*0.5;
if (likeRatio>1){
likeRatio = 1;
} else if (likeRatio <0){
likeRatio = 0;
userService.getRatedTagsOfUserById(req.tokenUserId, (err, tagarray) => {
if (err || !tagarray) {
return res.status(500).json({ error: 'Missing db response while getting tags of User'});
}
newTlr = 0.8 * tag.tlr + 0.2 * likeRatio;
tags.push({tagName: tag.tagName, neutralValue: tag.neutralValue, tlr: newTlr});
}
req.body.ratedTags = tags;
next();
for (let tag of tagLikeDislikeArray) {
let tagInTagArray = tagarray.find(element => element.tagName == tag.tagName);
if (tagInTagArray) {
let index = tagarray.indexOf(tagInTagArray);
likeRatio = tagInTagArray.neutralValue + (tag.likes / tag.totalMemes) * 0.5 - (tag.dislikes / tag.totalMemes) * 0.5;
if (likeRatio > 1) {
likeRatio = 1;
} else if (likeRatio < 0) {
likeRatio = 0;
}
newTlr = 0.8 * tagInTagArray.tlr + 0.2 * likeRatio;
tagInTagArray.tlr = newTlr;
tagarray[index] = tagInTagArray;
} else {
//in case frontend provides tags, which are missing in user object, they are added
//should not happend regulary
likeRatio = 0.5 + (tag.likes / tag.totalMemes) * 0.5 - (tag.dislikes / tag.totalMemes) * 0.5;
newTlr = 0.4 + 0.2 * likeRatio;
tagarray.push({tagName: tag.tagName, neutralValue: 0.5, tlr: newTlr})
}
}
req.body.ratedTags = tagarray;
next();
})
}
//AS IN USERMODEL PROVIDED: ratedTags in req contains tagName and tlr
//sets an array called tlrs with tagId and the "modified for algorithm"-tlr: {tagId, tlr}
//array contains all liked tags (not all tags!)
function setAlgorithmTLRForAllTags(req, res, next) {
if (!req.tokenUserId){
if (!req.tokenUserId) {
return next();
}
userService.getAlgorithmDataById(eq.tokenUserId, (err, values)=>{
if (err){
userService.getAlgorithmDataById(req.tokenUserId, (err, values) => {
if (err) {
return next();
}
let likedTags = [];
let weightedValue = 0;
for (let tag of values.ratedTags){
if (tag.tlr > 0.5){
for (let tag of values.ratedTags) {
if (tag.tlr > 0.5) {
weightedValue = tag.tlr - 0.5;
likedTags.push({tagName: tag.tagName, tlr: weightedValue});
likedTags.push({ tagName: tag.tagName, tlr: weightedValue });
}
}
req.values = ({tlrs: likedTags, followedUsers: values.following, seenMemes: values.seenMemes});
req.values = ({ tlrs: likedTags, followedUsers: values.following, seenMemes: values.seenMemes });
next();
})
})
}
module.exports = {
Loading