Browse Source

Changed max attachment size to 100MB, fixed problem with System user having invalid timestamp

master
parent
commit
a6a75c22a2
  1. 2
      src/api/limits.go
  2. 2
      src/api/message.go
  3. 10
      src/api/websocket.go
  4. 2
      src/main.go
  5. 6
      src/server/server.go
  6. 14
      src/server/userHandler.go
  7. 10
      src/server/websocketHandler.go

2
src/api/limits.go

@ -7,4 +7,4 @@ const MaxMessageContentLen uint = 500
const MaxMessagesRemembered uint = 50 const MaxMessagesRemembered uint = 50
// Max filesize to accept as attachment // Max filesize to accept as attachment
const MaxAttachmentSize uint64 = 31457280 // 30MB const MaxAttachmentSize uint64 = 104857600 // 100MB

2
src/api/message.go

@ -48,7 +48,7 @@ func (db *DB) AddMessage(message Message) error {
} }
if count >= uint64(MaxMessagesRemembered) { if count >= uint64(MaxMessagesRemembered) {
// remove the last one // remove the last one if the max messages threshold has been exceeded
command = fmt.Sprintf("DELETE FROM %s WHERE timestamp = (SELECT MIN(timestamp) FROM %s)", MessagesTablename, MessagesTablename) command = fmt.Sprintf("DELETE FROM %s WHERE timestamp = (SELECT MIN(timestamp) FROM %s)", MessagesTablename, MessagesTablename)
_, err := db.Exec(command) _, err := db.Exec(command)
if err != nil { if err != nil {

10
src/api/websocket.go

@ -70,8 +70,9 @@ func (holder *WSHolder) HandleNewWebSocketMessages(ws *WS, output chan Message)
holder.Remove(ws.User.Name) holder.Remove(ws.User.Name)
disconnectionMessage := Message{ disconnectionMessage := Message{
From: UserSystem, TimeStamp: uint64(time.Now().UnixMilli()),
Contents: fmt.Sprintf("%s has disconnected", ws.User.Name), From: UserSystem,
Contents: fmt.Sprintf("%s has disconnected", ws.User.Name),
} }
output <- disconnectionMessage output <- disconnectionMessage
break break
@ -83,8 +84,9 @@ func (holder *WSHolder) HandleNewWebSocketMessages(ws *WS, output chan Message)
holder.Remove(ws.User.Name) holder.Remove(ws.User.Name)
disconnectionMessage := Message{ disconnectionMessage := Message{
From: UserSystem, TimeStamp: uint64(time.Now().UnixMilli()),
Contents: fmt.Sprintf("%s has disconnected", ws.User.Name), From: UserSystem,
Contents: fmt.Sprintf("%s has disconnected", ws.User.Name),
} }
output <- disconnectionMessage output <- disconnectionMessage

2
src/main.go

@ -26,7 +26,7 @@ import (
"unbewohnte/gochat/server" "unbewohnte/gochat/server"
) )
const version string = "0.1.1" const version string = "0.1.2"
var ( var (
port *uint = flag.Uint("port", 8080, "Set working port") port *uint = flag.Uint("port", 8080, "Set working port")

6
src/server/server.go

@ -139,7 +139,11 @@ func (s *Server) Start() {
go s.BroadcastMessages() go s.BroadcastMessages()
// clean attachments storage from time to time // clean attachments storage from time to time
// max attachment filesize * 50 is the limit, check every 5 sec // max attachment filesize * 50 is the limit, check every 5 sec
go manageAttachmentsStorage(filepath.Join(s.workingDir, attachmentsDirName), api.MaxAttachmentSize*50, time.Second*5) go manageAttachmentsStorage(
filepath.Join(s.workingDir, attachmentsDirName),
api.MaxAttachmentSize,
time.Second*5,
)
// fire up either a TLS or non-TLS server // fire up either a TLS or non-TLS server
if s.keyFile != "" && s.certFile != "" { if s.keyFile != "" && s.certFile != "" {

14
src/server/userHandler.go

@ -108,20 +108,6 @@ func (s *Server) HandlerUsers(w http.ResponseWriter, req *http.Request) {
w.Write(responseBytes) w.Write(responseBytes)
} }
// users, err := db.getAllUsers()
// if err != nil {
// http.Error(w, "", http.StatusInternalServerError)
// return
// }
// w.Header().Add("Content-type", "application/json")
// userBytes, err := json.Marshal(*users)
// if err != nil {
// break
// }
// w.Write(userBytes)
default: default:
http.Error(w, "wrong method", http.StatusMethodNotAllowed) http.Error(w, "wrong method", http.StatusMethodNotAllowed)
} }

10
src/server/websocketHandler.go

@ -82,8 +82,9 @@ func (s *Server) HandlerWebsockets(w http.ResponseWriter, req *http.Request) {
// notify chat that a new user has connected // notify chat that a new user has connected
newConnectionMessage := api.Message{ newConnectionMessage := api.Message{
From: api.UserSystem, TimeStamp: uint64(time.Now().UnixMilli()),
Contents: fmt.Sprintf("%s has connected", newWS.User.Name), From: api.UserSystem,
Contents: fmt.Sprintf("%s has connected", newWS.User.Name),
} }
s.incomingMessages <- newConnectionMessage s.incomingMessages <- newConnectionMessage
} }
@ -96,11 +97,6 @@ func (s *Server) BroadcastMessages() {
break break
} }
if message.From.Name == api.UserSystem.Name {
// add timestapm manually for the system user
message.TimeStamp = uint64(time.Now().Unix())
}
// add incoming message to the db // add incoming message to the db
err := s.db.AddMessage(message) err := s.db.AddMessage(message)
if err != nil { if err != nil {

Loading…
Cancel
Save