From aa0598fb57bbc1b36f45710e4439ddc53999d4c9 Mon Sep 17 00:00:00 2001
From: GitKrakenOrange <pk.kuechenmeister@gmail.com>
Date: Thu, 21 Jan 2021 19:46:02 +0100
Subject: [PATCH 1/3] B19: auto Kommentar Entfernung

Was:
~ Kommenatre werden nach dem dritten Report inactive gesetzt
---
 Meme/Services/commentsService.js | 42 +++++++++++++++++++++++---------
 1 file changed, 30 insertions(+), 12 deletions(-)

diff --git a/Meme/Services/commentsService.js b/Meme/Services/commentsService.js
index 2475e05..6fa833e 100644
--- a/Meme/Services/commentsService.js
+++ b/Meme/Services/commentsService.js
@@ -62,18 +62,36 @@ function reportComment(mId, cId, uId, callback){
             console.error("Problem occurred during MemeModel access");
             callback(err);
         } else {
-            memeModel.findOneAndUpdate(
-                { _id: mId, active: true, "comments._id": cId },
-                {   
-                    $addToSet: { 
-                        "comments.$.reports": uId 
-                    }   
-                },
-                { projection: { comments: 1 }, new: true }
-            )
-            .then((comment) => {
-                console.log('Reported Comment');
-                callback(null, comment);
+            memeModel.findOne({ _id: mId, active: true }, function(err, meme){
+                if(err){
+                    callback(err)
+                } else {
+
+                    let comments = meme.comments;
+                    for(let comment of comments){
+                        if(comment._id == cId){
+                            let reports = comment.reports;
+                            if(reports.indexOf(uId) === -1){
+                                reports.push(uId);
+
+                                if(reports.length >= 3){
+                                    comment.active = false;
+                                    console.log('Comment set inactive because of too many reports');
+                                }
+
+                                meme.save(function(err, meme){
+                                    if(err) {
+                                        callback(err);
+                                    } else {
+                                        console.log('Reported Comment');
+                                        callback(null, comment);
+                                    }
+                                });
+                            }
+                        }
+                        break;
+                    }
+                }
             });
         }
     });
-- 
GitLab


From 2800e452dc011e0065c2ee52a6081b6afced0054 Mon Sep 17 00:00:00 2001
From: GitKrakenOrange <pk.kuechenmeister@gmail.com>
Date: Thu, 21 Jan 2021 23:36:38 +0100
Subject: [PATCH 2/3] B19:  Fix bereits reportet
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Was:
~ Wenn bereits reportet wird trotzdem geantwortet
~ Rückgabeobjekt ist gefiltert
---
 Meme/Services/commentsService.js | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/Meme/Services/commentsService.js b/Meme/Services/commentsService.js
index 6fa833e..1a9e4fc 100644
--- a/Meme/Services/commentsService.js
+++ b/Meme/Services/commentsService.js
@@ -68,8 +68,10 @@ function reportComment(mId, cId, uId, callback){
                 } else {
 
                     let comments = meme.comments;
+                    let foundComment;
                     for(let comment of comments){
                         if(comment._id == cId){
+                            foundComment = comment;
                             let reports = comment.reports;
                             if(reports.indexOf(uId) === -1){
                                 reports.push(uId);
@@ -78,19 +80,20 @@ function reportComment(mId, cId, uId, callback){
                                     comment.active = false;
                                     console.log('Comment set inactive because of too many reports');
                                 }
-
-                                meme.save(function(err, meme){
-                                    if(err) {
-                                        callback(err);
-                                    } else {
-                                        console.log('Reported Comment');
-                                        callback(null, comment);
-                                    }
-                                });
                             }
-                        }
-                        break;
+                            break;
+                        }                       
                     }
+                    meme.save(function(err, meme){
+                        if(err) {
+                            callback(err);
+                        } else {
+                            console.log('Reported Comment');
+                            foundComment = foundComment.toObject();
+                            let { active, reports, datePublished, ...foundCommentFiltered } = foundComment;
+                            callback(null, foundCommentFiltered);
+                        }
+                    });
                 }
             });
         }
-- 
GitLab


From b1a46bc4ae382116c85be27d0a3d4ad19ceea572 Mon Sep 17 00:00:00 2001
From: GitKrakenOrange <pk.kuechenmeister@gmail.com>
Date: Fri, 22 Jan 2021 00:05:08 +0100
Subject: [PATCH 3/3] B19: final fixes

Was:
~ wenn bereits reportet / inactive passende responses
---
 Meme/Services/commentsService.js | 60 ++++++++++++++++++++------------
 1 file changed, 37 insertions(+), 23 deletions(-)

diff --git a/Meme/Services/commentsService.js b/Meme/Services/commentsService.js
index 1a9e4fc..47d64d3 100644
--- a/Meme/Services/commentsService.js
+++ b/Meme/Services/commentsService.js
@@ -67,33 +67,47 @@ function reportComment(mId, cId, uId, callback){
                     callback(err)
                 } else {
 
-                    let comments = meme.comments;
-                    let foundComment;
-                    for(let comment of comments){
-                        if(comment._id == cId){
-                            foundComment = comment;
-                            let reports = comment.reports;
-                            if(reports.indexOf(uId) === -1){
-                                reports.push(uId);
+                    if(!meme){
+                        callback("No meme found");
+                    } else {
 
-                                if(reports.length >= 3){
-                                    comment.active = false;
-                                    console.log('Comment set inactive because of too many reports');
+                        let error = "Comment already reported";
+                        let comments = meme.comments;
+                        let foundComment;
+                        for(let comment of comments){
+                            if(comment._id == cId && comment.active){
+                                foundComment = comment;
+                                let reports = comment.reports;
+                                if(reports.indexOf(uId) === -1){
+                                    error = null;
+                                    reports.push(uId);
+
+                                    if(reports.length >= 3){
+                                        comment.active = false;
+                                        console.log('Comment set inactive because of too many reports');
+                                    }
                                 }
-                            }
-                            break;
-                        }                       
-                    }
-                    meme.save(function(err, meme){
-                        if(err) {
-                            callback(err);
+                                break;
+                            }                       
+                        }
+                        if(!foundComment){
+                            callback("No Comment found");
                         } else {
-                            console.log('Reported Comment');
-                            foundComment = foundComment.toObject();
-                            let { active, reports, datePublished, ...foundCommentFiltered } = foundComment;
-                            callback(null, foundCommentFiltered);
+                            meme.save(function(err, meme){
+                                if(err) {
+                                    callback(err);
+                                } else {
+                                    if(!error){
+                                        console.log('Reported Comment');
+                                    }
+                                    
+                                    foundComment = foundComment.toObject();
+                                    let { active, reports, datePublished, ...foundCommentFiltered } = foundComment;
+                                    callback(error, foundCommentFiltered);
+                                }
+                            });
                         }
-                    });
+                    }
                 }
             });
         }
-- 
GitLab