forked from Qortal/qortal
Improve /chat/active/{address} output to include latest message's sender address, and registered name if applicable
This commit is contained in:
parent
e5e60a5032
commit
75b15c6639
@ -12,17 +12,22 @@ public class ActiveChats {
|
|||||||
public static class GroupChat {
|
public static class GroupChat {
|
||||||
private int groupId;
|
private int groupId;
|
||||||
private String groupName;
|
private String groupName;
|
||||||
// Might not be present for groupId 0
|
|
||||||
|
// The following fields might not be present for groupId 0
|
||||||
private Long timestamp;
|
private Long timestamp;
|
||||||
|
private String sender;
|
||||||
|
private String senderName;
|
||||||
|
|
||||||
protected GroupChat() {
|
protected GroupChat() {
|
||||||
/* JAXB */
|
/* JAXB */
|
||||||
}
|
}
|
||||||
|
|
||||||
public GroupChat(int groupId, String groupName, Long timestamp) {
|
public GroupChat(int groupId, String groupName, Long timestamp, String sender, String senderName) {
|
||||||
this.groupId = groupId;
|
this.groupId = groupId;
|
||||||
this.groupName = groupName;
|
this.groupName = groupName;
|
||||||
this.timestamp = timestamp;
|
this.timestamp = timestamp;
|
||||||
|
this.sender = sender;
|
||||||
|
this.senderName = senderName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getGroupId() {
|
public int getGroupId() {
|
||||||
@ -36,6 +41,14 @@ public class ActiveChats {
|
|||||||
public Long getTimestamp() {
|
public Long getTimestamp() {
|
||||||
return this.timestamp;
|
return this.timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSender() {
|
||||||
|
return this.sender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSenderName() {
|
||||||
|
return this.senderName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@XmlAccessorType(XmlAccessType.FIELD)
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
@ -43,15 +56,19 @@ public class ActiveChats {
|
|||||||
private String address;
|
private String address;
|
||||||
private String name;
|
private String name;
|
||||||
private long timestamp;
|
private long timestamp;
|
||||||
|
private String sender;
|
||||||
|
private String senderName;
|
||||||
|
|
||||||
protected DirectChat() {
|
protected DirectChat() {
|
||||||
/* JAXB */
|
/* JAXB */
|
||||||
}
|
}
|
||||||
|
|
||||||
public DirectChat(String address, String name, long timestamp) {
|
public DirectChat(String address, String name, long timestamp, String sender, String senderName) {
|
||||||
this.address = address;
|
this.address = address;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.timestamp = timestamp;
|
this.timestamp = timestamp;
|
||||||
|
this.sender = sender;
|
||||||
|
this.senderName = senderName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAddress() {
|
public String getAddress() {
|
||||||
@ -65,6 +82,14 @@ public class ActiveChats {
|
|||||||
public long getTimestamp() {
|
public long getTimestamp() {
|
||||||
return this.timestamp;
|
return this.timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSender() {
|
||||||
|
return this.sender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSenderName() {
|
||||||
|
return this.senderName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Properties
|
// Properties
|
||||||
|
@ -157,17 +157,20 @@ public class HSQLDBChatRepository implements ChatRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<GroupChat> getActiveGroupChats(String address) throws DataException {
|
private List<GroupChat> getActiveGroupChats(String address) throws DataException {
|
||||||
// Find groups where address is a member and potential latest timestamp
|
// Find groups where address is a member and potential latest message details
|
||||||
String groupsSql = "SELECT group_id, group_name, ("
|
String groupsSql = "SELECT group_id, group_name, latest_timestamp, sender, sender_name "
|
||||||
+ "SELECT created_when "
|
+ "FROM GroupMembers "
|
||||||
+ "FROM Transactions "
|
+ "JOIN Groups USING (group_id) "
|
||||||
|
+ "CROSS JOIN LATERAL("
|
||||||
|
+ "SELECT created_when AS latest_timestamp, sender, name AS sender_name "
|
||||||
|
+ "FROM ChatTransactions "
|
||||||
|
+ "JOIN Transactions USING (signature) "
|
||||||
|
+ "LEFT OUTER JOIN Names AS SenderNames ON SenderNames.owner = sender "
|
||||||
// NOTE: We need to qualify "Groups.group_id" here to avoid "General error" bug in HSQLDB v2.5.0
|
// NOTE: We need to qualify "Groups.group_id" here to avoid "General error" bug in HSQLDB v2.5.0
|
||||||
+ "WHERE tx_group_id = Groups.group_id AND type = " + TransactionType.CHAT.value + " "
|
+ "WHERE tx_group_id = Groups.group_id AND type = " + TransactionType.CHAT.value + " "
|
||||||
+ "ORDER BY created_when DESC "
|
+ "ORDER BY created_when DESC "
|
||||||
+ "LIMIT 1"
|
+ "LIMIT 1"
|
||||||
+ ") AS latest_timestamp "
|
+ ") AS LatestMessages "
|
||||||
+ "FROM GroupMembers "
|
|
||||||
+ "JOIN Groups USING (group_id) "
|
|
||||||
+ "WHERE address = ?";
|
+ "WHERE address = ?";
|
||||||
|
|
||||||
List<GroupChat> groupChats = new ArrayList<>();
|
List<GroupChat> groupChats = new ArrayList<>();
|
||||||
@ -181,7 +184,10 @@ public class HSQLDBChatRepository implements ChatRepository {
|
|||||||
if (timestamp == 0 && resultSet.wasNull())
|
if (timestamp == 0 && resultSet.wasNull())
|
||||||
timestamp = null;
|
timestamp = null;
|
||||||
|
|
||||||
GroupChat groupChat = new GroupChat(groupId, groupName, timestamp);
|
String sender = resultSet.getString(4);
|
||||||
|
String senderName = resultSet.getString(5);
|
||||||
|
|
||||||
|
GroupChat groupChat = new GroupChat(groupId, groupName, timestamp, sender, senderName);
|
||||||
groupChats.add(groupChat);
|
groupChats.add(groupChat);
|
||||||
} while (resultSet.next());
|
} while (resultSet.next());
|
||||||
}
|
}
|
||||||
@ -190,9 +196,10 @@ public class HSQLDBChatRepository implements ChatRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// We need different SQL to handle group-less chat
|
// We need different SQL to handle group-less chat
|
||||||
String grouplessSql = "SELECT created_when "
|
String grouplessSql = "SELECT created_when, sender, SenderNames.name "
|
||||||
+ "FROM ChatTransactions "
|
+ "FROM ChatTransactions "
|
||||||
+ "JOIN Transactions USING (signature) "
|
+ "JOIN Transactions USING (signature) "
|
||||||
|
+ "LEFT OUTER JOIN Names AS SenderNames ON SenderNames.owner = sender "
|
||||||
+ "WHERE tx_group_id = 0 "
|
+ "WHERE tx_group_id = 0 "
|
||||||
+ "AND recipient IS NULL "
|
+ "AND recipient IS NULL "
|
||||||
+ "ORDER BY created_when DESC "
|
+ "ORDER BY created_when DESC "
|
||||||
@ -200,12 +207,17 @@ public class HSQLDBChatRepository implements ChatRepository {
|
|||||||
|
|
||||||
try (ResultSet resultSet = this.repository.checkedExecute(grouplessSql)) {
|
try (ResultSet resultSet = this.repository.checkedExecute(grouplessSql)) {
|
||||||
Long timestamp = null;
|
Long timestamp = null;
|
||||||
|
String sender = null;
|
||||||
|
String senderName = null;
|
||||||
|
|
||||||
if (resultSet != null)
|
if (resultSet != null) {
|
||||||
// We found a recipient-less, group-less CHAT message, so report its timestamp
|
// We found a recipient-less, group-less CHAT message, so report its details
|
||||||
timestamp = resultSet.getLong(1);
|
timestamp = resultSet.getLong(1);
|
||||||
|
sender = resultSet.getString(2);
|
||||||
|
senderName = resultSet.getString(3);
|
||||||
|
}
|
||||||
|
|
||||||
GroupChat groupChat = new GroupChat(0, null, timestamp);
|
GroupChat groupChat = new GroupChat(0, null, timestamp, sender, senderName);
|
||||||
groupChats.add(groupChat);
|
groupChats.add(groupChat);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new DataException("Unable to fetch active group chats from repository", e);
|
throw new DataException("Unable to fetch active group chats from repository", e);
|
||||||
@ -216,7 +228,7 @@ public class HSQLDBChatRepository implements ChatRepository {
|
|||||||
|
|
||||||
private List<DirectChat> getActiveDirectChats(String address) throws DataException {
|
private List<DirectChat> getActiveDirectChats(String address) throws DataException {
|
||||||
// Find chat messages involving address
|
// Find chat messages involving address
|
||||||
String directSql = "SELECT other_address, name, latest_timestamp "
|
String directSql = "SELECT other_address, name, latest_timestamp, sender, sender_name "
|
||||||
+ "FROM ("
|
+ "FROM ("
|
||||||
+ "SELECT recipient FROM ChatTransactions "
|
+ "SELECT recipient FROM ChatTransactions "
|
||||||
+ "WHERE sender = ? AND recipient IS NOT NULL "
|
+ "WHERE sender = ? AND recipient IS NOT NULL "
|
||||||
@ -225,13 +237,15 @@ public class HSQLDBChatRepository implements ChatRepository {
|
|||||||
+ "WHERE recipient = ?"
|
+ "WHERE recipient = ?"
|
||||||
+ ") AS OtherParties (other_address) "
|
+ ") AS OtherParties (other_address) "
|
||||||
+ "CROSS JOIN LATERAL("
|
+ "CROSS JOIN LATERAL("
|
||||||
+ "SELECT created_when FROM ChatTransactions "
|
+ "SELECT created_when AS latest_timestamp, sender, name AS sender_name "
|
||||||
|
+ "FROM ChatTransactions "
|
||||||
+ "NATURAL JOIN Transactions "
|
+ "NATURAL JOIN Transactions "
|
||||||
|
+ "LEFT OUTER JOIN Names AS SenderNames ON SenderNames.owner = sender "
|
||||||
+ "WHERE (sender = other_address AND recipient = ?) "
|
+ "WHERE (sender = other_address AND recipient = ?) "
|
||||||
+ "OR (sender = ? AND recipient = other_address) "
|
+ "OR (sender = ? AND recipient = other_address) "
|
||||||
+ "ORDER BY created_when DESC "
|
+ "ORDER BY created_when DESC "
|
||||||
+ "LIMIT 1"
|
+ "LIMIT 1"
|
||||||
+ ") AS LatestMessages (latest_timestamp) "
|
+ ") AS LatestMessages "
|
||||||
+ "LEFT OUTER JOIN Names ON owner = other_address";
|
+ "LEFT OUTER JOIN Names ON owner = other_address";
|
||||||
|
|
||||||
Object[] bindParams = new Object[] { address, address, address, address };
|
Object[] bindParams = new Object[] { address, address, address, address };
|
||||||
@ -245,8 +259,10 @@ public class HSQLDBChatRepository implements ChatRepository {
|
|||||||
String otherAddress = resultSet.getString(1);
|
String otherAddress = resultSet.getString(1);
|
||||||
String name = resultSet.getString(2);
|
String name = resultSet.getString(2);
|
||||||
long timestamp = resultSet.getLong(3);
|
long timestamp = resultSet.getLong(3);
|
||||||
|
String sender = resultSet.getString(4);
|
||||||
|
String senderName = resultSet.getString(5);
|
||||||
|
|
||||||
DirectChat directChat = new DirectChat(otherAddress, name, timestamp);
|
DirectChat directChat = new DirectChat(otherAddress, name, timestamp, sender, senderName);
|
||||||
directChats.add(directChat);
|
directChats.add(directChat);
|
||||||
} while (resultSet.next());
|
} while (resultSet.next());
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user