Line data Source code
1 : /*
2 : * Copyright (C) 2004-2026 Savoir-faire Linux Inc.
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "commit_message.h"
19 :
20 : #include "string_utils.h"
21 :
22 : namespace jami {
23 :
24 : Json::Value
25 23018 : CommitMessage::toJson() const
26 : {
27 23018 : Json::Value value;
28 23024 : value[CommitKey::TYPE] = type;
29 23029 : if (type == CommitType::TEXT || type == CommitType::EDITED_MESSAGE || type == CommitType::CHECKPOINT) {
30 1134 : value[CommitKey::BODY] = body;
31 : }
32 23019 : if (!replyTo.empty()) {
33 206 : value[CommitKey::REPLY_TO] = replyTo;
34 : }
35 23018 : if (!reactTo.empty()) {
36 108 : value[CommitKey::REACT_TO] = reactTo;
37 : }
38 23020 : if (!editedId.empty()) {
39 428 : value[CommitKey::EDIT] = editedId;
40 : }
41 23019 : if (!action.empty()) {
42 18907 : value[CommitKey::ACTION] = action;
43 : }
44 23027 : if (!uri.empty()) {
45 19591 : value[CommitKey::URI] = uri;
46 : }
47 23029 : if (!device.empty()) {
48 316 : value[CommitKey::DEVICE] = device;
49 : }
50 23025 : if (!confId.empty()) {
51 316 : value[CommitKey::CONF_ID] = confId;
52 : }
53 23023 : if (!to.empty()) {
54 216 : value[CommitKey::TO] = to;
55 : }
56 23023 : if (!reason.empty()) {
57 107 : value[CommitKey::REASON] = reason;
58 : }
59 23024 : if (!duration.empty()) {
60 365 : value[CommitKey::DURATION] = duration;
61 : }
62 23025 : if (type == CommitType::DATA_TRANSFER) {
63 361 : value[CommitKey::TID] = tid;
64 : }
65 23018 : if (!displayName.empty()) {
66 475 : value[CommitKey::DISPLAY_NAME] = displayName;
67 : }
68 23018 : if (totalSize >= 0) {
69 256 : value[CommitKey::TOTAL_SIZE] = std::to_string(totalSize);
70 : }
71 23018 : if (!sha3sum.empty()) {
72 256 : value[CommitKey::SHA3SUM] = sha3sum;
73 : }
74 23020 : if (mode >= 0) {
75 1502 : value[CommitKey::MODE] = mode;
76 : }
77 23020 : if (!invited.empty()) {
78 349 : value[CommitKey::INVITED] = invited;
79 : }
80 23017 : if (!mimeType.empty()) {
81 388 : value[CommitKey::MIME_TYPE] = mimeType;
82 : }
83 23019 : if (!parent.empty()) {
84 169 : value[CommitKey::PARENT] = parent;
85 : }
86 23020 : return value;
87 0 : }
88 :
89 : std::string
90 3179 : CommitMessage::toString() const
91 : {
92 3179 : if (type == CommitType::MERGE) {
93 1 : return body;
94 : }
95 3178 : return json::toString(toJson());
96 : }
97 :
98 : std::optional<CommitMessage>
99 25925 : CommitMessage::fromString(const std::string& str)
100 : {
101 25925 : if (str.starts_with("Merge commit")) {
102 56 : CommitMessage msg;
103 56 : msg.type = CommitType::MERGE;
104 56 : msg.body = str;
105 56 : return msg;
106 56 : }
107 25879 : Json::Value value;
108 25878 : if (!json::parse(str, value) || !value.isObject()) {
109 4 : return std::nullopt;
110 : }
111 :
112 25869 : CommitMessage msg;
113 : try {
114 25867 : msg.type = value.get(CommitKey::TYPE, "").asString();
115 25880 : msg.body = value.get(CommitKey::BODY, "").asString();
116 25879 : msg.replyTo = value.get(CommitKey::REPLY_TO, "").asString();
117 25880 : msg.reactTo = value.get(CommitKey::REACT_TO, "").asString();
118 25880 : msg.editedId = value.get(CommitKey::EDIT, "").asString();
119 25880 : msg.action = value.get(CommitKey::ACTION, "").asString();
120 25880 : msg.uri = value.get(CommitKey::URI, "").asString();
121 25880 : msg.device = value.get(CommitKey::DEVICE, "").asString();
122 25881 : msg.confId = value.get(CommitKey::CONF_ID, "").asString();
123 25877 : msg.to = value.get(CommitKey::TO, "").asString();
124 25880 : msg.reason = value.get(CommitKey::REASON, "").asString();
125 25878 : msg.duration = value.get(CommitKey::DURATION, "").asString();
126 25881 : msg.tid = value.get(CommitKey::TID, "").asString();
127 25880 : msg.displayName = value.get(CommitKey::DISPLAY_NAME, "").asString();
128 25881 : msg.totalSize = to_int<int64_t>(value.get(CommitKey::TOTAL_SIZE, "-1").asString());
129 25880 : msg.sha3sum = value.get(CommitKey::SHA3SUM, "").asString();
130 25880 : msg.mode = value.get(CommitKey::MODE, -1).asInt();
131 25881 : msg.invited = value.get(CommitKey::INVITED, "").asString();
132 25881 : msg.mimeType = value.get(CommitKey::MIME_TYPE, "").asString();
133 25881 : msg.parent = value.get(CommitKey::PARENT, "").asString();
134 0 : } catch (const std::exception& e) {
135 0 : JAMI_ERROR("Exception while parsing commit message '{}': {}", str, e.what());
136 0 : return std::nullopt;
137 0 : }
138 25880 : return msg;
139 25876 : }
140 :
141 : } // namespace jami
|