Add room event on changint topic

This commit is contained in:
nxshock 2019-07-21 16:30:14 +05:00
parent cbb1befd60
commit feb71433c4
5 changed files with 90 additions and 34 deletions

View File

@ -0,0 +1,30 @@
package memory
import (
"encoding/json"
"time"
"github.com/nxshock/signaller/internal"
"github.com/nxshock/signaller/internal/models/rooms"
)
type RoomEvent struct {
Content json.RawMessage
Type rooms.Type
EventID string
Sender internal.User
OriginServerTS time.Time
Room internal.Room
}
func (roomEvent *RoomEvent) ToEvent() rooms.Event {
event := rooms.Event{
Content: roomEvent.Content,
Type: roomEvent.Type,
EventID: roomEvent.EventID,
Sender: roomEvent.Sender.ID(),
OriginServerTS: roomEvent.OriginServerTS.Unix(),
RoomID: roomEvent.Room.ID()}
return event
}

View File

@ -2,6 +2,7 @@ package memory
import ( import (
"sync" "sync"
"time"
"github.com/nxshock/signaller/internal/models" "github.com/nxshock/signaller/internal/models"
@ -19,7 +20,7 @@ type Room struct {
creator internal.User creator internal.User
events []rooms.Event events []RoomEvent
mutex sync.RWMutex mutex sync.RWMutex
} }
@ -63,7 +64,12 @@ func (room *Room) Events() []rooms.Event {
room.mutex.RLock() room.mutex.RLock()
defer room.mutex.RUnlock() defer room.mutex.RUnlock()
return room.events result := make([]rooms.Event, 0)
for _, v := range room.events {
result = append(result, v.ToEvent())
}
return result
} }
func (room *Room) Creator() internal.User { func (room *Room) Creator() internal.User {
@ -73,13 +79,6 @@ func (room *Room) Creator() internal.User {
return room.creator return room.creator
} }
func (room *Room) NewEvent(event rooms.Event) {
room.mutex.Lock()
defer room.mutex.Unlock()
room.events = append(room.events, event)
}
func (room *Room) SetTopic(user internal.User, topic string) *models.ApiError { func (room *Room) SetTopic(user internal.User, topic string) *models.ApiError {
room.mutex.Lock() room.mutex.Lock()
defer room.mutex.Unlock() defer room.mutex.Unlock()
@ -89,6 +88,11 @@ func (room *Room) SetTopic(user internal.User, topic string) *models.ApiError {
} }
room.topic = topic room.topic = topic
room.events = append(room.events, RoomEvent{
Type: rooms.Topic,
Sender: user,
OriginServerTS: time.Now(),
Room: room})
return nil return nil
} }

View File

@ -79,6 +79,7 @@ func TestSetRoomTopic(t *testing.T) {
err := room.SetTopic(user, newTopic) err := room.SetTopic(user, newTopic)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, newTopic, room.Topic()) assert.Equal(t, newTopic, room.Topic())
assert.Equal(t, 1, len(room.Events()))
} }
func TestSetRoomTopicWithnprivelegedUser(t *testing.T) { func TestSetRoomTopicWithnprivelegedUser(t *testing.T) {

View File

@ -7,29 +7,6 @@ import (
// Type is type of event // Type is type of event
type Type string type Type string
const (
// https://matrix.org/docs/spec/client_server/latest#m-room-aliases
RoomAliases Type = "m.room.aliases"
// https://matrix.org/docs/spec/client_server/latest#m-room-canonical-alias
RoomCanonicalAlias Type = "m.room.canonical_alias"
// https://matrix.org/docs/spec/client_server/latest#m-room-create
RoomCreate Type = "m.room.create"
// https://matrix.org/docs/spec/client_server/latest#m-room-join-rules
RoomJoinRules Type = "m.room.join_rules"
// https://matrix.org/docs/spec/client_server/latest#m-room-member
RoomMember Type = "m.room.member"
// https://matrix.org/docs/spec/client_server/latest#m-room-power-levels
RoomPowerLevels Type = "m.room.power_levels"
// https://matrix.org/docs/spec/client_server/latest#m-room-redaction
RoomRedaction Type = "m.room.redaction"
)
// Event is the basic set of fields all events must have // Event is the basic set of fields all events must have
// https://matrix.org/docs/spec/client_server/latest#event-fields // https://matrix.org/docs/spec/client_server/latest#event-fields
type Event struct { type Event struct {

View File

@ -6,13 +6,57 @@ import (
"github.com/nxshock/signaller/internal/models/events" "github.com/nxshock/signaller/internal/models/events"
) )
// Type is type of event
type Type string
const (
// https://matrix.org/docs/spec/client_server/latest#m-room-aliases
Aliases Type = "m.room.aliases"
// https://matrix.org/docs/spec/client_server/latest#m-room-canonical-alias
CanonicalAlias = "m.room.canonical_alias"
// https://matrix.org/docs/spec/client_server/latest#m-room-create
Create = "m.room.create"
// https://matrix.org/docs/spec/client_server/latest#m-room-join-rules
JoinRules = "m.room.join_rules"
// https://matrix.org/docs/spec/client_server/latest#m-room-member
Member = "m.room.member"
// https://matrix.org/docs/spec/client_server/latest#m-room-power-levels
PowerLevels = "m.room.power_levels"
// https://matrix.org/docs/spec/client_server/latest#m-room-redaction
Redaction = "m.room.redaction"
// https://matrix.org/docs/spec/client_server/latest#m-room-message
Message = "m.room.message"
// https://matrix.org/docs/spec/client_server/latest#m-room-message-feedback
MessageFeedback = "m.room.message.feedback"
// https://matrix.org/docs/spec/client_server/latest#m-room-name
Name = "m.room.name"
// https://matrix.org/docs/spec/client_server/latest#m-room-topic
Topic = "m.room.topic"
// https://matrix.org/docs/spec/client_server/latest#m-room-avatar
Avatar = "m.room.avatar"
// https://matrix.org/docs/spec/client_server/latest#m-room-pinned-events
PinnedEvents = "m.room.pinned_events"
)
// https://matrix.org/docs/spec/client_server/latest#room-event-fields // https://matrix.org/docs/spec/client_server/latest#room-event-fields
type Event struct { type Event struct {
Content json.RawMessage `json:"content"` // Required. The fields in this object will vary depending on the type of event. When interacting with the REST API, this is the HTTP body. Content json.RawMessage `json:"content"` // Required. The fields in this object will vary depending on the type of event. When interacting with the REST API, this is the HTTP body.
Type string `json:"type"` // Required. The type of event. This SHOULD be namespaced similar to Java package naming conventions e.g. 'com.example.subdomain.event.type' Type Type `json:"type"` // Required. The type of event. This SHOULD be namespaced similar to Java package naming conventions e.g. 'com.example.subdomain.event.type'
EventID string `json:"event_id"` // Required. The globally unique event identifier. EventID string `json:"event_id"` // Required. The globally unique event identifier.
Sender string `json:"sender"` // Required. Contains the fully-qualified ID of the user who sent this event. Sender string `json:"sender"` // Required. Contains the fully-qualified ID of the user who sent this event.
OriginServerTS int `json:"origin_server_ts"` // Required. Timestamp in milliseconds on originating homeserver when this event was sent. OriginServerTS int64 `json:"origin_server_ts"` // Required. Timestamp in milliseconds on originating homeserver when this event was sent.
Unsigned UnsignedData `json:"unsigned,omitempty"` // Contains optional extra information about the event. Unsigned UnsignedData `json:"unsigned,omitempty"` // Contains optional extra information about the event.
RoomID string `json:"room_id"` // Required. The ID of the room associated with this event. Will not be present on events that arrive through /sync, despite being required everywhere else. RoomID string `json:"room_id"` // Required. The ID of the room associated with this event. Will not be present on events that arrive through /sync, despite being required everywhere else.
} }