mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 05:26:59 +00:00
Breaking change to FriendStatus, user cache sync.
This commit is contained in:
@@ -145,25 +145,17 @@ pub fn process_event(event: &Notification) {
|
||||
match event {
|
||||
Notification::group_user_join(ev) => {
|
||||
let mut cache = CACHE.lock().unwrap();
|
||||
let entry = cache.pop(&ev.id);
|
||||
|
||||
if entry.is_some() {
|
||||
let mut channel = entry.unwrap();
|
||||
if let Some(channel) = cache.peek_mut(&ev.id) {
|
||||
channel.recipients.as_mut().unwrap().push(ev.user.clone());
|
||||
cache.put(ev.id.clone(), channel);
|
||||
}
|
||||
}
|
||||
Notification::group_user_leave(ev) => {
|
||||
let mut cache = CACHE.lock().unwrap();
|
||||
let entry = cache.pop(&ev.id);
|
||||
|
||||
if entry.is_some() {
|
||||
let mut channel = entry.unwrap();
|
||||
if let Some(channel) = cache.peek_mut(&ev.id) {
|
||||
let recipients = channel.recipients.as_mut().unwrap();
|
||||
if let Some(pos) = recipients.iter().position(|x| *x == ev.user) {
|
||||
recipients.remove(pos);
|
||||
}
|
||||
cache.put(ev.id.clone(), channel);
|
||||
}
|
||||
}
|
||||
Notification::guild_channel_create(ev) => {
|
||||
|
||||
@@ -206,8 +206,8 @@ use crate::notifications::events::Notification;
|
||||
|
||||
pub fn process_event(event: &Notification) {
|
||||
match event {
|
||||
Notification::guild_channel_create(ev) => {} // ? for later use
|
||||
Notification::guild_channel_delete(ev) => {} // ? for later use
|
||||
Notification::guild_channel_create(_ev) => {} // ? for later use
|
||||
Notification::guild_channel_delete(_ev) => {} // ? for later use
|
||||
Notification::guild_delete(ev) => {
|
||||
let mut cache = CACHE.lock().unwrap();
|
||||
cache.pop(&ev.id);
|
||||
|
||||
@@ -84,7 +84,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
|
||||
type Error = AuthError;
|
||||
|
||||
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
||||
let u = request.headers().get("x-user").next();
|
||||
let u = request.headers().get("x-user").next();
|
||||
let t = request.headers().get("x-auth-token").next();
|
||||
|
||||
if let Some(uid) = u {
|
||||
@@ -130,3 +130,34 @@ impl<'r> FromParam<'r> for User {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use crate::notifications::events::Notification;
|
||||
|
||||
pub fn process_event(event: &Notification) {
|
||||
match event {
|
||||
Notification::user_friend_status(ev) => {
|
||||
let mut cache = CACHE.lock().unwrap();
|
||||
if let Some(user) = cache.peek_mut(&ev.id) {
|
||||
if let Some(relations) = user.relations.as_mut() {
|
||||
if ev.status == 0 {
|
||||
if let Some(pos) = relations.iter().position(|x| x.id == ev.user) {
|
||||
relations.remove(pos);
|
||||
}
|
||||
} else {
|
||||
if let Some(entry) = relations.iter_mut().find(|x| x.id == ev.user) {
|
||||
entry.status = ev.status as u8;
|
||||
} else {
|
||||
relations.push(
|
||||
UserRelationship {
|
||||
id: ev.id.clone(),
|
||||
status: ev.status as u8
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,5 +41,7 @@ impl Notification {
|
||||
|
||||
pub fn push_to_cache(&self) {
|
||||
crate::database::channel::process_event(&self);
|
||||
crate::database::guild::process_event(&self);
|
||||
crate::database::user::process_event(&self);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,5 +3,6 @@ use serde::{Deserialize, Serialize};
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct FriendStatus {
|
||||
pub id: String,
|
||||
pub user: String,
|
||||
pub status: i32,
|
||||
}
|
||||
|
||||
@@ -268,7 +268,8 @@ pub fn add_friend(user: User, target: User) -> Response {
|
||||
vec![target.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: user.id.clone(),
|
||||
id: target.id.clone(),
|
||||
user: user.id.clone(),
|
||||
status: Relationship::Friend as i32,
|
||||
}),
|
||||
);
|
||||
@@ -277,7 +278,8 @@ pub fn add_friend(user: User, target: User) -> Response {
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: target.id.clone(),
|
||||
id: user.id.clone(),
|
||||
user: target.id.clone(),
|
||||
status: Relationship::Friend as i32,
|
||||
}),
|
||||
);
|
||||
@@ -339,7 +341,8 @@ pub fn add_friend(user: User, target: User) -> Response {
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: target.id.clone(),
|
||||
id: user.id.clone(),
|
||||
user: target.id.clone(),
|
||||
status: Relationship::Outgoing as i32,
|
||||
}),
|
||||
);
|
||||
@@ -348,7 +351,8 @@ pub fn add_friend(user: User, target: User) -> Response {
|
||||
vec![target.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: user.id.clone(),
|
||||
id: target.id.clone(),
|
||||
user: user.id.clone(),
|
||||
status: Relationship::Incoming as i32,
|
||||
}),
|
||||
);
|
||||
@@ -414,7 +418,8 @@ pub fn remove_friend(user: User, target: User) -> Response {
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: target.id.clone(),
|
||||
id: user.id.clone(),
|
||||
user: target.id.clone(),
|
||||
status: Relationship::NONE as i32,
|
||||
}),
|
||||
);
|
||||
@@ -423,7 +428,8 @@ pub fn remove_friend(user: User, target: User) -> Response {
|
||||
vec![target.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: user.id.clone(),
|
||||
id: target.id.clone(),
|
||||
user: user.id.clone(),
|
||||
status: Relationship::NONE as i32,
|
||||
}),
|
||||
);
|
||||
@@ -488,7 +494,8 @@ pub fn block_user(user: User, target: User) -> Response {
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: target.id.clone(),
|
||||
id: user.id.clone(),
|
||||
user: target.id.clone(),
|
||||
status: Relationship::Blocked as i32,
|
||||
}),
|
||||
);
|
||||
@@ -497,7 +504,8 @@ pub fn block_user(user: User, target: User) -> Response {
|
||||
vec![target.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: user.id.clone(),
|
||||
id: target.id.clone(),
|
||||
user: user.id.clone(),
|
||||
status: Relationship::BlockedOther as i32,
|
||||
}),
|
||||
);
|
||||
@@ -554,7 +562,8 @@ pub fn block_user(user: User, target: User) -> Response {
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: target.id.clone(),
|
||||
id: user.id.clone(),
|
||||
user: target.id.clone(),
|
||||
status: Relationship::Blocked as i32,
|
||||
}),
|
||||
);
|
||||
@@ -563,7 +572,8 @@ pub fn block_user(user: User, target: User) -> Response {
|
||||
vec![target.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: user.id.clone(),
|
||||
id: target.id.clone(),
|
||||
user: user.id.clone(),
|
||||
status: Relationship::BlockedOther as i32,
|
||||
}),
|
||||
);
|
||||
@@ -603,7 +613,8 @@ pub fn block_user(user: User, target: User) -> Response {
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: target.id.clone(),
|
||||
id: user.id.clone(),
|
||||
user: target.id.clone(),
|
||||
status: Relationship::Blocked as i32,
|
||||
}),
|
||||
);
|
||||
@@ -646,7 +657,8 @@ pub fn unblock_user(user: User, target: User) -> Response {
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: target.id.clone(),
|
||||
id: user.id.clone(),
|
||||
user: target.id.clone(),
|
||||
status: Relationship::BlockedOther as i32,
|
||||
}),
|
||||
);
|
||||
@@ -695,7 +707,8 @@ pub fn unblock_user(user: User, target: User) -> Response {
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: target.id.clone(),
|
||||
id: user.id.clone(),
|
||||
user: target.id.clone(),
|
||||
status: Relationship::NONE as i32,
|
||||
}),
|
||||
);
|
||||
@@ -704,7 +717,8 @@ pub fn unblock_user(user: User, target: User) -> Response {
|
||||
vec![target.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: user.id.clone(),
|
||||
id: target.id.clone(),
|
||||
user: user.id.clone(),
|
||||
status: Relationship::NONE as i32,
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user