def service_sync_mattermost_add_channel(sender, **kwargs):
    project = Project.get(pid=kwargs['pid'])
    if not ('mattermost_ch_id' in project and project['mattermost_ch_id']):
        return

    mmt = MattermostTools(token=setting.MATTERMOST_BOT_TOKEN,
                          base_url=setting.MATTERMOST_BASEURL)
    for uid in kwargs['uids']:
        mid = mmt.find_possible_mid(uid=uid)
        if mid:
            r = mmt.post_user_to_channel(
                channel_id=project['mattermost_ch_id'], uid=mid)
            logger.info(r.json())
def service_sync_mattermost_projectuserin_channel(sender, **kwargs):
    pids = {}
    for project in Project.all():
        if project['action_date'] >= time(
        ) and 'mattermost_ch_id' in project and project['mattermost_ch_id']:
            pids[project['_id']] = project['mattermost_ch_id']

    if not pids:
        return

    mmt = MattermostTools(token=setting.MATTERMOST_BOT_TOKEN,
                          base_url=setting.MATTERMOST_BASEURL)
    for pid in pids:
        uids = set()
        for team in Team.list_by_pid(pid=pid):
            uids.update(team['chiefs'])
            uids.update(team['members'])

        for uid in uids:
            mid = mmt.find_possible_mid(uid=uid)
            if mid:
                r = mmt.post_user_to_channel(channel_id=pids[pid], uid=mid)
                logger.info(r.json())