代码拉取完成,页面将自动刷新
同步操作将从 GoBelieveIO/im_service 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/**
* Copyright (c) 2014-2015, GoBelieve
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import "sync"
import "database/sql"
import _ "github.com/go-sql-driver/mysql"
import log "github.com/golang/glog"
type Group struct {
gid int64
appid int64
super bool //超大群
mutex sync.Mutex
members IntSet
}
func NewGroup(gid int64, appid int64, members []int64) *Group {
group := new(Group)
group.appid = appid
group.gid = gid
group.super = false
group.members = NewIntSet()
for _, m := range members {
group.members.Add(m)
}
return group
}
func NewSuperGroup(gid int64, appid int64, members []int64) *Group {
group := new(Group)
group.appid = appid
group.gid = gid
group.super = true
group.members = NewIntSet()
for _, m := range members {
group.members.Add(m)
}
return group
}
func (group *Group) Members() IntSet {
return group.members
}
//修改成员,在副本修改,避免读取时的lock
func (group *Group) AddMember(uid int64) {
group.mutex.Lock()
defer group.mutex.Unlock()
members := group.members.Clone()
members.Add(uid)
group.members = members
}
func (group *Group) RemoveMember(uid int64) {
group.mutex.Lock()
defer group.mutex.Unlock()
members := group.members.Clone()
members.Remove(uid)
group.members = members
}
func (group *Group) IsMember(uid int64) bool {
_, ok := group.members[uid]
return ok
}
func (group *Group) IsEmpty() bool {
return len(group.members) == 0
}
func CreateGroup(db *sql.DB, appid int64, master int64, name string, super int8) int64 {
log.Info("create group super:", super)
stmtIns, err := db.Prepare("INSERT INTO `group`(appid, master, name, super) VALUES( ?, ?, ?, ? )")
if err != nil {
log.Info("error:", err)
return 0
}
defer stmtIns.Close()
result, err := stmtIns.Exec(appid, master, name, super)
if err != nil {
log.Info("error:", err)
return 0
}
gid, err := result.LastInsertId()
if err != nil {
log.Info("error:", err)
return 0
}
return gid
}
func DeleteGroup(db *sql.DB, group_id int64) bool {
var stmt1, stmt2 *sql.Stmt
tx, err := db.Begin()
if err != nil {
log.Info("error:", err)
return false
}
stmt1, err = tx.Prepare("DELETE FROM `group` WHERE id=?")
if err != nil {
log.Info("error:", err)
goto ROLLBACK
}
defer stmt1.Close()
_, err = stmt1.Exec(group_id)
if err != nil {
log.Info("error:", err)
goto ROLLBACK
}
stmt2, err = tx.Prepare("DELETE FROM group_member WHERE group_id=?")
if err != nil {
log.Info("error:", err)
goto ROLLBACK
}
defer stmt2.Close()
_, err = stmt2.Exec(group_id)
if err != nil {
log.Info("error:", err)
goto ROLLBACK
}
tx.Commit()
return true
ROLLBACK:
tx.Rollback()
return false
}
func AddGroupMember(db *sql.DB, group_id int64, uid int64) bool {
stmtIns, err := db.Prepare("INSERT INTO group_member(group_id, uid) VALUES( ?, ? )")
if err != nil {
log.Info("error:", err)
return false
}
defer stmtIns.Close()
_, err = stmtIns.Exec(group_id, uid)
if err != nil {
log.Info("error:", err)
return false
}
return true
}
func RemoveGroupMember(db *sql.DB, group_id int64, uid int64) bool {
stmtIns, err := db.Prepare("DELETE FROM group_member WHERE group_id=? AND uid=?")
if err != nil {
log.Info("error:", err)
return false
}
defer stmtIns.Close()
_, err = stmtIns.Exec(group_id, uid)
if err != nil {
log.Info("error:", err)
return false
}
return true
}
func LoadAllGroup(db *sql.DB) (map[int64]*Group, error) {
stmtIns, err := db.Prepare("SELECT id, appid, super FROM `group`")
if err != nil {
log.Info("error:", err)
return nil, err
}
defer stmtIns.Close()
groups := make(map[int64]*Group)
rows, err := stmtIns.Query()
for rows.Next() {
var id int64
var appid int64
var super int8
rows.Scan(&id, &appid, &super)
members, err := LoadGroupMember(db, id)
if err != nil {
log.Info("error:", err)
return nil, err
}
if super != 0 {
group := NewSuperGroup(id, appid, members)
groups[group.gid] = group
} else {
group := NewGroup(id, appid, members)
groups[group.gid] = group
}
}
return groups, nil
}
func LoadGroupMember(db *sql.DB, group_id int64) ([]int64, error) {
stmtIns, err := db.Prepare("SELECT uid FROM group_member WHERE group_id=?")
if err != nil {
log.Info("error:", err)
return nil, err
}
defer stmtIns.Close()
members := make([]int64, 0, 4)
rows, err := stmtIns.Query(group_id)
for rows.Next() {
var uid int64
rows.Scan(&uid)
members = append(members, uid)
}
return members, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。