Skip to content

Commit

Permalink
add sharing operation
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Oct 18, 2023
1 parent e9a85e4 commit 5936da8
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

👋 Lightweight ChatGPT Chat Platform

[官网](https://chatnio.net) | [开放文档](https://docs.chatnio.net) | [SDKs](https://docs.chatnio.net/kuai-su-kai-shi) | [QQ 群](http://qm.qq.com/cgi-bin/qm/qr?group_code=565902327)
[官网](https://chatnio.net) | [开放文档](https://docs.chatnio.net) | [SDKs](https://docs.chatnio.net/kuai-su-kai-shi) | [QQ 群](http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=YKcvGGlM03LYWlPk-iosqAqL4qHwOtBx&authKey=6cjCqRNKNuOUJltyo%2FNgmKm%2BS%2FUCtAyVHCnirHyxNuxGExUHsJRtlSaW1EeDxhNx&noverify=0&group_code=565902327)

[![code-stats](https://stats.deeptrain.net/repo/Deeptrain-Community/chatnio)](https://stats.deeptrain.net)

Expand Down
20 changes: 20 additions & 0 deletions connection/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func ConnectMySQL() *sql.DB {

CreateUserTable(db)
CreateConversationTable(db)
CreateSharingTable(db)
CreatePackageTable(db)
CreateQuotaTable(db)
CreateSubscriptionTable(db)
Expand Down Expand Up @@ -100,6 +101,25 @@ func CreateConversationTable(db *sql.DB) {
}
}

func CreateSharingTable(db *sql.DB) {
// refs is an array of message id, separated by comma (-1 means all messages)
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS sharing (
id INT PRIMARY KEY AUTO_INCREMENT,
hash CHAR(32) UNIQUE,
user_id INT,
conversation_id INT,
refs TEXT,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES auth(id),
FOREIGN KEY (conversation_id) REFERENCES conversation(id)
);
`)
if err != nil {
log.Fatal(err)
}
}

func CreateSubscriptionTable(db *sql.DB) {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS subscription (
Expand Down
4 changes: 4 additions & 0 deletions manager/conversation/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func (c *Conversation) GetMessage() []globals.Message {
return c.Message
}

func (c *Conversation) GetMessageById(id int) globals.Message {
return c.Message[id]
}

func (c *Conversation) GetMessageSize() int {
return len(c.Message)
}
Expand Down
98 changes: 98 additions & 0 deletions manager/conversation/shared.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package conversation

import (
"chat/auth"
"chat/globals"
"chat/utils"
"database/sql"
"strconv"
"strings"
"time"
)

type SharedForm struct {
Username string `json:"username"`
Name string `json:"name"`
Messages []globals.Message `json:"messages"`
Time time.Time `json:"time"`
}

type SharedHashForm struct {
Id int64 `json:"id"`
ConversationId int64 `json:"conversation_id"`
Refs []int `json:"refs"`
}

func GetRef(refs []int) (result string) {
for _, v := range refs {
result += strconv.Itoa(v) + ","
}
return strings.TrimSuffix(result, ",")
}

func (c *Conversation) ShareConversation(db *sql.DB, user *auth.User, refs []int) bool {
if c.GetId() < 0 || user == nil {
return false
}

ref := GetRef(refs)
hash := utils.Md5EncryptForm(SharedHashForm{
Id: c.GetId(),
ConversationId: c.GetId(),
Refs: refs,
})

_, err := db.Exec(`
INSERT INTO sharing (hash, user_id, conversation_id, refs) VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE refs = ?
`, hash, user.GetID(db), c.GetId(), ref, ref)

return err == nil
}

func GetSharedMessages(db *sql.DB, userId int64, conversationId int64, refs []string) []globals.Message {
conversation := LoadConversation(db, userId, conversationId)
if conversation == nil {
return nil
}

messages := make([]globals.Message, 0)
for _, v := range refs {
if v == "-1" {
return conversation.GetMessage()
} else {
id, err := strconv.Atoi(v)
if err != nil {
continue
}
messages = append(messages, conversation.GetMessageById(id))
}
}
return messages
}

func GetSharedConversation(db *sql.DB, hash string) (*SharedForm, error) {
var shared SharedForm
var (
uid int64
cid int64
ref string
updated []uint8
)
if err := db.QueryRow(`
SELECT auth.username, sharing.refs, sharing.updated_at, conversation.conversation_name,
sharing.user_id, sharing.conversation_id
FROM sharing
INNER JOIN auth ON auth.id = sharing.user_id
INNER JOIN conversation ON conversation.id = sharing.conversation_id
WHERE sharing.hash = ?
`, hash).Scan(&shared.Username, &ref, &updated, &shared.Name, &uid, &cid); err != nil {
return nil, err
}

shared.Time = *utils.ConvertTime(updated)
refs := strings.Split(ref, ",")
shared.Messages = GetSharedMessages(db, uid, cid, refs)

return &shared, nil
}
17 changes: 16 additions & 1 deletion utils/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,34 @@ package utils
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
crand "crypto/rand"
"crypto/sha256"
"encoding/hex"
"io"
)

func Sha2Encrypt(raw string) string {
// return 64-bit hash
hash := sha256.Sum256([]byte(raw))
return hex.EncodeToString(hash[:])
}

func Sha2EncryptForm(form interface{}) string {
// return 64-bit hash
hash := sha256.Sum256([]byte(ToJson(form)))
return hex.EncodeToString(hash[:])
}

func Md5Encrypt(raw string) string {
hash := sha256.Sum256([]byte(raw))
// return 32-bit hash
hash := md5.Sum([]byte(raw))
return hex.EncodeToString(hash[:])
}

func Md5EncryptForm(form interface{}) string {
// return 32-bit hash
hash := md5.Sum([]byte(ToJson(form)))
return hex.EncodeToString(hash[:])
}

Expand Down

0 comments on commit 5936da8

Please sign in to comment.