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 23016 : CommitMessage::toJson() const
26 : {
27 23016 : Json::Value value;
28 23020 : value[CommitKey::TYPE] = type;
29 23023 : if (type == CommitType::TEXT || type == CommitType::EDITED_MESSAGE || type == CommitType::CHECKPOINT) {
30 1148 : value[CommitKey::BODY] = body;
31 : }
32 23006 : if (!replyTo.empty()) {
33 206 : value[CommitKey::REPLY_TO] = replyTo;
34 : }
35 23008 : if (!reactTo.empty()) {
36 108 : value[CommitKey::REACT_TO] = reactTo;
37 : }
38 23011 : if (!editedId.empty()) {
39 428 : value[CommitKey::EDIT] = editedId;
40 : }
41 23011 : if (!action.empty()) {
42 18769 : value[CommitKey::ACTION] = action;
43 : }
44 23021 : if (!uri.empty()) {
45 19467 : value[CommitKey::URI] = uri;
46 : }
47 23022 : if (!device.empty()) {
48 311 : value[CommitKey::DEVICE] = device;
49 : }
50 23021 : if (!confId.empty()) {
51 311 : value[CommitKey::CONF_ID] = confId;
52 : }
53 23020 : if (!to.empty()) {
54 216 : value[CommitKey::TO] = to;
55 : }
56 23015 : if (!reason.empty()) {
57 107 : value[CommitKey::REASON] = reason;
58 : }
59 23018 : if (!duration.empty()) {
60 361 : value[CommitKey::DURATION] = duration;
61 : }
62 23011 : if (type == CommitType::DATA_TRANSFER) {
63 368 : value[CommitKey::TID] = tid;
64 : }
65 23004 : if (!displayName.empty()) {
66 481 : value[CommitKey::DISPLAY_NAME] = displayName;
67 : }
68 23004 : if (totalSize >= 0) {
69 263 : value[CommitKey::TOTAL_SIZE] = std::to_string(totalSize);
70 : }
71 23004 : if (!sha3sum.empty()) {
72 263 : value[CommitKey::SHA3SUM] = sha3sum;
73 : }
74 23006 : if (mode >= 0) {
75 1599 : value[CommitKey::MODE] = mode;
76 : }
77 23006 : if (!invited.empty()) {
78 436 : value[CommitKey::INVITED] = invited;
79 : }
80 23008 : if (!mimeType.empty()) {
81 387 : value[CommitKey::MIME_TYPE] = mimeType;
82 : }
83 23011 : if (!parent.empty()) {
84 169 : value[CommitKey::PARENT] = parent;
85 : }
86 23013 : return value;
87 0 : }
88 :
89 : std::string
90 3261 : CommitMessage::toString() const
91 : {
92 3261 : if (type == CommitType::MERGE) {
93 1 : return body;
94 : }
95 3260 : return json::toString(toJson());
96 : }
97 :
98 : std::optional<CommitMessage>
99 26160 : CommitMessage::fromString(const std::string& str)
100 : {
101 26160 : if (str.starts_with("Merge commit")) {
102 58 : CommitMessage msg;
103 58 : msg.type = CommitType::MERGE;
104 58 : msg.body = str;
105 58 : return msg;
106 58 : }
107 26100 : Json::Value value;
108 26105 : if (!json::parse(str, value) || !value.isObject()) {
109 4 : return std::nullopt;
110 : }
111 :
112 26090 : CommitMessage msg;
113 : try {
114 26079 : msg.type = value.get(CommitKey::TYPE, "").asString();
115 26109 : msg.body = value.get(CommitKey::BODY, "").asString();
116 26109 : msg.replyTo = value.get(CommitKey::REPLY_TO, "").asString();
117 26111 : msg.reactTo = value.get(CommitKey::REACT_TO, "").asString();
118 26110 : msg.editedId = value.get(CommitKey::EDIT, "").asString();
119 26111 : msg.action = value.get(CommitKey::ACTION, "").asString();
120 26111 : msg.uri = value.get(CommitKey::URI, "").asString();
121 26110 : msg.device = value.get(CommitKey::DEVICE, "").asString();
122 26111 : msg.confId = value.get(CommitKey::CONF_ID, "").asString();
123 26110 : msg.to = value.get(CommitKey::TO, "").asString();
124 26109 : msg.reason = value.get(CommitKey::REASON, "").asString();
125 26110 : msg.duration = value.get(CommitKey::DURATION, "").asString();
126 26110 : msg.tid = value.get(CommitKey::TID, "").asString();
127 26109 : msg.displayName = value.get(CommitKey::DISPLAY_NAME, "").asString();
128 26108 : msg.totalSize = to_int<int64_t>(value.get(CommitKey::TOTAL_SIZE, "-1").asString());
129 26110 : msg.sha3sum = value.get(CommitKey::SHA3SUM, "").asString();
130 26109 : msg.mode = value.get(CommitKey::MODE, -1).asInt();
131 26107 : msg.invited = value.get(CommitKey::INVITED, "").asString();
132 26110 : msg.mimeType = value.get(CommitKey::MIME_TYPE, "").asString();
133 26108 : 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 26111 : return msg;
139 26096 : }
140 :
141 : } // namespace jami
|