do they consider the fact that in doing so they might anger a friend, who happens to be me?
I think I recall seeing that the potential DOW target being a friend of friend does slightly decrease the likely hood of making the DOW. But even if that is true, it might also be the case that the player relations don’t count for that! In any case, and based on my own experience, I don't think the player having DOF with both noticeably decreases the likelihood of war between the two.
Vice versa: will they consider becoming friends if one is my friend and other is my enemy
No, sorry, but the player relationships are just not much valued by AI. I am pretty confident you will only see them make amends if they pick the same ideology.
I hate when they ask me that, it's a lose-lose situation. I think if you refuse, it counts as backstabbing as well
Yes, it is a lose-lose choice. It is only a backstab if you agree and denounce when you happened to have a DOF going.
The most common pattern is that an AI asks you to denounce, you say no (mostly because you have a DOF going with this other AI), and then the asking AI denounces you. If the asking AI had a DOF with you, they now have a backstab penalty with everyone. The problem is, as far as I can tell, none of the other AI much care!
The second most common pattern is that an AI asks you to denounce, and you agree to do so since (1) you do not have a DOF going with the other AI, so you would not be backstabbing, (2) you want to maintain relations with the asking AI, and (3) being denounced by the asking AI -- the likely outcome if you decline -- can only hurt your other diplomatic relationships, and would be worse than agreeing. It is still loose-loose because you (A) damage relations with the AI you denounced, damage relations with (B) any AI who likes the denounced AI more than they like you, and (C) you don’t even get a light-green modifier with the asking AI!
Recently, with one of my attempts at the current Persia CDG, the Lady (no DOF, and she was making me worried) asks me to denounce the crazy AI (no DOF, and I didn’t care) -- so I denounce. I really expected that would help my relations with the Lady, but just a few turns latter she denounces me!