chat

package
v0.0.0-...-73ddb9c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ChatTypePrivate 私聊
	ChatTypePrivate = iota
	// ChatTypeGroup 群聊
	ChatTypeGroup
)
View Source
const (
	// PermNormal 狗群员
	PermNormal = iota
	// PermAdmin 狗管理
	PermAdmin
	// PermOwner 狗群主
	PermOwner
)
View Source
const (
	// MsgTypeText 纯文本消息种类
	MsgTypeText = 0
	// MsgTypeImage 图片消息
	MsgTypeImage = 1
	// MsgTypeAudio 音频消息
	MsgTypeAudio = 2
	// MsgTypeVideo 视频消息
	MsgTypeVideo = 3
	// MsgTypeGeo 位置消息
	MsgTypeGeo = 4
	// MsgTypeWithdraw 撤回消息
	MsgTypeWithdraw = 100
)

Variables

This section is empty.

Functions

func ChangeGroupOwner

func ChangeGroupOwner(db *gorm.DB, execID int64, chatID int64, userID int64) error

ChangeGroupOwner 禅让群主

func CheckIfInChat

func CheckIfInChat(db *gorm.DB, chatID int64, userID int64) bool

CheckIfInChat 判断用户是否在属于某个聊天

func CheckIfUserInChats

func CheckIfUserInChats(db *gorm.DB, userID int64, chatIDs []int64) bool

func CheckIsWithdrawn

func CheckIsWithdrawn(db *gorm.DB, chatID int64, messageID int64) bool

CheckIsWithdrawn 判断消息是否被撤回

func DeleteChat

func DeleteChat(db *gorm.DB, execID int64, chatID int64) error

DeleteChat 删除聊天,execID 表示删除的发起人,会检查权限

func DeleteChatMember

func DeleteChatMember(db *gorm.DB, execID int64, chatID int64, userID int64) error

DeleteChatMember 通过用户 UserID 和聊天 UserID 删除成员,execID 为执行者的 UserID,会查询权限。可以删除自身.

func GetAllChatsLatestMessageID

func GetAllChatsLatestMessageID(db *gorm.DB, userID int64) ([]struct {
	ChatID       int64 `json:"chat_id"`
	MaxMessageID int64 `json:"max_message_id"`
}, error)

GetAllChatsLatestMessageID 获得某个用户所有的聊天的最新消息 id

func ModifyChatMemberAlias

func ModifyChatMemberAlias(db *gorm.DB, chatID int64, userID int64, alias string) error

ModifyChatMemberAlias 修改成员名片

func ModifyChatMemberPermission

func ModifyChatMemberPermission(db *gorm.DB, execID int64, chatID int64, userID int64, newPerm MemberPermission) error

ModifyChatMemberPermission 修改成员权限

Types

type Chat

type Chat struct {
	ID     int64    `gorm:"primarykey" json:"id"`
	Type   ChatType `gorm:"type:smallint not null;check:type >= 0 and type <= 1" json:"type"`
	Name   string   `gorm:"type:varChar(127) not null" json:"name"`
	Avatar string   `gorm:"type:varChar(255) not null" json:"avatar"`

	Members  []user.User `gorm:"many2many:chat_users;constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"-"`
	Messages []Message   `gorm:"foreignKey:ChatID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"-"`

	CreatedAt time.Time             `json:"created_at"`
	UpdatedAt time.Time             `json:"updated_at"`
	DeletedAt soft_delete.DeletedAt `gorm:"index" json:"deleted_at"`
}

Chat 聊天列表项的 dao

func CreateGroupChat

func CreateGroupChat(db *gorm.DB, name string, ownerID int64, members mapset.Set) (*Chat, error)

CreateGroupChat 创建群聊,members 为成员 UserID 的集合,允许为空

func CreatePrivateChat

func CreatePrivateChat(db *gorm.DB, user1 int64, user2 int64) (*Chat, error)

CreatePrivateChat 创建私人聊天,若已经存在私聊,则返回此私聊

func GetAllChats

func GetAllChats(db *gorm.DB, userID int64) ([]Chat, error)

GetAllChats 获得一个人加入的所有聊天

