Untitled

mail@pastecode.io avatar
unknown
plain_text
5 days ago
92 kB
3
Indexable
Never
/** @format */

require("dotenv").config();
const express = require("express");
const router = express.Router();
const Multer = require("multer");
const uuid = require("uuid");
const uuidv1 = uuid.v1;
const { Storage } = require("@google-cloud/storage");
const { default: mongoose } = require("mongoose");
const { config } = require("dotenv");
const sharp = require("sharp");
const Aerospike = require("aerospike");
const { getAerospikeClient } = require("../../../../databases/aerospike");
const maps = Aerospike.maps;
const { removeStopwords, eng, fra } = require("stopword");
const now = require("nano-time");
const createError = require("http-errors");
const {
  blockFetchFullPost,
  fetchBlockPost,
  fetchBlockSubScriberPost,
  blockPostLike,
  blockPinnedPost,
  blockPostDelete,
  blockPostEdit,
  blockPostSpam,
  blockPostDonate,
  fetchAllLikeUsers,
  createNewBlockPost,
  fetchBlockFeedPosts,
  fetchEventFeedPosts,
  handleReportPost,
  handleGetPostAnalytics,
  handleBookmarkPost,
  handleFetchFullPostInModal,
  handleAddPostLike,
  handleRemovePostLike,
  handleUpvoteNewsPost,
  handleDownvoteNewsPost,
  handleFakeNewsPost,
  handleFlagPost,
  handleExicitedAnnouncementPost,
  handleDisapointedAnnouncementPost,
  handleMisLeadingAnnouncementPost,
  handleUpvoteUsers,
  handleExcitedUsers,
  handleSharePost,
  handleFetchEarning,
  handleFetchDonatedUsers,
  handleRepostPost,
  handleCreateOrUpdateBlockPostBoost,
  handleGetBlockPostBoost,
  handleFlaggedUsersList,
} = require("../../../model/post/blockPost/blockPostModel");

const {
  createBlockPostComment,
  fetchAllBlockComments,
  fetchSingleBlockCommentMeta,
  fetchMyBlockComments,
  blockLikeComment,
  // blockRemoveLikeComment,
  blockDeleteComment,
  editBlockComment,
  repostBlockComment,
  //for replies
  createBlockReply,
  createBlockReplyReply,
  getReplies,
  hideCommentReplyModel,
  deleteBlockReply,
  editBlockReply,
  spamBlockReply,
  likeBlockReply,
  dislikeBlockReply,
  handlePinUnpinComment,
  fetchBlockCommentsMostReactions,
  handleRepostWithQuote,
  spamComment,
} = require("../../../model/comment/blockComment/blockPostCommentModel.js");

// const {
//   createBlockReply,
//   createBlockReplyReply,
//   getReplyes,
//   hideCommentReplyModel,
//   deleteBlockReply,
//   editBlockReply,
//   spamBlockReply,
//   likeBlockReply,
//   dislikeBlockReply,
// } = require("../../../model/reply/blockReply/blockPostReply");

const {
  createBlockEvent,
  addEventInterest,
  addEventNotInterest,
  addEventJoin,
  fetchAllEvents,
  fetchSingleEvent,
  blockEventDelete,
  blockEventJoin,
  hideEvent,
  handleReportEvent,
} = require("../../../model/post/blockEvent/blockEventModel");
const createHttpError = require("http-errors");

class BlockPostController {
  constructor() {}

  async blockFeed(req, res, next) {
    try {
      const page = req.query.page || 1;
      const limit = req.query.limit || 10;

      const data = await fetchBlockFeedPosts(page, limit);
      return res.status(200).json(data);
    } catch (error) {
      next(error);
    }
  }

  async eventFeed(req, res, next) {
    try {
      const page = req.query.page || 1;
      const limit = req.query.limit || 10;

      const data = await fetchEventFeedPosts(page, limit);
      return res.status(200).json(data);
    } catch (error) {
      next(error);
    }
  }

  // fetch group posts
  async fetchPosts(req, res) {
    console.log("CALL block post");
    const client = await getAerospikeClient();
    if (!req.params.id) {
      return res.status(401).json({ msg: "Group id is not present" });
    } else {
      const page = Number(req.query.page) || 0;
      const limit = Number(req.query.limit) || 10;
      const data = await fetchBlockPost(
        req.params.id,
        page,
        limit,
        req.query.sortedBy
      );
      return res.status(200).json(data);
    }
  }

  async fetchSubPosts(req, res) {
    console.log("CALL");
    const client = await getAerospikeClient();
    if (!req.params.id) {
      return res.status(401).json({ msg: "Group id is not present" });
    } else {
      var page = req.query.page || 0;
      var limit = req.query.limit || 10;
      const data = await fetchBlockSubScriberPost(
        req.params.id,
        page,
        limit,
        req.query.sortedBy,
        req.user
      );
      return res.status(200).json(data);
    }
  }

