import {ApiCall} from "tsrpc";
import {ChatFun} from '../../public/chat';
import {ReqGetList, ResGetList} from "../../shared/protocols/chat/PtlGetList";
import {PublicShared} from "../../shared/public/public";
import {FriendManage} from "../../public/friend/manage";
import {filter} from "mathjs";

export default async function (call: ApiCall<ReqGetList, ResGetList>) {

    let publicList = await G.mongodb.collection('chat').find({type: {$in: ['cross', 'local', `guild${call.conn.gud.ghId || ''}`]}}).toArray();
    let _guildList = publicList.find(l => l.type.indexOf('guild') != -1)?.list || []

    const my = await FriendManage.getFriend(call.uid);

    const filterBlackList = (list) => list.filter(i => !my.data.blacklist.includes(i.sender.uid))

    let list = {
        cross: filterBlackList(publicList.find(l => l.type == 'cross')?.list || []) || [],
        local: filterBlackList(publicList.find(l => l.type == 'local')?.list || []) || [],
        guild: filterBlackList(_guildList.filter(l => {
            return PublicShared.getToDayZeroTime(G.time) < l.time
        })),
        private: await ChatFun.getPrivateList(call.uid)
    };

    // 删除过期帮助
    if (_guildList.length != list.guild.length) {
        G.mongodb.collection('chat').updateOne({type: 'guild' + call.conn.gud.ghId}, {$set: {list: list.guild}});
    }

    call.succ(list);
}