Pārlūkot izejas kodu

Merge pull request #811 from slingamn/history_nits.4

tweaks to persistent history
tags/v2.0.0-rc1
Shivaram Lingamneni 4 gadus atpakaļ
vecāks
revīzija
ec1a718275
Revīzijas autora e-pasta adrese nav piesaistīta nevienam kontam
5 mainītis faili ar 106 papildinājumiem un 87 dzēšanām
  1. 16
    18
      irc/channel.go
  2. 12
    20
      irc/client.go
  3. 25
    10
      irc/config.go
  4. 13
    6
      irc/handlers.go
  5. 40
    33
      irc/server.go

+ 16
- 18
irc/channel.go Parādīt failu

113
 }
113
 }
114
 
114
 
115
 func (channel *Channel) resizeHistory(config *Config) {
115
 func (channel *Channel) resizeHistory(config *Config) {
116
-	_, ephemeral, _ := channel.historyStatus(config)
117
-	if ephemeral {
116
+	status, _ := channel.historyStatus(config)
117
+	if status == HistoryEphemeral {
118
 		channel.history.Resize(config.History.ChannelLength, config.History.AutoresizeWindow)
118
 		channel.history.Resize(config.History.ChannelLength, config.History.AutoresizeWindow)
119
 	} else {
119
 	} else {
120
 		channel.history.Resize(0, 0)
120
 		channel.history.Resize(0, 0)
588
 
588
 
589
 // figure out where history is being stored: persistent, ephemeral, or neither
589
 // figure out where history is being stored: persistent, ephemeral, or neither
590
 // target is only needed if we're doing persistent history
590
 // target is only needed if we're doing persistent history
591
-func (channel *Channel) historyStatus(config *Config) (persistent, ephemeral bool, target string) {
592
-	if !config.History.Persistent.Enabled {
593
-		return false, config.History.Enabled, ""
591
+func (channel *Channel) historyStatus(config *Config) (status HistoryStatus, target string) {
592
+	if !config.History.Enabled {
593
+		return HistoryDisabled, ""
594
 	}
594
 	}
595
 
595
 
596
 	channel.stateMutex.RLock()
596
 	channel.stateMutex.RLock()
599
 	registered := channel.registeredFounder != ""
599
 	registered := channel.registeredFounder != ""
600
 	channel.stateMutex.RUnlock()
600
 	channel.stateMutex.RUnlock()
601
 
601
 
602
-	historyStatus = historyEnabled(config.History.Persistent.RegisteredChannels, historyStatus)
603
-
604
 	// ephemeral history: either the channel owner explicitly set the ephemeral preference,
602
 	// ephemeral history: either the channel owner explicitly set the ephemeral preference,
605
 	// or persistent history is disabled for unregistered channels
603
 	// or persistent history is disabled for unregistered channels
606
 	if registered {
604
 	if registered {
607
-		ephemeral = (historyStatus == HistoryEphemeral)
608
-		persistent = (historyStatus == HistoryPersistent)
605
+		return historyEnabled(config.History.Persistent.RegisteredChannels, historyStatus), target
609
 	} else {
606
 	} else {
610
-		ephemeral = config.History.Enabled && !config.History.Persistent.UnregisteredChannels
611
-		persistent = config.History.Persistent.UnregisteredChannels
607
+		if config.History.Persistent.UnregisteredChannels {
608
+			return HistoryPersistent, target
609
+		} else {
610
+			return HistoryEphemeral, target
611
+		}
612
 	}
612
 	}
613
-	return
614
 }
613
 }
615
 
614
 
616
 func (channel *Channel) AddHistoryItem(item history.Item) (err error) {
615
 func (channel *Channel) AddHistoryItem(item history.Item) (err error) {
618
 		return
617
 		return
619
 	}
618
 	}
620
 
619
 
621
-	persistent, ephemeral, target := channel.historyStatus(channel.server.Config())
622
-	if ephemeral {
620
+	status, target := channel.historyStatus(channel.server.Config())
621
+	if status == HistoryPersistent {
622
+		err = channel.server.historyDB.AddChannelItem(target, item)
623
+	} else if status == HistoryEphemeral {
623
 		channel.history.Add(item)
624
 		channel.history.Add(item)
624
 	}
625
 	}
625
-	if persistent {
626
-		return channel.server.historyDB.AddChannelItem(target, item)
627
-	}
628
-	return nil
626
+	return
629
 }
627
 }
630
 
628
 
631
 // Join joins the given client to this channel (if they can be joined).
629
 // Join joins the given client to this channel (if they can be joined).

+ 12
- 20
irc/client.go Parādīt failu

354
 }
354
 }
355
 
