用户关系托管

SDK 提供了用户关系托管, 包括黑名单和静音列表

黑名单

静音列表

初始化参数

示例代码

var nim = NIM.getInstance({
    onblacklist: onBlacklist,
    onsyncmarkinblacklist: onMarkInBlacklist,
    onmutelist: onMutelist,
    onsyncmarkinmutelist: onMarkInMutelist
});
function onBlacklist(blacklist) {
    console.log('收到黑名单', blacklist);
    data.blacklist = nim.mergeRelations(data.blacklist, blacklist);
    data.blacklist = nim.cutRelations(data.blacklist, blacklist.invalid);
    refreshBlacklistUI();
}
function onMarkInBlacklist(obj) {
    console.log(obj);
    console.log(obj.account + '被你' + (obj.isAdd ? '加入' : '移除') + '黑名单');
    if (obj.isAdd) {
        addToBlacklist(obj);
    } else {
        removeFromBlacklist(obj);
    }
}
function addToBlacklist(obj) {
    data.blacklist = nim.mergeRelations(data.blacklist, obj.record);
    refreshBlacklistUI();
}
function removeFromBlacklist(obj) {
    data.blacklist = nim.cutRelations(data.blacklist, obj.record);
    refreshBlacklistUI();
}
function refreshBlacklistUI() {
    // 刷新界面
}
function onMutelist(mutelist) {
    console.log('收到静音列表', mutelist);
    data.mutelist = nim.mergeRelations(data.mutelist, mutelist);
    data.mutelist = nim.cutRelations(data.mutelist, mutelist.invalid);
    refreshMutelistUI();
}
function onMarkInMutelist(obj) {
    console.log(obj);
    console.log(obj.account + '被你' + (obj.isAdd ? '加入' : '移除') + '静音列表');
    if (obj.isAdd) {
        addToMutelist(obj);
    } else {
        removeFromMutelist(obj);
    }
}
function addToMutelist(obj) {
    data.mutelist = nim.mergeRelations(data.mutelist, obj.record);
    refreshMutelistUI();
}
function removeFromMutelist(obj) {
    data.mutelist = nim.cutRelations(data.mutelist, obj.record);
    refreshMutelistUI();
}
function refreshMutelistUI() {
    // 刷新界面
}

参数解释

加入黑名单/从黑名单移除

nim.markInBlacklist({
    account: 'account',
    // `true`表示加入黑名单, `false`表示从黑名单移除
    isAdd: true,
    done: markInBlacklistDone
});
function markInBlacklistDone(error, obj) {
    console.log(error);
    console.log(obj);
    console.log('将' + obj.account + (isAdd ? '加入黑名单' : '从黑名单移除') + (!error?'成功':'失败'));
    if (!error) {
        onMarkInBlacklist(obj);
    }
}

加入黑名单

nim.addToBlacklist({
    account: 'account',
    done: addToBlacklistDone
});
function addToBlacklistDone(error, obj) {
    console.log(error);
    console.log(obj);
    console.log('加入黑名单' + (!error?'成功':'失败'));
    if (!error) {
        addToBlacklist(obj);
    }
}

从黑名单移除

nim.removeFromBlacklist({
    account: 'account',
    done: removeFromBlacklistDone
});
function removeFromBlacklistDone(error, obj) {
    console.log(error);
    console.log(obj);
    console.log('从黑名单移除' + (!error?'成功':'失败'));
    if (!error) {
        removeFromBlacklist(obj);
    }
}

加入静音列表/从静音列表移除

nim.markInMutelist({
    account: 'account',
    // `true`表示加入静音列表, `false`表示从静音列表移除
    isAdd: 'true',
    done: markInMutelistDone
});
function markInMutelistDone(error, obj) {
    console.log(error);
    console.log(obj);
    console.log('将' + obj.account + (isAdd ? '加入静音列表' : '从静音列表移除') + (!error?'成功':'失败'));
    if (!error) {
        onMarkInMutelist(obj);
    }
}

加入静音列表

nim.addToMutelist({
    account: 'account',
    done: addToMutelistDone
});
function addToMutelistDone(error, obj) {
    console.log(error);
    console.log(obj);
    console.log('加入静音列表' + (!error?'成功':'失败'));
    if (!error) {
        addToMutelist(obj);
    }
}

从静音列表移除

nim.removeFromMutelist({
    account: 'account',
    done: removeFromMutelistDone
});
function removeFromMutelistDone(error, obj) {
    console.log(error);
    console.log(obj);
    console.log('从静音列表移除' + (!error?'成功':'失败'));
    if (!error) {
        removeFromMutelist(obj);
    }
}

获取黑名单和静音列表

nim.getRelations({
    done: getRelationsDone
});
function getRelationsDone(error, obj) {
    console.log('获取静音列表' + (!error?'成功':'失败'), error, obj);
    if (!error) {
        onBlacklist(obj.blacklist);
        onMutelist(obj.mutelist);
    }
}