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 7802 : CommitMessage::toJson() const
26 : {
27 7802 : Json::Value value;
28 7803 : value[CommitKey::TYPE] = type;
29 7806 : if (type == CommitType::TEXT || type == CommitType::EDITED_MESSAGE) {
30 462 : value[CommitKey::BODY] = body;
31 : }
32 7802 : if (!replyTo.empty()) {
33 4 : value[CommitKey::REPLY_TO] = replyTo;
34 : }
35 7802 : if (!reactTo.empty()) {
36 6 : value[CommitKey::REACT_TO] = reactTo;
37 : }
38 7806 : if (!editedId.empty()) {
39 16 : value[CommitKey::EDIT] = editedId;
40 : }
41 7805 : if (!action.empty()) {
42 6220 : value[CommitKey::ACTION] = action;
43 : }
44 7806 : if (!uri.empty()) {
45 6384 : value[CommitKey::URI] = uri;
46 : }
47 7805 : if (!device.empty()) {
48 114 : value[CommitKey::DEVICE] = device;
49 : }
50 7806 : if (!confId.empty()) {
51 114 : value[CommitKey::CONF_ID] = confId;
52 : }
53 7804 : if (!to.empty()) {
54 6 : value[CommitKey::TO] = to;
55 : }
56 7804 : if (!reason.empty()) {
57 6 : value[CommitKey::REASON] = reason;
58 : }
59 7805 : if (!duration.empty()) {
60 54 : value[CommitKey::DURATION] = duration;
61 : }
62 7803 : if (type == CommitType::DATA_TRANSFER) {
63 58 : value[CommitKey::TID] = tid;
64 : }
65 7801 : if (!displayName.empty()) {
66 54 : value[CommitKey::DISPLAY_NAME] = displayName;
67 : }
68 7806 : if (totalSize >= 0) {
69 54 : value[CommitKey::TOTAL_SIZE] = std::to_string(totalSize);
70 : }
71 7806 : if (!sha3sum.empty()) {
72 54 : value[CommitKey::SHA3SUM] = sha3sum;
73 : }
74 7807 : if (mode >= 0) {
75 865 : value[CommitKey::MODE] = mode;
76 : }
77 7807 : if (!invited.empty()) {
78 321 : value[CommitKey::INVITED] = invited;
79 : }
80 7805 : return value;
81 0 : }
82 :
83 : std::string
84 686 : CommitMessage::toString() const
85 : {
86 686 : if (type == CommitType::MERGE) {
87 0 : return body;
88 : }
89 686 : return json::toString(toJson());
90 : }
91 :
92 : std::optional<CommitMessage>
93 10005 : CommitMessage::fromString(const std::string& str)
94 : {
95 10005 : if (str.starts_with("Merge commit")) {
96 24 : CommitMessage msg;
97 24 : msg.type = CommitType::MERGE;
98 24 : msg.body = str;
99 24 : return msg;
100 24 : }
101 9980 : Json::Value value;
102 9981 : if (!json::parse(str, value) || !value.isObject()) {
103 0 : return std::nullopt;
104 : }
105 :
106 9980 : CommitMessage msg;
107 : try {
108 9976 : msg.type = value.get(CommitKey::TYPE, "").asString();
109 9984 : msg.body = value.get(CommitKey::BODY, "").asString();
110 9984 : msg.replyTo = value.get(CommitKey::REPLY_TO, "").asString();
111 9983 : msg.reactTo = value.get(CommitKey::REACT_TO, "").asString();
112 9984 : msg.editedId = value.get(CommitKey::EDIT, "").asString();
113 9984 : msg.action = value.get(CommitKey::ACTION, "").asString();
114 9984 : msg.uri = value.get(CommitKey::URI, "").asString();
115 9984 : msg.device = value.get(CommitKey::DEVICE, "").asString();
116 9984 : msg.confId = value.get(CommitKey::CONF_ID, "").asString();
117 9984 : msg.to = value.get(CommitKey::TO, "").asString();
118 9983 : msg.reason = value.get(CommitKey::REASON, "").asString();
119 9984 : msg.duration = value.get(CommitKey::DURATION, "").asString();
120 9985 : msg.tid = value.get(CommitKey::TID, "").asString();
121 9985 : msg.displayName = value.get(CommitKey::DISPLAY_NAME, "").asString();
122 9984 : msg.totalSize = to_int<int64_t>(value.get(CommitKey::TOTAL_SIZE, "-1").asString());
123 9985 : msg.sha3sum = value.get(CommitKey::SHA3SUM, "").asString();
124 9985 : msg.mode = value.get(CommitKey::MODE, -1).asInt();
125 9983 : 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 9985 : return msg;
131 9982 : }
132 :
133 : } // namespace jami
|