More group badges

Plugin Development
  • Hi, I'm writing a custom theme and am struggling to display all user's groups next to their username in the partials/topic/badge.tpl template using this code:

    <!-- IF posts.user.groups.length -->
    	<div class="text-center">
    	<!-- BEGIN groups -->
    		<span class="label group-label inline-block" style="background-color: {posts.user.groups.labelColor};"><!-- IF posts.user.groups.icon --><i class="fa {posts.user.groups.icon}"></i> <!-- ENDIF posts.user.groups.icon -->{posts.user.groups.userTitle}</span><br/>
    	<!-- END groups -->
    	</div>
    <!-- ENDIF posts.user.groups.length -->
    

    but nothing actually works. How do I access the posted user's groups in templates?

  • You will want to write a plugin that uses filter:posts.modifyUserInfo and add that data in.

  • I got it working. If anyone wants all groups to be shown infront of the user's name then:

    Hook

    Add { "hook": "filter:posts.modifyUserInfo", "method": "modifyUserInfo" } to plugin.json.

    Method

    Add the following to your library .js file:

    library.modifyUserInfo = function(userData, callback)
    {
    	var groupsData;
    	async.waterfall([
    		function (next) {
    			groups.getUserGroups([userData.uid], next);
    		},
    		function (_groupsData, next) {
    			groupsData = _groupsData[0];
    			var groupNames = groupsData.filter(Boolean).map(function(group) {
    				return group.name;
    			});
    
    			groups.getMemberUsers(groupNames, 0, 3, next);
    		},
    		function (members, next) {
    			groupsData.forEach(function(group, index) {
    				group.members = members[index];
    			});
    			
    			next();
    		}
    	], function(err) {
    		if (err) {
    			return callback(err);
    		}
    
    		userData.groups = groupsData;
    		
    		callback(null, userData);
    	});
    }
    

    Template

    Template example code:

    <!-- BEGIN posts.user.groups -->
    	<a href="{config.relative_path}/groups/{posts.user.groups.slug}">
    		<small class="label group-label inline-block" style="background-color: {posts.user.groups.labelColor};">
    			<!-- IF posts.user.groups.icon -->
    				<i class="fa {posts.user.groups.icon}"></i>
    			<!-- ENDIF posts.user.groups.icon -->
    			
    			{posts.user.groups.userTitle}
    		</small>
    	</a>
    <!-- END posts.user.groups -->
    

    Thanks @baris for the tip!

  • @labaguette This looks perfect for what I need, however, this part is giving an error:

    function (next) {
    	groups.getUserGroups([userData.uid], next);
    },
    

    ReferenceError: groups is not defined

    Do you know what I need to use instead?

    I'm wondering if there's a missing function that I need getUserGroups?

    Thanks!

  • Should be groups.getUsersGroups I think.

  • @baris said in More group badges:

    getUsersGroups

    Nevermind it's groups.getUserGroups check if your copy of nodebb has that function.

  • If groups is undefined then in your plugin you need to require that from core nodebb with const groups = require.main.require('./src/groups');

  • @baris Yes, thanks.

    I had to include this line, the function is in there:

    var groups = require.main.require('./src/groups');

  • For anyone looking at this, there is no need to load the members of the groups so this code should work fine.

    library.modifyUserInfo = function(userData, callback)
    {
    	var groupsData;
    	async.waterfall([
    		function (next) {
    			groups.getUserGroups([userData.uid], next);
    		},
    		function (_groupsData, next) {
    			groupsData = _groupsData[0];
    			userData.groups = groupsData.filter(Boolean);
    			next(null, userData);
    		},
    	], callback);
    }
    

Suggested Topics


  • 0 Votes
    1 Posts
    253 Views

    Hi!

    I wrote a plugin for OpenLDAP.
    It overrides the default login and falls back to local login if the user is not found in LDAP.
    It also creates all groups found in LDAP and automatically joins the user into them.
    Using it as an alternative login is not implemented as I couldn't figure it out quick enough. It should be easy to do - feel free to add a pull request.

    The source is on github. Pull requests are welcome!
    Please be kind, my JavaScript skills are limited.

  • 0 Votes
    2 Posts
    288 Views

    Hello, I really like this plugin: https://github.com/adlerosn/nodebb-plugin-groups-autoassigncategory

    But I really need following changes:

    1. An update (and test!) of all dependencies and changes like:

    [recommended in warning logs] var db = require.main.require("./src/database") [current, deprecated version] var db = module.parent.require('./database');

    2. Setup permissions like this (or give an option in the admin panel):

    Guests, Spiders, Registered Users: No permissions at all (not even read or see)

    Group members: All permissions except "see deleted posts", "permanently delete" and "moderate" (I hope I got the translations right, it's the last three checkboxes).

    My Forum with a lot of users will require this and I'd really appreciate it.

  • 0 Votes
    2 Posts
    2k Views

    On the server, you can do

    var groups = require.main.require('./src/groups'); groups.getUserGroups([1] /*array of uids*/, function(err, userGroups){ if (err) return; // Returns an array of an array of group data. console.log(userGroups[0]); // Prints an array of groups for uid 1 });

    on the client, you can get a list of visible groups by doing:

    $.getJSON('/api/user/USERSLUG/groups', function (data) { // Returns an object with a 'groups' property, an array of visible group data. console.log(data.groups); });
  • API: Groups

    Solved Plugin Development
    6
    0 Votes
    6 Posts
    3k Views

    Created a small plugin, to have access to all groups - https://community.nodebb.org/topic/4591/nodebb-plugin-ns-api-ns-api-extended

  • 0 Votes
    2 Posts
    2k Views

    @akumbhare Best bet is to have a client-side file loaded that listens for the action:ajaxify.end hook.

    $(window).on('action:ajaxify.end', function(ev, data) { var url = data.url; });