  // Post like ID
  async createPostLike(req, res) {
    const id = req.params.id;
    const client = await getAerospikeClient();
    if (!id) {
      return res.status(400).json({ msg: "Invalid request" });
    } else {
      const post_key = new Aerospike.Key(
        process.env.CLUSTER_NAME,
        process.env.SET_GROUP_POSTS,
        id
      );
      const post_meta_key = new Aerospike.Key(
        process.env.CLUSTER_NAME,
        process.env.SET_GROUP_POST_META,
        id
      );

      const post_feed_key = new Aerospike.Key(
        process.env.CLUSTER_NAME,
        process.env.SET_POSTS,
        id
      );

      const post = await client.get(post_key);
      if (post.bins.l_c > 0) {
        console.log("Like count greater than 0");
        const meta = await client.get(post_meta_key);
        if (meta.bins.likes.includes(req.user.handleUn)) {
          console.log("Already like");
          const ops = [
            Aerospike.lists.removeByValue("likes", req.user.handleUn),
          ];
          client.operate(post_meta_key, ops, (err, result) => {
            if (err) {
              return res.status(400).json({ msg: err.message });
            } else {
              const ops = [Aerospike.operations.incr("l_c", -1)];
              client.operate(post_key, ops, async (err, result) => {
                if (err) {
                  return res.status(400).json({ msg: err.message });
                } else {
                  if (meta.bins.likes.length === 1) {
                    const ops = [Aerospike.operations.incr("like", -1)];
                    const data = await client.operate(post_key, ops);
                    return res.status(200).json({ msg: "remove Liked post" });
                  } else {
                    return res.status(200).json({ msg: "remove Liked post" });
                  }
                }
              });
            }
          });
        } else {
          console.log("Not like");
          if (
            meta.bins.haha.includes(req.user.handleUn) ||
            meta.bins.angry.includes(req.user.handleUn) ||
            meta.bins.dislikes.includes(req.user.handleUn)
          ) {
            console.log("Not like Other");
            const ops = [
              Aerospike.lists.removeByValue("haha", req.user.handleUn),
              Aerospike.lists.removeByValue("angry", req.user.handleUn),
              Aerospike.lists.removeByValue("dislikes", req.user.handleUn),
            ];
            client.operate(post_meta_key, ops, (err, result) => {
              if (err) {
                return res.status(400).json({ msg: err.message });
              } else {
                const ops = [
                  Aerospike.lists.append("likes", req.user.handleUn),
                ];
                client.operate(post_meta_key, ops, async (err, result) => {
                  if (err) {
                    return res.status(400).json({ msg: err.message });
                  } else {
                    // return res.status(200).json({ msg: "You liked this post" });
                    if (meta.bins.likes.length === 0) {
                      const ops = [Aerospike.operations.incr("like", 1)];
                      const data = await client.operate(post_key, ops);

                      return res.status(200).json({ msg: "Liked post" });
                    } else {
                      return res.status(200).json({ msg: "Liked post" });
                    }
                  }
                });
              }
            });
          } else {
            const ops = [Aerospike.lists.append("likes", req.user.handleUn)];
            client.operate(post_meta_key, ops, (err, result) => {
              if (err) {
                return res.status(400).json({ msg: err.message });
              } else {
                const ops = [Aerospike.operations.incr("l_c", 1)];
                client.operate(post_key, ops, async (err, result) => {
                  if (err) {
                    return res.status(400).json({ msg: err.message });
                  } else {
                    // return res.status(200).json({ msg: "Liked post" });
                    if (meta.bins.likes.length === 0) {
                      const ops = [Aerospike.operations.incr("like", 1)];
                      const data = await client.operate(post_key, ops);
                      // return res.status(200).json({ msg: "Liked post" });
                      if (post.bins.u_dun === req.user.handleUn) {
                        return res.status(200).json({ msg: "Liked post" });
                      } else {
                        if (req.user.flwr_c > 2) {
                          console.log("Popular");
                          if (post.bins.pop.length <= 2) {
                            const ops = [
                              Aerospike.lists.append("pop", {
                                handleUn: req.user.handleUn,
                                pic: req.user.p_i || "",
                              }),
                            ];
                            const data = await client.operate(post_key, ops);
                            return res.status(200).json({ msg: "Liked post" });
                          } else {
                            return res.status(200).json({ msg: "Liked post" });
                          }
                        } else {
                          console.log("Normal");
                          if (post.bins.ran.length <= 2) {
                            const ops = [
                              Aerospike.lists.append("ran", {
                                handleUn: req.user.handleUn,
                                pic: req.user.p_i || "",
                              }),
                            ];
                            const data = await client.operate(post_key, ops);
                            return res.status(200).json({ msg: "Liked post" });
                          } else {
                            return res.status(200).json({ msg: "Liked post" });
                          }
                        }
                      }
                    } else {
                      // return res.status(200).json({ msg: "Liked post" });
                      if (post.bins.u_dun === req.user.handleUn) {
                        return res.status(200).json({ msg: "Liked post" });
                      } else {
                        if (req.user.flwr_c > 2) {
                          console.log("Popular");
                          if (post.bins.pop.length <= 2) {
                            const ops = [
                              Aerospike.lists.append("pop", {
                                handleUn: req.user.handleUn,
                                pic: req.user.p_i || "",
                              }),
                            ];
                            const data = await client.operate(post_key, ops);
                            return res.status(200).json({ msg: "Liked post" });
                          } else {
                            return res.status(200).json({ msg: "Liked post" });
                          }
                        } else {
                          console.log("Normal ", post.bins);
                          return res.status(200).json({ msg: "Liked post" });
                        }
                      }
                    }
                  }
                });
              }
            });
          }
        }
      } else {
        const ops = [Aerospike.lists.append("likes", req.user.handleUn)];
        client.operate(post_meta_key, ops, async (err, result) => {
          if (err) {
            return res.status(400).json({ msg: err.message });
          } else {
            const ops = [
              Aerospike.operations.incr("l_c", 1),
              Aerospike.operations.incr("like", true),
            ];
            await client.operate(post_feed_key, ops);

            client.operate(post_key, ops, async (err, result) => {
              if (err) {
                return res.status(400).json({ msg: err.message });
              } else {
                // return res.status(200).json({ msg: "Liked post" });
                // ***
                if (post.bins.u_dun === req.user.handleUn) {
                  return res.status(200).json({ msg: "Liked post" });
                } else {
                  if (req.user.flwr_c > 2) {
                    console.log("Popular");
                    if (post.bins.pop.length <= 2) {
                      const ops = [
                        Aerospike.lists.append("pop", {
                          handleUn: req.user.handleUn,
                          pic: req.user.p_i || "",
                        }),
                      ];
                      const data = await client.operate(post_key, ops);
                      return res.status(200).json({ msg: "Liked post" });
                    } else {
                      return res.status(200).json({ msg: "Liked post" });
                    }
                  } else {
                    console.log("Normal");
                    return res.status(200).json({ msg: "Liked post" });
                  }
                }
              }
            });
          }
        });
      }
    }
  }