355
 
356
 func (client *Client) resizeHistory(config *Config) {
356
 func (client *Client) resizeHistory(config *Config) {
357
-	_, ephemeral, _ := client.historyStatus(config)
358
-	if ephemeral {
357
+	status, _ := client.historyStatus(config)
358
+	if status == HistoryEphemeral {
359
 		client.history.Resize(config.History.ClientLength, config.History.AutoresizeWindow)
359
 		client.history.Resize(config.History.ClientLength, config.History.AutoresizeWindow)
360
 	} else {
360
 	} else {
361
 		client.history.Resize(0, 0)
361
 		client.history.Resize(0, 0)
749
 		for _, member := range channel.Members() {
749
 		for _, member := range channel.Members() {
750
 			friends.Add(member)
750
 			friends.Add(member)
751
 		}
751
 		}
752
-		_, ephemeral, _ := channel.historyStatus(config)
753
-		if ephemeral {
752
+		status, _ := channel.historyStatus(config)
753
+		if status == HistoryEphemeral {
754
 			lastDiscarded := channel.history.LastDiscarded()
754
 			lastDiscarded := channel.history.LastDiscarded()
755
 			if oldestLostMessage.Before(lastDiscarded) {
755
 			if oldestLostMessage.Before(lastDiscarded) {
756
 				oldestLostMessage = lastDiscarded
756
 				oldestLostMessage = lastDiscarded
757
 			}
757
 			}
758
 		}
758
 		}
759
 	}
759
 	}
760
-	_, cEphemeral, _ := client.historyStatus(config)
761
-	if cEphemeral {
760
+	cHistoryStatus, _ := client.historyStatus(config)
761
+	if cHistoryStatus == HistoryEphemeral {
762
 		lastDiscarded := client.history.LastDiscarded()
762
 		lastDiscarded := client.history.LastDiscarded()
763
 		if oldestLostMessage.Before(lastDiscarded) {
763
 		if oldestLostMessage.Before(lastDiscarded) {
764
 			oldestLostMessage = lastDiscarded
764
 			oldestLostMessage = lastDiscarded
1551
 	}
1551
 	}
1552
 }
1552
 }
1553
 
1553
 
1554
-func (client *Client) historyStatus(config *Config) (persistent, ephemeral bool, target string) {
1554
+func (client *Client) historyStatus(config *Config) (status HistoryStatus, target string) {
1555
 	if !config.History.Enabled {
1555
 	if !config.History.Enabled {
1556
-		return
1557
-	} else if !config.History.Persistent.Enabled {
1558
-		ephemeral = true
1559
-		return
1556
+		return HistoryDisabled, ""
1560
 	}
1557
 	}
1561
 
1558
 
1562
 	client.stateMutex.RLock()
1559
 	client.stateMutex.RLock()
1563
-	alwaysOn := client.alwaysOn
1560
+	loggedIn := client.account != ""
1564
 	historyStatus := client.accountSettings.DMHistory
1561
 	historyStatus := client.accountSettings.DMHistory
1565
 	target = client.nickCasefolded
1562
 	target = client.nickCasefolded
1566
 	client.stateMutex.RUnlock()
1563
 	client.stateMutex.RUnlock()
1567
 
1564
 
1568
-	if !alwaysOn {
1569
-		ephemeral = true
1570
-		return
1565
+	if !loggedIn {
1566
+		return HistoryEphemeral, ""
1571
 	}
1567
 	}
1572
-
1573
-	historyStatus = historyEnabled(config.History.Persistent.DirectMessages, historyStatus)
1574
-	ephemeral = (historyStatus == HistoryEphemeral)
1575
-	persistent = (historyStatus == HistoryPersistent)
1576
-	return
1568
+	return historyEnabled(config.History.Persistent.DirectMessages, historyStatus), target
1577
 }
1569
 }
1578
 
1570
 
1579
 // these are bit flags indicating what part of the client status is "dirty"
1571
 // these are bit flags indicating what part of the client status is "dirty"

+ 25
- 10
irc/config.go Parādīt failu

185
 	}
185
 	}
186
 }
186
 }
187
 
187
 