func GetChat

func GetChat(db *gorm.DB, chatID int64) (*Chat, error)

GetChat 获得聊天

func GetPrivateChat

func GetPrivateChat(db *gorm.DB, user1 int64, user2 int64) (*Chat, error)

GetPrivateChat 获得私聊

func (*Chat) UpdateFrom

func (chat *Chat) UpdateFrom(db *gorm.DB) error

func (*Chat) UpdateTo

func (chat *Chat) UpdateTo(db *gorm.DB) error

type ChatType

type ChatType int64

type ChatUser

type ChatUser struct {
	ID     int64 `gorm:"primaryKey" json:"-"`
	ChatID int64 `gorm:"uniqueIndex:idx_chat_user_delete" json:"chat_id"`
	UserID int64 `gorm:"uniqueIndex:idx_chat_user_delete" json:"user_id"`
	// Alias 群内备注
	Alias string `gorm:"type:varChar(127) not null" json:"alias"`
	// 群内权限
	Permission MemberPermission `gorm:"type:smallint not null;check:permission >= 0;default:0;" json:"permission"`

	CreatedAt time.Time             `json:"created_at"`
	UpdatedAt time.Time             `json:"-"`
	DeletedAt soft_delete.DeletedAt `gorm:"uniqueIndex:idx_chat_user_delete" json:"deleted_at"`
}

func AddChatMember

func AddChatMember(db *gorm.DB, chatID int64, userID int64) (*ChatUser, error)

AddChatMember 通过用户 UserID 和聊天 UserID 加入新成员

func GetChatMember

func GetChatMember(db *gorm.DB, chatID int64, userID int64) (*ChatUser, error)

GetChatMember 获得某个群成员表项

func GetChatMembers

func GetChatMembers(db *gorm.DB, chatID int64) ([]ChatUser, error)

GetChatMembers 获得一个群的所有成员表项

type MemberPermission

type MemberPermission int64

type Message

type Message struct {
	ID int64 `gorm:"primaryKey" json:"-"`
	// ChatID 消息所属的聊天
	ChatID int64 `gorm:"uniqueIndex:idx_chat_message_delete" json:"chat_id"`
	// MessageID 单个聊天中,MessageID 从 0 开始计数且独立
	MessageID int64 `gorm:"uniqueIndex:idx_chat_message_delete" json:"message_id"`

	Type    MsgType `gorm:"type:smallint not null;check:type >= 0" json:"type"`
	Message string  `gorm:"text not null" json:"message"`
	// SenderID 消息所属的用户,外键
	SenderID int64 `gorm:"type:bigint not null" json:"sender_id"`

	CreatedAt time.Time             `json:"created_at"`
	UpdatedAt time.Time             `json:"updated_at"`
	DeletedAt soft_delete.DeletedAt `gorm:"uniqueIndex:idx_chat_message_delete" json:"-"`
}

func CreateMessage

func CreateMessage(db *gorm.DB, chatID int64, senderID int64, typ MsgType, message string) (*Message, error)

CreateMessage 创建一条新的信息,每个 chat 中都有自己独立的一套从 1 开始的 message_id

func GetLatestMessages

func GetLatestMessages(db *gorm.DB, userID int64, chatIDs []int64) ([]Message, error)

GetLatestMessages 获得某个用户给定某些聊天的最新消息

func GetMessage

func GetMessage(db *gorm.DB, chatID int64, userID int64, messageID int64) (*Message, error)

GetMessage 使用 userID 的身份,获取从 chat 中某条消息.

func GetMessages

func GetMessages(db *gorm.DB, chatID int64, userID int64, fromID int64, toID int64) ([]Message, error)

GetMessages 使用 userID 的身份,获取从 chat 中从 fromID(inclusive) 到 toID(exclusive) 的信息。 如果 fromID 为 0,表示从头开始;如果 toID 为 0,表示到末尾为止.

func WithdrawMessage

func WithdrawMessage(db *gorm.DB, chatID int64, userID int64, messageID int64) (*Message, error)

WithdrawMessage 使用 userID 的身份,撤回某一条消息

type MsgType

type MsgType int64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL