summaryrefslogtreecommitdiffstats
path: root/store/sql_channel_store.go
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2015-07-29 11:30:56 -0400
committerChristopher Speller <crspeller@gmail.com>2015-07-29 14:35:25 -0400
commit3f97e06bf80c789ce8e2d572afdac65a73d417c8 (patch)
treece40eba90031c423c209670c28f902966e0d4f9b /store/sql_channel_store.go
parent0f54233f668901fde880f18354f406b981fc4d6d (diff)
downloadchat-3f97e06bf80c789ce8e2d572afdac65a73d417c8.tar.gz
chat-3f97e06bf80c789ce8e2d572afdac65a73d417c8.tar.bz2
chat-3f97e06bf80c789ce8e2d572afdac65a73d417c8.zip
Adding etag to channel extra_info api call.
Diffstat (limited to 'store/sql_channel_store.go')
-rw-r--r--store/sql_channel_store.go71
1 files changed, 57 insertions, 14 deletions
diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go
index ce1a8c6fa..cac5c681b 100644
--- a/store/sql_channel_store.go
+++ b/store/sql_channel_store.go
@@ -36,6 +36,7 @@ func NewSqlChannelStore(sqlStore *SqlStore) ChannelStore {
}
func (s SqlChannelStore) UpgradeSchemaIfNeeded() {
+ s.CreateColumnIfNotExists("Channels", "ExtraUpdateAt", "TotalMsgCount", "bigint(20)", "0")
}
func (s SqlChannelStore) CreateIndexesIfNotExists() {
@@ -142,6 +143,25 @@ func (s SqlChannelStore) Update(channel *model.Channel) StoreChannel {
return storeChannel
}
+func (s SqlChannelStore) extraUpdated(channel *model.Channel) StoreChannel {
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := StoreResult{}
+
+ channel.ExtraUpdated()
+
+ if count, err := s.GetMaster().Update(channel); err != nil || count != 1 {
+ result.Err = model.NewAppError("SqlChannelStore.extraUpdated", "Problem updating members last updated time", "id="+channel.Id+", "+err.Error())
+ }
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
+
func (s SqlChannelStore) Get(id string) StoreChannel {
storeChannel := make(StoreChannel)
@@ -288,20 +308,31 @@ func (s SqlChannelStore) SaveMember(member *model.ChannelMember) StoreChannel {
go func() {
result := StoreResult{}
- member.PreSave()
- if result.Err = member.IsValid(); result.Err != nil {
- storeChannel <- result
- return
- }
+ // Grab the channel we are saving this member to
+ if cr := <-s.Get(member.ChannelId); cr.Err != nil {
+ result.Err = cr.Err
+ } else {
+ channel := cr.Data.(*model.Channel)
+
+ member.PreSave()
+ if result.Err = member.IsValid(); result.Err != nil {
+ storeChannel <- result
+ return
+ }
- if err := s.GetMaster().Insert(member); err != nil {
- if IsUniqueConstraintError(err.Error(), "ChannelId", "channelmembers_pkey") {
- result.Err = model.NewAppError("SqlChannelStore.SaveMember", "A channel member with that id already exists", "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error())
+ if err := s.GetMaster().Insert(member); err != nil {
+ if IsUniqueConstraintError(err.Error(), "ChannelId", "channelmembers_pkey") {
+ result.Err = model.NewAppError("SqlChannelStore.SaveMember", "A channel member with that id already exists", "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error())
+ } else {
+ result.Err = model.NewAppError("SqlChannelStore.SaveMember", "We couldn't save the channel member", "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error())
+ }
} else {
- result.Err = model.NewAppError("SqlChannelStore.SaveMember", "We couldn't save the channel member", "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error())
+ result.Data = member
+ // If sucessfull record members have changed in channel
+ if mu := <-s.extraUpdated(channel); mu.Err != nil {
+ result.Err = mu.Err
+ }
}
- } else {
- result.Data = member
}
storeChannel <- result
@@ -383,9 +414,21 @@ func (s SqlChannelStore) RemoveMember(channelId string, userId string) StoreChan
go func() {
result := StoreResult{}
- _, err := s.GetMaster().Exec("DELETE FROM ChannelMembers WHERE ChannelId = :ChannelId AND UserId = :UserId", map[string]interface{}{"ChannelId": channelId, "UserId": userId})
- if err != nil {
- result.Err = model.NewAppError("SqlChannelStore.RemoveMember", "We couldn't remove the channel member", "channel_id="+channelId+", user_id="+userId+", "+err.Error())
+ // Grab the channel we are saving this member to
+ if cr := <-s.Get(channelId); cr.Err != nil {
+ result.Err = cr.Err
+ } else {
+ channel := cr.Data.(*model.Channel)
+
+ _, err := s.GetMaster().Exec("DELETE FROM ChannelMembers WHERE ChannelId = :ChannelId AND UserId = :UserId", map[string]interface{}{"ChannelId": channelId, "UserId": userId})
+ if err != nil {
+ result.Err = model.NewAppError("SqlChannelStore.RemoveMember", "We couldn't remove the channel member", "channel_id="+channelId+", user_id="+userId+", "+err.Error())
+ } else {
+ // If sucessfull record members have changed in channel
+ if mu := <-s.extraUpdated(channel); mu.Err != nil {
+ result.Err = mu.Err
+ }
+ }
}
storeChannel <- result