🗃️ Add relations between related event and original event

This commit is contained in:
LittleSheep 2024-11-18 23:28:17 +08:00
parent 6c60e250e1
commit c642f5ee44
3 changed files with 43 additions and 38 deletions

View File

@ -57,14 +57,15 @@ func newMessageEvent(c *fiber.Ctx) error {
_ = jsoniter.Unmarshal(raw, &parsed) _ = jsoniter.Unmarshal(raw, &parsed)
event := models.Event{ event := models.Event{
Uuid: data.Uuid, Uuid: data.Uuid,
Body: parsed, Body: parsed,
Type: data.Type, Type: data.Type,
Sender: member, Sender: member,
Channel: channel, Channel: channel,
QuoteEventID: data.Body.QuoteEventID, QuoteEventID: data.Body.QuoteEventID,
ChannelID: channel.ID, RelatedEventID: data.Body.RelatedEventID,
SenderID: member.ID, ChannelID: channel.ID,
SenderID: member.ID,
} }
if event, err = services.NewEvent(event); err != nil { if event, err = services.NewEvent(event); err != nil {

View File

@ -15,24 +15,26 @@ const (
type Event struct { type Event struct {
cruda.BaseModel cruda.BaseModel
Uuid string `json:"uuid"` Uuid string `json:"uuid"`
Body datatypes.JSONMap `json:"body"` Body datatypes.JSONMap `json:"body"`
Type string `json:"type"` Type string `json:"type"`
Channel Channel `json:"channel"` Channel Channel `json:"channel"`
Sender ChannelMember `json:"sender"` Sender ChannelMember `json:"sender"`
QuoteEventID *uint `json:"quote_event_id,omitempty"` QuoteEventID *uint `json:"quote_event_id,omitempty"`
QuoteEvent *Event `json:"quote_event,omitempty" gorm:"foreignKey:QuoteEventID"` QuoteEvent *Event `json:"quote_event,omitempty" gorm:"foreignKey:QuoteEventID"`
ChannelID uint `json:"channel_id"` RelatedEventID *uint `json:"related_event_id,omitempty"`
SenderID uint `json:"sender_id"` RelatedEvent *Event `json:"related_event,omitempty" gorm:"foreignKey:RelatedEventID"`
ChannelID uint `json:"channel_id"`
SenderID uint `json:"sender_id"`
} }
// Event Payloads // Event Payloads
type EventMessageBody struct { type EventMessageBody struct {
Text string `json:"text,omitempty"` Text string `json:"text,omitempty"`
Algorithm string `json:"algorithm,omitempty"` Algorithm string `json:"algorithm,omitempty"`
Attachments []string `json:"attachments,omitempty"` Attachments []string `json:"attachments,omitempty"`
QuoteEventID *uint `json:"quote_event,omitempty"` QuoteEventID *uint `json:"quote_event,omitempty"`
RelatedEvent *uint `json:"related_event,omitempty"` RelatedEventID *uint `json:"related_event,omitempty"`
RelatedUsers []uint `json:"related_users,omitempty"` RelatedUsers []uint `json:"related_users,omitempty"`
} }

View File

@ -19,16 +19,17 @@ func EditMessage(event models.Event, body models.EventMessageBody) (models.Event
if err != nil { if err != nil {
return event, err return event, err
} }
body.RelatedEvent = &event.ID body.RelatedEventID = &event.ID
_, err = NewEvent(models.Event{ _, err = NewEvent(models.Event{
Uuid: uuid.NewString(), Uuid: uuid.NewString(),
Body: EncodeMessageBody(body), Body: EncodeMessageBody(body),
Type: models.EventMessageEdit, Type: models.EventMessageEdit,
Channel: event.Channel, Channel: event.Channel,
Sender: event.Sender, Sender: event.Sender,
QuoteEventID: body.QuoteEventID, QuoteEventID: body.QuoteEventID,
ChannelID: event.ChannelID, RelatedEventID: body.RelatedEventID,
SenderID: event.SenderID, ChannelID: event.ChannelID,
SenderID: event.SenderID,
}) })
if err != nil { if err != nil {
return event, err return event, err
@ -45,13 +46,14 @@ func DeleteMessage(event models.Event) (models.Event, error) {
_, err = NewEvent(models.Event{ _, err = NewEvent(models.Event{
Uuid: uuid.NewString(), Uuid: uuid.NewString(),
Body: EncodeMessageBody(models.EventMessageBody{ Body: EncodeMessageBody(models.EventMessageBody{
RelatedEvent: &event.ID, RelatedEventID: &event.ID,
}), }),
Type: models.EventMessageDelete, Type: models.EventMessageDelete,
Channel: event.Channel, Channel: event.Channel,
Sender: event.Sender, Sender: event.Sender,
ChannelID: event.ChannelID, RelatedEventID: &event.ID,
SenderID: event.SenderID, ChannelID: event.ChannelID,
SenderID: event.SenderID,
}) })
if err != nil { if err != nil {
return event, err return event, err