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 11164 : CommitMessage::toJson() const
26 : {
27 11164 : Json::Value value;
28 11164 : value[CommitKey::TYPE] = type;
29 11164 : if (type == CommitType::TEXT || type == CommitType::EDITED_MESSAGE) {
30 463 : value[CommitKey::BODY] = body;
31 : }
32 11151 : if (!replyTo.empty()) {
33 4 : value[CommitKey::REPLY_TO] = replyTo;
34 : }
35 11153 : if (!reactTo.empty()) {
36 6 : value[CommitKey::REACT_TO] = reactTo;
37 : }
38 11161 : if (!editedId.empty()) {
39 16 : value[CommitKey::EDIT] = editedId;
40 : }
41 11161 : if (!action.empty()) {
42 9518 : value[CommitKey::ACTION] = action;
43 : }
44 11164 : if (!uri.empty()) {
45 9671 : value[CommitKey::URI] = uri;
46 : }
47 11166 : if (!device.empty()) {
48 113 : value[CommitKey::DEVICE] = device;
49 : }
50 11163 : if (!confId.empty()) {
51 113 : value[CommitKey::CONF_ID] = confId;
52 : }
53 11163 : if (!to.empty()) {
54 6 : value[CommitKey::TO] = to;
55 : }
56 11161 : if (!reason.empty()) {
57 6 : value[CommitKey::REASON] = reason;
58 : }
59 11161 : if (!duration.empty()) {
60 54 : value[CommitKey::DURATION] = duration;
61 : }
62 11160 : if (type == CommitType::DATA_TRANSFER) {
63 58 : value[CommitKey::TID] = tid;
64 : }
65 11154 : if (!displayName.empty()) {
66 54 : value[CommitKey::DISPLAY_NAME] = displayName;
67 : }
68 11154 : if (totalSize >= 0) {
69 54 : value[CommitKey::TOTAL_SIZE] = std::to_string(totalSize);
70 : }
71 11154 : if (!sha3sum.empty()) {
72 54 : value[CommitKey::SHA3SUM] = sha3sum;
73 : }
74 11158 : if (mode >= 0) {
75 935 : value[CommitKey::MODE] = mode;
76 : }
77 11158 : if (!invited.empty()) {
78 327 : value[CommitKey::INVITED] = invited;
79 : }
80 11157 : return value;
81 0 : }
82 :
83 : std::string
84 689 : CommitMessage::toString() const
85 : {
86 689 : if (type == CommitType::MERGE) {
87 0 : return body;
88 : }
89 689 : return json::toString(toJson());
90 : }
91 :
92 : std::optional<CommitMessage>
93 13269 : CommitMessage::fromString(const std::string& str)
94 : {
95 13269 : if (str.starts_with("Merge commit")) {
96 29 : CommitMessage msg;
97 29 : msg.type = CommitType::MERGE;
98 29 : msg.body = str;
99 29 : return msg;
100 29 : }
101 13238 : Json::Value value;
102 13248 : if (!json::parse(str, value) || !value.isObject()) {
103 0 : return std::nullopt;
104 : }
105 :
106 13236 : CommitMessage msg;
107 : try {
108 13244 : msg.type = value.get(CommitKey::TYPE, "").asString();
109 13254 : msg.body = value.get(CommitKey::BODY, "").asString();
110 13254 : msg.replyTo = value.get(CommitKey::REPLY_TO, "").asString();
111 13254 : msg.reactTo = value.get(CommitKey::REACT_TO, "").asString();
112 13255 : msg.editedId = value.get(CommitKey::EDIT, "").asString();
113 13255 : msg.action = value.get(CommitKey::ACTION, "").asString();
114 13252 : msg.uri = value.get(CommitKey::URI, "").asString();
115 13255 : msg.device = value.get(CommitKey::DEVICE, "").asString();
116 13255 : msg.confId = value.get(CommitKey::CONF_ID, "").asString();
117 13255 : msg.to = value.get(CommitKey::TO, "").asString();
118 13255 : msg.reason = value.get(CommitKey::REASON, "").asString();
119 13252 : msg.duration = value.get(CommitKey::DURATION, "").asString();
120 13255 : msg.tid = value.get(CommitKey::TID, "").asString();
121 13255 : msg.displayName = value.get(CommitKey::DISPLAY_NAME, "").asString();
122 13255 : msg.totalSize = to_int<int64_t>(value.get(CommitKey::TOTAL_SIZE, "-1").asString());
123 13254 : msg.sha3sum = value.get(CommitKey::SHA3SUM, "").asString();
124 13253 : msg.mode = value.get(CommitKey::MODE, -1).asInt();
125 13254 : msg.invited = value.get(CommitKey::INVITED, "").asString();
126 0 : } catch (const std::exception& e) {
127 0 : JAMI_ERROR("Exception while parsing commit message '{}': {}", str, e.what());
128 0 : return std::nullopt;
129 0 : }
130 13254 : return msg;
131 13243 : }
132 :
133 : } // namespace jami
|