Beispiel #1
0
def add_group_member(gid, members):
    appid = config.APPID

    if len(members) == 0:
        return

    for member_id in members:
        Group.add_group_member(db, gid, member_id)

    for member_id in members:
        v = {
            "group_id": gid,
            "member_id": member_id,
            "timestamp": int(time.time())
        }
        op = {"add_member": v}
        print(op)
        send_group_notification(appid, gid, op, [member_id])

        content = "%d,%d" % (gid, member_id)
        publish_message(rds, "group_member_add", content)
Beispiel #2
0
def add_group_member(gid):
    appid = request.appid
    obj = request.get_json(force=True, silent=True, cache=False)
    if obj is None:
        logging.debug("json decode err:%s", e)
        raise ResponseMeta(400, "json decode error")
    inviter = None
    if type(obj) is dict:
        if 'members' in obj:
            members = obj['members']
            inviter = obj.get('inviter')
        else:
            members = [obj]
    else:
        members = obj

    if len(members) == 0:
        return ""

    group = Group.get_group(g._db, gid)
    if not group:
        raise ResponseMeta(400, "group non exists")

    # 支持members参数为对象数组
    memberIDs = [m['uid'] if type(m) == dict else m for m in members]

    g._db.begin()
    for member_id in memberIDs:
        try:
            Group.add_group_member(g._db, gid, member_id)
            User.reset_group_synckey(g.rds, appid, member_id, gid)
        except pymysql.err.IntegrityError as e:
            # 可能是重新加入群
            #1062 duplicate member
            if e.args[0] != 1062:
                raise

    g._db.commit()

    for m in members:
        member_id = m['uid'] if type(m) == dict else m
        v = {
            "group_id": gid,
            "group_name": group['name'],
            "member_id": member_id,
            "timestamp": int(time.time())
        }
        if type(m) == dict and m.get('name'):
            v['name'] = m['name']
        if type(m) == dict and m.get('avatar'):
            v['avatar'] = m['avatar']
        if inviter:
            v['inviter'] = inviter

        op = {"add_member": v}
        send_group_notification(appid, gid, op, [member_id])

        Group.publish_member_add_event(g.rds, gid, member_id)

    resp = {"success": True}
    return make_response(200, resp)