VirtusLab-Open-Source / strapi-plugin-comments

A plugin for Strapi Headless CMS that provides end to end comments feature with their moderation panel, bad words filtering, abuse reporting and more.
MIT License
407 stars 63 forks source link

How do I display an avatar object? #43

Closed NooBiToo closed 3 years ago

NooBiToo commented 3 years ago

First, thank you so much for your work.

My user object have avatar object

USER:Object
  articles:Array[5]
  **avatar:Object**
  blocked:null
  confirmed:true
  created_at:"2021-03-02T14:12:27.000Z"
  email:"remaps@desync.ru"
  fav_game:""
  id:1
  provider:"local"
  role:Object
  updated_at:"2021-03-07T08:45:21.000Z"
  username:"remapS"

But there is no such field in the comments

1:Object
  authorAvatar:null
  authorEmail:null
  authorId:null
  authorName:null
  authorType:null
  authorUser:Object
    blocked:null
    confirmed:true
    created_at:"2021-03-02T14:12:27.000Z"
    email:"remaps@desync.ru"
    fav_game:""
    id:29
    provider:"local"
    role:1
    updated_at:"2021-03-07T08:45:21.000Z"
    username:"remapS"
  blockReason:null
  blocked:false
  blockedThread:false
  children:Array[1]
  content:"Comment content"
  created_at:"2021-03-06T16:58:46.000Z"
  created_by:1
  id:11
  points:0
  relatedSlug:"articles:1"
  reports:Array[0]
  updated_at:"2021-03-06T17:07:28.000Z"
  updated_by:1
cyp3rius commented 3 years ago

@NooBiToo that seem as a case of author field population. By default we're populating just the user as other nested fields are custom and depends on you own implementation. We're considering a possibility to define the populate array in the configuration or cover the profile field by default.

NooBiToo commented 3 years ago

solved

Tragio commented 3 years ago

@NooBiToo I'm having the same challenge. Could you share your solution? 🙂

Nisthar commented 2 years ago

solved

How did you solve it?

Tragio commented 2 years ago

@cyp3rius I'm also to start working on this. Could you advise what would be the best way to extend the get comments endpoint? 🤔

cyp3rius commented 2 years ago

@Nisthar I've responded in #80

Please open the new issue / discussion related with Strapi v4 version for clarify and visibility. Do not follow closed issues, rather use them as reference.

NooBiToo commented 2 years ago

@Tragio @Nisthar Hello, I can't remember the exact solution, but I can attach my file to you

'use strict';

const { first, isEmpty, isNil } = require('lodash');
const { sanitizeEntity } = require('strapi-utils');
const PluginError = require('./utils/error');
const { isEqualEntity, extractMeta, buildNestedStructure, checkBadWords, filterOurResolvedReports, convertContentTypeNameToSlug, isValidUserContext, resolveUserContextError } = require('./utils/functions');

/**
 * comments.js service
 *
 * @description: A set of functions similar to controller's actions to avoid code duplication.
 */

module.exports = {
    // Find comments in the flat structure
    async findAllFlat(relation, query) {
        const { pluginName, model } = extractMeta(strapi.plugins);
        let baseCriteria = {};
        if (relation) {
            baseCriteria = {
                ...baseCriteria,
                relatedSlug: relation,
            };
        }

        let criteria = {
            ...baseCriteria,
            threadOf_null: true,
        };
        if (query) {
            criteria = {
                ...criteria,
                ...query,
            };
        }

        const entitiesRoot = query && query._q ? 
            await strapi.query( model.modelName, pluginName)
                .search(criteria, ['authorUser', 'authorUser.avatar', 'related', 'reports']) :
            await strapi.query( model.modelName, pluginName)
                .find(criteria, ['authorUser', 'authorUser.avatar', 'related', 'reports']);
        const entitiesNested = await strapi.query( model.modelName, pluginName)
                .find({
                    ...baseCriteria,
                    threadOf_null: false, 
                }, ['authorUser', 'authorUser.avatar', 'related', 'reports']);
        return [...entitiesRoot, ...entitiesNested].map(_ => filterOurResolvedReports(this.sanitizeCommentEntity(_)));
    },

    // Find comments and create relations tree structure
    async findAllInHierarchy (relation, query, startingFromId = null, dropBlockedThreads = false) {
        const entities = await this.findAllFlat(relation, query);
        return buildNestedStructure(entities, startingFromId, 'threadOf', dropBlockedThreads);
    },

    sanitizeCommentEntity: (entity) => ({
        ...entity,
        authorUser: sanitizeEntity(entity.authorUser, { model: strapi.plugins['users-permissions'].models.user }),
    }),
};
Nisthar commented 2 years ago

@NooBiToo where do you place this file?

NooBiToo commented 2 years ago

functions.js

Nisthar commented 2 years ago

@NooBiToo in src/extensions/comments/?

NooBiToo commented 2 years ago

@NooBiToo in src/extensions/comments/?

src/extensions/comments/services/utils/functions.js

Nisthar commented 2 years ago

@NooBiToo in src/extensions/comments/?

src/extensions/comments/services/utils/functions.js

I can't seems to find that function in the original file. are you using v3 of the plugin by any chance?

NooBiToo commented 2 years ago

@NooBiToo in src/extensions/comments/?

src/extensions/comments/services/utils/functions.js

I can't seems to find that function in the original file. are you using v3 of the plugin by any chance?

yep this v3)