//---------------------------------------------------------- save to db func _save(group *GroupInfo) { c := db.Collection(COLLECTION) info, err := c.Upsert(bson.M{"groupid": group.GroupId}, group) if err != nil { log.Println(info, err) } }
//---------------------------------------------------------- startup load groups func LoadGroups() { c := db.Collection(COLLECTION) var groups []GroupInfo err := c.Find(nil).All(&groups) if err != nil { log.Println(err) } for _, v := range groups { _groups[v.GroupId] = &v _group_names[v.Name] = &v } }
//---------------------------------------------------------- leave group func (group *GroupInfo) Leave(user_id int32) { _lock.Lock() defer _lock.Unlock() defer func() { // if no member, delete group if group._members.Len() == 0 { delete(_groups, group.GroupId) delete(_group_names, group.Name) c := db.Collection(COLLECTION) err := c.Remove(bson.M{"groupid": group.GroupId}) if err != nil { log.Println(err) } } }() group._members._remove(user_id) }