188
+// XXX you must have already checked History.Enabled before calling this
188
 func historyEnabled(serverSetting PersistentStatus, localSetting HistoryStatus) (result HistoryStatus) {
189
 func historyEnabled(serverSetting PersistentStatus, localSetting HistoryStatus) (result HistoryStatus) {
189
-	if serverSetting == PersistentDisabled {
190
-		return HistoryDisabled
191
-	} else if serverSetting == PersistentMandatory {
190
+	switch serverSetting {
191
+	case PersistentMandatory:
192
 		return HistoryPersistent
192
 		return HistoryPersistent
193
-	} else if serverSetting == PersistentOptOut {
193
+	case PersistentOptOut:
194
 		if localSetting == HistoryDefault {
194
 		if localSetting == HistoryDefault {
195
 			return HistoryPersistent
195
 			return HistoryPersistent
196
 		} else {
196
 		} else {
197
 			return localSetting
197
 			return localSetting
198
 		}
198
 		}
199
-	} else if serverSetting == PersistentOptIn {
200
-		if localSetting >= HistoryEphemeral {
201
-			return localSetting
202
-		} else {
199
+	case PersistentOptIn:
200
+		switch localSetting {
201
+		case HistoryPersistent:
202
+			return HistoryPersistent
203
+		case HistoryEphemeral, HistoryDefault:
204
+			return HistoryEphemeral
205
+		default:
203
 			return HistoryDisabled
206
 			return HistoryDisabled
204
 		}
207
 		}
205
-	} else {
206
-		return HistoryDisabled
208
+	case PersistentDisabled:
209
+		if localSetting == HistoryDisabled {
210
+			return HistoryDisabled
211
+		} else {
212
+			return HistoryEphemeral
213
+		}
214
+	default:
215
+		// PersistentUnspecified: shouldn't happen because the deserializer converts it
216
+		// to PersistentDisabled
217
+		if localSetting == HistoryDefault {
218
+			return HistoryEphemeral
219
+		} else {
220
+			return localSetting
221
+		}
207
 	}
222
 	}
208
 }
223
 }
209
 
224
 

+ 13
- 6
irc/handlers.go Parādīt failu

551
 	if maxChathistoryLimit == 0 {
551
 	if maxChathistoryLimit == 0 {
552
 		return
552
 		return
553
 	}
553
 	}
554
+	preposition := strings.ToLower(msg.Params[0])
555
+	target = msg.Params[1]
554
 
556
 
