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