Skip to content

Commit 520f0e8

Browse files
mikimiki-totefu
authored andcommitted
clients: persist FT download/upload msg
1 parent 43f3f88 commit 520f0e8

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

bruig/flutterui/bruig/lib/models/client.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ class SynthChatEvent extends ChatEvent with ChangeNotifier {
8282
// Regexp for messages in the history about suggested KX.
8383
var _suggestedKXMsgRegexp = RegExp(r'Suggested KX to ([0-9a-f]{64}) "(.*)"');
8484

85+
// Regexp for messages in the history about a completed download.
86+
var _completedFTDownloadMsgRegexp =
87+
RegExp(r'Download completed: "(.*)" \(([0-9a-f]{64})\)');
88+
89+
// Regexp for messages in the history about completed uploads (/ft send).
90+
var _sentFTUploadMsgRegexp = RegExp(r'Sent file "(.*)" \(([0-9a-f]{64})\)');
91+
8592
class RequestedResourceEvent extends ChatEvent {
8693
final PagesSession session;
8794

@@ -1034,9 +1041,15 @@ class ClientModel extends ChangeNotifier {
10341041
ChatEvent m;
10351042
var source = !mine ? c : null;
10361043
if (chatHistory[i].internal) {
1037-
var matchSuggestKX = _suggestedKXMsgRegexp.firstMatch(
1044+
RegExpMatch? matchSuggestKX = _suggestedKXMsgRegexp.firstMatch(
10381045
chatHistory[i].message,
10391046
);
1047+
RegExpMatch? matchDownload = matchSuggestKX == null
1048+
? _completedFTDownloadMsgRegexp.firstMatch(chatHistory[i].message)
1049+
: null;
1050+
RegExpMatch? matchUpload = (matchSuggestKX ?? matchDownload) == null
1051+
? _sentFTUploadMsgRegexp.firstMatch(chatHistory[i].message)
1052+
: null;
10401053
if (matchSuggestKX != null) {
10411054
var targetUID = matchSuggestKX[1]!;
10421055
var alreadyKnown = getExistingChat(targetUID) != null;
@@ -1047,6 +1060,10 @@ class ClientModel extends ChangeNotifier {
10471060
matchSuggestKX[2]!,
10481061
targetUID,
10491062
);
1063+
} else if (matchDownload != null) {
1064+
m = FileDownloadedEvent(id, matchDownload[1]!);
1065+
} else if (matchUpload != null) {
1066+
m = SynthChatEvent("Sent file ${matchUpload[1]!}", SCE_sent);
10501067
} else {
10511068
m = SynthChatEvent(chatHistory[i].message, SCE_history);
10521069
}

client/client_content.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,9 @@ func (c *Client) handleFTPayForChunk(ru *RemoteUser, pfc rpc.RMFTPayForChunk) er
898898
}
899899

900900
// handleFTGetChunkReply is called to handle received chunk data for a download.
901-
func (c *Client) handleFTGetChunkReply(ru *RemoteUser, gcr rpc.RMFTGetChunkReply) error {
901+
//
902+
// This is the main handler for processing download chunks in the receiver.
903+
func (c *Client) handleFTGetChunkReply(ru *RemoteUser, gcr rpc.RMFTGetChunkReply, ts time.Time) error {
902904
var fid clientdb.FileID
903905
if err := fid.FromString(gcr.FileID); err != nil {
904906
return err
@@ -917,6 +919,17 @@ func (c *Client) handleFTGetChunkReply(ru *RemoteUser, gcr rpc.RMFTGetChunkReply
917919

918920
completedFname, err = c.db.SaveFileDownloadChunk(tx, ru.Nick(), &fd, gcr.Index, gcr.Chunk)
919921
nbMissingChunks = len(c.db.MissingFileDownloadChunks(tx, &fd))
922+
923+
if completedFname != "" {
924+
logMsg := fmt.Sprintf(clientdb.CompletedFTDownloadMsg,
925+
completedFname, fd.FID)
926+
_, err := c.db.LogPM(tx, ru.ID(), true, ru.Nick(),
927+
logMsg, ts)
928+
if err != nil {
929+
return err
930+
}
931+
}
932+
920933
return err
921934
})
922935
if err != nil {
@@ -1047,7 +1060,15 @@ func (c *Client) SendFile(uid UserID, chunkSize uint64, filepath string,
10471060
return nil
10481061
}
10491062

1050-
ru.log.Infof("Finished sending file %s to user", filepath)
1063+
ru.log.Infof("Sent file %q (%s)", filepath, fileId)
1064+
err = c.dbUpdate(func(tx clientdb.ReadWriteTx) error {
1065+
logMsg := fmt.Sprintf(clientdb.SentFTUploadMsg, filepath, fileId)
1066+
_, err := c.db.LogPM(tx, uid, true, ru.Nick(), logMsg, time.Now())
1067+
return err
1068+
})
1069+
if err != nil {
1070+
return err
1071+
}
10511072
return nil
10521073
}
10531074

client/client_rm_handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func (c *Client) handleUserRM(ru *RemoteUser, h *rpc.RMHeader, p interface{}, ts
384384
return nil
385385

386386
case rpc.RMFTGetChunkReply:
387-
err := c.handleFTGetChunkReply(ru, p)
387+
err := c.handleFTGetChunkReply(ru, p, ts)
388388
c.logHandlerError(ru, h.Command, p, err)
389389
return nil
390390

client/clientdb/fscdb.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ var (
100100
//
101101
// This MUST match log messages generated by SuggestedKXLogMsg.
102102
suggestedKXRegexp = regexp.MustCompile(`Suggested KX to ([0-9a-f]{64}) "(.*)"`)
103+
104+
CompletedFTDownloadMsg = "Download completed: %q (%s)"
105+
106+
SentFTUploadMsg = "Sent file %q (%s)"
103107
)
104108

105109
func (db *DB) LocalID(tx ReadTx) (*zkidentity.FullIdentity, error) {

0 commit comments

Comments
 (0)