555
 	parseQueryParam := func(param string) (msgid string, timestamp time.Time, err error) {
557
 	parseQueryParam := func(param string) (msgid string, timestamp time.Time, err error) {
558
+		if param == "*" && (preposition == "before" || preposition == "between") {
559
+			// XXX compatibility with kiwi, which as of February 2020 is
560
+			// using BEFORE * as a synonym for LATEST *
561
+			return
562
+		}
556
 		err = utils.ErrInvalidParams
563
 		err = utils.ErrInvalidParams
557
 		pieces := strings.SplitN(param, "=", 2)
564
 		pieces := strings.SplitN(param, "=", 2)
558
 		if len(pieces) < 2 {
565
 		if len(pieces) < 2 {
580
 		return
587
 		return
581
 	}
588
 	}
582
 
589
 
583
-	preposition := strings.ToLower(msg.Params[0])
584
-	target = msg.Params[1]
585
 	channel, sequence, err = server.GetHistorySequence(nil, client, target)
590
 	channel, sequence, err = server.GetHistorySequence(nil, client, target)
586
 	if err != nil || sequence == nil {
591
 	if err != nil || sequence == nil {
587
 		return
592
 		return
1995
 		}
2000
 		}
1996
 		targetedItem := item
2001
 		targetedItem := item
1997
 		targetedItem.Params[0] = tnick
2002
 		targetedItem.Params[0] = tnick
1998
-		cPersistent, cEphemeral, _ := client.historyStatus(config)
1999
-		tPersistent, tEphemeral, _ := user.historyStatus(config)
2003
+		cStatus, _ := client.historyStatus(config)
2004
+		tStatus, _ := user.historyStatus(config)
2000
 		// add to ephemeral history
2005
 		// add to ephemeral history
2001
-		if cEphemeral {
2006
+		if cStatus == HistoryEphemeral {
2002
 			targetedItem.CfCorrespondent = tDetails.nickCasefolded
2007
 			targetedItem.CfCorrespondent = tDetails.nickCasefolded
2003
 			client.history.Add(targetedItem)
2008
 			client.history.Add(targetedItem)
2004
 		}
2009
 		}
2005
-		if tEphemeral && client != user {
2010
+		if tStatus == HistoryEphemeral && client != user {
2006
 			item.CfCorrespondent = details.nickCasefolded
2011
 			item.CfCorrespondent = details.nickCasefolded
2007
 			user.history.Add(item)
2012
 			user.history.Add(item)
2008
 		}
2013
 		}
2014
+		cPersistent := cStatus == HistoryPersistent
2015
+		tPersistent := tStatus == HistoryPersistent
2009
 		if cPersistent || tPersistent {
2016
 		if cPersistent || tPersistent {
2010
 			item.CfCorrespondent = ""
2017
 			item.CfCorrespondent = ""
2011
 			server.historyDB.AddDirectMessage(details.nickCasefolded, user.NickCasefolded(), cPersistent, tPersistent, targetedItem)
2018
 			server.historyDB.AddDirectMessage(details.nickCasefolded, user.NickCasefolded(), cPersistent, tPersistent, targetedItem)

+ 40
- 33
irc/server.go Parādīt failu

864
 // privilege checking.
864
 // privilege checking.
865
 func (server *Server) GetHistorySequence(providedChannel *Channel, client *Client, target string) (channel *Channel, sequence history.Sequence, err error) {
865
 func (server *Server) GetHistorySequence(providedChannel *Channel, client *Client, target string) (channel *Channel, sequence history.Sequence, err error) {
866
 	config := server.Config()
866
 	config := server.Config()
867
+	// 4 cases: {persistent, ephemeral} x {normal, conversation}
868
+	// with ephemeral history, recipient is implicit in the choice of `hist`,
869
+	// and sender is "" if we're retrieving a channel or *, and the correspondent's name
870
+	// if we're retrieving a DM conversation ("query buffer"). with persistent history,
871
+	// recipient is always nonempty, and sender is either empty or nonempty as before.
872
+	var status HistoryStatus
867
 	var sender, recipient string
873
 	var sender, recipient string
868
 	var hist *history.Buffer
874
 	var hist *history.Buffer
869
-	if target == "*" {
870
-		persistent, ephemeral, target := client.historyStatus(config)
871
-		if persistent {
872
-			recipient = target
873
-		} else if ephemeral {
874
-			hist = &client.history
875
-		} else {
876
-			return
877
-		}
878
-	} else {
879
-		channel = providedChannel
880
-		if channel == nil {
875
+	channel = providedChannel
876
+	if channel == nil {
877
+		if strings.HasPrefix(target, "#") {
881
 			channel = server.channels.Get(target)
878
 			channel = server.channels.Get(target)
882
-		}
883
-		if channel != nil {
884
-			if !channel.hasClient(client) {
885
-				err = errInsufficientPrivs
879
+			if channel == nil {
886
 				return
880
 				return
887
 			}
881
 			}
888
-			persistent, ephemeral, cfTarget := channel.historyStatus(config)
889
-			if persistent {
890
-				recipient = cfTarget
891
-			} else if ephemeral {
892
-				hist = &channel.history
893
-			} else {
894
-				return
895
-			}
896
-		} else {
897
-			sender = client.NickCasefolded()
898
-			var cfTarget string
899
-			cfTarget, err = CasefoldName(target)
882
+		}
883
+	}
884
+	if channel != nil {
885
+		if !channel.hasClient(client) {
886
+			err = errInsufficientPrivs
887
+			return
888
+		}
889
+		status, recipient = channel.historyStatus(config)
890
+		switch status {
891
+		case HistoryEphemeral:
892
+			hist = &channel.history
893
+		case HistoryPersistent:
894
+			// already set `recipient`
895
+		default:
896
+			return
897
+		}
898
+	} else {
899
+		status, recipient = client.historyStatus(config)
900
+		if target != "*" {
901
+			sender, err = CasefoldName(target)
900
 			if err != nil {
902
 			if err != nil {
901
 				return
903
 				return
902
 			}
904
 			}
903
-			recipient = cfTarget
904
-			if !client.AlwaysOn() {
905
-				hist = &client.history
906
-			}
905
+		}
906
+		switch status {
907
+		case HistoryEphemeral:
908
+			hist = &client.history
909
+		case HistoryPersistent:
910
+			// already set `recipient`, and `sender` if necessary
911
+		default:
912
+			return
907
 		}
913
 		}
908
 	}
914
 	}
909
 
915
 
921
 	if !cutoff.IsZero() {
927
 	if !cutoff.IsZero() {
922
 		cutoff = cutoff.Add(-time.Duration(config.History.Restrictions.GracePeriod))
928
 		cutoff = cutoff.Add(-time.Duration(config.History.Restrictions.GracePeriod))
923
 	}
929
 	}
930
+
924
 	if hist != nil {
931
 	if hist != nil {
925
-		sequence = hist.MakeSequence(recipient, cutoff)
932
+		sequence = hist.MakeSequence(sender, cutoff)
926
 	} else if recipient != "" {
933
 	} else if recipient != "" {
927
 		sequence = server.historyDB.MakeSequence(sender, recipient, cutoff)
934
 		sequence = server.historyDB.MakeSequence(sender, recipient, cutoff)
928
 	}
935
 	}

Notiek ielāde…
Atcelt
Saglabāt