  async pinnedPost(req, res, next) {
    try {
      if (!req.params.id) {
        return res.status(401).json({ msg: "Invalid request" });
      } else {
        const data = await blockPinnedPost(req.params.id);
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  // view full post
  async viewFullPost(req, res, next) {
    try {
      const data = await blockFetchFullPost(req.params.id);
      return res.status(200).json(data);
    } catch (err) {
      next(err);
    }
  }

  async deletePost(req, res, next) {
    try {
      // const client = await getAerospikeClient();
      if (!req.params.id) {
        return res.status(401).json({ msg: "Invalid request" });
      } else {
        const data = await blockPostDelete(req.params.id);
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async editPost(req, res, next) {
    const client = await getAerospikeClient();
    const data = await blockPostEdit(req.params.id, req.body);
    return res.status(200).json(data);
  }

  async spamPost(req, res, next) {
    const client = await getAerospikeClient();

    if (!req.params.id) {
      return res.status(400).json({ msg: "Invalid request" });
    } else {
      const data = await blockPostSpam(req.params.id, req.user.handleUn);
      return res.status(200).json(data);
    }
  }

  async donatePost(req, res, next) {
    try {
      const client = await getAerospikeClient();
      const id = req.params.id;
      console.log(req.query);
      if (!id) {
        return res.status(400).json({ msg: "Invalid request" });
      } else {
        const data = await blockPostDonate(id, req.query, req.user);
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async fetchFlaggedUsersList(req, res, next) {
    if (!req.params.id) {
      return res.status(400).json({ msg: "Invalid parameter" });
    } else {
      const data = await handleFlaggedUsersList(
        req.params.id,
        req.query.postType
      );
      return res.status(200).json(data);
    }
  }

  async fetchLikeUser(req, res, next) {
    try {
      console.log("likeType ", req.query.type);
      const data = await fetchAllLikeUsers(
        req.params.id,
        req.query.type,
        req.query.postType
      );
      return res.status(200).json(data);
    } catch (error) {
      next(error);
    }
  }

  // ***************************** //

  // ***************************** //

  async handleDislike(req, res, next) {
    try {
      const id = req.params.id;
      const client = await getAerospikeClient();
      if (!id) {
        return res.status(400).json({ msg: "Invalid request" });
      } else {
        const post_key = new Aerospike.Key(
          process.env.CLUSTER_NAME,
          process.env.SET_GROUP_POSTS,
          id
        );
        const post_meta_key = new Aerospike.Key(
          process.env.CLUSTER_NAME,
          process.env.SET_GROUP_POST_META,
          id
        );
        const post_feed_key = new Aerospike.Key(
          process.env.CLUSTER_NAME,
          process.env.SET_POSTS,
          id
        );

        const post = await client.get(post_key);
        const meta = await client.get(post_meta_key);
        if (post.bins.l_c > 0) {
          const meta = await client.get(post_meta_key);
          if (meta.bins.dislikes.includes(req.user.handleUn)) {
            const ops = [
              Aerospike.lists.removeByValue("dislikes", req.user.handleUn),
            ];
            client.operate(post_meta_key, ops, async (err, result) => {
              if (err) {
                return res.status(400).json({ msg: err.message });
              } else {
                const ops = [Aerospike.operations.incr("l_c", -1)];
                await client.operate(post_feed_key, ops);
                client.operate(post_key, ops, async (err, result) => {
                  if (err) {
                    return res.status(400).json({ msg: err.message });
                  } else {
                    // return res.status(200).json({ msg: "Dislikes added" });
                    if (meta.bins.haha.length === 1) {
                      const ops = [Aerospike.operations.incr("haha", -1)];
                      const data = await client.operate(post_key, ops);
                      return res.status(200).json({ msg: "remove haha post" });
                    } else {
                      return res.status(200).json({ msg: "remove haha post" });
                    }
                  }
                });
              }
            });
          } else {
            if (
              meta.bins.likes.includes(req.user.handleUn) ||
              meta.bins.haha.includes(req.user.handleUn) ||
              meta.bins.angry.includes(req.user.handleUn) ||
              meta.bins.dislikes.includes(req.user.handleUn)
            ) {
              const ops = [
                Aerospike.lists.removeByValue("likes", req.user.handleUn),
                Aerospike.lists.removeByValue("haha", req.user.handleUn),
                Aerospike.lists.removeByValue("angry", req.user.handleUn),
                Aerospike.lists.removeByValue("heart", req.user.handleUn),
              ];
              client.operate(post_meta_key, ops, (err, result) => {
                if (err) {
                  return res.status(400).json({ msg: err.message });
                } else {
                  const ops = [
                    Aerospike.lists.append("dislikes", req.user.handleUn),
                  ];
                  client.operate(post_meta_key, ops, async (err, result) => {
                    if (err) {
                      return res.status(400).json({ msg: err.message });
                    } else {
                      // return res.status(200).json({ msg: "Dislike post" });
                      if (meta.bins.dislikes.length === 0) {
                        const ops = [Aerospike.operations.incr("dislike", 1)];
                        const data = await client.operate(post_key, ops);
                        // return res.status(200).json({ msg: "Dislikes added" });

                        if (post.bins.u_dun === req.user.handleUn) {
                          return res.status(200).json({ msg: "Dislike post" });
                        } else {
                          if (req.user.flwr_c > 2) {
                            console.log("Popular");
                            if (post.bins.pop.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("pop", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res
                                .status(200)
                                .json({ msg: "Dislike post" });
                            } else {
                              return res
                                .status(200)
                                .json({ msg: "Dislike post" });
                            }
                          } else {
                            console.log("Normal");
                            if (post.bins.ran.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("ran", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res
                                .status(200)
                                .json({ msg: "Dislike post" });
                            } else {
                              return res
                                .status(200)
                                .json({ msg: "Dislike post" });
                            }
                          }
                        }
                      } else {
                        // return res.status(200).json({ msg: "Dislikes added" });
                        if (post.bins.u_dun === req.user.handleUn) {
                          return res.status(200).json({ msg: "Dislike post" });
                        } else {
                          if (req.user.flwr_c > 2) {
                            console.log("Popular");
                            if (post.bins.pop.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("pop", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res
                                .status(200)
                                .json({ msg: "Dislike post" });
                            } else {
                              return res
                                .status(200)
                                .json({ msg: "Dislike post" });
                            }
                          } else {
                            console.log("Normal");
                            if (post.bins.ran.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("ran", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res
                                .status(200)
                                .json({ msg: "Dislike post" });
                            } else {
                              return res
                                .status(200)
                                .json({ msg: "Dislike post" });
                            }
                          }
                        }
                      }
                    }
                  });
                }
              });
            } else {
              const ops = [
                Aerospike.lists.append("dislikes", req.user.handleUn),
              ];
              client.operate(post_meta_key, ops, async (err, result) => {
                if (err) {
                  return res.status(400).json({ msg: err.message });
                } else {
                  const ops = [Aerospike.operations.incr("l_c", 1)];
                  await client.operate(post_feed_key, ops);
                  client.operate(post_key, ops, async (err, result) => {
                    if (err) {
                      return res.status(400).json({ msg: err.message });
                    } else {
                      // return res.status(200).json({ msg: "Dislikes added" });
                      if (meta.bins.dislikes.length === 0) {
                        const ops = [Aerospike.operations.incr("dislike", 1)];
                        const data = await client.operate(post_key, ops);
                        return res.status(200).json({ msg: "Dislikes added" });
                      } else {
                        return res.status(200).json({ msg: "Dislikes added" });
                      }
                    }
                  });
                }
              });
            }
          }
        } else {
          const ops = [Aerospike.lists.append("dislikes", req.user.handleUn)];
          client.operate(post_meta_key, ops, async (err, result) => {
            if (err) {
              return res.status(400).json({ msg: err.message });
            } else {
              const ops = [
                Aerospike.operations.incr("l_c", 1),
                Aerospike.operations.incr("dislike", true),
              ];
              await client.operate(post_feed_key, ops);
              client.operate(post_key, ops, async (err, result) => {
                if (err) {
                  return res.status(400).json({ msg: err.message });
                } else {
                  // return res.status(200).json({ msg: "Dislikes added" });
                  if (meta.bins.dislikes.length === 0) {
                    const ops = [Aerospike.operations.incr("dislike", 1)];
                    const data = await client.operate(post_key, ops);
                    // return res.status(200).json({ msg: "Dislikes added" });

                    if (post.bins.u_dun === req.user.handleUn) {
                      return res.status(200).json({ msg: "Dislike post" });
                    } else {
                      if (req.user.flwr_c > 2) {
                        console.log("Popular");
                        if (post.bins.pop.length <= 2) {
                          const ops = [
                            Aerospike.lists.append("pop", {
                              handleUn: req.user.handleUn,
                              pic: req.user.p_i || "",
                            }),
                          ];
                          const data = await client.operate(post_key, ops);
                          return res.status(200).json({ msg: "Dislike post" });
                        } else {
                          return res.status(200).json({ msg: "Dislike post" });
                        }
                      } else {
                        console.log("Normal");
                        if (post.bins.ran.length <= 2) {
                          const ops = [
                            Aerospike.lists.append("ran", {
                              handleUn: req.user.handleUn,
                              pic: req.user.p_i || "",
                            }),
                          ];
                          const data = await client.operate(post_key, ops);
                          return res.status(200).json({ msg: "Dislike post" });
                        } else {
                          return res.status(200).json({ msg: "Dislike post" });
                        }
                      }
                    }
                  } else {
                    // return res.status(200).json({ msg: "Dislikes added" });
                    if (post.bins.u_dun === req.user.handleUn) {
                      return res.status(200).json({ msg: "Liked post" });
                    } else {
                      if (req.user.flwr_c > 2) {
                        console.log("Popular");
                        if (post.bins.pop.length <= 2) {
                          const ops = [
                            Aerospike.lists.append("pop", {
                              handleUn: req.user.handleUn,
                              pic: req.user.p_i || "",
                            }),
                          ];
                          const data = await client.operate(post_key, ops);
                          return res.status(200).json({ msg: "Dislike post" });
                        } else {
                          return res.status(200).json({ msg: "Dislike post" });
                        }
                      } else {
                        console.log("Normal");
                        if (post.bins.ran.length <= 2) {
                          const ops = [
                            Aerospike.lists.append("ran", {
                              handleUn: req.user.handleUn,
                              pic: req.user.p_i || "",
                            }),
                          ];
                          const data = await client.operate(post_key, ops);
                          return res.status(200).json({ msg: "Dislike post" });
                        } else {
                          return res.status(200).json({ msg: "Dislike post" });
                        }
                      }
                    }
                  }
                }
              });
            }
          });
        }
        // const data = await blockPostLike(id, req.user);
        // return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async handleHaha(req, res) {
    try {
      const id = req.params.id;
      const client = await getAerospikeClient();
      console.log(req.params.id);
      if (!id) {
        return res.status(400).json({ msg: "Invalid request" });
      } else {
        const post_key = new Aerospike.Key(
          process.env.CLUSTER_NAME,
          process.env.SET_GROUP_POSTS,
          id
        );
        const post_meta_key = new Aerospike.Key(
          process.env.CLUSTER_NAME,
          process.env.SET_GROUP_POST_META,
          id
        );
        const post_feed_key = new Aerospike.Key(
          process.env.CLUSTER_NAME,
          process.env.SET_POSTS,
          id
        );

        const post = await client.get(post_key);
        if (post.bins.l_c > 0) {
          console.log("Like count greater than 0");
          const meta = await client.get(post_meta_key);
          if (meta.bins.haha.includes(req.user.handleUn)) {
            console.log("Already haha");
            const ops = [
              Aerospike.lists.removeByValue("haha", req.user.handleUn),
            ];
            client.operate(post_meta_key, ops, async (err, result) => {
              if (err) {
                return res.status(400).json({ msg: err.message });
              } else {
                const ops = [Aerospike.operations.incr("l_c", -1)];
                await client.operate(post_feed_key, ops);
                client.operate(post_key, ops, async (err, result) => {
                  if (err) {
                    return res.status(400).json({ msg: err.message });
                  } else {
                    // return res.status(200).json({ msg: "remove haha post" });
                    if (meta.bins.haha.length === 1) {
                      const ops = [Aerospike.operations.incr("haha", -1)];
                      const data = await client.operate(post_key, ops);
                      return res.status(200).json({ msg: "remove haha post" });
                    } else {
                      return res.status(200).json({ msg: "remove haha post" });
                    }
                  }
                });
              }
            });
          } else {
            console.log("Not like");
            if (
              meta.bins.likes.includes(req.user.handleUn) ||
              meta.bins.angry.includes(req.user.handleUn) ||
              meta.bins.dislikes.includes(req.user.handleUn)
            ) {
              console.log("Not like Other");
              const ops = [
                Aerospike.lists.removeByValue("likes", req.user.handleUn),
                Aerospike.lists.removeByValue("angry", req.user.handleUn),
                Aerospike.lists.removeByValue("dislikes", req.user.handleUn),
              ];
              client.operate(post_meta_key, ops, (err, result) => {
                if (err) {
                  return res.status(400).json({ msg: err.message });
                } else {
                  const ops = [
                    Aerospike.lists.append("haha", req.user.handleUn),
                  ];
                  client.operate(post_meta_key, ops, async (err, result) => {
                    if (err) {
                      return res.status(400).json({ msg: err.message });
                    } else {
                      if (meta.bins.haha.length === 0) {
                        const ops = [Aerospike.operations.incr("haha", 1)];
                        const data = await client.operate(post_key, ops);
                        // return res.status(200).json({ msg: "haha post" });
                        if (post.bins.u_dun === req.user.handleUn) {
                          return res.status(200).json({ msg: "HAHA post" });
                        } else {
                          if (req.user.flwr_c > 2) {
                            console.log("Popular");
                            if (post.bins.pop.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("pop", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res.status(200).json({ msg: "HAHA post" });
                            } else {
                              return res.status(200).json({ msg: "HAHA post" });
                            }
                          } else {
                            console.log("Normal");
                            if (post.bins.ran.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("ran", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res.status(200).json({ msg: "HAHA post" });
                            } else {
                              return res.status(200).json({ msg: "HAHA post" });
                            }
                          }
                        }
                      } else {
                        // return res.status(200).json({ msg: "haha post" });

                        if (post.bins.u_dun === req.user.handleUn) {
                          return res.status(200).json({ msg: "Liked post" });
                        } else {
                          if (req.user.flwr_c > 2) {
                            console.log("Popular");
                            if (post.bins.pop.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("pop", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res.status(200).json({ msg: "HAHA post" });
                            } else {
                              return res.status(200).json({ msg: "haha post" });
                            }
                          } else {
                            console.log("Normal");
                            if (post.bins.ran.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("ran", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res.status(200).json({ msg: "haha post" });
                            } else {
                              return res.status(200).json({ msg: "haha post" });
                            }
                          }
                        }
                      }
                    }
                  });
                }
              });
            } else {
              const ops = [Aerospike.lists.append("haha", req.user.handleUn)];
              client.operate(post_meta_key, ops, async (err, result) => {
                if (err) {
                  return res.status(400).json({ msg: err.message });
                } else {
                  const ops = [Aerospike.operations.incr("l_c", 1)];
                  await client.operate(post_feed_key, ops);
                  client.operate(post_key, ops, async (err, result) => {
                    if (err) {
                      return res.status(400).json({ msg: err.message });
                    } else {
                      if (meta.bins.haha.length === 0) {
                        const ops = [Aerospike.operations.incr("haha", 1)];
                        const data = await client.operate(post_key, ops);
                        // return res.status(200).json({ msg: "haha post" });
                        if (post.bins.u_dun === req.user.handleUn) {
                          return res.status(200).json({ msg: "Liked post" });
                        } else {
                          if (req.user.flwr_c > 2) {
                            console.log("Popular");
                            if (post.bins.pop.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("pop", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res
                                .status(200)
                                .json({ msg: "Liked post" });
                            } else {
                              return res
                                .status(200)
                                .json({ msg: "Liked post" });
                            }
                          } else {
                            console.log("Normal");
                            if (post.bins.ran.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("ran", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res
                                .status(200)
                                .json({ msg: "Liked post" });
                            } else {
                              return res
                                .status(200)
                                .json({ msg: "Liked post" });
                            }
                          }
                        }
                      } else {
                        if (post.bins.u_dun === req.user.handleUn) {
                          return res.status(200).json({ msg: "Liked post" });
                        } else {
                          if (req.user.flwr_c > 2) {
                            console.log("Popular");
                            if (post.bins.pop.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("pop", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res
                                .status(200)
                                .json({ msg: "Liked post" });
                            } else {
                              return res
                                .status(200)
                                .json({ msg: "Liked post" });
                            }
                          } else {
                            console.log("Normal");
                            if (post.bins.ran.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("ran", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res
                                .status(200)
                                .json({ msg: "Liked post" });
                            } else {
                              return res
                                .status(200)
                                .json({ msg: "Liked post" });
                            }
                          }
                        }
                        return res.status(200).json({ msg: "haha post" });
                      }
                    }
                  });
                }
              });
            }
          }
        } else {
          console.log("Like count 0");
          const ops = [Aerospike.lists.append("haha", req.user.handleUn)];
          client.operate(post_meta_key, ops, async (err, result) => {
            if (err) {
              return res.status(400).json({ msg: err.message });
            } else {
              const ops = [
                Aerospike.operations.incr("l_c", 1),
                Aerospike.operations.incr("haha", true),
              ];
              await client.operate(post_feed_key, ops);
              client.operate(post_key, ops, async (err, result) => {
                if (err) {
                  return res.status(400).json({ msg: err.message });
                } else {
                  // return res.status(200).json({ msg: "HAHA post" });
                  if (post.bins.u_dun === req.user.handleUn) {
                    return res.status(200).json({ msg: "Liked post" });
                  } else {
                    if (req.user.flwr_c > 2) {
                      console.log("Popular");
                      if (post.bins.pop.length <= 2) {
                        const ops = [
                          Aerospike.lists.append("pop", {
                            handleUn: req.user.handleUn,
                            pic: req.user.p_i || "",
                          }),
                        ];
                        const data = await client.operate(post_key, ops);
                        return res.status(200).json({ msg: "Liked post" });
                      } else {
                        return res.status(200).json({ msg: "Liked post" });
                      }
                    } else {
                      console.log("Normal");
                      if (post.bins.ran.length <= 2) {
                        const ops = [
                          Aerospike.lists.append("ran", {
                            handleUn: req.user.handleUn,
                            pic: req.user.p_i || "",
                          }),
                        ];
                        const data = await client.operate(post_key, ops);
                        return res.status(200).json({ msg: "Liked post" });
                      } else {
                        return res.status(200).json({ msg: "Liked post" });
                      }
                    }
                  }
                }
              });
            }
          });
        }
      }
    } catch (error) {
      next(error);
    }
  }

  async handleAngry(req, res, next) {
    try {
      const id = req.params.id;
      const client = await getAerospikeClient();
      if (!id) {
        return res.status(400).json({ msg: "Invalid request" });
      } else {
        const post_key = new Aerospike.Key(
          process.env.CLUSTER_NAME,
          process.env.SET_GROUP_POSTS,
          id
        );
        const post_meta_key = new Aerospike.Key(
          process.env.CLUSTER_NAME,
          process.env.SET_GROUP_POST_META,
          id
        );
        const post_feed_key = new Aerospike.Key(
          process.env.CLUSTER_NAME,
          process.env.SET_POSTS,
          id
        );

        const post = await client.get(post_key);
        if (post.bins.l_c > 0) {
          const meta = await client.get(post_meta_key);
          if (meta.bins.angry.includes(req.user.handleUn)) {
            const ops = [
              Aerospike.lists.removeByValue("angry", req.user.handleUn),
            ];
            client.operate(post_meta_key, ops, async (err, result) => {
              if (err) {
                return res.status(400).json({ msg: err.message });
              } else {
                const ops = [Aerospike.operations.incr("l_c", -1)];
                await client.operate(post_feed_key, ops);
                client.operate(post_key, ops, async (err, result) => {
                  if (err) {
                    return res.status(400).json({ msg: err.message });
                  } else {
                    // return res.status(200).json({ msg: "angry added" });
                    if (meta.bins.angry.length === 1) {
                      const ops = [Aerospike.operations.incr("angry", -1)];
                      const data = await client.operate(post_key, ops);
                      return res.status(200).json({ msg: "angry added" });
                    } else {
                      return res.status(200).json({ msg: "angry added" });
                    }
                  }
                });
              }
            });
          } else {
            if (
              meta.bins.likes.includes(req.user.handleUn) ||
              meta.bins.haha.includes(req.user.handleUn) ||
              meta.bins.angry.includes(req.user.handleUn) ||
              meta.bins.dislikes.includes(req.user.handleUn)
            ) {
              const ops = [
                Aerospike.lists.removeByValue("likes", req.user.handleUn),
                Aerospike.lists.removeByValue("haha", req.user.handleUn),
                Aerospike.lists.removeByValue("angry", req.user.handleUn),
                Aerospike.lists.removeByValue("heart", req.user.handleUn),
              ];
              client.operate(post_meta_key, ops, (err, result) => {
                if (err) {
                  return res.status(400).json({ msg: err.message });
                } else {
                  const ops = [
                    Aerospike.lists.append("angry", req.user.handleUn),
                  ];
                  client.operate(post_meta_key, ops, async (err, result) => {
                    if (err) {
                      return res.status(400).json({ msg: err.message });
                    } else {
                      // return res.status(200).json({ msg: "Angry post" });
                      if (meta.bins.angry.length === 0) {
                        const ops = [Aerospike.operations.incr("angry", 1)];
                        const data = await client.operate(post_key, ops);
                        return res.status(200).json({ msg: "angry added" });
                      } else {
                        return res.status(200).json({ msg: "angry added" });
                      }
                    }
                  });
                }
              });
            } else {
              const ops = [Aerospike.lists.append("angry", req.user.handleUn)];
              client.operate(post_meta_key, ops, (err, result) => {
                if (err) {
                  return res.status(400).json({ msg: err.message });
                } else {
                  const ops = [Aerospike.operations.incr("l_c", 1)];
                  client.operate(post_key, ops, async (err, result) => {
                    if (err) {
                      return res.status(400).json({ msg: err.message });
                    } else {
                      // return res.status(200).json({ msg: "Angry  added" });
                      if (meta.bins.angry.length === 0) {
                        const ops = [Aerospike.operations.incr("angry", 1)];
                        const data = await client.operate(post_key, ops);
                        // return res.status(200).json({ msg: "angry added" });
                        if (post.bins.u_dun === req.user.handleUn) {
                          return res.status(200).json({ msg: "Angry post" });
                        } else {
                          if (req.user.flwr_c > 2) {
                            console.log("Popular");
                            if (post.bins.pop.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("pop", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res
                                .status(200)
                                .json({ msg: "Angry post" });
                            } else {
                              return res
                                .status(200)
                                .json({ msg: "Angry post" });
                            }
                          } else {
                            console.log("Normal");
                            if (post.bins.ran.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("ran", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res
                                .status(200)
                                .json({ msg: "Angry post" });
                            } else {
                              return res
                                .status(200)
                                .json({ msg: "Angry post" });
                            }
                          }
                        }
                      } else {
                        // return res.status(200).json({ msg: "angry added" });

                        if (post.bins.u_dun === req.user.handleUn) {
                          return res.status(200).json({ msg: "Liked post" });
                        } else {
                          if (req.user.flwr_c > 2) {
                            console.log("Popular");
                            if (post.bins.pop.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("pop", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res
                                .status(200)
                                .json({ msg: "Angry post" });
                            } else {
                              return res
                                .status(200)
                                .json({ msg: "Angry post" });
                            }
                          } else {
                            console.log("Normal");
                            if (post.bins.ran.length <= 2) {
                              const ops = [
                                Aerospike.lists.append("ran", {
                                  handleUn: req.user.handleUn,
                                  pic: req.user.p_i || "",
                                }),
                              ];
                              const data = await client.operate(post_key, ops);
                              return res
                                .status(200)
                                .json({ msg: "Angry post" });
                            } else {
                              return res
                                .status(200)
                                .json({ msg: "Angry post" });
                            }
                          }
                        }
                      }
                    }
                  });
                }
              });
            }
          }
        } else {
          const ops = [Aerospike.lists.append("angry", req.user.handleUn)];
          client.operate(post_meta_key, ops, async (err, result) => {
            if (err) {
              return res.status(400).json({ msg: err.message });
            } else {
              const ops = [Aerospike.operations.incr("l_c", 1)];
              await client.operate(post_feed_key, ops);
              client.operate(post_key, ops, async (err, result) => {
                if (err) {
                  return res.status(400).json({ msg: err.message });
                } else {
                  // return res.status(200).json({ msg: "angry added" });
                  if (meta.bins.angry.length === 0) {
                    const ops = [Aerospike.operations.incr("angry", 1)];
                    const data = await client.operate(post_key, ops);
                    // return res.status(200).json({ msg: "angry added" });

                    if (post.bins.u_dun === req.user.handleUn) {
                      return res.status(200).json({ msg: "Angry post" });
                    } else {
                      if (req.user.flwr_c > 2) {
                        console.log("Popular");
                        if (post.bins.pop.length <= 2) {
                          const ops = [
                            Aerospike.lists.append("pop", {
                              handleUn: req.user.handleUn,
                              pic: req.user.p_i || "",
                            }),
                          ];
                          const data = await client.operate(post_key, ops);
                          return res.status(200).json({ msg: "Angry post" });
                        } else {
                          return res.status(200).json({ msg: "Angry post" });
                        }
                      } else {
                        console.log("Normal");
                        if (post.bins.ran.length <= 2) {
                          const ops = [
                            Aerospike.lists.append("ran", {
                              handleUn: req.user.handleUn,
                              pic: req.user.p_i || "",
                            }),
                          ];
                          const data = await client.operate(post_key, ops);
                          return res.status(200).json({ msg: "Angry post" });
                        } else {
                          return res.status(200).json({ msg: "Angry post" });
                        }
                      }
                    }
                  } else {
                    // return res.status(200).json({ msg: "angry added" });
                    if (post.bins.u_dun === req.user.handleUn) {
                      return res.status(200).json({ msg: "Angry post" });
                    } else {
                      if (req.user.flwr_c > 2) {
                        console.log("Popular");
                        if (post.bins.pop.length <= 2) {
                          const ops = [
                            Aerospike.lists.append("pop", {
                              handleUn: req.user.handleUn,
                              pic: req.user.p_i || "",
                            }),
                          ];
                          const data = await client.operate(post_key, ops);
                          return res.status(200).json({ msg: "Angry post" });
                        } else {
                          return res.status(200).json({ msg: "Angry post" });
                        }
                      } else {
                        console.log("Normal");
                        if (post.bins.ran.length <= 2) {
                          const ops = [
                            Aerospike.lists.append("ran", {
                              handleUn: req.user.handleUn,
                              pic: req.user.p_i || "",
                            }),
                          ];
                          const data = await client.operate(post_key, ops);
                          return res.status(200).json({ msg: "Angry post" });
                        } else {
                          return res.status(200).json({ msg: "Angry post" });
                        }
                      }
                    }
                  }
                }
              });
            }
          });
        }
      }
    } catch (error) {
      next(error);
    }
  }

  /**
   * @FULLPOST_PAST_CODE
   * */
  async fetchBlockFeed(req, res, next) {}

  async ctrlCreateNewBlockPost(req, res, next) {
    try {
      console.log("New API Call ", req.body);
      if (!req.params.id) {
        return res.status(400).json({ msg: "Request params id" });
      } else {
        const data = await createNewBlockPost(
          req.file,
          req.params.id,
          req.body,
          req.user
        );
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  /**
   * @BOOKMARK_PAST
   * */

  async bookmarkPost(req, res, next) {
    console.log("res body", req.body);
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Invalid post id");
      } else {
        const result = await handleBookmarkPost(
          req.params.id,
          req.user.handleUn,
          req.body.btnType
        );
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error.message);
    }
  }

  async hideUnhidePost(req, res, next) {
    try {
      const client = await getAerospikeClient();
      if (!req.params.id) {
        return res.status(400).json({ msg: "Inva;lid request" });
      } else {
        const group_post_key = new Aerospike.Key(
          process.env.CLUSTER_NAME,
          process.env.SET_GROUP_POSTS,
          req.params.id
        );
        const post_feed_key = new Aerospike.Key(
          process.env.CLUSTER_NAME,
          process.env.SET_POSTS,
          req.params.id
        );

        const ops = [Aerospike.lists.append("hide", req.user.handleUn)];
        const data = await client.operate(group_post_key, ops);
        try {
          const data = await client.operate(post_feed_key, ops);
          try {
            return res.status(200).json(data);
          } catch (error) {
            return res.status(400).json({ msg: error.message });
          }
        } catch (error) {
          return res.status(400).json({ msg: error.message });
        }
      }
    } catch (error) {
      next(error);
    }
  }

  async fetchAnalytics(req, res, next) {
    try {
      const client = await getAerospikeClient();
      if (!req.params.id) {
        return res.status(400).json({ msg: "Invalid request" });
      } else {
        console.log(req.params.id);
        const post_key = new Aerospike.Key(
          process.env.CLUSTER_NAME,
          process.env.SET_GROUP_POSTS,
          req.params.id
        );
        const post_meta_key = new Aerospike.Key(
          process.env.CLUSTER_NAME,
          process.env.SET_GROUP_POST_META,
          req.params.id
        );

        var post;

        client.exists(post_key, async (err, result) => {
          if (err) {
            return res.status(400).json({ msg: err.message });
          } else {
            if (!result) {
              return res.status(400).json({ msg: "Invalid request" });
            } else {
              const mapOps = [
                Aerospike.maps
                  .getByKey("analytics", req.query.date)
                  .andReturn(maps.returnType.KEY_VALUE),
              ];
              client.operate(post_meta_key, mapOps, async (err, result) => {
                const post = await client.get(post_meta_key);
                if (err) {
                  return res.status(400).json({ msg: err.message });
                } else {
                  return res
                    .status(200)
                    .json({ metaData: post, analytics: result.bins.analytics });
                }
              });
            }
          }
        });
      }
    } catch (error) {
      next(error);
    }
  }

  //**************** GROUP POST COMMENT **************//
  async createComment(req, res, next) {
    console.log("createComment controller user", req.user);
    try {
      const id = req.params.id;
      const data = await createBlockPostComment(
        id,
        req.file,
        req.body,
        req.user,
        req.body.isGroupCmnt
      );
      return res.status(200).json(data);
    } catch (error) {
      next(error);
    }
  }

  async BlockCmntRepostWithQuote(req, res, next) {
    console.log("repost body", req.body);
    if (!req.params.id) {
      return res.status(400).json({ msg: "Invalid request" });
    } else {
      const data = await handleRepostWithQuote(
        req.params.id,
        req.body,
        req.user
      );
      return res.status(200).json(data);
    }
  }

  async fetchComments(req, res, next) {
    try {
      if (!req.params.id) {
        return res.status(400).json({ msg: "Invalid" });
      } else {
        const page = req.query.page;
        const limit = req.query.limit;
        const sortedBy = req.query.sortedBy;
        const type = req.query.type;
        const data = await fetchAllBlockComments(
          req.params.id,
          req.user.handleUn,
          sortedBy,
          page,
          limit,
          type
        );
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async fetchSingleCommentMeta(req, res, next) {
    console.log("cmnt meta id =>", req.params.id);
    try {
      if (!req.params.id) {
        return res.status(400).json({ msg: "Invalid" });
      } else {
        const data = await fetchSingleBlockCommentMeta(req.params.id);
        console.log("data", data);
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async fetchMyComments(req, res, next) {
    try {
      const client = await getAerospikeClient();
      if (!req.params.id) {
        return res.status(400).json({ msg: "Invalid" });
      } else {
        const page = req.query.page;
        const limit = req.query.limit;
        const sortedBy = req.query.sortedBy;
        const data = await fetchMyBlockComments(
          req.params.id,
          req.user.handleUn,
          sortedBy,
          page,
          limit
        );
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async likeComment(req, res, next) {
    console.log("reached here ####", req.body);
    if (!req.params.id || !req.body.username || !req.body.type) {
      return res.status(400).json({ msg: "Invalid request" });
    } else {
      const data = await blockLikeComment(
        req.params.id,
        req.body.type,
        // req.params.likeCount,
        req.body.username,
        req.user,
        req.body.p_type
      );
      return res.status(200).json(data);
    }
  }

  async fetchCommentsMostReactions(req, res, next) {
    console.log("data===> ", req.params.id);
    if (!req.params.id) {
      return res.status(400).json({ msg: "Invalid request" });
    } else {
      const data = await fetchBlockCommentsMostReactions(
        req.params.id,
        req.user
      );
      return res.status(200).json(data);
    }
  }

  async handleRepostBlockComment(req, res, next) {
    if (!req.params.id) {
      return res.status(400).json({ msg: "Invalid parameter" });
    } else {
      const data = await repostBlockComment(req.params.id, req.user);
      return res.status(200).json(data);
    }
  }

  /* async removeLike(req, res, next) {
	  console.log("data===> ")
    if (!req.params.id) {
      return res.status(400).json({ msg: "Invalid parameter" });
    } else {
      const data = await blockRemoveLikeComment(
        req.params.id,
        req.user.handleUn
      );
      return res.status(200).json(data);
    }
  }
*/

  async pinAndUnpinComment(req, res, next) {
    console.log(
      "req.body of cmnt controller pin pos ===>",
      req.query.postId,
      "and --",
      req.params.id
    );
    if (!req.params.id || !req.query.postId) {
      return res.status(400).json({ msg: "Invalid parameter" });
    } else {
      const data = await handlePinUnpinComment(
        req.params.id,
        req.query.postId,
        req.body.cmntAuthor,
        req.user,
        req.body.type
      );
      return res.status(200).json(data);
    }
  }

  async handleSpamComment(req, res, next) {
    try {
      if (!req.params.id) {
        return res.status(400).json({ msg: "Invalid parameter" });
      } else {
        const data = await spamComment(
          req.params.id,
          req.user.handleUn,
          req.body.type
        );
        return res.status(200).json(data);
      }
    } catch (err) {
      next(err);
    }
  }

  /**
   *
   * @REPORT_PAST
   */

  /*
  async reportComment(req, res, next) {
    const commentId = req.params.id;
    const client = await getAerospikeClient();
    if (!commentId) {
      return res.status(401).json({ msg: "Invalid request" });
    } else {
      const comment_meta_key = new Aerospike.Key(
        process.env.CLUSTER_NAME,
        process.env.SET_GROUP_POST_COMMENT_META,
        req.params.id
      );
      const comment_key = new Aerospike.Key(
        process.env.CLUSTER_NAME,
        process.env.SET_GROUP_POST_COMMENT,
        req.params.id
      );

      const data = await client.get(comment_meta_key);
      const metaData = data.bins;

      if (metaData.spam.includes(req.user.handleUn)) {
        const ops = [Aerospike.lists.removeByValue("spam", req.user.handleUn)];
        client.operate(comment_meta_key, ops, (err, result) => {
          if (err) {
            return res.status(400).json({ msg: err.message });
          } else {
            const ops1 = [Aerospike.operations.incr("s_c", -1)];
            client.operate(comment_key, ops1, (err, result) => {
              if (err) {
                return res.status(400).json({ msg: err.message });
              } else {
                return res.status(200).json({ msg: "spam removed" });
              }
            });
          }
        });
      } else {
        const ops = [Aerospike.lists.append("spam", req.user.handleUn)];
        client.operate(comment_meta_key, ops, (err, result) => {
          if (err) {
            return res.status(400).json({ msg: err.message });
          } else {
            const ops1 = [Aerospike.operations.incr("s_c", 1)];
            client.operate(comment_key, ops1, (err, result) => {
              if (err) {
                return res.status(400).json({ msg: err.message });
              } else {
                return res.status(200).json({ msg: "spam added" });
              }
            });
          }
        });
      }
    }
  }
  */

  async editComment(req, res, next) {
    try {
      const commentId = req.params.id;
      const data = await editBlockComment(commentId, req.body, req.body.type);
      return res.status(200).json(data);
    } catch (error) {
      next(error);
    }
  }

  async deleteComment(req, res, next) {
    console.log("req.body ==>", req.body.type);
    try {
      const commentId = req.params.id;
      // const type = req.params.type;
      const type = req.body.type;
      const data = await blockDeleteComment(commentId, type);
      return res.status(200).json(data);
    } catch (error) {
      next(error);
    }
  }

  async hideComment(req, res, next) {
    try {
      const commentId = req.params.id;
      const client = await getAerospikeClient();
      const key = new Aerospike.Key(
        process.env.CLUSTER_NAME,
        process.env.SET_GROUP_POST_COMMENT,
        commentId
      );

      client.exists(key, async (err, data) => {
        console.log("DATA >>", data);
        if (err) {
          return res.status(400).json({ msg: err.message });
        } else {
          if (!data) {
            return res.status(400).json({ msg: "No comment found" });
          } else {
            const data = await client.get(key);
            if (data.bins.hide.includes(req.user.handleUn)) {
              console.log("Already hide");
              const ops = [
                Aerospike.lists.removeByValue("hide", req.user.handleUn),
              ];
              client.operate(key, ops, (err, result) => {
                if (err) {
                  return res.status(400).json({ msg: "err.message" });
                } else {
                  return res
                    .status(200)
                    .json({ msg: "You unhide this comment" });
                }
              });
            } else {
              const ops = [Aerospike.lists.append("hide", req.user.handleUn)];
              client.operate(key, ops, (err, result) => {
                if (err) {
                  return res.status(400).json({ msg: "err.message" });
                } else {
                  return res.status(200).json({ msg: "You hide this comment" });
                }
              });
            }
          }
        }
      });
    } catch (error) {
      next(error);
    }
  }

  // ******************* Replies ******************* //
  async createReply(req, res) {
    console.log("createreply", req.body);
    const data = await createBlockReply(
      req.params.id,
      req.body,
      req.user,
      req.body.type
    );
    return res.status(200).json(data);
  }

  async replyReply(req, res) {
    try {
      if (!req.params.id) {
        return res.status(400).json({ msg: "Invalid request" });
      } else {
        const data = await createBlockReplyReply(
          req.params.id,
          req.body,
          req.user,
          req.body.type
        );
        return res.status(200).json(data);
      }
    } catch (error) {
      throw error;
    }
  }

  async fetchReplies(req, res, next) {
    try {
      const page = req.query.page || 1;
      const limit = req.query.limit || 3;
      const data = await getReplies(req.params.id, page, limit, req.query.type);

      return res.status(200).json(data.data);
    } catch (error) {
      next(error);
    }
  }

  async hideCommentReply(req, res, next) {
    const replyId = req.params.id;
    const commentId = req.body.commentId;
    const handleUn = req.user.handleUn;

    try {
      if (!req.params.id) {
        return res.status(400).json({ msg: "Invalid request" });
      } else {
        const data = await hideCommentReplyModel(replyId, commentId, handleUn);
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async deleteReply(req, res, next) {
    try {
      const client = await getAerospikeClient();
      if (!req.params.id) {
        return res.status(400).json({ msg: "Invalid request" });
      } else {
        const data = await deleteBlockReply(
          req.params.id,
          req.body.c_id,
          req.body.type,
          req.body.replyType
        );
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async editReply(req, res, next) {
    console.log(req.params.id);
    try {
      if (!req.params.id) {
        return res.status(400).json({ msg: "Invalid request" });
      } else {
        const data = await editBlockReply(
          req.params.id,
          req.body,
          req.body.type
        );
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async likeReply(req, res, next) {
    console.log("come here ###", req.body);
    const client = await getAerospikeClient();
    if (!req.params.id) {
      return res.status(400).json({ msg: "Invalid request" });
    } else {
      const data = await likeBlockReply(
        req.params.id,
        req.user.handleUn,
        req.user,
        req.body.type
      );
      return res.status(200).json(data);
    }
  }

  async dislikeReply(req, res, next) {
    try {
      const client = await getAerospikeClient();
      if (!req.params.id) {
        return res.status(400).json({ msg: "Invalid request" });
      } else {
        const data = await dislikeBlockReply(
          req.params.id,
          req.user.handleUn,
          req.body.type
        );
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async spamReply(req, res, next) {
    try {
      const client = await getAerospikeClient();
      if (!req.params.id) {
        return res.status(400).json({ msg: "Invalid request" });
      } else {
        const data = await spamBlockReply(
          req.params.id,
          req.body.handleUn,
          req.body.type
        );
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  // ******************* Events ******************* //
  async createEvent(req, res, next) {
    console.log("Event create");
    try {
      const data = await createBlockEvent(req.file, req.body, req.user);
      return res.status(200).json(data);
    } catch (error) {
      next(error.message);
    }
  }

  async fetchEvents(req, res, next) {
    console.log("Call Events");
    try {
      const client = await getAerospikeClient();
      if (!req.params.id) {
        return res.status(400).json({ msg: "Invalid block id" });
      } else {
        const page = req.query.page || 1;
        const limit = req.query.limit || 3;
        const data = await fetchAllEvents(req.params.id, page, limit);
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async joinEvent(req, res, next) {
    try {
      const data = req.body;

      if (!data.id) {
        return res.status(400).json({ msg: "Invalid request" });
      } else {
        const response = await blockEventJoin(req.user, data);
        console.log("DATA:", response);
        return res.status(200).json(response);
      }
    } catch (error) {
      next(error);
    }
  }

  async fetchEvent(req, res, next) {
    try {
      if (!req.params.id) {
        return res.status(400).json({ msg: "Invalid request" });
      } else {
        const data = await fetchSingleEvent(req.params.id);
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async deleteEvent(req, res, next) {
    try {
      if (!req.params.id) {
        return res.status(400).json({ msg: "Invalid request" });
      } else {
        console.log(req.params.id);
        const data = await blockEventDelete(req.params.id);
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async eventInterest(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Invalid request paramster");
      } else {
        const array = JSON.parse(req.body.list);
        console.log(array);
        const result = await addEventInterest(
          req.params.id,
          req.user,
          req.query.value,
          array
        );
        console.log(result);
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  async eventNotInterest(req, res, next) {
    console.log("NOT INTEREST");
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Invalid request paramster");
      } else {
        const array = JSON.parse(req.body.list);
        const data = await addEventNotInterest(
          req.params.id,
          req.user,
          req.query.value,
          array
        );
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async eventjoin(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Invalid request paramster");
      } else {
        const array = JSON.parse(req.body.list);
        const data = await addEventJoin(
          req.params.id,
          req.user,
          req.query.value,
          array
        );
        return res.status(200).json(data);
      }
    } catch (error) {
      next(error);
    }
  }

  async updateEvent(req, res, next) {
    const client = await getAerospikeClient();
    if (!req.params.id) {
      return res.status(400).json({ msg: "Invalid request" });
    } else {
      const event_key = new Aerospike.Key(
        process.env.CLUSTER_NAME,
        process.env.SET_BLOCK_EVENT,
        req.params.id
      );

      let query = client.query(
        process.env.CLUSTER_NAME,
        process.env.SET_BLOCK_EVENT
      );
      const tempBin = "ExpVar"; // this bin is to hold expression read operation output
      // query.select(["tit", "des", "s_d", "e_d", "s_t", "e_t", "price"]); //select single bin
      query.where(Aerospike.filter.equal("e_id", req.params.id));

      // const queryPolicy = new Aerospike.QueryPolicy({});
      const stream = query.foreach();
      stream.on("data", async function (record) {
        var data = record.bins;
        // console.log(data)
        if (
          !req.body.title.trim() ||
          !req.body.description.trim() ||
          !req.body.startdate.trim() ||
          !req.body.enddate.trim() ||
          !req.body.starttime.trim() ||
          !req.body.endtime.trim()
        ) {
          return res.status(400).json({ msg: "Invalid request" });
        } else {
          const ops = [
            Aerospike.operations.write("tit", req.body.title),
            Aerospike.operations.write("des", req.body.description),
            Aerospike.operations.write("s_d", req.body.startdate),
            Aerospike.operations.write("e_d", req.body.enddate),
            Aerospike.operations.write("s_t", req.body.starttime),
            Aerospike.operations.write("e_t", req.body.endtime),
            Aerospike.operations.write("price", req.body.price),
            Aerospike.operations.write("slot", req.body.slotValue),
          ];
          await client.operate(event_key, ops);
        }
      });

      stream.on("end", async function (record) {
        const data = await client.get(event_key);
        console.log(data.bins);
        return res.status(200).json({ msg: "Event has been updated" });
      });
    }
  }

  async handleHideEvent(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("No event id found");
      } else {
        const result = await hideEvent(req.user.handleUn, req.params.id);
        console.log(result);
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  async reportEvent(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Event id is not defined");
      } else {
        const result = await handleReportEvent(
          req.params.id,
          req.user.handleUn,
          req.body
        );
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  async reportPost(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Event id is not defined");
      } else {
        const result = await handleReportPost(
          req.params.id,
          req.user.handleUn,
          req.body
        );
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  async getPostAnalytics(req, res, next) {
    console.log("res", req.params.id);
    try {
      const id = String(req.params.id);
      if (!req.params.id) {
        throw createError.BadRequest("Post id is not defined");
      } else {
        const result = await handleGetPostAnalytics(id, req.query.date);
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  async fetchFullPostInModal(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Post id is not defined");
      } else {
        console.log("******Came here blockpostcontroller");
        const id = req.params.id;
        const result = await handleFetchFullPostInModal(id);
        return res.status(200).json(result.bins);
      }
    } catch (error) {
      next(error);
    }
  }

  async addPostLike(req, res, next) {
    try {
      //console.log("*********", req.params.id, req.query.type)
      const result = await handleAddPostLike(
        req.params.id,
        req.query.type,
        req.user
      );
      return res.status(200).json(result);
    } catch (error) {
      next(error.message);
    }
  }

  async removePostLike(req, res, next) {
    try {
      const result = await handleRemovePostLike(req.params.id, req.user);
      return res.status(200).json(result);
    } catch (error) {
      next(error.message);
    }
  }

  async updateNewsPost(req, res, next) {
    try {
      console.log("*********", req.params.id, req.body.value);
    } catch (error) {
      next(error.message);
    }
  }

  // **** Handle Upvote news post
  async upvoteNewsPost(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Post id is not defined");
      } else {
        const result = await handleUpvoteNewsPost(
          req.params.id,
          req.user.handleUn,
          req.body.owner,
          req.user
        );
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  // *** Handle Downvote news post
  async downvoteNewsPost(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Post id is not defined");
      } else {
        const result = await handleDownvoteNewsPost(
          req.params.id,
          req.user.handleUn
        );
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  // *** Handle Fake news post
  async fakeNewsPost(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Post id is not defined");
      } else {
        const result = await handleFakeNewsPost(
          req.params.id,
          req.user.handleUn
        );
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  async flagPost(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Post id is not defined");
      } else {
        const result = await handleFlagPost(req.params.id, req.user.handleUn);
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  // *** Announcement post Announcement Block post
  async excitedAnnouncementPost(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Post id is not defined");
      } else {
        const result = await handleExicitedAnnouncementPost(
          req.params.id,
          req.user.handleUn,
          req.user
        );
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  // *** Disapointed Announcement Block post
  async disapointedAnnouncementPost(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Post id is not defined");
      } else {
        const result = await handleDisapointedAnnouncementPost(
          req.params.id,
          req.user.handleUn
        );
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  // *** Misleading Announcement Block post
  async misleadingAnnouncementPost(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Post id is not defined");
      } else {
        const result = await handleMisLeadingAnnouncementPost(
          req.params.id,
          req.user.handleUn
        );
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  async fetchUpvoteUsers(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Post id is not defined");
      } else {
        const page = req.query.page || 1;
        const limit = req.query.limit || 10;
        const result = await handleUpvoteUsers(req.params.id, page, limit);
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  async fetchExcitedUsers(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Post id is not defined");
      } else {
        const page = req.query.page || 1;
        const limit = req.query.limit || 10;
        const result = await handleExcitedUsers(req.params.id, page, limit);
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  async sharePost(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest("Post ID is not present");
      } else {
        const result = await handleSharePost(req.body, req.user);
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error);
    }
  }

  async fetchPostEarning(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest(error.message);
      } else {
        const result = await handleFetchEarning(req.params.id);
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error.message);
    }
  }

  async fetchDonateUsers(req, res, next) {
    try {
      if (!req.params.id) {
        throw createError.BadRequest(error.message);
      } else {
        const result = await handleFetchDonatedUsers(req.params.id);
        return res.status(200).json(result);
      }
    } catch (error) {
      next(error.message);
    }
  }

  /**
   * @REPOST_POST
   */
  async repostPost(req, res, next) {
    console.log("called this function");
    console.log("repostPost->req.params.id", req.params.id);
    try {
      const result = await handleRepostPost(req.params.id, req.user);
      console.log("result after repost ==>");
      // return res.status(200).json(result);
    } catch (error) {
      throw createError.BadRequest(error.message);
    }
  }

  /**
   * For block post booster ad
   */

  async createOrUpdateBlockPostBoost(req, res, next) {
    //

    try {
      const result = await handleCreateOrUpdateBlockPostBoost(
        req.user.handleUn,
        req.body
      );
      return res.status(200).json(result);
    } catch (error) {
      throw createError.BadRequest(error.message);
    }
  }

  async getBlockPostBoost(req, res, next) {
    console.log("req.query.filter", req.query);
    //
    try {
      const postId =
        req.query.postId === "undefined" ||
        (req.query.postId === "null") | (req.query.postId === "0")
          ? 0
          : req.query.postId;
      const reqFilter = req.query.filter === "null" ? "7D" : req.query.filter;
      const pageNum = Number(req.query.page) || 0;
      const pageLimit = Number(req.query.limit) || 10;

      const result = await handleGetBlockPostBoost(
        req.user.handleUn,
        reqFilter,
        pageNum,
        pageLimit,
        postId
      );
      return res.status(200).json(result);
    } catch (error) {
      throw createError.BadRequest(error.message);
    }
  }
}
module.exports = new BlockPostController();

// eventData.j_u
Leave a Comment